Skip to content

Commit 8b64043

Browse files
Update
1 parent 560b852 commit 8b64043

6 files changed

+145
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
* [关于贪心算法,你该了解这些!](https://mp.weixin.qq.com/s/O935TaoHE9Eexwe_vSbRAg)
174174
* [贪心算法:分发饼干](https://mp.weixin.qq.com/s/YSuLIAYyRGlyxbp9BNC1uw)
175175
* [贪心算法:摆动序列](https://mp.weixin.qq.com/s/Xytl05kX8LZZ1iWWqjMoHA)
176+
* [贪心算法:最大子序和](https://mp.weixin.qq.com/s/DrjIQy6ouKbpletQr0g1Fg)
176177

177178

178179
* 动态规划
@@ -333,12 +334,16 @@
333334
|[0973.最接近原点的K个点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0973.最接近原点的K个点.md) |优先级队列 |中等|**优先级队列**|
334335
|[0977.有序数组的平方](https://github.com/youngyangyang04/leetcode/blob/master/problems/0977.有序数组的平方.md) |数组 |中等|**双指针** 还是比较巧妙的|
335336
|[1002.查找常用字符](https://github.com/youngyangyang04/leetcode/blob/master/problems/1002.查找常用字符.md) ||简单|****|
337+
|[1005.K次取反后最大化的数组和](https://github.com/youngyangyang04/leetcode/blob/master/problems/1005.K次取反后最大化的数组和.md) |贪心/排序 |**贪心算法** 贪心基础题目|
336338
|[1047.删除字符串中的所有相邻重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/1047.删除字符串中的所有相邻重复项.md) |哈希表 |简单|**哈希表/数组**|
337339
|[1049.最后一块石头的重量II](https://github.com/youngyangyang04/leetcode/blob/master/problems/1049.最后一块石头的重量II.md) |动态规划 |中等|**01背包**|
338340
|[1207.独一无二的出现次数](https://github.com/youngyangyang04/leetcode/blob/master/problems/1207.独一无二的出现次数.md) |哈希表 |简单|**哈希** 两层哈希|
341+
|[1221.分割平衡字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/1221.分割平衡字符串.md) |贪心 |简单|**贪心算法** 基础题目|
339342
|[1356.根据数字二进制下1的数目排序](https://github.com/youngyangyang04/leetcode/blob/master/problems/1356.根据数字二进制下1的数目排序.md) |位运算 |简单|**位运算** 巧妙的计算二进制中1的数量|
340343
|[1365.有多少小于当前数字的数字](https://github.com/youngyangyang04/leetcode/blob/master/problems/1365.有多少小于当前数字的数字.md) |数组、哈希表 |简单|**哈希** 从后遍历的技巧很不错|
341344
|[1382.将二叉搜索树变平衡](https://github.com/youngyangyang04/leetcode/blob/master/problems/1047.删除字符串中的所有相邻重复项.md) |二叉搜索树 |中等|**递归** **迭代** 98和108的组合题目|
345+
|[1403.非递增顺序的最小子序列](https://github.com/youngyangyang04/leetcode/blob/master/problems/1403.非递增顺序的最小子序列.md) | 贪心算法|简单|**贪心算法** 贪心基础题目|
346+
|[1518.换酒问题](https://github.com/youngyangyang04/leetcode/blob/master/problems/1518.换酒问题.md) | 贪心算法|简单|**贪心算法** 贪心基础题目|
342347
|[剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md) |字符串 |简单|**双指针**|
343348
|[ 剑指Offer58-I.翻转单词顺序](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md) |字符串 |简单|**模拟/双指针**|
344349
|[剑指Offer58-II.左旋转字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer58-II.左旋转字符串.md) |字符串 |简单|**反转操作**|

problems/0322.零钱兑换.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
[1] 0 ,输出的是0,不是-1啊,这颗真是天坑j
3+
4+
```
5+
// dp初始化很重要
6+
class Solution {
7+
public:
8+
int coinChange(vector<int>& coins, int amount) {
9+
//int dp[10003] = {0}; // 并没有给所有元素赋值0
10+
if (amount == 0) return 0; // 这个要注意
11+
vector<int> dp(10003, 0);
12+
// 不能这么初始化啊,[2147483647],2 这种例子 直接gg,但是这种初始化有助于理解
13+
for (int i = 0; i < coins.size(); i++) {
14+
if (coins[i] <= amount) // 还必须要加这个判断
15+
dp[coins[i]] = 1;
16+
}
17+
for (int i = 1; i <= amount; i++) {
18+
for (int j = 0; j < coins.size(); j++) {
19+
if (i - coins[j] >= 0 && dp[i - coins[j]]!=0 ) {
20+
if (dp[i] == 0) dp[i] = dp[i - coins[j]] + 1;
21+
else dp[i] = min(dp[i - coins[j]] + 1, dp[i]);
22+
}
23+
24+
25+
}
26+
//for (int k = 0 ; k<= amount; k++) {
27+
// cout << dp[k] << " ";
28+
//}
29+
//cout << endl;
30+
}
31+
if (dp[amount] == 0) return -1;
32+
return dp[amount];
33+
34+
}
35+
};
36+
```
37+
38+
这种标记d代码简短,但思路有点绕
39+
```
40+
class Solution {
41+
public:
42+
int coinChange(vector<int>& coins, int amount) {
43+
//int dp[10003] = {0}; // 并没有给所有元素赋值0
44+
// if (amount == 0) return 0; 这个都可以省略了,但很多同学不知道 还需要注意这个
45+
vector<int> dp(10003, 0);
46+
for (int i = 1; i <= amount; i++) {
47+
dp[i] = INT_MAX;
48+
for (int j = 0; j < coins.size(); j++) {
49+
if (i - coins[j] >= 0 && dp[i - coins[j]]!=INT_MAX ) {
50+
dp[i] = min(dp[i - coins[j]] + 1, dp[i]);
51+
}
52+
}
53+
}
54+
if (dp[amount] == INT_MAX) return -1;
55+
return dp[amount];
56+
}
57+
};
58+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
```
3+
class Solution {
4+
static bool cmp(int a, int b) {
5+
return abs(a) < abs(b);
6+
}
7+
public:
8+
int largestSumAfterKNegations(vector<int>& A, int K) {
9+
sort(A.begin(), A.end(), cmp);
10+
for (int i = A.size() - 1; i >= 0; i--) {
11+
if (A[i] < 0 && K > 0) {
12+
A[i] *= -1;
13+
K--;
14+
}
15+
}
16+
while (K--) A[0] *= -1;
17+
int result = 0;
18+
for (int a : A) result += a;
19+
return result;
20+
}
21+
};
22+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
这是贪心啊,LRLR 这本身就是平衡子串 , 但要LR这么分割,这是贪心
3+
4+
5+
```
6+
class Solution {
7+
public:
8+
int balancedStringSplit(string s) {
9+
int result = 0;
10+
int count = 0;
11+
for (int i = 0; i < s.size(); i++) {
12+
if (s[i] == 'R') count++;
13+
else count--;
14+
if (count == 0) result++;
15+
}
16+
return result;
17+
}
18+
};
19+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
你说这是简单题吧,也是这就使用了贪心算法
3+
4+
```
5+
class Solution {
6+
private:
7+
static bool cmp(int a, int b) {
8+
return a > b;
9+
}
10+
public:
11+
vector<int> minSubsequence(vector<int>& nums) {
12+
sort(nums.begin(), nums.end(), cmp);
13+
int sum = 0;
14+
for (int i = 0; i < nums.size(); i++) sum += nums[i];
15+
vector<int> result;
16+
int resultSum = 0;
17+
for (int i = 0; i < nums.size(); i++) {
18+
resultSum += nums[i];
19+
result.push_back(nums[i]);
20+
if (resultSum > (sum - resultSum)) break;
21+
}
22+
return result;
23+
}
24+
};
25+
```

problems/1518.换酒问题.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
```
3+
// 这道题还是有陷阱啊,15 4 这个例子,答案应该是19 而不是18
4+
class Solution {
5+
public:
6+
int numWaterBottles(int numBottles, int numExchange) {
7+
int result = numBottles;
8+
while (numBottles / numExchange) {
9+
result += numBottles / numExchange;
10+
// 所以不是 numBottles = (numBottles / numExchange)
11+
numBottles = (numBottles / numExchange) + (numBottles % numExchange);
12+
}
13+
return result;
14+
}
15+
};
16+
```

0 commit comments

Comments
 (0)