• Leetcode: 1301.Number of Paths with Max Score


    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 难度的题目还要简单~
  • 相关阅读:
    剑指offer系列——41.和为S的连续正数序列
    剑指offer系列——40.数组中只出现一次的数字i-ii
    指针初始化
    剑指offer系列——39.平衡二叉树
    剑指offer系列——38.二叉树的深度
    剑指offer系列——37.数字在排序数组中出现的次数/在排序数组中查找元素的第一个和最后一个位置
    剑指offer系列——36.两个链表的第一个公共结点?
    剑指offer系列——35.数组中的逆序对**
    查看机器上GPU情况
    Linux下fork()、vfork()、clone()和exec()的区别
  • 原文地址:https://www.cnblogs.com/tmortred/p/13160505.html
Copyright © 2020-2023  润新知