發表文章

https://leetcode.com/contest/leetcode-weekly-contest-54/problems/partition-to-k-equal-sum-subsets/

1st slow but passed solution #include <iostream> #include <limits.h> #include <vector> #include <string> #include <iterator> #include <map> #include <queue> #include <list> #include <algorithm> #include <stdio.h> #include <set> #include <vector> using namespace std; class Solution { public: bool check(vector<int>& nums, int start, vector<int>& remain) { bool complete = true; for (int i = 0; i < remain.size(); i++) { if (remain[i] != 0) { complete = false; break; } } if (complete) { return true; } for (int i = start; i < nums.size(); i++) { for (int j = 0; j < remain.size(); j++) { if (remain[j] >= nums[i]) { remain[j] -= nums[i]; bool res = check(nums, (i + 1), remain); if (r...

https://leetcode.com/contest/leetcode-weekly-contest-54/problems/degree-of-an-array/

圖片
[Idea] [實作邏輯] Use a map to store following info: {number, {freq, {start, end}}} Then reuse start to store (end - start +1). Finally, apply multi-key sort; the 1st key is freq in ascending order & then distance(2nd key) in descending order. #include <iostream> #include <vector> #include <map> #include <algorithm> #include <stdio.h> using namespace std; class Solution { public: int findShortestSubArray(vector<int>& nums) { if (nums.size() == 0) return 0; map< int, pair<int/*count*/, pair<int/*first*/, int/*last*/> > > statistic; for (int i = 0; i < nums.size(); i++) { if (statistic.find(nums[i]) != statistic.end()) { statistic[nums[i]].second.second = i; statistic[nums[i]].first++; } else { statistic.insert(make_pair(nums[i], make_pair(1, make_pair(i, i)))); } }...

General problem

1. Write set difference by C++ STL; assume sets = A - B, put result in set C. std::set_difference(A.begin(), A.end(), B.begin(), B.end(), std::inserter(C, C.begin()));

容器型態

圖片

https://leetcode.com/contest/leetcode-weekly-contest-53/problems/stickers-to-spell-word/

圖片
Test pattern generator: modify: 1.  testStringLength 2.  maxStickerLength 3.  numStickers #include <iostream> #include <fstream> #include <vector> #include <stdlib.h> /* srand, rand */ #include <time.h> using namespace std; int main () { int testStringLength = 25; int maxStickerLength = 8; int numStickers = 15; srand (time(NULL)); string input(testStringLength, 0); std::vector<std::string> stickersVector(numStickers); for (int i = 0; i < testStringLength; i++) { input.at(i) = rand() % 26 + 'a'; }; cout << input << "\n"; for (int i = 0; i < numStickers; i++) { int wordLength = (rand() % 10); wordLength = ((wordLength < 3) ? 3 : wordLength); for (int j = 0; j < wordLength; j++) { stickersVector[i].push_back(rand() % 26 + 'a'); } cout << i << ": " <...

https://leetcode.com/contest/leetcode-weekly-contest-53/problems/binary-number-with-alternating-bits/

class Solution { public: bool hasAlternatingBits(int n) { n = (n >> 1) ^ n; return ((((n + 1) & n) == 0) ? true : false); } };

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/repeated-string-match/

Paste your text #include <iostream> #include <string> using namespace std; class Solution { public: int repeatedStringMatch(string A, string B) { int a = A.length(); int b = B.length(); string tmp = A; if (!a) return 0; if (!b) return 1; int res = -1; int repeat = (b / a) + 1; while (repeat-- > 0) { tmp = tmp + A; } for (int i = 0; i < a; i++) { if (tmp.compare(i, b, B) == 0) { res = (((i + b) / a) + ((((i + b) % a) != 0) ? 1 : 0)); return res; } } return res; } }; int main() { string A = "abcd"; string B = "cdabcdab"; Solution sol; int res = sol.repeatedStringMatch(A, B); printf("res = %d\n", res); return 1; }here.