class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int len = gas.size(); int start = 0; int MAX = 0; int now = 0; for (int i = 0; i < len; i++) { now += (gas[i] - cost[i]); if (now < 0) { now = 0; start = i + 1; } if (now > MAX) { MAX = now; } } MAX = 0; for (int i = start; i < len; i++) { MAX += gas[i] - cost[i]; if (MAX < 0) return -1; } for (int i = 0; i < start; i++) { MAX += gas[i] - cost[i]; if (MAX < 0) return -1; } return start; } };
class Solution { public: int findMinArrowShots(vector<vector<int>>& points) { sort(points.begin(), points.end(), [](const vector<int>& a, const vector<int>& b) { return a[1] < b[1]; }); int len = points.size(); int ans = 0; for (int i = 0; i < len; i++) { ans++; int now = points[i][1]; while(i < len && points[i][0] <= now) { i++; } i--; } return ans; } };
class Solution { public: int maximumGap(vector<int>& nums) { int len = nums.size(); if (len < 2) return 0; int Max = *max_element(nums.begin(), nums.end()); int Min = *min_element(nums.begin(), nums.end()); int d = max(1, (Max - Min) / (len - 1)); int bucketSize = (Max - Min) / d + 1; vector<pair<long long, long long>> v(bucketSize, {999999999, -1}); for (int i = 0; i < len; i++) { int index = (nums[i] - Min) / d; v[index].first = min(v[index].first, nums[i] * 1ll); v[index].second = max(v[index].second, nums[i] * 1ll); } long long ans = 0; int pre = -1; for (int i = 0; i < bucketSize; i++) { if (v[i].second == -1) continue; if (pre != -1 ) { ans = max(ans, v[i].first - v[pre].second); } pre = i; } return ans; } };
class Solution { public: int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { unordered_map<int, int> countAB; for (int& u: A) { for (int& v: B) { countAB[u + v]++; } } int ans = 0; for (int& u: C) { for (int& v: D) { if (countAB.count(-u - v)) { ans += countAB[-u - v]; } } } return ans; } };
class Solution { public: int minMoves(vector<int>& nums, int limit) { int len = nums.size(); map<int, int> m; for (int i = 0; i < len / 2; i++) { int Min = min(nums[i], nums[len - 1 - i]); int Max = max(nums[i], nums[len - 1 - i]); int l = Min + 1; int r = Max + limit; int sum = nums[i] + nums[len - 1 - i]; m[l]--; m[sum]--; m[sum + 1]++; m[r + 1]++; } int ans = 999999; int now = len; for (auto &s : m) { now += s.second; ans = min(ans, now); } return ans; } };
class Solution { public: vector<int> mostCompetitive(vector<int>& nums, int k) { deque<int> v; int len = nums.size(); for (int i = 0; i < len - k + 1; i++) { while (!v.empty() && nums[i] < v.back()) { v.pop_back(); } v.push_back(nums[i]); }
vector<int> ans; int lsum = v.size() - 1;
for (int i = len - k + 1; i < len; i++) { while(lsum > 0 && nums[i] < v.back()) { v.pop_back(); lsum--; } v.push_back(nums[i]); }
for (int i = 0; i < k; i++) { ans.push_back(v[i]); } return ans;
class Solution { public: int wiggleMaxLength(vector<int>& nums) { if (nums.size() == 0) return 0; int up = 1; int down = 1; for (int i = 1; i < nums.size(); i++) { if (nums[i] > nums[i - 1]) { up = max(down + 1, up); } else if (nums[i] < nums[i - 1]) { down = max(up + 1, down); } } return max(up, down); } };