class Solution{public: int digitSum(int x) { int ret = 0; while (x > 0) { ret += x % 10; x = x / 10; } return ret; } int countEven(int num) { int ans = 0; for (int i = 1; i <= num; i++) { if (digitSum(i) % 2 == 0) { ans++; } } return ans; }};
2. Merge Nodes in Between Zeros
Naive.
● ● ●c++
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */class Solution{public: ListNode* mergeNodes(ListNode* head) { vector<int> nums; int current = 0; for (ListNode *p = head->next; p != NULL; p = p->next) { if (p->val == 0) { nums.push_back(current); current = 0; } else { current += p->val; } } ListNode *ret = NULL; for (int i = nums.size() - 1; i >= 0; i--) { ret = new ListNode(nums[i], ret); } return ret; }};
3. Construct String With Repeat Limit
Greedy.
● ● ●c++
class Solution{public: string repeatLimitedString(string s, int repeatLimit) { unordered_map<char, int> cnt; for (auto &c : s) { cnt[c]++; } char lastChr = '?'; int lastCnt = 0; string ans = ""; while (true) { char opt = '?'; for (char c = 'z'; c >= 'a'; c--) { if (cnt[c] == 0) { continue; } if (lastChr == c && lastCnt + 1 > repeatLimit) { continue; } opt = c; break; } if (opt == '?') { break; } ans.push_back(opt); cnt[opt]--; if (lastChr == opt) { lastCnt++; } else { lastChr = opt; lastCnt = 1; } } return ans; }};
4. Count Array Pairs Divisible by K
Let's say Ci represents the number of integers where nummodk=i.
First, let's count the pairs (a,b) where amodk=0∧bmodk=0: C0∗(C0−1)÷2.
Then, let's count the pairs (a,b) where amodk=0∧bmodk=0: C0∗∑i=1k−1Ci.
And now, let's dicuss the pairs where (a,b) where two amodk=0∧bmodk=0:
We know there must be 1≤p≤k to make a×b=k×p where.
A simple idea is to enumerate a and k×p where the time complexity is O(k2).
And we can further improve the enumeration of k×p by delta=gcd(a,k) so the time complexity is O(klog(k)).
● ● ●c++
#pragma GCC optimize ("O3")typedef long long ll;class Solution{public: ll gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } long long coutPairs(vector<int>& nums, int k) { ll cnt[100000] = { 0 }; for (auto &num : nums) { cnt[num % k]++; } ll ans = (cnt[0] * (cnt[0] - 1)) / 2; for (ll i = 1; i < k; i++) { ans += cnt[i] * cnt[0]; } for (ll a = 2; a < k; a++) { ll t = gcd(a, k); ll d = k * a / t; for (ll kp = d; kp <= ((ll) (k - 1)) * k; kp += d) { if (kp % a != 0) { continue; } ll b = kp / a; if (b >= k) { continue; } if (a < b) { ans += cnt[a] * cnt[b]; } else if (a == b) { ans += (cnt[a] * (cnt[a] - 1)) / 2; } } } return ans; }};