本帖最后由 lyvlsi 于 2025-2-23 20:49 编辑
. 1point3acres. Waral dи,
Movie Ratings 参考答案- def dp_func(ratings): # all ratings are negative
- n = len(ratings)
- if n==1: return 0
- if n==2: return max(ratings[0], ratings[1])
- dp = [0] * n
- dp[0] = ratings[0]
- dp[1] = ratings[1]
- for i in range(2, n):
- dp[i] = max(dp[i-2], dp[i-1]) + ratings[i]
- return max(dp[n-2], dp[n-1])
- #. check 1point3acres for more.
- def maximizeRatings(s):
- ans = 0
- i = 0
- n = len(ratings). 1point3acres.com
- while i < n:. .и
- if s[i] < 0 :
- j = i+1. Waral dи,
- while j<n and s[j] < 0: j += 1
- ans += dp_func(s[i:j])
- i = j
- else:
- ans += s[i]-baidu 1point3acres
- i += 1
- return ans
- # Example usage:. check 1point3acres for more.
- ratings = [-3, 2, 4, -1, -2, -5] # 4
- print(maximizeRatings(ratings)) # Output: 4
复制代码 |