Leetcode 1937. Maximum Number of Points with Cost

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 1937. Maximum Number of Points with Cost相关的知识,希望对你有一定的参考价值。

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

2. Solution

**解析:**Version 1,先假设points[i][j]取最大值的上一行数值位于第j列的左侧或右侧,然后分别求第j列的上一行左侧最大值以及右侧最大值,points[i][j]的最大值为其上一行左侧最大值及右侧最大值中较大的一个与其相加,依次更新矩阵,取最后一行的最大值。

  • Version 1
class Solution:
    def maxPoints(self, points: List[List[int]]) -> int:
        m = len(points)
        n = len(points[0])
        left = [0] * n
        right = [0] * n
        for i in range(1, m):
            for k in range(n):
                if k == 0:
                    left[k] = points[i-1][k]
                else:
                    left[k] = max(left[k-1] - 1, points[i-1][k])
            for k in range(n - 1, -1, -1):
                if k == n - 1:
                    right[k] = points[i-1][k]
                else:
                    right[k] = max(right[k+1] - 1, points[i-1][k])
            for j in range(n):
                points[i][j] = points[i][j] + max(left[j], right[j])
        return max(points[m-1])

Reference

  1. https://leetcode.com/problems/maximum-number-of-points-with-cost/

以上是关于Leetcode 1937. Maximum Number of Points with Cost的主要内容,如果未能解决你的问题,请参考以下文章

[Leetcode] Maximum Subarray

[LeetCode]Maximum Subarray

#Leetcode# 53. Maximum Subarray

LeetCode 53. Maximum Subarray

LeetCode -- Maximum Product Subarray

leetcode 53. Maximum Subarray