当前位置:网站首页>LeetCode 1289. Descent path min and II
LeetCode 1289. Descent path min and II
2022-06-24 01:45:00 【freesan44】
Title address (1289. The descent path is the smallest and II)
https://leetcode-cn.com/problems/minimum-falling-path-sum-ii/
Title Description
Here's an integer matrix arr , Definition 「 Nonzero offset descent path 」 by : from arr Select a number for each row in the array , And in the numbers selected in order , Adjacent numbers are not in the same column of the original array .
Please return the minimum value of the sum of the number of nonzero offset descent paths .
Example 1: Input :arr = [[1,2,3],[4,5,6],[7,8,9]] Output :13 explain : All nonzero offset descent paths include : [1,5,9], [1,5,7], [1,6,7], [1,6,8], [2,4,8], [2,4,9], [2,6,7], [2,6,8], [3,4,8], [3,4,9], [3,5,7], [3,5,9] The path of the smallest number and number is the descent [1,5,7] , So the answer is 13 .
Tips :
1 <= arr.length == arri.length <= 200
-99 <= arri <= 99
Ideas
use DP planning , Calculation of different columns min
Code
- Language support :Python3
Python3 Code:
class Solution:
def minFallingPathSum(self, grid: List[List[int]]) -> int:
length = len(grid)
# structure DP Matrix for recording
dp = [[0]*length for i in range(length)]
dp[0] = grid[0]
for i in range(1,length):
lastList = dp[i - 1]
for j in range(length):
val = 0
# print(j,lastList[:j],lastList[j+1:])
# ## Set the particularity of boundary conditions
if j == 0:
val = min(lastList[j+1:])
elif j == length-1:
val = min(lastList[:j])
else:# Calculate the minimum value of the left and right arrays of the previous layer
val = min(min(lastList[:j]),min(lastList[j+1:]))
# Add this value after calculating the minimum value
dp[i][j] = val + grid[i][j]
# print(dp)
return min(dp[-1])
if __name__ == '__main__':
arr = [[1,2,3],[4,5,6],[7,8,9]]
res = Solution().minFallingPathSum(arr)
print(res)Complexity analysis
Make n Is array length .
- Time complexity :$O(n^2)$
- Spatial complexity :$O(n^2)$
边栏推荐
- How to make a fixed asset identification card
- Location and troubleshooting of memory leakage: analysis of heap profiling principle
- How to handle the loading exception in easynvr sharing video playback through iframe?
- Why promote steam education?
- Mobile direct payment, super convenient
- [tcapulusdb knowledge base] how to clean up tables in tcapulusdb table management?
- November 17, 2021: the longest path of the same value. Given a binary tree, find the longest path
- "Cross border" layout photovoltaic circle Green Island wind invested 4.6 million to layout distributed photovoltaic power generation
- 4-data persistence and shared interconnection
- How to access the server through the fortress machine? What if the fortress cannot access the server?
猜你喜欢

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
![[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)
![[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)
随机推荐
Location and troubleshooting of memory leakage: analysis of heap profiling principle
November 15, 2021: add four numbers II. Here are four integer arrays nums1, num
Note 3 of disruptor: basic operation of ring queue (without disruptor class)
4-data persistence and shared interconnection
Glusterfs version 4.1 selection and deployment
Oracle sqlldr quick import and sqluldr2 quick export
Tcapulusdb Jun · industry news collection (November 22)
Is the domain name available for trademark registration? How long is the trademark registration cycle?
How to access the server through the fortress machine? What if the fortress cannot access the server?
Logistics industry supplier collaborative management platform supplier life cycle management to optimize logistics costs
EasyPlayer. JS play m3u8 error net:: err_ EMPTY_ Response, how to solve it?
Can the server be restarted through the fortress machine? How are the fortress machines connected to the server
Analysis of the whole process of webapck5 core packaging principle
Five things programmers need to consider when developing with low code – thenewstack
Tke cluster setup linkerd2
Spatial4j introduction practice
Behind the 1.6 trillion requests per day, the secret of DNSPod - state secret DOH
Interviewer: let's talk about the snowflake algorithm. The more detailed, the better
Go language core 36 lectures (go language practice and application VII) -- learning notes
Disruptor note 2: disruptor class analysis