| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.SolvingQuestionsWithBrainpower; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class SolvingQuestionsWithBrainpowerDynamicProgramming : ISolvingQuestionsWithBrainpower |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="questions"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public long MostPoints(int[][] questions) |
| 2 | 24 | | { |
| 2 | 25 | | var maximumPoints = new long[questions.Length]; |
| | 26 | |
|
| 22 | 27 | | for (var currentQuestionIndex = questions.Length - 1; currentQuestionIndex >= 0; currentQuestionIndex--) |
| 9 | 28 | | { |
| 9 | 29 | | var currentQuestionPoints = questions[currentQuestionIndex][0]; |
| 9 | 30 | | var currentQuestionBrainpower = questions[currentQuestionIndex][1]; |
| 9 | 31 | | var nextSolvableQuestionIndex = currentQuestionIndex + currentQuestionBrainpower + 1; |
| | 32 | |
|
| 9 | 33 | | long solvePoints = currentQuestionPoints; |
| | 34 | |
|
| 9 | 35 | | if (nextSolvableQuestionIndex < questions.Length) |
| 3 | 36 | | { |
| 3 | 37 | | solvePoints += maximumPoints[nextSolvableQuestionIndex]; |
| 3 | 38 | | } |
| | 39 | |
|
| 9 | 40 | | long skipPoints = 0; |
| | 41 | |
|
| 9 | 42 | | var nextQuestionIndex = currentQuestionIndex + 1; |
| | 43 | |
|
| 9 | 44 | | if (nextQuestionIndex < questions.Length) |
| 7 | 45 | | { |
| 7 | 46 | | skipPoints += maximumPoints[nextQuestionIndex]; |
| 7 | 47 | | } |
| | 48 | |
|
| 9 | 49 | | maximumPoints[currentQuestionIndex] = Math.Max(solvePoints, skipPoints); |
| 9 | 50 | | } |
| | 51 | |
|
| 2 | 52 | | return maximumPoints[0]; |
| 2 | 53 | | } |
| | 54 | | } |