提交 cfe12021 编写于 作者: 每日一练社区's avatar 每日一练社区

add 3 leetcode exercises

上级 5cebd413
......@@ -45,30 +45,104 @@
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> prices = {1, 2, 3, 4, 5};
int res;
res = sol.maxProfit(prices);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int dp[prices.size()][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < prices.size(); i++)
{
dp[i][0] = max(dp[i - 1][0], dp[i][1] + prices[i]);
dp[i][1] = max(dp[i - 1][0] - prices[i], dp[i][1]);
}
return dp[prices.size() - 1][0];
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int len = prices.size();
int mpro = 0;
if (len < 2)
return 0;
for (int i = 0; i < len - 1; i++)
{
if (prices[i + 1] > prices[i])
{
mpro += prices[i + 1] - prices[i];
}
}
return mpro;
}
};
```
### B
```cpp
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int valley, peak, maxProfit = 0, index = 0;
int len = prices.size();
while (index < len - 1)
{
while (index < prices.size() - 1 && prices[index] >= prices[index + 1])
index++;
valley = prices[index];
while (index < prices.size() - 1 && prices[index] <= prices[index + 1])
index++;
peak = prices[index];
maxProfit += peak - valley;
}
return maxProfit;
}
};
```
### C
```cpp
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int profit = 0;
for (auto i = prices.begin(); i != prices.end(); ++i)
if (i + 1 != prices.end() && *(i + 1) > *i)
profit += *(i + 1) - *i;
return profit;
}
};
```
......@@ -50,30 +50,158 @@ cost = [3,4,3]
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> gas = {1, 2, 3, 4, 5};
vector<int> cost = {3, 4, 5, 1, 2};
int res;
res = sol.canCompleteCircuit(gas, cost);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
for (int i = 0; i < gas.size(); i++)
{
if (gas[i] >= cost[i])
{
if (whetherEnough(i, gas, cost))
return i;
}
}
return -1;
}
bool whetherEnough(int n, const vector<int> gas, const vector<int> cost)
{
int i = n;
int save = 0;
while (i < gas.size())
{
save += (gas[i] - cost[i]);
if (save < 0)
return false;
else
i++;
}
for (int j = 0; j < i; j++)
{
save = (gas[j] - cost[j]);
if (save < 0)
return false;
}
return true;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int n = gas.size();
int i = 0;
while (i < n)
{
int sum_gas = 0, sum_cost = 0;
int count = 0;
while (count < n)
{
int j = (i + count) % n;
sum_gas += gas[j];
sum_cost += cost[j];
if (sum_cost > sum_gas)
{
break;
}
count++;
}
if (count == n)
{
return i;
}
else
{
i = i + count + 1;
}
}
return -1;
}
};
```
### B
```cpp
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int total = 0, cur = 0, start = 0;
for (int i = 0; i < gas.size(); ++i)
{
total += gas[i] - cost[i];
cur += gas[i] - cost[i];
if (cur < 0)
{
start = i + 1;
cur = 0;
}
}
return total < 0 ? -1 : start;
}
};
```
### C
```cpp
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int n = gas.size();
for (int i = 0; i < n; i++)
{
int sum = 0, num = 0;
int tmp = i;
while (num < n)
{
sum = sum + gas[tmp % n] - cost[tmp % n];
if (sum < 0)
{
break;
}
num++;
tmp++;
}
if (num == n)
return i;
else
continue;
}
return -1;
}
};
```
......@@ -32,30 +32,152 @@
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> ratings = {1, 0, 2};
int res;
res = sol.candy(ratings);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int candy(vector<int> &ratings)
{
int size = ratings.size();
vector<int> num(size, 1);
for (int i = 1; i < size; i++)
{
if (ratings[i] > ratings[i - 1])
num[i] = num[i - 1] + 1;
}
for (int i = size - 1; i > 0; i--)
{
if ((ratings[i] < ratings[i - 1]) && (num[i - 1] <= num[i]))
num[i - 1] = num[i];
}
return accumulate(num.begin(), num.end(), 0);
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int candy(vector<int> &ratings)
{
int n = ratings.size(), sum = 0;
vector<int> left(n, 1), right(n, 1);
for (int i = 1; i < n; i++)
{
if (ratings[i] > ratings[i - 1])
left[i] = left[i - 1] + 1;
}
for (int i = n - 2; i >= 0; i--)
{
if (ratings[i] > ratings[i + 1])
right[i] = right[i + 1] + 1;
sum += max(left[i], right[i]);
}
sum += max(right[n - 1], left[n - 1]);
return sum;
}
};
```
### B
```cpp
class Solution
{
public:
int candy(vector<int> &ratings)
{
int sum = 0;
int pre = 0;
int st = 0;
int i = 0;
while (i < ratings.size())
{
if (i == 0)
{
sum += 1;
pre = 1;
st = i;
}
else if (ratings[i] > ratings[i - 1])
{
pre = pre + 1;
sum = sum + pre;
st = i;
}
else if (ratings[i] == ratings[i - 1])
{
pre = 1;
sum += pre;
st = i;
}
else
{
int k = i;
while (k < ratings.size() && ratings[k] < ratings[k - 1])
{
k++;
}
int m = k - i;
sum += (m * (m + 1) / 2);
sum += max(0, m + 1 - pre);
pre = 1;
i = k;
continue;
}
i++;
}
return sum;
}
};
```
### C
```cpp
class Solution
{
public:
int candy(vector<int> &ratings)
{
int len = ratings.size();
if (len < 2)
return len;
int candy[len + 1];
candy[0] = 1;
for (int i = 1; i < len; i++)
{
if (ratings[i] > ratings[i - 1])
candy[i] = candy[i - 1] + 1;
else
candy[i] = 1;
}
int ans = 0;
for (int i = len - 1; i > 0; i--)
{
if (candy[i] >= candy[i - 1] && ratings[i] < ratings[i - 1])
candy[i - 1] = max(candy[i - 1], candy[i] + 1);
ans += candy[i];
}
return ans + candy[0];
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"买卖股票的最佳时机 II"
],
"children": [],
"export": [
"solution.json"
],
"title": "买卖股票的最佳时机 II"
}
\ No newline at end of file
<p>给定一个数组 <code>prices</code> ,其中 <code>prices[i]</code> 是一支给定股票第 <code>i</code> 天的价格。</p>
<p>设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。</p>
<p><strong>注意:</strong>你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> prices = [7,1,5,3,6,4]
<strong>输出:</strong> 7
<strong>解释:</strong> 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
  随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> prices = [1,2,3,4,5]
<strong>输出:</strong> 4
<strong>解释:</strong> 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
  注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> prices = [7,6,4,3,1]
<strong>输出:</strong> 0
<strong>解释:</strong> 在这种情况下, 没有交易完成, 所以最大利润为 0。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= prices.length <= 3 * 10<sup>4</sup></code></li>
<li><code>0 <= prices[i] <= 10<sup>4</sup></code></li>
</ul>
#include <vector>
using std::vector;
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int profit = 0;
for (auto i = prices.begin(); i != prices.end(); ++i)
if (i + 1 != prices.end() && *(i + 1) > *i)
profit += *(i + 1) - *i;
return profit;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "bb231155c0f74d2487e0e94741090a6b"
}
\ No newline at end of file
# 买卖股票的最佳时机 II
<p>给定一个数组 <code>prices</code> ,其中 <code>prices[i]</code> 是一支给定股票第 <code>i</code> 天的价格。</p>
<p>设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。</p>
<p><strong>注意:</strong>你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> prices = [7,1,5,3,6,4]
<strong>输出:</strong> 7
<strong>解释:</strong> 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
  随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> prices = [1,2,3,4,5]
<strong>输出:</strong> 4
<strong>解释:</strong> 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
  注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> prices = [7,6,4,3,1]
<strong>输出:</strong> 0
<strong>解释:</strong> 在这种情况下, 没有交易完成, 所以最大利润为 0。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= prices.length <= 3 * 10<sup>4</sup></code></li>
<li><code>0 <= prices[i] <= 10<sup>4</sup></code></li>
</ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> prices = {1, 2, 3, 4, 5};
int res;
res = sol.maxProfit(prices);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int dp[prices.size()][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < prices.size(); i++)
{
dp[i][0] = max(dp[i - 1][0], dp[i][1] + prices[i]);
dp[i][1] = max(dp[i - 1][0] - prices[i], dp[i][1]);
}
return dp[prices.size() - 1][0];
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int len = prices.size();
int mpro = 0;
if (len < 2)
return 0;
for (int i = 0; i < len - 1; i++)
{
if (prices[i + 1] > prices[i])
{
mpro += prices[i + 1] - prices[i];
}
}
return mpro;
}
};
```
### B
```cpp
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int valley, peak, maxProfit = 0, index = 0;
int len = prices.size();
while (index < len - 1)
{
while (index < prices.size() - 1 && prices[index] >= prices[index + 1])
index++;
valley = prices[index];
while (index < prices.size() - 1 && prices[index] <= prices[index + 1])
index++;
peak = prices[index];
maxProfit += peak - valley;
}
return maxProfit;
}
};
```
### C
```cpp
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int profit = 0;
for (auto i = prices.begin(); i != prices.end(); ++i)
if (i + 1 != prices.end() && *(i + 1) > *i)
profit += *(i + 1) - *i;
return profit;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"加油站"
],
"children": [],
"export": [
"solution.json"
],
"title": "加油站"
}
\ No newline at end of file
<p>在一条环路上有&nbsp;<em>N</em>&nbsp;个加油站,其中第&nbsp;<em>i</em>&nbsp;个加油站有汽油&nbsp;<code>gas[i]</code><em>&nbsp;</em>升。</p>
<p>你有一辆油箱容量无限的的汽车,从第<em> i </em>个加油站开往第<em> i+1&nbsp;</em>个加油站需要消耗汽油&nbsp;<code>cost[i]</code><em>&nbsp;</em>升。你从其中的一个加油站出发,开始时油箱为空。</p>
<p>如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。</p>
<p><strong>说明:</strong>&nbsp;</p>
<ul>
<li>如果题目有解,该答案即为唯一答案。</li>
<li>输入数组均为非空数组,且长度相同。</li>
<li>输入数组中的元素均为非负数。</li>
</ul>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong>
gas = [1,2,3,4,5]
cost = [3,4,5,1,2]
<strong>输出:</strong> 3
<strong>解释:
</strong>从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
因此,3 可为起始索引。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>
gas = [2,3,4]
cost = [3,4,3]
<strong>输出:</strong> -1
<strong>解释:
</strong>你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。</pre>
#include <vector>
using std::vector;
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int tank{0}, start{0}, stored{0};
for (decltype(gas.size()) i = 0; i < gas.size(); ++i)
if ((tank += gas[i] - cost[i]) < 0)
{
start = i + 1;
stored += tank;
tank = 0;
}
return (tank + stored) < 0 ? -1 : start;
}
};
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "61f0cdfcaf5b46fcaf2b3080a79d484e"
}
\ No newline at end of file
# 加油站
<p>在一条环路上有&nbsp;<em>N</em>&nbsp;个加油站,其中第&nbsp;<em>i</em>&nbsp;个加油站有汽油&nbsp;<code>gas[i]</code><em>&nbsp;</em>升。</p>
<p>你有一辆油箱容量无限的的汽车,从第<em> i </em>个加油站开往第<em> i+1&nbsp;</em>个加油站需要消耗汽油&nbsp;<code>cost[i]</code><em>&nbsp;</em>升。你从其中的一个加油站出发,开始时油箱为空。</p>
<p>如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。</p>
<p><strong>说明:</strong>&nbsp;</p>
<ul>
<li>如果题目有解,该答案即为唯一答案。</li>
<li>输入数组均为非空数组,且长度相同。</li>
<li>输入数组中的元素均为非负数。</li>
</ul>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong>
gas = [1,2,3,4,5]
cost = [3,4,5,1,2]
<strong>输出:</strong> 3
<strong>解释:
</strong>从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
因此,3 可为起始索引。</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>
gas = [2,3,4]
cost = [3,4,3]
<strong>输出:</strong> -1
<strong>解释:
</strong>你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。</pre>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> gas = {1, 2, 3, 4, 5};
vector<int> cost = {3, 4, 5, 1, 2};
int res;
res = sol.canCompleteCircuit(gas, cost);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
for (int i = 0; i < gas.size(); i++)
{
if (gas[i] >= cost[i])
{
if (whetherEnough(i, gas, cost))
return i;
}
}
return -1;
}
bool whetherEnough(int n, const vector<int> gas, const vector<int> cost)
{
int i = n;
int save = 0;
while (i < gas.size())
{
save += (gas[i] - cost[i]);
if (save < 0)
return false;
else
i++;
}
for (int j = 0; j < i; j++)
{
save = (gas[j] - cost[j]);
if (save < 0)
return false;
}
return true;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int n = gas.size();
int i = 0;
while (i < n)
{
int sum_gas = 0, sum_cost = 0;
int count = 0;
while (count < n)
{
int j = (i + count) % n;
sum_gas += gas[j];
sum_cost += cost[j];
if (sum_cost > sum_gas)
{
break;
}
count++;
}
if (count == n)
{
return i;
}
else
{
i = i + count + 1;
}
}
return -1;
}
};
```
### B
```cpp
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int total = 0, cur = 0, start = 0;
for (int i = 0; i < gas.size(); ++i)
{
total += gas[i] - cost[i];
cur += gas[i] - cost[i];
if (cur < 0)
{
start = i + 1;
cur = 0;
}
}
return total < 0 ? -1 : start;
}
};
```
### C
```cpp
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int n = gas.size();
for (int i = 0; i < n; i++)
{
int sum = 0, num = 0;
int tmp = i;
while (num < n)
{
sum = sum + gas[tmp % n] - cost[tmp % n];
if (sum < 0)
{
break;
}
num++;
tmp++;
}
if (num == n)
return i;
else
continue;
}
return -1;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"分发糖果"
],
"children": [],
"export": [
"solution.json"
],
"title": "分发糖果"
}
\ No newline at end of file
<p>老师想给孩子们分发糖果,有 <em>N</em> 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。</p>
<p>你需要按照以下要求,帮助老师给这些孩子分发糖果:</p>
<ul>
<li>每个孩子至少分配到 1 个糖果。</li>
<li>评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果。</li>
</ul>
<p>那么这样下来,老师至少需要准备多少颗糖果呢?</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>[1,0,2]
<strong>输出:</strong>5
<strong>解释:</strong>你可以分别给这三个孩子分发 2、1、2 颗糖果。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>[1,2,2]
<strong>输出:</strong>4
<strong>解释:</strong>你可以分别给这三个孩子分发 1、2、1 颗糖果。
第三个孩子只得到 1 颗糖果,这已满足上述两个条件。</pre>
class Solution
{
public:
int candy(vector<int> &ratings)
{
int n = ratings.size();
int ans = 0;
vector<int> sum(n, 1);
if (n < 2)
return n;
for (int i = 0; i < n; ++i)
{
if (i > 0 && ratings[i] > ratings[i - 1])
sum[i] = sum[i - 1] + 1;
}
for (int i = n - 1; i >= 0; --i)
{
if (i < n - 1 && ratings[i] > ratings[i + 1])
sum[i] = max(sum[i + 1] + 1, sum[i]);
ans += sum[i];
}
return ans;
}
};
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "9d1991504bb04795836014c3ce2abef8"
}
\ No newline at end of file
# 分发糖果
<p>老师想给孩子们分发糖果,有 <em>N</em> 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。</p>
<p>你需要按照以下要求,帮助老师给这些孩子分发糖果:</p>
<ul>
<li>每个孩子至少分配到 1 个糖果。</li>
<li>评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果。</li>
</ul>
<p>那么这样下来,老师至少需要准备多少颗糖果呢?</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>[1,0,2]
<strong>输出:</strong>5
<strong>解释:</strong>你可以分别给这三个孩子分发 2、1、2 颗糖果。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>[1,2,2]
<strong>输出:</strong>4
<strong>解释:</strong>你可以分别给这三个孩子分发 1、2、1 颗糖果。
第三个孩子只得到 1 颗糖果,这已满足上述两个条件。</pre>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<int> ratings = {1, 0, 2};
int res;
res = sol.candy(ratings);
cout << res;
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int candy(vector<int> &ratings)
{
int size = ratings.size();
vector<int> num(size, 1);
for (int i = 1; i < size; i++)
{
if (ratings[i] > ratings[i - 1])
num[i] = num[i - 1] + 1;
}
for (int i = size - 1; i > 0; i--)
{
if ((ratings[i] < ratings[i - 1]) && (num[i - 1] <= num[i]))
num[i - 1] = num[i];
}
return accumulate(num.begin(), num.end(), 0);
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
int candy(vector<int> &ratings)
{
int n = ratings.size(), sum = 0;
vector<int> left(n, 1), right(n, 1);
for (int i = 1; i < n; i++)
{
if (ratings[i] > ratings[i - 1])
left[i] = left[i - 1] + 1;
}
for (int i = n - 2; i >= 0; i--)
{
if (ratings[i] > ratings[i + 1])
right[i] = right[i + 1] + 1;
sum += max(left[i], right[i]);
}
sum += max(right[n - 1], left[n - 1]);
return sum;
}
};
```
### B
```cpp
class Solution
{
public:
int candy(vector<int> &ratings)
{
int sum = 0;
int pre = 0;
int st = 0;
int i = 0;
while (i < ratings.size())
{
if (i == 0)
{
sum += 1;
pre = 1;
st = i;
}
else if (ratings[i] > ratings[i - 1])
{
pre = pre + 1;
sum = sum + pre;
st = i;
}
else if (ratings[i] == ratings[i - 1])
{
pre = 1;
sum += pre;
st = i;
}
else
{
int k = i;
while (k < ratings.size() && ratings[k] < ratings[k - 1])
{
k++;
}
int m = k - i;
sum += (m * (m + 1) / 2);
sum += max(0, m + 1 - pre);
pre = 1;
i = k;
continue;
}
i++;
}
return sum;
}
};
```
### C
```cpp
class Solution
{
public:
int candy(vector<int> &ratings)
{
int len = ratings.size();
if (len < 2)
return len;
int candy[len + 1];
candy[0] = 1;
for (int i = 1; i < len; i++)
{
if (ratings[i] > ratings[i - 1])
candy[i] = candy[i - 1] + 1;
else
candy[i] = 1;
}
int ans = 0;
for (int i = len - 1; i > 0; i--)
{
if (candy[i] >= candy[i - 1] && ratings[i] < ratings[i - 1])
candy[i - 1] = max(candy[i - 1], candy[i] + 1);
ans += candy[i];
}
return ans + candy[0];
}
};
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册