当前位置:首页|资讯

GFG Maximum Points

作者:您是打尖儿还是住店呢发布时间:2024-10-06

You are given a 2d array of integers arr of size n, where each array of arr is of the form {a ,b },

where 0<=i<n. Start iterating the arr from  rst index to the last index, in order. If you capture

index i, then you get a points, but you can't capture next b indices. But, if you don't capture

index i, then you can move to the index i+1.

Find the maximum number of points that you can gather from arr.

Example 1:

Input:

n = 4

arr = [[3,2],[4,3],[4,4],[2,5]]

Output: 5

Explanation:

The maximum points can be earned by capturing indices 0 and 3.

- Capture index 0: Get 3 points, will be unable to capture the next 2 indices

- Unable to capture indices 1 and 2

- Capture index 3: Get 2 points

Total points got: 3 + 2 = 5. There is no other way to get 5 or more points.

Example 2:

Input:

n = 5

arr = [[1,1],[2,2],[3,3],[4,4],[5,5]]

Output: 7

Explanation:

The maximum points can be earned by capturing indices 1 and 4.

- Skip index 0

- Capture index 1: Earn 2 points, will be unable to capture the next 2 indices

- Unable to capture indices 2 and 3

- Capture index 4: Earn 5 points

Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.

Your Task: 

You have to complete the function maxPoints() which takes an integer n and a 2-d integer

array arr as input, and returns an integer denoting maximum number of points.


Constraints:

1 <= n <= 10

1 <= a , b <= 10

---

第一次写dp写出来了,没想到。

看题解是用递归来做了。于是自己就想着能不能用dp来做。类似于背包问题。当条件满足的时候,就可以依次去比对最大值。真的没想到居然就过了。。。不可思议



Copyright © 2024 aigcdaily.cn  北京智识时代科技有限公司  版权所有  京ICP备2023006237号-1