diff --git a/solution/1500-1599/1531.String Compression II/README.md b/solution/1500-1599/1531.String Compression II/README.md index 5e2a928334de5..8bf1a1422b5a2 100644 --- a/solution/1500-1599/1531.String Compression II/README.md +++ b/solution/1500-1599/1531.String Compression II/README.md @@ -69,6 +69,24 @@ tags: +### cpp +```cpp +class Solution { +public: + bool winnerSquareGame(int n) { + vector dp(n + 1, false); + for (int i = 1; i <= n; i++) { + for (int j = 1; j * j <= i; j++) { + if (!dp[i - j * j]) { + dp[i] = true; + break; + } + } + } + return dp[n]; + } +}; + #### Java ```java diff --git a/solution/1500-1599/1531.String Compression II/solutions.cpp b/solution/1500-1599/1531.String Compression II/solutions.cpp new file mode 100644 index 0000000000000..b117440ade3a0 --- /dev/null +++ b/solution/1500-1599/1531.String Compression II/solutions.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + bool winnerSquareGame(int n) { + vector dp(n + 1, false); + for (int i = 1; i <= n; i++) { + for (int j = 1; j * j <= i; j++) { + if (!dp[i - j * j]) { + dp[i] = true; + break; + } + } + } + return dp[n]; + } +}; diff --git a/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/README.md b/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/README.md index dc6aca161b065..89366783417a9 100644 --- a/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/README.md +++ b/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/README.md @@ -129,6 +129,29 @@ tags: #### C++ ```cpp +class Solution { +public: + vector validSequence(string word1, string word2) { + int m = word1.size(), n = word2.size(); + vector suf(n + 1, -1); + suf[n] = m; + for (int i = m - 1, j = n - 1; i >= 0; i--) { + if (word1[i] == word2[j]) suf[j--] = i; + if (j < 0) break; + } + vector res(n); + bool changed = false; + for (int i = 0, j = 0; i < m; i++) { + if (word1[i] == word2[j]) res[j++] = i; + else if (!changed && i < suf[j + 1]) { + changed = true; + res[j++] = i; + } + if (j == n) return res; + } + return {}; + } +}; ``` diff --git a/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/README_EN.md b/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/README_EN.md index 066dfffe10e12..98ad30da7258a 100644 --- a/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/README_EN.md +++ b/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/README_EN.md @@ -126,6 +126,29 @@ tags: #### C++ ```cpp +class Solution { +public: + vector validSequence(string word1, string word2) { + int m = word1.size(), n = word2.size(); + vector suf(n + 1, -1); + suf[n] = m; + for (int i = m - 1, j = n - 1; i >= 0; i--) { + if (word1[i] == word2[j]) suf[j--] = i; + if (j < 0) break; + } + vector res(n); + bool changed = false; + for (int i = 0, j = 0; i < m; i++) { + if (word1[i] == word2[j]) res[j++] = i; + else if (!changed && i < suf[j + 1]) { + changed = true; + res[j++] = i; + } + if (j == n) return res; + } + return {}; + } +}; ``` diff --git a/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/solutions.cpp b/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/solutions.cpp new file mode 100644 index 0000000000000..41a3bfd976e53 --- /dev/null +++ b/solution/3300-3399/3302.Find the Lexicographically Smallest Valid Sequence/solutions.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector validSequence(string word1, string word2) { + int m = word1.size(), n = word2.size(); + vector suf(n + 1, -1); + suf[n] = m; + for (int i = m - 1, j = n - 1; i >= 0; i--) { + if (word1[i] == word2[j]) suf[j--] = i; + if (j < 0) break; + } + vector res(n); + bool changed = false; + for (int i = 0, j = 0; i < m; i++) { + if (word1[i] == word2[j]) res[j++] = i; + else if (!changed && i < suf[j + 1]) { + changed = true; + res[j++] = i; + } + if (j == n) return res; + } + return {}; + } +}; diff --git a/solution/3300-3399/3348.Smallest Divisible Digit Product II/README.md b/solution/3300-3399/3348.Smallest Divisible Digit Product II/README.md index 0031aaa52e680..225a2a7cde7db 100644 --- a/solution/3300-3399/3348.Smallest Divisible Digit Product II/README.md +++ b/solution/3300-3399/3348.Smallest Divisible Digit Product II/README.md @@ -103,6 +103,147 @@ tags: #### C++ ```cpp +using u64=unsigned long long; +int prime[]={2, 3, 5, 7}; + +class Solution { +public: + array exp={0}; + + bool primeFactor(u64 x) { + if (x==0) return 0; + + // Count powers of 2 + exp[0]=countr_zero(x); + x>>=exp[0]; + + // Count powers of 3, 5, and 7 + for (int i=1; i< 4; i++) { + int p=prime[i]; + for (; x%p==0; x/=p) + exp[i]++; + } + return x==1; + } + + void modifyExp(char c, int dir) { + int x=c-'0'; + switch (x) { + case 2: exp[0]+=dir; break; + case 4: exp[0]+=dir<<1; break; + case 8: exp[0]+=dir*3; break; + case 3: exp[1]+=dir; break; + case 5: exp[2]+=dir; break; + case 6: exp[0]+=dir; exp[1]+=dir; break; + case 7: exp[3]+=dir; break; + case 9: exp[1]+=dir<<1; break; + } + } + + string buildSuffix(int len, bool &valid) { + + int digit[10]={0}; + + int e0=max(0, exp[0]); + int e1=max(0, exp[1]); + int e2=max(0, exp[2]); + int e3=max(0, exp[3]); + + digit[8]=e0/3; + int r0=e0%3; + + digit[9]=e1>>1; + int r1=e1&1; + + digit[5]=e2; + digit[7]=e3; + + if (r0==1 && r1==1) + digit[6]=1; + else if (r0==2 && r1==1) { + digit[2]=1; + digit[6]=1; + } + else { + if (r0==1) digit[2]=1; + else if (r0==2) digit[4]=1; + if (r1==1) digit[3]=1; + } + + int total_digits = 0; + for (int i=2; i<=9; i++) total_digits+=digit[i]; + + if (total_digits>len) { + valid=0; + return ""; + } + + digit[1]=len-total_digits; + valid=1; + + string ans; + for (int i=1; i<=9; i++) { + ans.append(digit[i], '0'+i); + } + return ans; + } + + string smallestNumber(string& num, long long t) { + if (!primeFactor(t)) return "-1"; + + int n=num.size(); + auto origExp=exp; + + // 1. Check if num itself works + bool zeroFound=0; + int firstZero=-1; + for (int i=0; i= 0; i--) { + int startDigit=(i0) + modifyExp(num[i-1], +1); + } + + // Expand length if necessary + exp=origExp; + int targetLen=n+1; + while (1) { + string suffix=buildSuffix(targetLen, valid); + if (valid) return suffix; + targetLen++; + } + } +}; ``` diff --git a/solution/3300-3399/3348.Smallest Divisible Digit Product II/solutions.cpp b/solution/3300-3399/3348.Smallest Divisible Digit Product II/solutions.cpp new file mode 100644 index 0000000000000..6c9eb8776568f --- /dev/null +++ b/solution/3300-3399/3348.Smallest Divisible Digit Product II/solutions.cpp @@ -0,0 +1,141 @@ +using u64=unsigned long long; +int prime[]={2, 3, 5, 7}; + +class Solution { +public: + array exp={0}; + + bool primeFactor(u64 x) { + if (x==0) return 0; + + // Count powers of 2 + exp[0]=countr_zero(x); + x>>=exp[0]; + + // Count powers of 3, 5, and 7 + for (int i=1; i< 4; i++) { + int p=prime[i]; + for (; x%p==0; x/=p) + exp[i]++; + } + return x==1; + } + + void modifyExp(char c, int dir) { + int x=c-'0'; + switch (x) { + case 2: exp[0]+=dir; break; + case 4: exp[0]+=dir<<1; break; + case 8: exp[0]+=dir*3; break; + case 3: exp[1]+=dir; break; + case 5: exp[2]+=dir; break; + case 6: exp[0]+=dir; exp[1]+=dir; break; + case 7: exp[3]+=dir; break; + case 9: exp[1]+=dir<<1; break; + } + } + + string buildSuffix(int len, bool &valid) { + + int digit[10]={0}; + + int e0=max(0, exp[0]); + int e1=max(0, exp[1]); + int e2=max(0, exp[2]); + int e3=max(0, exp[3]); + + digit[8]=e0/3; + int r0=e0%3; + + digit[9]=e1>>1; + int r1=e1&1; + + digit[5]=e2; + digit[7]=e3; + + if (r0==1 && r1==1) + digit[6]=1; + else if (r0==2 && r1==1) { + digit[2]=1; + digit[6]=1; + } + else { + if (r0==1) digit[2]=1; + else if (r0==2) digit[4]=1; + if (r1==1) digit[3]=1; + } + + int total_digits = 0; + for (int i=2; i<=9; i++) total_digits+=digit[i]; + + if (total_digits>len) { + valid=0; + return ""; + } + + digit[1]=len-total_digits; + valid=1; + + string ans; + for (int i=1; i<=9; i++) { + ans.append(digit[i], '0'+i); + } + return ans; + } + + string smallestNumber(string& num, long long t) { + if (!primeFactor(t)) return "-1"; + + int n=num.size(); + auto origExp=exp; + + // 1. Check if num itself works + bool zeroFound=0; + int firstZero=-1; + for (int i=0; i= 0; i--) { + int startDigit=(i0) + modifyExp(num[i-1], +1); + } + + // Expand length if necessary + exp=origExp; + int targetLen=n+1; + while (1) { + string suffix=buildSuffix(targetLen, valid); + if (valid) return suffix; + targetLen++; + } + } +}; diff --git a/solution/3400-3499/3464.Maximize the Distance Between Points on a Square/README.md b/solution/3400-3499/3464.Maximize the Distance Between Points on a Square/README.md index 471cf92a720b7..92936bcc0e371 100644 --- a/solution/3400-3499/3464.Maximize the Distance Between Points on a Square/README.md +++ b/solution/3400-3499/3464.Maximize the Distance Between Points on a Square/README.md @@ -119,6 +119,51 @@ tags: #### C++ ```cpp +uint32_t dists[15000]; +uint16_t nxt[15000]; + +class Solution { +public: + int maxDistance(int s, vector>& points, int k) { + const uint64_t n = points.size(), perim = 4UL * s; + + for (uint i = 0; i < n; i++) { + uint64_t x = points[i][0], y = points[i][1]; + dists[i] = (x == 0 || y == s) ? perim - x - y : x + y; + } + + sort(dists, dists + n); + + const auto check = [&](uint64_t x) -> bool { + for (uint i = 0, r = 0; i < n; i++) { + r = max(r, i + 1); + while (r < n && dists[r] < dists[i] + x) r++; + nxt[i] = r; + } + uint64_t maxreach = dists[n - 1] + perim - (k - 1) * x; + auto limit = perim - x; + + for (uint i = 0; i < n && dists[i] <= maxreach; i++) { + uint j = i; + for (uint r = k - 1; r > 0 && j != n; r--) { + j = nxt[j]; + } + if (j != n && dists[j] - dists[i] <= limit) return true; + } + return false; + }; + + uint64_t lo = 1, hi = perim / k; + while (lo < hi) { + auto x = (lo + hi + 1) / 2; + if (check(x)) + lo = x; + else + hi = x - 1; + } + return hi; + } +}; ``` diff --git a/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md b/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md index 8ed17fe41e0e8..b2f5a189b3c45 100644 --- a/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md +++ b/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md @@ -97,7 +97,44 @@ tags: #### C++ ```cpp - +class Solution { +public: + int uniqueXorTriplets(vector& nums) { + const int SIZE = 2048; // since nums[i] <= 1500 < 2048, all XORs fit in 11 bits + + vector present(SIZE, false); + for (int v : nums) present[v] = true; + + vector distinct; + for (int v = 0; v < SIZE; v++) { + if (present[v]) distinct.push_back(v); + } + + // Step 1: compute all possible pairwise XORs (a ^ b) + vector pairXor(SIZE, false); + for (int a : distinct) { + for (int b : distinct) { + pairXor[a ^ b] = true; + } + } + + // Step 2: compute all possible triple XORs (s ^ c) where s is from pairXor set + vector tripleXor(SIZE, false); + for (int s = 0; s < SIZE; s++) { + if (!pairXor[s]) continue; + for (int c : distinct) { + tripleXor[s ^ c] = true; + } + } + + int count = 0; + for (int i = 0; i < SIZE; i++) { + if (tripleXor[i]) count++; + } + + return count; + } +}; ``` #### Go diff --git a/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md b/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md index e201e656cae0e..df3c373f8b45d 100644 --- a/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md +++ b/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md @@ -121,6 +121,96 @@ tags: #### C++ ```cpp +long long comb(long long n, long long m, long long k) { + long long res = 1; + if (n - m < m) { + m = n - m; + } + + for (long long i = 1; i <= m; i++) { + res = res * (n - i + 1) / i; + if (res > k) { + return k + 1; + } + } + return res; +} + +long long permutations(int rem, int* bucket, long long k) { + long long ways = 1; + for (int i = 0; i < 26; i++) { + if (bucket[i] == 0) { + continue; + } + + ways *= comb(rem, bucket[i], k); + if (ways > k) { + break; + } + rem -= bucket[i]; + } + return ways; +} + +char* smallestPalindrome(char* s, long long k) { + int len = strlen(s); + int partition = len / 2; + int bucket[26] = {0}; + + for (int i = 0; i < partition; i++) { + bucket[s[i] - 'a'] += 1; + } + + char* left = (char*)malloc(partition + 1); + int left_idx = 0; + long long start_index = 1; + + for (int pos = 0; pos < partition; pos++) { + for (int i = 0; i < 26; i++) { + if (bucket[i] == 0) { + continue; + } + + bucket[i] -= 1; + + long long ways = permutations(partition - pos - 1, bucket, k); + if (start_index + ways > k) { + left[left_idx++] = i + 'a'; + break; + } + + bucket[i] += 1; + start_index += ways; + } + } + left[left_idx] = '\0'; + + if (left_idx < partition) { + char* empty_res = (char*)malloc(1); + empty_res[0] = '\0'; + free(left); + return empty_res; + } + + char* result = (char*)malloc(len + 1); + int res_idx = 0; + + for (int i = 0; i < partition; i++) { + result[res_idx++] = left[i]; + } + + if (len % 2 != 0) { + result[res_idx++] = s[partition]; + } + + for (int i = partition - 1; i >= 0; i--) { + result[res_idx++] = left[i]; + } + result[res_idx] = '\0'; + + free(left); + return result; +} ``` diff --git a/solution/3600-3699/3614.Process String with Special Operations II/README_EN.md b/solution/3600-3699/3614.Process String with Special Operations II/README_EN.md index b4814d548e947..e712e3b1b7371 100644 --- a/solution/3600-3699/3614.Process String with Special Operations II/README_EN.md +++ b/solution/3600-3699/3614.Process String with Special Operations II/README_EN.md @@ -229,6 +229,45 @@ tags: #### C++ ```cpp +class Solution { +public: + char processStr(string s, long long k) { + long long len = 0; + for (char c : s) { + if (islower(c)) { + len++; + } + else if (c == '*') { + if (len > 0) len--; + } + else if (c == '#') { + len *= 2; + } + else if (c == '%') {} + } + if (k >= len) return '.'; + for (int i = s.size() - 1; i >= 0; i--) { + char c = s[i]; + if (islower(c)) { + if (k == len - 1) { + return c; + } + len--; + } + else if (c == '*') { + len++; + } + else if (c == '#') { + len /= 2; + k %= len; + } + else if (c == '%') { + k = len - 1 - k; + } + } + return '.'; + } +}; ``` diff --git a/solution/3600-3699/3655.XOR After Range Multiplication Queries II/solution.cpp b/solution/3600-3699/3655.XOR After Range Multiplication Queries II/solution.cpp new file mode 100644 index 0000000000000..6f1adf5cfd13b --- /dev/null +++ b/solution/3600-3699/3655.XOR After Range Multiplication Queries II/solution.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; + +class Solution { + static constexpr int MOD = 1000000007; + + long long modpow(long long a, long long e) { + long long r = 1 % MOD; + a %= MOD; + while (e > 0) { + if (e & 1) r = (r * a) % MOD; + a = (a * a) % MOD; + e >>= 1; + } + return r; + } + +public: + int xorAfterQueries(vector& nums, vector>& queries) { + int n = nums.size(); + int B = std::sqrt(n) + 1; + + vector>>> events(B + 1); + for (int k = 1; k <= B; ++k) + events[k].resize(k); + + for (auto &qq : queries) { + int l = qq[0], r = qq[1], k = qq[2], v = qq[3]; + if (k > B) { + for (int idx = l; idx <= r; idx += k) + nums[idx] = (long long)nums[idx] * v % MOD; + } else { + int res = l % k; + int t1 = (l - res) / k; + int t2 = (r - res) / k; + events[k][res].push_back({t1, v}); + + if (t2 + 1 <= (n - 1 - res) / k) { + int invv = modpow(v, MOD - 2); + events[k][res].push_back({t2 + 1, invv}); + } + } + } + + for (int k = 1; k <= B; ++k) + for (int res = 0; res < k; ++res) { + auto &ev = events[k][res]; + if (ev.empty()) + continue; + + sort(ev.begin(), ev.end()); + vector> comp; + + for (auto &p : ev) { + if (!comp.empty() && comp.back().first == p.first) + comp.back().second = (long long)comp.back().second * p.second % MOD; + else + comp.push_back(p); + } + + long long cur = 1; + int ptr = 0; + int t = 0; + for (int idx = res; idx < n; idx += k, ++t) { + while (ptr < comp.size() && comp[ptr].first == t) { + cur = (cur * comp[ptr].second) % MOD; + ++ptr; + } + nums[idx] = nums[idx] * cur % MOD; + } + } + + int xr = 0; + for (int x : nums) + xr ^= x; + + return xr; + } +}; \ No newline at end of file diff --git a/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/README.md b/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/README.md index 4ab82d839a117..72b97d1aa5cfb 100644 --- a/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/README.md +++ b/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/README.md @@ -156,6 +156,83 @@ tags: #### C++ ```cpp +class Solution { +public: + using ll = long long; + using t3 = tuple; + ll enc(ll a, ll b, ll c) + { + return (a << 26) + (b << 15) + (c); + } + t3 dec(ll l) + { + ll c = l & ((1 << 15) - 1); + ll a = l >> 26; + ll b = (l >> 15) & ((1 << 11) - 1); + return {a, b, c}; + } + long long minMergeCost(vector>& lists) { + int n = lists.size(); + int m = 1 << n; + + vector vals; + for(auto& v: lists) + { + for(auto x: v) vals.push_back(x); + } + sort(vals.begin(), vals.end()); + vals.erase(unique(vals.begin(), vals.end()), vals.end()); + + vector cnt(m), med(m); + for(int i = 1; i < m; ++i) + { + for(int j = 0; j < n; ++j) + { + if((i >> j) & 1) cnt[i] += lists[j].size(); + } + } + for(int mask = 1; mask < m; ++mask) + { + ll need = (cnt[mask] + 1) / 2; + int l = 0, r = vals.size() - 1; + + while(l < r) + { + int mid = (l + r) / 2; + ll le = 0; + + for(int b = mask; b; b &= b - 1) + { + int id = __builtin_ctz(b); + le += upper_bound(lists[id].begin(), lists[id].end(), vals[mid]) - lists[id].begin(); + if(le >= need) break; + } + + if(le >= need) r = mid; + else l = mid + 1; + } + + med[mask] = vals[l]; + } + vector dp(m, LLONG_MAX); + for(int i = 1; i < m; ++i) + { + if(__builtin_popcount(i) == 1) + { + dp[i] = 0; + continue; + } + for(int j = (i - 1) & i; j > 0; j = (j - 1) & i) + { + int k = (i ^ j) & i; + dp[i] = min(dp[i], dp[j] + dp[k] + abs(med[j] - med[k])); + } + dp[i] += cnt[i]; + //cout << i << " " << cnt[i] << " " << dp[i] << endl; + } + return dp[m - 1]; + } +}; ``` diff --git a/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/solution.cpp b/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/solution.cpp new file mode 100644 index 0000000000000..61f22b375a08e --- /dev/null +++ b/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/solution.cpp @@ -0,0 +1,77 @@ +class Solution { +public: + using ll = long long; + using t3 = tuple; + ll enc(ll a, ll b, ll c) + { + return (a << 26) + (b << 15) + (c); + } + t3 dec(ll l) + { + ll c = l & ((1 << 15) - 1); + ll a = l >> 26; + ll b = (l >> 15) & ((1 << 11) - 1); + return {a, b, c}; + } + long long minMergeCost(vector>& lists) { + int n = lists.size(); + int m = 1 << n; + + vector vals; + for(auto& v: lists) + { + for(auto x: v) vals.push_back(x); + } + sort(vals.begin(), vals.end()); + vals.erase(unique(vals.begin(), vals.end()), vals.end()); + + vector cnt(m), med(m); + for(int i = 1; i < m; ++i) + { + for(int j = 0; j < n; ++j) + { + if((i >> j) & 1) cnt[i] += lists[j].size(); + } + } + for(int mask = 1; mask < m; ++mask) + { + ll need = (cnt[mask] + 1) / 2; + int l = 0, r = vals.size() - 1; + + while(l < r) + { + int mid = (l + r) / 2; + ll le = 0; + + for(int b = mask; b; b &= b - 1) + { + int id = __builtin_ctz(b); + le += upper_bound(lists[id].begin(), lists[id].end(), vals[mid]) - lists[id].begin(); + if(le >= need) break; + } + + if(le >= need) r = mid; + else l = mid + 1; + } + + med[mask] = vals[l]; + } + vector dp(m, LLONG_MAX); + for(int i = 1; i < m; ++i) + { + if(__builtin_popcount(i) == 1) + { + dp[i] = 0; + continue; + } + for(int j = (i - 1) & i; j > 0; j = (j - 1) & i) + { + int k = (i ^ j) & i; + dp[i] = min(dp[i], dp[j] + dp[k] + abs(med[j] - med[k])); + } + dp[i] += cnt[i]; + //cout << i << " " << cnt[i] << " " << dp[i] << endl; + } + return dp[m - 1]; + } +};