forked from youngyangyang04/leetcode-master
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
141cccb
commit c08aa26
Showing
10 changed files
with
328 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
# 思路 | ||
|
||
这道题目贪心不好讲解啊 | ||
|
||
如果 get总和大于cost总和,那么一定是可以跑一圈的,因为油是可以存储的。 | ||
|
||
本题贪心的思路,不是那么好想。 | ||
|
||
那么来看一下贪心主要贪在哪里 | ||
|
||
* 如果gas的总和小于cost总和,那么无论从哪里出发,一定是跑不了一圈的 | ||
* remain[i] = gas[i]-cost[i]为一天剩下的油,remain[i],i从0开始计算累加到最后一站,如果累加没有出现负数,说明从0出发,油就没有断过,那么0就是起点。 | ||
|
||
* 如果累加的最小值是负数,就要从非0节点出发,从后向前,看那个节点能这个负数填平。 | ||
|
||
这个方法太绝了 | ||
``` | ||
class Solution { | ||
public: | ||
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { | ||
int curSum = 0; | ||
int totalSum = 0; | ||
int start = 0; | ||
for (int i = 0; i < gas.size(); i++) { | ||
curSum += gas[i] - cost[i]; | ||
totalSum += gas[i] - cost[i]; | ||
if (curSum < 0) { | ||
start = i + 1; | ||
curSum = 0; | ||
} | ||
} | ||
if (totalSum < 0) return -1; | ||
return start; | ||
} | ||
}; | ||
``` | ||
|
||
这个方法太复杂了 | ||
``` | ||
class Solution { | ||
public: | ||
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { | ||
int remainSum = 0; | ||
int min = INT_MAX; // 从起点出发,油箱里的油量 | ||
for (int i = 0; i < gas.size(); i++) { | ||
int remain = gas[i] - cost[i]; | ||
remainSum += remain; | ||
if (remainSum < min) { | ||
min = remainSum; | ||
} | ||
} | ||
if (remainSum < 0) return -1; // 如果总油量-总消耗都小于零,一定是哪里作为起点都不行 | ||
if (min >= 0) return 0; // 从0的位置出发,油箱里的油量没有出现负数,说明从0触发可以跑一圈 | ||
// 否则就一定是从其他节点触发 | ||
// 从后向前遍历,如果那个节点可以补上从0触发油箱出现负数的情况,那么这个i就是起点 | ||
for (int i = gas.size() - 1; i >= 0; i--) { | ||
int remain = gas[i] - cost[i]; | ||
min += remain; | ||
if (min >= 0) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
}; | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
这道题目上来也是没什么思路啊 | ||
``` | ||
class Solution { | ||
public: | ||
int candy(vector<int>& ratings) { | ||
vector<int> candyVec(ratings.size(), 1); | ||
for (int i = 1; i < ratings.size(); i++) { | ||
if (ratings[i] > ratings[i - 1]) candyVec[i] = candyVec[i - 1] + 1; | ||
} | ||
for (int i = ratings.size() - 2; i >= 0; i--) { | ||
if (ratings[i] > ratings[i + 1] && candyVec[i] < candyVec[i + 1] + 1) { | ||
candyVec[i] = candyVec[i + 1] + 1; | ||
} | ||
} | ||
int result = 0; | ||
for (int i = 0; i < candyVec.size(); i++) result += candyVec[i]; | ||
return result; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
题目链接: https://leetcode-cn.com/problems/wiggle-subsequence/ | ||
|
||
## 思路 | ||
|
||
本题要求通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。 | ||
|
||
相信这么一说吓退不少同学,这又可以修改数组,这得如何修改呢? | ||
|
||
我们来分析一下,要求删除元素使其达到最大摆动序列,应该删除什么元素呢? | ||
|
||
用示例二来举例,如图所示: | ||
|
||
<img src='../pics/376.摆动序列.png' width=600> </img></div> | ||
|
||
图中可以看出,为了让摆动序列最长,只需要把单一坡度(递增或者递减)上的节点删掉就可以了。 | ||
|
||
**这就是贪心所贪的地方,让峰值尽可能的保持峰值,然后删除单一坡度上的节点**。 | ||
|
||
**实际操作上,其实练删除的操作都不用做,因为题目要求的是摆动序列的长度,所以只需要统计数组的峰值数量就可以了(相当于是删除单一坡度上的节点,然后统计长度)** | ||
|
||
代码实现中,还有一些技巧,例如统计峰值的时候,数组最左面和最右面是最不好统计的。 | ||
|
||
例如数组[2,5],它的峰值数量是2,如果靠统计差值来计算峰值就需要考虑数组最左面和最右面的特殊情况。 | ||
|
||
所以可以针对数组[2,5],假设为[2,2,5],这样它就有坡度了,如图: | ||
|
||
<img src='../pics/376.摆动序列1.png' width=600> </img></div> | ||
|
||
这样result初始为1,curDiff > 0 && preDiff <= 0,result++,最后得到的result就是2了。 | ||
|
||
C++代码如下: | ||
|
||
``` | ||
class Solution { | ||
public: | ||
int wiggleMaxLength(vector<int>& nums) { | ||
if (nums.size() <= 1) return nums.size(); | ||
int curDiff = 0; // 当前一对差值 | ||
int preDiff = 0; // 前一对差值 | ||
int result = 1; // 记录峰值,起始位置峰值为1 | ||
for (int i = 1; i < nums.size(); i++) { | ||
curDiff = nums[i] - nums[i - 1]; | ||
// 出现峰值 | ||
if ((curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0)) { | ||
result++; | ||
preDiff = curDiff; | ||
} | ||
} | ||
return result; | ||
} | ||
}; | ||
``` | ||
|
||
> 我是[程序员Carl](https://github.com/youngyangyang04),组队刷题可以找我,本文[leetcode刷题攻略](https://github.com/youngyangyang04/leetcode-master)已收录,更多[精彩算法文章](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzUxNjY5NTYxNA==&action=getalbum&album_id=1485825793120387074&scene=173#wechat_redirect)尽在:[代码随想录](https://img-blog.csdnimg.cn/20200815195519696.png),期待你的关注! | ||
Oops, something went wrong.