当前位置:网站首页>LeetCode 931. Descent path min sum
LeetCode 931. Descent path min sum
2022-06-24 01:45:00 【freesan44】
Title address (931. The descent path is the smallest and )
https://leetcode-cn.com/problems/minimum-falling-path-sum/
Title Description
To give you one n x n Of square An array of integers matrix , Please find out and return through matrix The descent path Of Minimum and .
descent path You can start with any element in the first line , And select an element from each row . The element selected in the next row is at most one column apart from the element selected in the current row ( That is, the first element directly below or diagonally left or right ). say concretely , Location (row, col) The next element of should be (row + 1, col - 1)、(row + 1, col) perhaps (row + 1, col + 1) .
Example 1: Input :matrix = [[2,1,3],[6,5,4],[7,8,9]] Output :13 explain : Here are the two and smallest descent paths , Bold with + Italic annotation : [[2,1,3], [[2,1,3], [6,5,4], [6,5,4], [7,8,9]] [7,8,9]] Example 2: Input :matrix = [[-19,57],[-40,-5]] Output :-59 explain : Here is a and minimum descent path , Bold with + Italic annotation : [[-19,57], [-40,-5]] Example 3: Input :matrix = [[-48]] Output :-48
Tips :
n == matrix.length
n == matrixi.length
1 <= n <= 100
-100 <= matrixi <= 100
Ideas
DP planning , Storage 【-1】 Array of min Content , Then traverse according to different special conditions
Code
- Language support :Python3
Python3 Code:
class Solution:
def minFallingPathSum(self, matrix: List[List[int]]) -> int:
length = len(matrix)
# structure DP Matrix for recording
dp = [[0]\*length for i in range(length)]
dp[0] = matrix[0]
for i in range(1,length):
for j in range(length):
val = 0
## Set the particularity of boundary conditions
if j == 0:
val = min(dp[i-1][j],dp[i-1][j+1])
elif j == length-1:
val = min(dp[i - 1][j], dp[i - 1][j - 1])
else:
val = min(dp[i - 1][j-1],dp[i - 1][j], dp[i - 1][j + 1])
dp[i][j] = val+matrix[i][j]
return min(dp[-1])
if \_\_name\_\_ == '\_\_main\_\_':
matrix = [[2,1,3],[6,5,4],[7,8,9]]
matrix = [[-19, 57], [-40, -5]]
res = Solution().minFallingPathSum(matrix)
print(res)** Complexity analysis **
Make n Is array length .
- Time complexity :$O(n^2)$
- Spatial complexity :$O(n^2)$
边栏推荐
- Salesforce uses hyperlink formula field to implement custom jump
- What is function point analysis - FPA
- Blog platform was falsely blackmailed and the new hacker organization claimed responsibility for the Israeli attack | November 16 global network security hotspot
- Line/kotlin jdsl: kotlin DSL for JPA criteria API
- Moment. JS how to use epoch time to construct objects
- Technology sharing | Clickhouse cluster's way of expanding replicas under sharding
- [technical grass planting] how can this double eleven be cost-effective!
- November 17, 2021: the longest path of the same value. Given a binary tree, find the longest path
- [tcapulusdb knowledge base] how to clean up tables in tcapulusdb table management?
- [tcapulusdb knowledge base] how to get started with tcapulus SQL driver?
猜你喜欢

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
随机推荐
Introduction to trusted service manager
Logistics industry supplier collaborative management platform supplier life cycle management to optimize logistics costs
Behind the 1.6 trillion requests per day, the secret of DNSPod - state secret DOH
Output type SPED trigger inbound delivery after PGI for inter-company STO's outb
[technical grass planting] cdn+ lightweight server +hugo= let the blog "cloud native"
How to choose a website construction company self-study website or website construction company
[technical grass planting] how can this double eleven be cost-effective!
How does easynvr set the video recording to be saved for more than 30 days?
Introduction to easycvr interfacing with Huawei IVS subscription camera and user change request interface
How to implement NSQ delay streaming technology in easycvr?
Line/kotlin jdsl: kotlin DSL for JPA criteria API
Collation of commonly used glusterfs commands
Online and offline integrated operation of channel sales system in the home furnishing industry to promote product update and iteration
Tencent cloud Weibo was selected into the analysis report on the status quo of China's low code platform market in 2021 by Forrester, an international authoritative research institution
[tcapulusdb knowledge base] how to rebuild tables in tcapulusdb table management?
Salesforce uses hyperlink formula field to implement custom jump
How to make a ECS into a fortress machine how long does it take to build a fortress machine
SMS marketing is the key to retain customers
How to restart the server through the fortress machine how to log in to the fortress machine
Many ministries and commissions strengthened regulation, and Tencent security helped enterprises resist the "mining" Trojan horse