李恒道
发表于 2024-9-19 02:40:57
https://leetcode.cn/problems/triangle/submissions/565938667/?envType=study-plan-v2&envId=dynamic-programming
基础dp,秒
```js
var minimumTotal = function (triangle) {
const dp = new Array(triangle.length).fill(0)
for (let index = triangle.length - 1; index >= 0; index--) {
for (let pos = 0; pos <= index; pos++) {
dp = Math.min(dp, dp ?? 0) + triangle
}
}
return dp
};
```
李恒道
发表于 2024-9-19 02:41:26
wuxin0011 发表于 2024-9-18 23:59
这是我LC主页: https://leetcode.cn/u/agitated-curranfnd/
1291!论坛竟然大佬恐怖如斯!
@王一之 快来围观
李恒道
发表于 2024-9-19 02:41:57
wuxin0011 发表于 2024-9-19 00:02
刷题也就图一乐
握草,大佬牛逼啊
我估计刷到年末才能勉强上500
李恒道
发表于 2024-9-19 02:43:31
wuxin0011 发表于 2024-9-18 23:59
这是我LC主页: https://leetcode.cn/u/agitated-curranfnd/
冒昧问一嘴,哥哥大厂工作吗
李恒道
发表于 2024-9-19 02:53:59
https://leetcode.cn/problems/minimum-falling-path-sum/submissions/565939151/?envType=study-plan-v2&envId=dynamic-programming
基础dp,秒
```js
var minFallingPathSum = function (matrix) {
const dp = new Array(matrix.length).fill(0).map(() => {
return new Array(matrix.length).fill(0)
})
dp = matrix
for (let index = matrix.length - 2; index >= 0; index--) {
for (let indey = matrix.length - 1; indey >= 0; indey--) {
const min = Math.min(dp, dp ?? Number.MAX_SAFE_INTEGER,
dp ?? Number.MAX_SAFE_INTEGER
)
dp = min + matrix
}
}
return Math.min(...dp)
};
```
李恒道
发表于 2024-9-19 04:35:00
https://leetcode.cn/problems/maximal-square/submissions/565941333/?envType=study-plan-v2&envId=dynamic-programming
dp的转移方程没理明白
但是凭直觉过了
离谱
```js
var maximalSquare = function (matrix) {
const dp = new Array(matrix.length).fill(0).map(() => {
return new Array(matrix.length).fill(0)
})
dp = matrix.map((num) => parseInt(num))
let maxReact = Math.max(...dp)
for (let index = 1; index < matrix.length; index++) {
for (let indey = 0; indey < matrix.length; indey++) {
if (matrix == 1) {
if (indey !== 0 && dp !== 0 && dp !== 0 && dp !== 0) {
const height = Math.min(dp, dp)
const width = Math.min(dp, dp)
dp = Math.min(height, width)+1
if (dp == 3) {
debugger
}
} else {
dp = 1
}
maxReact = Math.max(maxReact, dp)
}
}
}
return maxReact * maxReact
};
```
李恒道
发表于 2024-9-19 05:10:48
https://leetcode.cn/problems/merge-sorted-array/submissions/565941857/?envType=study-plan-v2&envId=top-interview-150
简单题,放松一下
```js
var merge = function (nums1, m, nums2, n) {
n=n-1;
m=m-1;
let pos = nums1.length - 1
while (m >= 0 || n >= 0) {
if (m < 0) {
nums1 = nums2;
pos--;
n--;
continue;
}
if (n < 0) {
nums1 = nums1;
pos--;
m--;
break;
}
if (nums2 > nums1) {
nums1 = nums2;
pos--;
n--;
continue;
} else {
nums1 = nums1;
pos--;
m--;
continue;
}
}
};
```
![图片.png](data/attachment/forum/202409/19/051041ktb5t55tpyvgbgb5.png)
wuxin0011
发表于 2024-9-19 09:44:59
不是大佬,是菜鸡
李恒道
发表于 2024-9-19 11:01:10
wuxin0011 发表于 2024-9-19 09:44
不是大佬,是菜鸡
哥哥还算菜鸡
我听说刷500道基本中厂面试medium无脑过,hard随缘秒
哥哥1300简直路人王了
李恒道
发表于 2024-9-19 11:01:42
https://leetcode.cn/problems/count-square-submatrices-with-all-ones/
正方形的类似dp题
会了正方形的这题就好过了
```js
var countSquares = function (matrix) {
const dp = new Array(matrix.length).fill(0).map(() => {
return new Array(matrix.length).fill(0)
})
dp = matrix.map((num) => parseInt(num))
let result = 0
for (let index = 0; index < matrix.length; index++) {
if (matrix == 1) {
result++
}
}
for (let index = 1; index < matrix.length; index++) {
for (let indey = 0; indey < matrix.length; indey++) {
if (matrix == 1) {
if (indey !== 0 && dp !== 0 && dp !== 0 && dp !== 0) {
dp = Math.min(dp, dp, dp) + 1
result+=dp
} else {
dp = 1
result++
}
}
}
}
return result
};
```