diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.md" index e0d6ec21e002e31c53abb91e89c1040cd9f3e33a..2d8fb750e5b7e05d35c2a0181403e754d05f4f50 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.md" @@ -45,30 +45,104 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector prices = {1, 2, 3, 4, 5}; + + int res; + res = sol.maxProfit(prices); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int maxProfit(vector &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 &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 &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 &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; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/133_\345\212\240\346\262\271\347\253\231/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/133_\345\212\240\346\262\271\347\253\231/solution.md" index 1d0f2720d4838b63cd8b34a4e0061b021b28d5e5..5a12d6c151db8419f609aa7ce4765b05c6db2116 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/133_\345\212\240\346\262\271\347\253\231/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/133_\345\212\240\346\262\271\347\253\231/solution.md" @@ -50,30 +50,158 @@ cost = [3,4,3] ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector gas = {1, 2, 3, 4, 5}; + vector cost = {3, 4, 5, 1, 2}; + + int res; + res = sol.canCompleteCircuit(gas, cost); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int canCompleteCircuit(vector &gas, vector &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 gas, const vector 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 &gas, vector &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 &gas, vector &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 &gas, vector &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; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.md" index 69e60a1b2c300ef2611ee5db77664937da810407..4af408996702f2fd5b73212e4c021f9512ffc193 100644 --- "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.md" +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/1.leetcode/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.md" @@ -32,30 +32,152 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp - +int main() +{ + Solution sol; + vector ratings = {1, 0, 2}; + int res; + res = sol.candy(ratings); + cout << res; + return 0; +} ``` ## 答案 ```cpp - +class Solution +{ +public: + int candy(vector &ratings) + { + int size = ratings.size(); + vector 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 &ratings) + { + int n = ratings.size(), sum = 0; + vector 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 &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 &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]; + } +}; ``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..abe7bbb3fa35bfa47ad9dcd2ee9d865de39f6820 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "买卖股票的最佳时机 II" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "买卖股票的最佳时机 II" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/desc.html" new file mode 100644 index 0000000000000000000000000000000000000000..93dc6405179c3d84fd596dce4f0cfd88d3815e66 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/desc.html" @@ -0,0 +1,41 @@ +

给定一个数组 prices ,其中 prices[i] 是一支给定股票第 i 天的价格。

+ +

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

+ +

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

+ +

 

+ +

示例 1:

+ +
+输入: prices = [7,1,5,3,6,4]
+输出: 7
+解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
+     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
+
+ +

示例 2:

+ +
+输入: prices = [1,2,3,4,5]
+输出: 4
+解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
+     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
+
+ +

示例 3:

+ +
+输入: prices = [7,6,4,3,1]
+输出: 0
+解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
+ +

 

+ +

提示:

+ +
    +
  • 1 <= prices.length <= 3 * 104
  • +
  • 0 <= prices[i] <= 104
  • +
diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..cf8b903836c27230e3a5cb7ec22f75067a1131c2 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.cpp" @@ -0,0 +1,17 @@ + + +#include +using std::vector; + +class Solution +{ +public: + int maxProfit(vector &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 diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0367df4eef4e95dcc9eb1011ea39ed192a2d3d0a --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "bb231155c0f74d2487e0e94741090a6b" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2d8fb750e5b7e05d35c2a0181403e754d05f4f50 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/121_\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272 II/solution.md" @@ -0,0 +1,148 @@ +# 买卖股票的最佳时机 II +

给定一个数组 prices ,其中 prices[i] 是一支给定股票第 i 天的价格。

+ +

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

+ +

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

+ +

 

+ +

示例 1:

+ +
+输入: prices = [7,1,5,3,6,4]
+输出: 7
+解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
+     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
+
+ +

示例 2:

+ +
+输入: prices = [1,2,3,4,5]
+输出: 4
+解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
+     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
+
+ +

示例 3:

+ +
+输入: prices = [7,6,4,3,1]
+输出: 0
+解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
+ +

 

+ +

提示:

+ +
    +
  • 1 <= prices.length <= 3 * 104
  • +
  • 0 <= prices[i] <= 104
  • +
+ +

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector prices = {1, 2, 3, 4, 5}; + + int res; + res = sol.maxProfit(prices); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int maxProfit(vector &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 &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 &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 &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; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a653a62fd90581ec7542fdc5b56a13598a50a68b --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "加油站" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "加油站" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/desc.html" new file mode 100644 index 0000000000000000000000000000000000000000..d0b20bbf93ba6275487ab2728bc2da2ac52ebb2c --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/desc.html" @@ -0,0 +1,46 @@ +

在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。

+ +

你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。

+ +

如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。

+ +

说明: 

+ +
    +
  • 如果题目有解,该答案即为唯一答案。
  • +
  • 输入数组均为非空数组,且长度相同。
  • +
  • 输入数组中的元素均为非负数。
  • +
+ +

示例 1:

+ +
输入: 
+gas  = [1,2,3,4,5]
+cost = [3,4,5,1,2]
+
+输出: 3
+
+解释:
+从 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 可为起始索引。
+ +

示例 2:

+ +
输入: 
+gas  = [2,3,4]
+cost = [3,4,3]
+
+输出: -1
+
+解释:
+你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
+我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
+开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
+开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
+你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
+因此,无论怎样,你都不可能绕环路行驶一周。
diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/solution.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..332ae440f658022d841e47bb70739d7c986f3ba0 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/solution.cpp" @@ -0,0 +1,19 @@ +#include +using std::vector; + +class Solution +{ +public: + int canCompleteCircuit(vector &gas, vector &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 diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..09b85b4d0029716dabdc959e8021841e7488956d --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "61f0cdfcaf5b46fcaf2b3080a79d484e" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5a12d6c151db8419f609aa7ce4765b05c6db2116 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/133_\345\212\240\346\262\271\347\253\231/solution.md" @@ -0,0 +1,207 @@ +# 加油站 +

在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。

+ +

你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。

+ +

如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。

+ +

说明: 

+ +
    +
  • 如果题目有解,该答案即为唯一答案。
  • +
  • 输入数组均为非空数组,且长度相同。
  • +
  • 输入数组中的元素均为非负数。
  • +
+ +

示例 1:

+ +
输入: 
+gas  = [1,2,3,4,5]
+cost = [3,4,5,1,2]
+
+输出: 3
+
+解释:
+从 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 可为起始索引。
+ +

示例 2:

+ +
输入: 
+gas  = [2,3,4]
+cost = [3,4,3]
+
+输出: -1
+
+解释:
+你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
+我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
+开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
+开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
+你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
+因此,无论怎样,你都不可能绕环路行驶一周。
+ +

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector gas = {1, 2, 3, 4, 5}; + vector cost = {3, 4, 5, 1, 2}; + + int res; + res = sol.canCompleteCircuit(gas, cost); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int canCompleteCircuit(vector &gas, vector &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 gas, const vector 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 &gas, vector &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 &gas, vector &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 &gas, vector &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; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..47cc1991a35505dacbc9b73939eee6aedf291aa6 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "569d5e11c4fc5de7844053d9a733c5e8", + "keywords": [ + "leetcode", + "分发糖果" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "分发糖果" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/desc.html" new file mode 100644 index 0000000000000000000000000000000000000000..52bd921ac4afa003775417757f0d1b3509ddbd77 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/desc.html" @@ -0,0 +1,28 @@ +

老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。

+ +

你需要按照以下要求,帮助老师给这些孩子分发糖果:

+ +
    +
  • 每个孩子至少分配到 1 个糖果。
  • +
  • 评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果。
  • +
+ +

那么这样下来,老师至少需要准备多少颗糖果呢?

+ +

 

+ +

示例 1:

+ +
+输入:[1,0,2]
+输出:5
+解释:你可以分别给这三个孩子分发 2、1、2 颗糖果。
+
+ +

示例 2:

+ +
+输入:[1,2,2]
+输出:4
+解释:你可以分别给这三个孩子分发 1、2、1 颗糖果。
+     第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..df79d39a640014a528c283721361ba7f6cf83cf0 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.cpp" @@ -0,0 +1,24 @@ +class Solution +{ +public: + int candy(vector &ratings) + { + int n = ratings.size(); + int ans = 0; + vector 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; + } +}; diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..10b17392216602e0f71d6d2c16a51d9827aa274a --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "9d1991504bb04795836014c3ce2abef8" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4af408996702f2fd5b73212e4c021f9512ffc193 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/13.leetcode\350\264\252\345\277\203/134_\345\210\206\345\217\221\347\263\226\346\236\234/solution.md" @@ -0,0 +1,183 @@ +# 分发糖果 +

老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。

+ +

你需要按照以下要求,帮助老师给这些孩子分发糖果:

+ +
    +
  • 每个孩子至少分配到 1 个糖果。
  • +
  • 评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果。
  • +
+ +

那么这样下来,老师至少需要准备多少颗糖果呢?

+ +

 

+ +

示例 1:

+ +
+输入:[1,0,2]
+输出:5
+解释:你可以分别给这三个孩子分发 2、1、2 颗糖果。
+
+ +

示例 2:

+ +
+输入:[1,2,2]
+输出:4
+解释:你可以分别给这三个孩子分发 1、2、1 颗糖果。
+     第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
+ +

以下错误的选项是?

+## aop +### before +```cpp +#include +using namespace std; +``` +### after +```cpp +int main() +{ + Solution sol; + vector ratings = {1, 0, 2}; + int res; + res = sol.candy(ratings); + cout << res; + return 0; +} +``` + +## 答案 +```cpp +class Solution +{ +public: + int candy(vector &ratings) + { + int size = ratings.size(); + vector 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 &ratings) + { + int n = ratings.size(), sum = 0; + vector 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 &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 &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]; + } +}; +```