代码随想录算法训练营Day29 | 134. 加油站 | 135. 分发糖果 | 860.柠檬水找零 | 406.根据身高重建队列
今日任务
134. 加油站
- 题目链接: https://leetcode.cn/problems/gas-station/
- 题目描述:
Code
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
// int current_gas, current_cost;
// int len, j;
// if(gas.size()==1&&gas[0] >= cost[0]) return 0;
// for (int i = 0; i < gas.size(); i++) {
// if (gas[i] <= cost[i]) continue;
// current_gas = gas[i];
// len = gas.size();
// j = i;
// while (--len) {
// current_cost = cost[j];
// if (++j >= gas.size()) j = 0;
// if (current_gas >= current_cost) {
// current_gas = current_gas - current_cost + gas[j];
// }
// else {
// break;
// }
// }
// if (len == 0 && current_gas - cost[j] >= 0) return i;
// }
// return -1;
// int sum=0,judge_pos=0,n=0;
// for(int i=0;i<gas.size();i++){
// sum+=gas[i]-cost[i];
// judge_pos+=gas[i]-cost[i];
// if(judge_pos<0){
// n=i+1;
// judge_pos=0;
// }
// }
// if(sum<0) return -1;
// return n;
// int n = gas.size();
// vector<int> diff(n);
// for(int i = 0; i < n; i++){
// diff[i] = gas[i] - cost[i];
// }
// int post = 0;
// int sum = reduce(diff.begin(), diff.end(), 0);
// if(sum < 0){
// return -1;
// }
// int ans = 0;
// for(int i = n - 1; i >=0 ; i--){
// post += diff[i];
// if(post >= 0 && diff[i] >= 0){
// ans = i;
// }else if(post - diff[i] >= 0 && diff[i] < 0){
// post = diff[i];
// }
// }
// return ans;
int sum = 0;
int pre = 0;
int n = gas.size();
int ans = 0;
for(int i = 0; i < n; i++){
int diff = gas[i] - cost[i];
sum += diff;
pre += diff;
if(pre < 0){
pre = 0;
ans = i + 1;
}
}
return sum < 0 ? -1 : ans;
}
};
135. 分发糖果
- 题目链接: https://leetcode.cn/problems/candy/description/
- 题目描述:
Code 遍历两次
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> candy(n, 1);
for(int i = 1; i < n; i++){
if(ratings[i] > ratings[i - 1]){
candy[i] = candy[i - 1] + 1;
}
}
for(int i = n - 2; i >= 0; i--){
if(ratings[i] > ratings[i + 1]){
candy[i] = candy[i] > candy[i + 1] ? candy[i] : candy[i + 1] + 1;
}
}
return reduce(candy.begin(), candy.end(), 0);
}
};
860.柠檬水找零
- 题目链接: https://leetcode.cn/problems/lemonade-change/description/
- 题目描述:
Code
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
// if(bills[0] != 5){
// return false;
// }
// unordered_map<int, int> money;
// int n = bills.size();
// for(int i = 0; i < n; i++){
// int pay = bills[i];
// if(pay == 10){
// if(money[5]){
// money[5]--;
// }else{
// return false;
// }
// }
// if(pay == 20){
// if(money[10] && money[5]){
// money[10]--;
// money[5]--;
// }else if(money[5] >= 3){
// money[5] -= 3;
// }else{
// return false;
// }
// }
// money[pay]++;
// }
// return true;
int five = 0, ten = 0;
for(int b : bills){
if(b == 5){
five++;
}else if(b == 10){
five--;
ten++;
}else if(ten){
ten--;
five--;
}else{
five -= 3;
}
if(five < 0){
return false;
}
}
return true;
}
};
406.根据身高重建队列
- 题目链接: https://leetcode.cn/problems/queue-reconstruction-by-height/description/
- 题目描述:
Code 排序+倒序插入
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
auto cmp = [&](auto &a, auto &b)->bool{
return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0];
};
sort(people.begin(), people.end(), cmp);
list<vector<int>> ans;
for(auto &elem : people){
ans.insert(next(ans.begin(), elem[1]), elem);
}
return vector<vector<int>>(ans.begin(), ans.end());
}
};
原文地址:https://blog.csdn.net/wonderlandlja/article/details/140572978
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!