标签:length false fir col 循环 中文 bin 动态 ++
class Solution { public: vector<int> minSubsequence(vector<int>& nums) { sort(nums.begin(), nums.end(), [](int x, int y) { return x > y; }); int sum = 0; for (auto v : nums) { sum += v; } int cur = 0; vector<int> res; for (auto v : nums) { if (cur * 2 > sum) break; cur += v; res.push_back(v); } return res; } };
class Solution { public: int numSteps(string s) { int ans = 0, len = s.length(), cur = len-1; for (int i = len-1 ; i >= 1 ; ) { cout << i << endl; while (cur >= 0 && s[cur] == s[i]) cur--; ans += i-cur; if (s[i] == ‘1‘) { ans++; if (cur >= 0) s[cur] = ‘1‘; } i = cur; } return ans; } };
class Solution { public: string longestDiverseString(int a, int b, int c) { map<int, vector<char>, greater<int>> mp; if (a > 0) mp[a].push_back(‘a‘); if (b > 0) mp[b].push_back(‘b‘); if (c > 0) mp[c].push_back(‘c‘); int last = a + b + c; string ans = ""; char tail = ‘ ‘; int num = 0; while (last) { bool find = false; for (auto i = mp.begin() ; i != mp.end() ; i++) { int cnt = i->first; if (i == mp.begin()) { for (auto j = i->second.begin() ; j != i->second.end() ; j++) { char cur = *j; if (tail != cur) { if (cnt >= 2) { ans += cur; ans += cur; cnt -= 2; last -= 2; } else { ans += cur; cnt -= 1; last -= 1; } i->second.erase(j); if (i->second.size() == 0) { mp.erase(i); } if (cnt > 0) { mp[cnt].push_back(cur); } find = true; tail = cur; break; } } } else { for (auto j = i->second.begin() ; j != i->second.end() ; j++) { char cur = *j; if (tail != cur) { if (cnt >= 1) { ans += cur; cnt -= 1; last -= 1; } i->second.erase(j); if (i->second.size() == 0) { mp.erase(i); } if (cnt > 0) { mp[cnt].push_back(cur); } find = true; tail = cur; break; } } } if (find) break; } if (!find) break; } return ans; } };
class Solution { public: string stoneGameIII(vector<int>& stoneValue) { int n = stoneValue.size(); vector<int> dp = vector<int>(n+1, INT_MIN); dp[n] = 0; int sum = 0; for (int i = n-1 ; i >= 0 ; i--) { sum += stoneValue[i]; for (int j = 1 ; j <= 3 && i+j <= n ; j++) { dp[i] = max(dp[i], sum-dp[i+j]); } } int alice = dp[0], bob = sum-dp[0]; cout << alice << endl; cout << bob << endl; if (alice > bob) return "Alice"; else if (alice < bob) return "Bob"; else return "Tie"; } };
LeetCode(Weekly Contest 183)题解
标签:length false fir col 循环 中文 bin 动态 ++
原文地址:https://www.cnblogs.com/wangao1236/p/12636784.html