更新 完全背包理论基础 0139.单词拆分 0279.完全平方数 0322.零钱兑换 0377.组合总和IV 0518.零钱兑换II 多重背包理论基础 背包总结篇 排版格式修复

This commit is contained in:
jinbudaily
2023-07-26 15:28:27 +08:00
parent fc19feb049
commit 7ac217942f
8 changed files with 80 additions and 66 deletions

View File

@ -33,9 +33,9 @@
* 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
* 输出: false
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[你的背包如何装满?| LeetCode139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[你的背包如何装满?| LeetCode139.单词拆分](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>

View File

@ -28,9 +28,9 @@
提示:
* 1 <= n <= 10^4
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[换汤不换药!| LeetCode279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[换汤不换药!| LeetCode279.完全平方数](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>

View File

@ -39,9 +39,9 @@
* 1 <= coins[i] <= 2^31 - 1
* 0 <= amount <= 10^4
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[装满背包最少的物品件数是多少?| LeetCode322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[装满背包最少的物品件数是多少?| LeetCode322.零钱兑换](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>

View File

@ -31,9 +31,9 @@
因此输出为 7。
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[装满背包有几种方法?求排列数?| LeetCode377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[装满背包有几种方法?求排列数?| LeetCode377.组合总和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>

View File

@ -41,9 +41,9 @@
* 硬币种类不超过 500 种
* 结果符合 32 位符号整数
# 算法公开课
## 算法公开课
**《代码随想录》算法视频公开课:[装满背包有多少种方法?组合与排列有讲究!| LeetCode518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[装满背包有多少种方法?组合与排列有讲究!| LeetCode518.零钱兑换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>

View File

@ -106,3 +106,4 @@
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -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>

View File

@ -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>