zoukankan      html  css  js  c++  java
  • 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 难度的题目还要简单~
  • 相关阅读:
    UNIX网络编程--简介(一)【转】
    linux网络编程--网络编程的基本函数介绍与使用【转】
    微内核VS宏内核【转】
    Linux内核同步机制--自旋锁【转】
    多线程中的信号机制--signwait()函数【转】
    线程同步--递归锁和非递归锁【转】
    linux中线程池【转】
    多进程多线程优先级理解--优先级反转【转】
    【转】如何检测wifi信号强度? -- 不错
    【转】安卓手机有安全模式?安卓4.1安全模式介绍
  • 原文地址:https://www.cnblogs.com/tmortred/p/13160505.html
Copyright © 2011-2022 走看看