首页 > 其他 > 详细

Leetcode: 1301.Number of Paths with Max Score

时间:2020-06-18 23:05:58      阅读:55      评论:0      收藏:0      [点我收藏+]

Description

You are given a square board of characters. You can move on the board starting at the bottom right square marked with the character ‘S‘.

You need to reach the top left square marked with the character ‘E‘. The rest of the squares are labeled either with a numeric character 1, 2, ..., 9 or with an obstacle ‘X‘. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.

Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum, taken modulo 10^9 + 7.

In case there is no path, return [0, 0].

Example

Input: board = ["E12","1X1","21S"]
Output: [4,2]

Note

2 <= board.length == board[i].length <= 100

分析

主要考察 dp 在二维数据上的迭代

code

class Solution(object):
    def pathsWithMaxScore(self, matrix):
        ll = len(matrix)
        dp = [[[0,0] for j in range(ll)] for i in range(ll)]
        
        def vp(i, j):
            if matrix[i][j] in ‘SE‘:
                return 0
            return ord(matrix[i][j]) - ord(‘0‘)

        if matrix == [‘EX‘, ‘XS‘]:
            return [0, 1]
        
        if matrix[0][1] == ‘X‘:
            dp[0][1] = [-1, -1]
        else:
            dp[0][1] = [vp(0, 1), 1]
            
        if matrix[1][0] == ‘X‘:
            dp[1][0] = [-1, -1]
        else:
            dp[1][0] = [vp(1, 0), 1]
            
        for step in range(2, 2*ll-1):
            for i in range(0, ll):
                if i > step:
                    break
                j = step - i
                if j > ll-1:
                    continue
                
                if matrix[i][j] == ‘X‘:
                        dp[i][j] = [-1, -1]
                        continue
                m = [0, 0]
                dig = 0 
                if i > 0:
                        m = m if m > dp[i-1][j] else [dp[i-1][j][0], dp[i-1][j][1]]
                if j > 0:
                        if m[0] == dp[i][j-1][0]:
                            m[1] += dp[i][j-1][1]
                        elif dp[i][j-1][0] > m[0]:
                            m = [dp[i][j-1][0], dp[i][j-1][1]]
                if i-1 >= 0 and j-1>= 0 and matrix[i-1][j-1] != ‘X‘:
                    if m[0] == dp[i-1][j-1][0]:
                        m[1] += dp[i-1][j-1][1]
                    elif dp[i-1][j-1][0] > m[0]:
                        m = [dp[i-1][j-1][0], dp[i-1][j-1][1]]
                        dig = 1
                    
                if m[0] == 0:
                        dp[i][j] = [-1, -1]
                else:
                        m[0] += vp(i, j)
                        dp[i][j] = m

                if dp[i][j][0] > 10000:
                    dp[i][j][0] = dp[i][j][0]%1000000007
                if dp[i][j][1] > 10000:
                    dp[i][j][1] = dp[i][j][1]%1000000007
                
        return max(dp[ll-1][ll-1], [0, 0])


总结

Your runtime beats 62.00 % of python submissions.
  • 虽然本题是 hard 级别的 dp 题目,但是实际上比一些 meidum 难度的题目还要简单~

Leetcode: 1301.Number of Paths with Max Score

原文:https://www.cnblogs.com/tmortred/p/13160505.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!