herrDeng網內搜尋

自訂搜尋

Ads

2024年12月27日 星期五

C++Python Greedy DP速解Leetcode 1014 Best Sightseeing Pair


C++Python Greedy DP速解Leetcode 1014  Best Sightseeing Pair算是貪婪演算還是動態規劃,其實都算解法類似Kadane演算法。
-----------
Leetcode 1014. Best Sightseeing Pair is considered greedy algorithm or dynamic programming, but it is actually both. It's similar to Kadane's Algorithm
[Py3 解答請進]

  1. class Solution:
  2. def maxScoreSightseeingPair(self, values: List[int]) -> int:
  3. # score=(values[i]+i)+(values[j]-j) for i<j
  4. # dp[i]=max(values[k]+k for k<=i)
  5. dp, score=0, 0
  6. for i, x in enumerate(values):
  7. score=max(score, dp+x-i)
  8. dp=max(dp, x+i)
  9. return score

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章