mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
@ -29,9 +29,9 @@
|
||||
* 1 阶 + 2 阶
|
||||
* 2 阶 + 1 阶
|
||||
|
||||
# 视频讲解
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -522,3 +522,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -127,8 +127,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public int climbStairs(int n) {
|
||||
@ -148,7 +148,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python3:
|
||||
### Python3:
|
||||
|
||||
|
||||
```python
|
||||
@ -166,8 +166,8 @@ class Solution:
|
||||
return dp[n]
|
||||
```
|
||||
|
||||
### Go:
|
||||
|
||||
Go:
|
||||
```go
|
||||
func climbStairs(n int) int {
|
||||
//定义
|
||||
@ -189,7 +189,8 @@ func climbStairs(n int) int {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
var climbStairs = function(n) {
|
||||
const dp = new Array(n + 1).fill(0);
|
||||
@ -206,7 +207,7 @@ var climbStairs = function(n) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function climbStairs(n: number): number {
|
||||
@ -226,7 +227,7 @@ function climbStairs(n: number): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
@ -250,4 +251,3 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -40,8 +40,8 @@ exection -> execution (插入 'u')
|
||||
* 0 <= word1.length, word2.length <= 500
|
||||
* word1 和 word2 由小写英文字母组成
|
||||
|
||||
# 算法公开课
|
||||
**《代码随想录》算法视频公开课:[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
## 算法公开课
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
@ -227,8 +227,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
public int minDistance(String word1, String word2) {
|
||||
int m = word1.length();
|
||||
@ -256,7 +256,8 @@ public int minDistance(String word1, String word2) {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def minDistance(self, word1: str, word2: str) -> int:
|
||||
@ -274,7 +275,8 @@ class Solution:
|
||||
return dp[-1][-1]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```Go
|
||||
func minDistance(word1 string, word2 string) int {
|
||||
m, n := len(word1), len(word2)
|
||||
@ -310,8 +312,8 @@ func Min(args ...int) int {
|
||||
}
|
||||
```
|
||||
|
||||
### Javascript:
|
||||
|
||||
Javascript:
|
||||
```javascript
|
||||
const minDistance = (word1, word2) => {
|
||||
let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0));
|
||||
@ -338,7 +340,7 @@ const minDistance = (word1, word2) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function minDistance(word1: string, word2: string): number {
|
||||
@ -373,7 +375,7 @@ function minDistance(word1: string, word2: string): number {
|
||||
};
|
||||
```
|
||||
|
||||
C:
|
||||
### C:
|
||||
|
||||
|
||||
```c
|
||||
@ -405,3 +407,4 @@ int minDistance(char * word1, char * word2){
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -157,8 +157,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public int numDistinct(String s, String t) {
|
||||
@ -182,7 +182,8 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def numDistinct(self, s: str, t: str) -> int:
|
||||
@ -200,7 +201,8 @@ class Solution:
|
||||
return dp[-1][-1]
|
||||
```
|
||||
|
||||
Python3:
|
||||
### Python3:
|
||||
|
||||
```python
|
||||
class SolutionDP2:
|
||||
"""
|
||||
@ -234,7 +236,8 @@ class SolutionDP2:
|
||||
return dp[-1]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func numDistinct(s string, t string) int {
|
||||
dp:= make([][]int,len(s)+1)
|
||||
@ -259,8 +262,8 @@ func numDistinct(s string, t string) int {
|
||||
}
|
||||
```
|
||||
|
||||
### Javascript:
|
||||
|
||||
Javascript:
|
||||
```javascript
|
||||
const numDistinct = (s, t) => {
|
||||
let dp = Array.from(Array(s.length + 1), () => Array(t.length +1).fill(0));
|
||||
@ -283,7 +286,7 @@ const numDistinct = (s, t) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function numDistinct(s: string, t: string): number {
|
||||
@ -312,9 +315,8 @@ function numDistinct(s: string, t: string): number {
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -24,11 +24,9 @@
|
||||
* 输出:0
|
||||
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
|
||||
|
||||
# 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 算法公开课
|
||||
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
@ -202,7 +200,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
> 贪心法:
|
||||
|
||||
@ -294,8 +292,7 @@ class Solution {
|
||||
|
||||
```
|
||||
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
> 贪心法:
|
||||
```python
|
||||
@ -351,7 +348,8 @@ class Solution:
|
||||
return dp1
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
> 贪心法:
|
||||
```Go
|
||||
func maxProfit(prices []int) int {
|
||||
@ -418,7 +416,7 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
### JavaScript:
|
||||
|
||||
> 动态规划
|
||||
|
||||
@ -454,7 +452,7 @@ var maxProfit = function(prices) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> 贪心法
|
||||
|
||||
@ -492,7 +490,7 @@ function maxProfit(prices: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
C#:
|
||||
### C#:
|
||||
|
||||
> 贪心法
|
||||
|
||||
@ -533,7 +531,7 @@ public class Solution
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
> 贪心
|
||||
|
||||
|
@ -34,9 +34,9 @@
|
||||
* 1 <= prices.length <= 3 * 10 ^ 4
|
||||
* 0 <= prices[i] <= 10 ^ 4
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -133,8 +133,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
// 动态规划
|
||||
class Solution
|
||||
@ -191,7 +191,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
> 版本一:
|
||||
```python
|
||||
@ -221,7 +221,8 @@ class Solution:
|
||||
return dp[(length-1) % 2][1]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
// 买卖股票的最佳时机Ⅱ 动态规划
|
||||
// 时间复杂度:O(n) 空间复杂度:O(n)
|
||||
@ -250,7 +251,8 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
// 方法一:动态规划(dp 数组)
|
||||
const maxProfit = (prices) => {
|
||||
@ -290,7 +292,7 @@ const maxProfit = (prices) => {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> 动态规划
|
||||
|
||||
@ -326,7 +328,7 @@ function maxProfit(prices: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
C#:
|
||||
### C#:
|
||||
|
||||
> 贪心法
|
||||
|
||||
@ -363,7 +365,7 @@ public class Solution
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
> 贪心
|
||||
|
||||
@ -414,4 +416,3 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -39,9 +39,9 @@
|
||||
* 1 <= prices.length <= 10^5
|
||||
* 0 <= prices[i] <= 10^5
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -221,7 +221,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```java
|
||||
// 版本一
|
||||
@ -277,7 +277,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
> 版本一:
|
||||
```python
|
||||
@ -314,7 +314,7 @@ class Solution:
|
||||
return dp[4]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func maxProfit(prices []int) int {
|
||||
@ -344,7 +344,7 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
### JavaScript:
|
||||
|
||||
> 版本一:
|
||||
|
||||
@ -383,7 +383,7 @@ const maxProfit = prices => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> 版本一
|
||||
|
||||
@ -413,7 +413,7 @@ function maxProfit(prices: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
> 版本一
|
||||
|
||||
@ -465,3 +465,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -33,9 +33,9 @@
|
||||
* 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
|
||||
* 输出: false
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -123,7 +123,7 @@ public:
|
||||
|
||||
**这个代码就可以AC了,当然回溯算法不是本题的主菜,背包才是!**
|
||||
|
||||
## 背包问题
|
||||
### 背包问题
|
||||
|
||||
单词就是物品,字符串s就是背包,单词能否组成字符串s,就是问物品能不能把背包装满。
|
||||
|
||||
@ -239,7 +239,7 @@ public:
|
||||
|
||||
}
|
||||
};
|
||||
```
|
||||
```
|
||||
|
||||
使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下:
|
||||
|
||||
@ -259,8 +259,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public boolean wordBreak(String s, List<String> wordDict) {
|
||||
@ -335,7 +335,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
回溯
|
||||
```python
|
||||
@ -397,7 +397,8 @@ class Solution:
|
||||
|
||||
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```Go
|
||||
func wordBreak(s string,wordDict []string) bool {
|
||||
wordDictSet := make(map[string]bool)
|
||||
@ -433,7 +434,8 @@ func wordBreak(s string, wordDict []string) bool {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
const wordBreak = (s, wordDict) => {
|
||||
|
||||
@ -454,7 +456,7 @@ const wordBreak = (s, wordDict) => {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> 动态规划
|
||||
|
||||
@ -496,7 +498,7 @@ function wordBreak(s: string, wordDict: string[]): boolean {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
@ -519,3 +521,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -31,9 +31,9 @@
|
||||
* 0 <= prices.length <= 1000
|
||||
* 0 <= prices[i] <= 1000
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -173,7 +173,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```java
|
||||
// 版本一: 三维 dp数组
|
||||
@ -295,7 +295,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
版本一
|
||||
|
||||
@ -329,7 +329,7 @@ class Solution:
|
||||
dp[j] = max(dp[j],dp[j-1]+prices[i])
|
||||
return dp[2*k]
|
||||
```
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
版本一:
|
||||
|
||||
@ -404,7 +404,7 @@ func max188(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
// 方法一:动态规划
|
||||
@ -454,7 +454,7 @@ var maxProfit = function(k, prices) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function maxProfit(k: number, prices: number[]): number {
|
||||
@ -474,7 +474,7 @@ function maxProfit(k: number, prices: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
@ -529,3 +529,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -31,9 +31,9 @@
|
||||
* 0 <= nums.length <= 100
|
||||
* 0 <= nums[i] <= 400
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -121,8 +121,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```Java
|
||||
// 动态规划
|
||||
class Solution {
|
||||
@ -194,7 +194,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
1维DP
|
||||
```python
|
||||
@ -255,7 +255,8 @@ class Solution:
|
||||
|
||||
|
||||
```
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```Go
|
||||
func rob(nums []int) int {
|
||||
n := len(nums)
|
||||
@ -275,7 +276,7 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
const rob = nums => {
|
||||
@ -291,7 +292,7 @@ const rob = nums => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function rob(nums: number[]): number {
|
||||
@ -314,7 +315,7 @@ function rob(nums: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
@ -338,4 +339,3 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -31,9 +31,9 @@
|
||||
* 1 <= nums.length <= 100
|
||||
* 0 <= nums[i] <= 1000
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -104,8 +104,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```Java
|
||||
class Solution {
|
||||
public int rob(int[] nums) {
|
||||
@ -129,7 +129,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
```Python
|
||||
class Solution:
|
||||
@ -219,7 +219,7 @@ class Solution:
|
||||
|
||||
|
||||
```
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
// 打家劫舍Ⅱ 动态规划
|
||||
@ -257,7 +257,8 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
javascipt:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
var rob = function(nums) {
|
||||
const n = nums.length
|
||||
@ -279,7 +280,7 @@ const robRange = (nums, start, end) => {
|
||||
return dp[end]
|
||||
}
|
||||
```
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function rob(nums: number[]): number {
|
||||
@ -301,7 +302,7 @@ function robRange(nums: number[], start: number, end: number): number {
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
@ -336,3 +337,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -28,9 +28,9 @@
|
||||
提示:
|
||||
* 1 <= n <= 10^4
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -106,8 +106,6 @@ dp[5] = min(dp[4] + 1, dp[1] + 1) = 2
|
||||
|
||||
最后的dp[n]为最终结果。
|
||||
|
||||
## C++代码
|
||||
|
||||
以上动规五部曲分析完毕C++代码如下:
|
||||
|
||||
```CPP
|
||||
@ -165,8 +163,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```Java
|
||||
class Solution {
|
||||
// 版本一,先遍历物品, 再遍历背包
|
||||
@ -219,7 +217,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
先遍历物品, 再遍历背包
|
||||
```python
|
||||
@ -276,7 +274,8 @@ class Solution:
|
||||
|
||||
|
||||
```
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
// 版本一,先遍历物品, 再遍历背包
|
||||
func numSquares1(n int) int {
|
||||
@ -327,7 +326,8 @@ func min(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```Javascript
|
||||
// 先遍历物品,再遍历背包
|
||||
var numSquares1 = function(n) {
|
||||
@ -357,7 +357,7 @@ var numSquares2 = function(n) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
// 先遍历物品
|
||||
@ -389,7 +389,7 @@ function numSquares(n: number): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
// 先遍历背包
|
||||
@ -439,3 +439,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -33,7 +33,7 @@
|
||||
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -124,8 +124,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```Java
|
||||
class Solution {
|
||||
public int lengthOfLIS(int[] nums) {
|
||||
@ -147,7 +147,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
DP
|
||||
```python
|
||||
@ -189,7 +189,8 @@ class Solution:
|
||||
return len(tails) # 返回递增子序列的长度
|
||||
|
||||
```
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
// 动态规划求解
|
||||
func lengthOfLIS(nums []int) int {
|
||||
@ -248,7 +249,8 @@ func lengthOfLIS(nums []int ) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
const lengthOfLIS = (nums) => {
|
||||
let dp = Array(nums.length).fill(1);
|
||||
@ -267,7 +269,7 @@ const lengthOfLIS = (nums) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function lengthOfLIS(nums: number[]): number {
|
||||
@ -288,7 +290,8 @@ function lengthOfLIS(nums: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
|
||||
let mut dp = vec![1; nums.len() + 1];
|
||||
@ -307,13 +310,8 @@ pub fn length_of_lis(nums: Vec<i32>) -> i32 {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -20,9 +20,9 @@
|
||||
* 输出: 3
|
||||
* 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -174,7 +174,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
@ -273,8 +273,9 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
版本一
|
||||
|
||||
```python
|
||||
from typing import List
|
||||
|
||||
@ -319,7 +320,8 @@ class Solution:
|
||||
return max(dp[-1][1], dp[-1][2])
|
||||
|
||||
```
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
// 最佳买卖股票时机含冷冻期 动态规划
|
||||
// 时间复杂度O(n) 空间复杂度O(n)
|
||||
@ -355,7 +357,7 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
const maxProfit = (prices) => {
|
||||
@ -397,7 +399,7 @@ const maxProfit = (prices) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> 版本一,与本文思路一致
|
||||
|
||||
@ -455,7 +457,7 @@ function maxProfit(prices: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
@ -484,3 +486,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -39,9 +39,9 @@
|
||||
* 1 <= coins[i] <= 2^31 - 1
|
||||
* 0 <= amount <= 10^4
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
|
||||
@ -110,7 +110,6 @@ dp[0] = 0;
|
||||
|
||||
dp[amount]为最终结果。
|
||||
|
||||
## C++代码
|
||||
以上分析完毕,C++ 代码如下:
|
||||
|
||||
```CPP
|
||||
@ -187,8 +186,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```Java
|
||||
class Solution {
|
||||
public int coinChange(int[] coins, int amount) {
|
||||
@ -215,7 +214,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
|
||||
先遍历物品 后遍历背包
|
||||
@ -288,7 +287,8 @@ class Solution:
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
// 版本一, 先遍历物品,再遍历背包
|
||||
func coinChange1(coins []int, amount int) int {
|
||||
@ -352,7 +352,7 @@ func min(a, b int) int {
|
||||
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
// 遍历物品
|
||||
@ -398,7 +398,8 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
// 遍历物品
|
||||
const coinChange = (coins, amount) => {
|
||||
@ -435,7 +436,7 @@ var coinChange = function(coins, amount) {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
// 遍历物品
|
||||
@ -473,3 +474,4 @@ function coinChange(coins: number[], amount: number): number {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -16,9 +16,9 @@
|
||||
|
||||

|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -523,3 +523,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -31,9 +31,9 @@
|
||||
|
||||
因此输出为 7。
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
@ -154,8 +154,7 @@ C++测试用例有两个数相加超过int的数据,所以需要在if里加上
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```Java
|
||||
class Solution {
|
||||
@ -174,7 +173,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
|
||||
卡哥版
|
||||
@ -207,7 +206,8 @@ class Solution:
|
||||
|
||||
|
||||
```
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func combinationSum4(nums []int, target int) int {
|
||||
//定义dp数组
|
||||
@ -226,7 +226,8 @@ func combinationSum4(nums []int, target int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
const combinationSum4 = (nums, target) => {
|
||||
|
||||
@ -245,7 +246,7 @@ const combinationSum4 = (nums, target) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function combinationSum4(nums: number[], target: number): number {
|
||||
@ -264,7 +265,7 @@ function combinationSum4(nums: number[], target: number): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust
|
||||
### Rust:
|
||||
|
||||
```Rust
|
||||
impl Solution {
|
||||
@ -289,3 +290,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -28,9 +28,9 @@
|
||||
|
||||
两个字符串都只由小写字符组成。
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -149,8 +149,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
@ -174,7 +174,8 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def isSubsequence(self, s: str, t: str) -> bool:
|
||||
@ -190,7 +191,7 @@ class Solution:
|
||||
return False
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
const isSubsequence = (s, t) => {
|
||||
@ -213,7 +214,7 @@ const isSubsequence = (s, t) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function isSubsequence(s: string, t: string): boolean {
|
||||
@ -237,7 +238,7 @@ function isSubsequence(s: string, t: string): boolean {
|
||||
};
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func isSubsequence(s string, t string) bool {
|
||||
@ -266,3 +267,4 @@ func isSubsequence(s string, t string) bool {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -30,9 +30,9 @@
|
||||
* 1 <= nums.length <= 200
|
||||
* 1 <= nums[i] <= 100
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划之背包问题,这个包能装满吗?| LeetCode:416.分割等和子集](https://www.bilibili.com/video/BV1rt4y1N7jE/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之背包问题,这个包能装满吗?| LeetCode:416.分割等和子集](https://www.bilibili.com/video/BV1rt4y1N7jE/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -53,7 +53,7 @@
|
||||
* [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html)
|
||||
* [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)
|
||||
|
||||
## 01背包问题
|
||||
### 01背包问题
|
||||
|
||||
背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
|
||||
|
||||
@ -479,7 +479,7 @@ func canPartition(nums []int) bool {
|
||||
}
|
||||
```
|
||||
|
||||
### javaScript:
|
||||
### JavaScript:
|
||||
|
||||
```js
|
||||
var canPartition = function(nums) {
|
||||
@ -499,7 +499,7 @@ var canPartition = function(nums) {
|
||||
```
|
||||
|
||||
|
||||
### Rust
|
||||
### Rust:
|
||||
|
||||
```Rust
|
||||
impl Solution {
|
||||
@ -681,7 +681,7 @@ function canPartition(nums: number[]): boolean {
|
||||
};
|
||||
```
|
||||
|
||||
### Scala
|
||||
### Scala:
|
||||
|
||||
滚动数组:
|
||||
```scala
|
||||
@ -730,3 +730,4 @@ object Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -34,9 +34,9 @@
|
||||
* strs[i] 仅由 '0' 和 '1' 组成
|
||||
* 1 <= m, n <= 100
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -538,3 +538,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -37,9 +37,9 @@
|
||||
* 初始的数组的和不会超过 1000 。
|
||||
* 保证返回的最终结果能被 32 位整数存下。
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -67,7 +67,7 @@ target是固定的,sum是固定的,left就可以求出来。
|
||||
|
||||
此时问题就是在集合nums中找出和为left的组合。
|
||||
|
||||
## 回溯算法
|
||||
### 回溯算法
|
||||
|
||||
在回溯算法系列中,一起学过这道题目[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)的录友应该感觉很熟悉,这不就是组合总和问题么?
|
||||
|
||||
@ -118,7 +118,7 @@ public:
|
||||
|
||||
也可以使用记忆化回溯,但这里我就不在回溯上下功夫了,直接看动规吧
|
||||
|
||||
## 动态规划
|
||||
### 动态规划
|
||||
|
||||
如何转化为01背包问题呢。
|
||||
|
||||
@ -519,8 +519,6 @@ const findTargetSumWays = (nums, target) => {
|
||||
|
||||
### TypeScript
|
||||
|
||||
TypeScript:
|
||||
|
||||
```ts
|
||||
function findTargetSumWays(nums: number[], target: number): number {
|
||||
// 把数组分成两个组合left, right.left + right = sum, left - right = target.
|
||||
@ -590,3 +588,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -32,9 +32,9 @@ F(n) = F(n - 1) + F(n - 2),其中 n > 1
|
||||
|
||||
* 0 <= n <= 30
|
||||
|
||||
# 视频讲解
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
@ -278,7 +278,7 @@ class Solution:
|
||||
return self.fib(n - 1) + self.fib(n - 2)
|
||||
```
|
||||
|
||||
### Go:
|
||||
### Go
|
||||
```Go
|
||||
func fib(n int) int {
|
||||
if n < 2 {
|
||||
@ -479,3 +479,4 @@ public class Solution
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -152,8 +152,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```java
|
||||
public class Solution {
|
||||
@ -175,8 +174,7 @@ public class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
@ -193,7 +191,7 @@ class Solution:
|
||||
return dp[0][-1]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```Go
|
||||
func longestPalindromeSubseq(s string) int {
|
||||
@ -222,7 +220,7 @@ func longestPalindromeSubseq(s string) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
const longestPalindromeSubseq = (s) => {
|
||||
@ -247,7 +245,7 @@ const longestPalindromeSubseq = (s) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function longestPalindromeSubseq(s: string): number {
|
||||
@ -281,3 +279,4 @@ function longestPalindromeSubseq(s: string): number {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -41,9 +41,9 @@
|
||||
* 硬币种类不超过 500 种
|
||||
* 结果符合 32 位符号整数
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
|
||||
@ -202,8 +202,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```Java
|
||||
class Solution {
|
||||
public int change(int amount, int[] coins) {
|
||||
@ -242,7 +242,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
|
||||
```python
|
||||
@ -260,7 +260,8 @@ class Solution:
|
||||
|
||||
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func change(amount int, coins []int) int {
|
||||
// 定义dp数组
|
||||
@ -280,7 +281,8 @@ func change(amount int, coins []int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn change(amount: i32, coins: Vec<i32>) -> i32 {
|
||||
@ -297,7 +299,8 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
const change = (amount, coins) => {
|
||||
let dp = Array(amount + 1).fill(0);
|
||||
@ -313,7 +316,7 @@ const change = (amount, coins) => {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function change(amount: number, coins: number[]): number {
|
||||
@ -328,7 +331,7 @@ function change(amount: number, coins: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
Scala:
|
||||
### Scala:
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
@ -349,4 +352,3 @@ object Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -17,9 +17,9 @@
|
||||
* 解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"
|
||||
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划之子序列,还是为了编辑距离做铺垫 | LeetCode:583.两个字符串的删除操(https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[LeetCode:583.两个字符串的删除操](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -143,8 +143,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
// dp数组中存储word1和word2最长相同子序列的长度
|
||||
class Solution {
|
||||
@ -215,8 +215,8 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
### Python:
|
||||
|
||||
Python:
|
||||
```python
|
||||
class Solution:
|
||||
def minDistance(self, word1: str, word2: str) -> int:
|
||||
@ -234,7 +234,8 @@ class Solution:
|
||||
return dp[-1][-1]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func minDistance(word1 string, word2 string) int {
|
||||
dp := make([][]int, len(word1)+1)
|
||||
@ -267,7 +268,8 @@ func min(a, b int) int {
|
||||
return b
|
||||
}
|
||||
```
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
// 方法一
|
||||
var minDistance = (word1, word2) => {
|
||||
@ -309,7 +311,7 @@ var minDistance = function (word1, word2) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> dp版本一:
|
||||
|
||||
|
@ -26,11 +26,13 @@
|
||||
|
||||
提示:输入的字符串长度不会超过 1000 。
|
||||
|
||||
## 暴力解法
|
||||
## 思路
|
||||
|
||||
### 暴力解法
|
||||
|
||||
两层for循环,遍历区间起始位置和终止位置,然后还需要一层遍历判断这个区间是不是回文。所以时间复杂度:O(n^3)
|
||||
|
||||
## 动态规划
|
||||
### 动态规划
|
||||
|
||||
动规五部曲:
|
||||
|
||||
@ -187,7 +189,7 @@ public:
|
||||
* 时间复杂度:O(n^2)
|
||||
* 空间复杂度:O(n^2)
|
||||
|
||||
## 双指针法
|
||||
### 双指针法
|
||||
|
||||
动态规划的空间复杂度是偏高的,我们再看一下双指针法。
|
||||
|
||||
@ -231,7 +233,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
动态规划:
|
||||
|
||||
@ -337,7 +339,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
> 动态规划:
|
||||
```python
|
||||
@ -390,7 +392,8 @@ class Solution:
|
||||
return res
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```Go
|
||||
func countSubstrings(s string) int {
|
||||
res:=0
|
||||
@ -416,7 +419,8 @@ func countSubstrings(s string) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript
|
||||
### Javascript:
|
||||
|
||||
> 动态规划
|
||||
```javascript
|
||||
const countSubstrings = (s) => {
|
||||
@ -462,7 +466,7 @@ const countSubstrings = (s) => {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> 动态规划:
|
||||
|
||||
@ -524,3 +528,4 @@ function expandRange(s: string, left: number, right: number): number {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
||||
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -157,8 +157,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
> 动态规划:
|
||||
```java
|
||||
@ -207,7 +206,7 @@ public static int findLengthOfLCIS(int[] nums) {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
DP
|
||||
```python
|
||||
@ -261,7 +260,8 @@ class Solution:
|
||||
return result
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
> 动态规划:
|
||||
```go
|
||||
func findLengthOfLCIS(nums []int) int {
|
||||
@ -302,7 +302,8 @@ func findLengthOfLCIS(nums []int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
|
||||
if nums.is_empty() {
|
||||
@ -320,7 +321,7 @@ pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
> 动态规划:
|
||||
```javascript
|
||||
@ -363,7 +364,7 @@ const findLengthOfLCIS = (nums) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> 动态规划:
|
||||
|
||||
@ -409,3 +410,4 @@ function findLengthOfLCIS(nums: number[]): number {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -32,9 +32,9 @@
|
||||
* 0 < prices[i] < 50000.
|
||||
* 0 <= fee < 50000.
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -97,8 +97,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
/**
|
||||
* 卖出时支付手续费
|
||||
@ -173,9 +173,9 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
### python
|
||||
|
||||
|
||||
Python:
|
||||
```python
|
||||
class Solution:
|
||||
def maxProfit(self, prices: List[int], fee: int) -> int:
|
||||
@ -188,7 +188,8 @@ class Solution:
|
||||
return max(dp[-1][0], dp[-1][1])
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
// 买卖股票的最佳时机含手续费 动态规划
|
||||
// 时间复杂度O(n) 空间复杂度O(n)
|
||||
@ -211,7 +212,8 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
const maxProfit = (prices,fee) => {
|
||||
let dp = Array.from(Array(prices.length), () => Array(2).fill(0));
|
||||
@ -224,7 +226,7 @@ const maxProfit = (prices,fee) => {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function maxProfit(prices: number[], fee: number): number {
|
||||
@ -245,8 +247,9 @@ function maxProfit(prices: number[], fee: number): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
**贪心**
|
||||
|
||||
```Rust
|
||||
impl Solution {
|
||||
pub fn max_profit(prices: Vec<i32>, fee: i32) -> i32 {
|
||||
|
@ -25,9 +25,7 @@
|
||||
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划之子序列问题,想清楚DP数组的定义 | LeetCode:718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,想清楚DP数组的定义 | LeetCode:718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
@ -126,7 +124,7 @@ public:
|
||||
* 时间复杂度:O(n × m),n 为A长度,m为B长度
|
||||
* 空间复杂度:O(n × m)
|
||||
|
||||
## 滚动数组
|
||||
### 滚动数组
|
||||
|
||||
在如下图中:
|
||||
|
||||
@ -257,8 +255,8 @@ class Solution {
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
// 版本一
|
||||
class Solution {
|
||||
@ -300,7 +298,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
2维DP
|
||||
```python
|
||||
@ -395,7 +393,8 @@ class Solution:
|
||||
|
||||
|
||||
```
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```Go
|
||||
func findLength(A []int, B []int) int {
|
||||
m, n := len(A), len(B)
|
||||
@ -442,7 +441,7 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
### JavaScript:
|
||||
|
||||
> 动态规划
|
||||
|
||||
@ -489,7 +488,7 @@ const findLength = (nums1, nums2) => {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> 动态规划:
|
||||
|
||||
@ -544,3 +543,4 @@ function findLength(nums1: number[], nums2: number[]): number {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -37,9 +37,9 @@
|
||||
* cost[i] 将会是一个整型数据,范围为 [0, 999] 。
|
||||
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
-----------
|
||||
@ -523,3 +523,4 @@ public class Solution
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -82,8 +82,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public int maxUncrossedLines(int[] nums1, int[] nums2) {
|
||||
@ -106,7 +106,8 @@ Java:
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def maxUncrossedLines(self, A: List[int], B: List[int]) -> int:
|
||||
@ -120,8 +121,7 @@ class Solution:
|
||||
return dp[-1][-1]
|
||||
```
|
||||
|
||||
|
||||
Golang:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func maxUncrossedLines(A []int, B []int) int {
|
||||
@ -152,7 +152,7 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
pub fn max_uncrossed_lines(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
|
||||
@ -173,7 +173,7 @@ pub fn max_uncrossed_lines(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
const maxUncrossedLines = (nums1, nums2) => {
|
||||
@ -196,7 +196,7 @@ const maxUncrossedLines = (nums1, nums2) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function maxUncrossedLines(nums1: number[], nums2: number[]): number {
|
||||
@ -224,3 +224,4 @@ function maxUncrossedLines(nums1: number[], nums2: number[]): number {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -35,9 +35,9 @@
|
||||
* 1 <= stones.length <= 30
|
||||
* 1 <= stones[i] <= 1000
|
||||
|
||||
# 算法公开课
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -341,7 +341,7 @@ func max(a, b int) int {
|
||||
}
|
||||
```
|
||||
|
||||
### JavaScript
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
/**
|
||||
@ -364,7 +364,7 @@ var lastStoneWeightII = function (stones) {
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
### C:
|
||||
```c
|
||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
|
||||
@ -413,7 +413,7 @@ function lastStoneWeightII(stones: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
### Scala
|
||||
### Scala:
|
||||
|
||||
滚动数组:
|
||||
```scala
|
||||
@ -455,7 +455,7 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
@ -477,3 +477,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -39,7 +39,7 @@
|
||||
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
@ -136,7 +136,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```java
|
||||
/*
|
||||
@ -207,8 +207,9 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
2维DP
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
|
||||
@ -252,7 +253,8 @@ class Solution:
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```Go
|
||||
func longestCommonSubsequence(text1 string, text2 string) int {
|
||||
t1 := len(text1)
|
||||
@ -283,7 +285,8 @@ func max(a,b int)int {
|
||||
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### JavaScript:
|
||||
|
||||
```javascript
|
||||
const longestCommonSubsequence = (text1, text2) => {
|
||||
let dp = Array.from(Array(text1.length+1), () => Array(text2.length+1).fill(0));
|
||||
@ -302,7 +305,7 @@ const longestCommonSubsequence = (text1, text2) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function longestCommonSubsequence(text1: string, text2: string): number {
|
||||
@ -326,7 +329,8 @@ function longestCommonSubsequence(text1: string, text2: string): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
|
||||
let (n, m) = (text1.len(), text2.len());
|
||||
@ -353,3 +357,4 @@ pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -163,8 +163,8 @@ else {
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public int minDistance(String word1, String word2) {
|
||||
@ -193,11 +193,6 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
Go:
|
||||
|
||||
|
||||
|
||||
|
||||
@ -205,3 +200,4 @@ Go:
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -471,21 +471,10 @@ public:
|
||||
「代码随想录」值得推荐给身边每一位学习算法的朋友同学们,关注后都会发现相见恨晚!
|
||||
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
Go:
|
||||
|
||||
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -136,3 +136,4 @@
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -12,7 +12,7 @@
|
||||
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 什么是动态规划
|
||||
@ -135,3 +135,4 @@
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# 本周小结!(动态规划系列一)
|
||||
|
||||
这周我们正式开始动态规划的学习!
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# 本周小结!(动态规划系列二)
|
||||
|
||||
## 周一
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# 本周小结!(动态规划系列六)
|
||||
|
||||
本周我们主要讲解了打家劫舍系列,这个系列也是dp解决的经典问题,那么来看看我们收获了哪些呢,一起来回顾一下吧。
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
# 本周小结!(动态规划系列七)
|
||||
|
||||
本周的主题就是股票系列,来一起回顾一下吧
|
||||
|
||||
|
@ -106,3 +106,4 @@
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -8,8 +8,11 @@
|
||||
|
||||
# 动态规划:01背包理论基础
|
||||
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
这周我们正式开始讲解背包问题!
|
||||
@ -37,7 +40,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目,
|
||||
|
||||
之前可能有些录友已经可以熟练写出背包了,但只要把这个文章仔细看完,相信你会意外收获!
|
||||
|
||||
## 01 背包
|
||||
### 01 背包
|
||||
|
||||
有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。
|
||||
|
||||
@ -67,7 +70,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目,
|
||||
|
||||
以下讲解和图示中出现的数字都是以这个例子为例。
|
||||
|
||||
## 二维dp数组01背包
|
||||
### 二维dp数组01背包
|
||||
|
||||
依然动规五部曲分析一波。
|
||||
|
||||
@ -226,9 +229,6 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括
|
||||
|
||||
主要就是自己没有动手推导一下dp数组的演变过程,如果推导明白了,代码写出来就算有问题,只要把dp数组打印出来,对比一下和自己推导的有什么差异,很快就可以发现问题了。
|
||||
|
||||
|
||||
## 完整c++测试代码
|
||||
|
||||
```cpp
|
||||
void test_2_wei_bag_problem1() {
|
||||
vector<int> weight = {1, 3, 4};
|
||||
@ -275,7 +275,7 @@ int main() {
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### java
|
||||
### Java
|
||||
|
||||
```java
|
||||
public class BagProblem {
|
||||
@ -396,7 +396,8 @@ public class BagProblem {
|
||||
|
||||
```
|
||||
|
||||
### python
|
||||
### Python
|
||||
|
||||
无参数版
|
||||
```python
|
||||
def test_2_wei_bag_problem1():
|
||||
@ -456,8 +457,7 @@ if __name__ == "__main__":
|
||||
|
||||
```
|
||||
|
||||
|
||||
### go
|
||||
### Go
|
||||
|
||||
```go
|
||||
func test_2_wei_bag_problem1(weight, value []int, bagweight int) int {
|
||||
@ -498,7 +498,7 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### javascript
|
||||
### Javascript
|
||||
|
||||
```js
|
||||
function testWeightBagProblem (weight, value, size) {
|
||||
|
@ -3,10 +3,13 @@
|
||||
<img src="../pics/训练营.png" width="1000"/>
|
||||
</a>
|
||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
# 动态规划:01背包理论基础(滚动数组)
|
||||
|
||||
**《代码随想录》算法视频公开课:[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
## 算法公开课
|
||||
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
昨天[动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html)中是用二维dp数组来讲解01背包。
|
||||
@ -29,7 +32,7 @@
|
||||
|
||||
问背包能背的物品最大价值是多少?
|
||||
|
||||
## 一维dp数组(滚动数组)
|
||||
### 一维dp数组(滚动数组)
|
||||
|
||||
对于背包问题其实状态都是可以压缩的。
|
||||
|
||||
@ -154,8 +157,6 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15
|
||||
|
||||
|
||||
|
||||
## 一维dp01背包完整C++测试代码
|
||||
|
||||
```CPP
|
||||
void test_1_wei_bag_problem() {
|
||||
vector<int> weight = {1, 3, 4};
|
||||
@ -318,7 +319,7 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### javaScript
|
||||
### JavaScript
|
||||
|
||||
```js
|
||||
|
||||
@ -459,3 +460,4 @@ fn test_wei_bag_problem2() {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -144,8 +144,7 @@ int main() {
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```Java
|
||||
public void testMultiPack1(){
|
||||
@ -192,7 +191,7 @@ public void testMultiPack2(){
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
|
||||
改变物品数量为01背包格式(无参版)
|
||||
```python
|
||||
@ -315,7 +314,7 @@ if __name__ == "__main__":
|
||||
test_multi_pack(weight, value, nums, bagWeight)
|
||||
|
||||
```
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
package theory
|
||||
@ -406,7 +405,7 @@ func Test_multiplePack(t *testing.T) {
|
||||
PASS
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
> 版本一(改变数据源):
|
||||
|
||||
@ -469,3 +468,4 @@ testMultiPack();
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -7,9 +7,13 @@
|
||||
|
||||
# 动态规划:完全背包理论基础
|
||||
|
||||
**《代码随想录》算法视频公开课:[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
## 算法公开课
|
||||
|
||||
## 完全背包
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
### 完全背包
|
||||
|
||||
|
||||
有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品都有无限个(也就是可以放入背包多次)**,求解将哪些物品装入背包里物品价值总和最大。
|
||||
@ -113,8 +117,6 @@ for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量
|
||||
}
|
||||
```
|
||||
|
||||
## C++测试代码
|
||||
|
||||
完整的C++测试代码如下:
|
||||
|
||||
```CPP
|
||||
@ -181,7 +183,7 @@ int main() {
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```java
|
||||
//先遍历物品,再遍历背包
|
||||
@ -221,9 +223,7 @@ private static void testCompletePackAnotherWay(){
|
||||
|
||||
|
||||
|
||||
Python:
|
||||
|
||||
|
||||
### Python:
|
||||
|
||||
先遍历物品,再遍历背包(无参版)
|
||||
```python
|
||||
@ -299,7 +299,8 @@ if __name__ == "__main__":
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
|
||||
// test_CompletePack1 先遍历物品, 在遍历背包
|
||||
@ -352,7 +353,8 @@ func main() {
|
||||
fmt.Println(test_CompletePack2(weight, price, 4))
|
||||
}
|
||||
```
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
```Javascript
|
||||
// 先遍历物品,再遍历背包容量
|
||||
function test_completePack1() {
|
||||
@ -385,7 +387,7 @@ function test_completePack2() {
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
// 先遍历物品,再遍历背包容量
|
||||
@ -404,7 +406,7 @@ function test_CompletePack(): void {
|
||||
test_CompletePack();
|
||||
```
|
||||
|
||||
Scala:
|
||||
### Scala:
|
||||
|
||||
```scala
|
||||
// 先遍历物品,再遍历背包容量
|
||||
@ -426,7 +428,7 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
@ -468,3 +470,4 @@ fn test_complete_pack() {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user