From a5829c4e72171824d80efd5825ea921d0953a989 Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Sat, 25 Oct 2025 00:21:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?20251025=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 "exercise/LC2048_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LC2048_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC2048_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..0196d5d --- /dev/null +++ "b/exercise/LC2048_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,132 @@ +#include +using namespace std; + +// 【题目】LC2048. 下一个更大的数值平衡数 +// 【难度】中等 (难度分:1734) +// 【提交】2025.10.24 https://leetcode.cn/problems/next-greater-numerically-balanced-number/submissions/673273348 +// 【标签】哈希表、数学、回溯、计数、枚举、第264场周赛 + +// 方法1:枚举(执行时间:659ms | 击败:57.14%) +class Solution { +public: + bool is_balance(int x){ + vector count(10); + while(x > 0){ + count[x % 10]++; + x /= 10; + } + for(int i = 0; i < 10; i++){ + if(count[i] > 0 && count[i] != i) return false; + } + return true; + } + + int nextBeautifulNumber(int n) { + for(int i = n + 1; i <= 1224444; i++){ + if(is_balance(i)) return i; + } + return -1; + } +}; + +// 方法2:倒序贪心(执行时间:0ms | 击败:100.00%) +class Solution { + pair, bool> zero_one_knapspace(vector &a, int target){ + int n = a.size(); + vector f(n + 1, vector(target + 1)); + f[n][0] = true; + + for(int i = n - 1; i >= 0; i--){ + int v = a[i]; + for(int j = 0; j <= target; j++){ + if(j < v) f[i][j] = f[i + 1][j]; + else f[i][j] = f[i + 1][j] || f[i + 1][j - v]; + } + } + if(!f[0][target]) return {}; + vector ans; + int j = target; + for(int i = 0; i < n; i++){ + int v = a[i]; + if(j >= v && f[i + 1][j - v]){ + ans.emplace_back(v); + j -= v; + } + } + return {ans, true}; + } + +public: + int nextBeautifulNumber(int n) { + string s = "0" + to_string(n); + int m = s.size(); + constexpr int MX = 10; + int cnt[MX]{}; + for(int i = 1; i < m; i++) cnt[s[i] - '0']++; + + for(int i = m - 1; i >= 0; i--){ + if(i > 0) cnt[s[i] - '0']--; + for(int j = s[i] - '0' + 1; j < MX; j++){ + cnt[j]++; + int free = m - 1 - i; + for(int k = 0; k < MX; k++){ + int c = cnt[k]; + if(k < c){ + free = -1; + break; + } + if(c > 0) free -= k - c; + } + if(free < 0){ + cnt[j]--; + continue; + } + vector a; + for(int k = 1; k < MX; k++){ + if(cnt[k] == 0) a.emplace_back(k); + } + auto [missing, ok] = zero_one_knapspace(a, free); + if(!ok){ + cnt[j]--; + continue; + } + for(int v : missing) cnt[v] = -v; + s[i] = '0' + j; + s.resize(i + 1); + for(int k = 1; k < MX; k++){ + int c = cnt[k]; + c = c > 0 ? k - c : -c; + s += string(c, '0' + k); + } + return stoi(s); + } + } + return -1; + } +}; + +// 方法3:打表 + 二分查找(执行时间:0ms | 击败:100.00%) +class Solution { +public: + const vector balance { + 1, 22, 122, 212, 221, 333, 1333, 3133, 3313, 3331, 4444, + 14444, 22333, 23233, 23323, 23332, 32233, 32323, 32332, + 33223, 33232, 33322, 41444, 44144, 44414, 44441, 55555, + 122333, 123233, 123323, 123332, 132233, 132323, 132332, + 133223, 133232, 133322, 155555, 212333, 213233, 213323, + 213332, 221333, 223133, 223313, 223331, 224444, 231233, + 231323, 231332, 232133, 232313, 232331, 233123, 233132, + 233213, 233231, 233312, 233321, 242444, 244244, 244424, + 244442, 312233, 312323, 312332, 313223, 313232, 313322, + 321233, 321323, 321332, 322133, 322313, 322331, 323123, + 323132, 323213, 323231, 323312, 323321, 331223, 331232, + 331322, 332123, 332132, 332213, 332231, 332312, 332321, + 333122, 333212, 333221, 422444, 424244, 424424, 424442, + 442244, 442424, 442442, 444224, 444242, 444422, 515555, + 551555, 555155, 555515, 555551, 666666, 1224444 + }; + + int nextBeautifulNumber(int n) { + return *upper_bound(balance.begin(), balance.end(), n); + } +}; -- Gitee From 00e4f4a96841c4221a63ce89e7125a3ed7a3c3dd Mon Sep 17 00:00:00 2001 From: Shadow <15656193+shadowatomic@user.noreply.gitee.com> Date: Sun, 26 Oct 2025 01:08:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?20251025=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._\351\203\221\346\235\203\345\263\260.cpp" | 17 ++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 79 +++++++++++++++++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 47 +++++++++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 55 +++++++++++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 58 ++++++++++++++ ..._\351\203\221\346\235\203\345\263\260.cpp" | 52 ++++++++++++ 6 files changed, 308 insertions(+) create mode 100644 "exercise/LC1716_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "topic05/submit/LC2266_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "topic05/submit/LC2466_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "topic05/submit/LC300_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "topic05/submit/LC377_\351\203\221\346\235\203\345\263\260.cpp" create mode 100644 "topic05/submit/LC746_\351\203\221\346\235\203\345\263\260.cpp" diff --git "a/exercise/LC1716_\351\203\221\346\235\203\345\263\260.cpp" "b/exercise/LC1716_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..a772610 --- /dev/null +++ "b/exercise/LC1716_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,17 @@ +#include +using namespace std; + +// 【题目】LC1716. 计算力扣银行的钱 +// 【难度】简单 +// 【提交】2025.10.25 https://leetcode.cn/problems/calculate-money-in-leetcode-bank/submissions/673373028 +// 【标签】数学、463双周赛 + +// 方法1:(执行时间:0ms | 击败:100.00%) +class Solution { +public: + int totalMoney(int n) { + int cnt = n / 7; + int remain = n % 7; + return (cnt * 21 + 7 * (1 + cnt) * cnt / 2) + (1 + remain) * remain / 2 + remain * cnt; + } +}; diff --git "a/topic05/submit/LC2266_\351\203\221\346\235\203\345\263\260.cpp" "b/topic05/submit/LC2266_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..40e0ca4 --- /dev/null +++ "b/topic05/submit/LC2266_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,79 @@ +#include +using namespace std; + +// 【题目】LC2266. 统计打字方案数 +// 【难度】中等 (难度分:1857) +// 【提交】2025.10.25 +// 【标签】哈希表、数学、字符串、动态规划、第292场双周赛 + +// 方法1:动态规划(执行时间:4ms | 击败:80.82%) +#define ll long long + +int m = 1'000'000'007; +int MAX = 100001; +vector dp3 = {1, 1, 2, 4}; +vector dp4 = {1, 1, 2, 4}; + +int init = [](){ + for(int i = 4; i <= MAX; i++){ + dp3.emplace_back((dp3[i - 1] + dp3[i - 2] + dp3[i - 3]) % m); + dp4.emplace_back((dp4[i - 1] + dp4[i - 2] + dp4[i - 3] + dp4[i - 4]) % m); + } + return 0; +}(); + +class Solution { +public: + int countTexts(string pressedKeys) { + int n = pressedKeys.size(); + ll res = 1; + int cnt = 1; + for(int i = 1; i < n; i++){ + if(pressedKeys[i] == pressedKeys[i - 1]) cnt++; + else { + if(pressedKeys[i - 1] == '7' || pressedKeys[i - 1] == '9') res *= dp4[cnt]; + else res *= dp3[cnt]; + res %= m; + cnt = 1; + } + } + if(pressedKeys[n - 1] == '7' || pressedKeys[n - 1] == '9') res *= dp4[cnt]; + else res *= dp3[cnt]; + res %= m; + return res; + } +}; + +// 方法2: (执行时间:0ms | 击败:100.00%) +#define ll long long + +int m = 1'000'000'007; +int MAX = 100001; +vector dp3 = {1, 1, 2, 4}; +vector dp4 = {1, 1, 2, 4}; + +int init = [](){ + for(int i = 4; i <= MAX; i++){ + dp3.emplace_back((dp3[i - 1] + dp3[i - 2] + dp3[i - 3]) % m); + dp4.emplace_back((dp4[i - 1] + dp4[i - 2] + dp4[i - 3] + dp4[i - 4]) % m); + } + return 0; +}(); + +class Solution { +public: + int countTexts(string s) { + int n = s.size(); + ll res = 1; + int cnt = 0; + for(int i = 0; i < n; i++){ + char c = s[i]; + cnt++; + if(i == n - 1 || s[i] != s[i + 1]) { + res = res * (c != '7' && c != '9' ? dp3[cnt] : dp4[cnt]) % m; + cnt = 0; + } + } + return res; + } +}; diff --git "a/topic05/submit/LC2466_\351\203\221\346\235\203\345\263\260.cpp" "b/topic05/submit/LC2466_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..f97fcd5 --- /dev/null +++ "b/topic05/submit/LC2466_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,47 @@ +#include +using namespace std; + +// 【题目】LC2466. 统计构造好字符串的方案数 +// 【难度】中等 (难度分:1694) +// 【提交】2025.10.25 +// 【标签】动态规划、第91场双周赛 + +// 方法1:用动态规划递推(执行时间:3ms | 击败:98.32%) +class Solution { +public: + int countGoodStrings(int low, int high, int zero, int one) { + const int MOD = 1'000'000'007; + int ans = 0; + vector dp(high + 1); + dp[0] = 1; + for(int i = 1; i <= high; i++){ + if(i >= zero) dp[i] = dp[i - zero]; + if(i >= one) dp[i] = (dp[i] + dp[i - one]) % MOD; + if(i >= low) ans = (ans + dp[i]) % MOD; + } + return ans; + } +}; + +// 方法2:DFS + 记忆化搜索(执行时间:11ms | 击败:20.09%) +class Solution { +public: + int countGoodStrings(int low, int high, int zero, int one) { + const int MOD = 1'000'000'007; + vector memo(high + 1, -1); + + auto dfs = [&](this auto&& dfs, int i){ + if(i < 0) return 0; + if(i == 0) return 1; + int &res = memo[i]; + if(res != -1) return res; + return res = (dfs(i - zero) + dfs(i - one)) % MOD; + }; + + int ans = 0; + for(int i = low; i <= high; i++){ + ans = (ans + dfs(i)) % MOD; + } + return ans; + } +}; diff --git "a/topic05/submit/LC300_\351\203\221\346\235\203\345\263\260.cpp" "b/topic05/submit/LC300_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..30d9628 --- /dev/null +++ "b/topic05/submit/LC300_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,55 @@ +#include +using namespace std; + +// 【题目】LC300. 最长递增子序列 +// 【难度】中等 +// 【提交】2025.10.25 https://leetcode.cn/problems/longest-increasing-subsequence/submissions/673376067 +// 【标签】数组、二分查找、动态规划 + +// 方法1:(执行时间:79ms | 击败:49.22%) +class Solution { +public: + int lengthOfLIS(vector& nums) { + int n = nums.size(); + vector dp(n + 1); + dp[1] = 1; + for(int i = 2; i <= n; i++){ + int max_num = 0; + for(int j = 1; j < i; j++){ + if(nums[j - 1] < nums[i - 1] && dp[j] > max_num) max_num = dp[j]; + } + dp[i] = max_num + 1; + } + return ranges::max(dp); + } +}; + +// 贪心 + 二分查找 (执行时间:0ms | 击败:100.00%) +class Solution { +public: + int lengthOfLIS(vector& nums) { + vector g; + for(int x : nums){ + auto it = ranges::lower_bound(g, x); + if(it == g.end()) g.emplace_back(x); + else *it = x; + } + return g.size(); + } +}; + +// 原地修改(空间复杂度: O(1)) (执行时间:0ms | 击败:100.00%) +class Solution { +public: + int lengthOfLIS(vector& nums) { + auto end = nums.begin(); + for(int x : nums){ + auto it = lower_bound(nums.begin(), end, x); + if(it == end){ + end++; + } + *it = x; + } + return end - nums.begin(); + } +}; diff --git "a/topic05/submit/LC377_\351\203\221\346\235\203\345\263\260.cpp" "b/topic05/submit/LC377_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..127e587 --- /dev/null +++ "b/topic05/submit/LC377_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,58 @@ +#include +using namespace std; + +// 【题目】LC377. 组合总和 Ⅳ +// 【难度】中等 +// 【提交】2025.10.25 https://leetcode.cn/problems/combination-sum-iv/submissions/673467029 +// 【标签】数组、动态规划 + +// 方法1:(执行时间:0ms | 击败:100.00%) +class Solution { +public: + int combinationSum4(vector& nums, int target) { + vector f(target + 1); + f[0] = 1; + for(int i = 1; i <= target; i++){ + for(int x : nums){ + if(x <= i && f[i - x] <= INT_MAX - f[i]) f[i] += f[i - x]; + } + } + return f[target]; + } +}; + +// 方法2:(执行时间:0ms | 击败:100.00%) +using ll = long long; +class Solution { +public: + int combinationSum4(vector& nums, int target) { + vector f(target + 1); + f[0] = 1; + for(int i = 1; i <= target; i++){ + for(int x : nums){ + if(x <= i) f[i] += f[i - x]; + } + } + return f[target]; + } +}; + +// 方法3:DFS剪枝(执行时间:0ms | 击败:100.00%) +class Solution { +public: + int combinationSum4(vector& nums, int target) { + vector memo(target + 1, -1); + + auto dfs = [&](this auto&& dfs, int i){ + if(i == 0) return 1; + int &res = memo[i]; + if(res != -1) return res; + + res = 0; + for(int x : nums) if(x <= i) res += dfs(i - x); + return res; + }; + + return dfs(target); + } +}; diff --git "a/topic05/submit/LC746_\351\203\221\346\235\203\345\263\260.cpp" "b/topic05/submit/LC746_\351\203\221\346\235\203\345\263\260.cpp" new file mode 100644 index 0000000..01565e0 --- /dev/null +++ "b/topic05/submit/LC746_\351\203\221\346\235\203\345\263\260.cpp" @@ -0,0 +1,52 @@ +#include +using namespace std; + +// 【题目】LC746. 使用最小花费爬楼梯 +// 【难度】简单 (难度分:1358) +// 【提交】2025.10.25 https://leetcode.cn/problems/min-cost-climbing-stairs/submissions/673422808 +// 【标签】数组、动态规划 + +// 方法1:(执行时间:0ms | 击败:100.00%) +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + int n = cost.size(); + if(n == 2) return min(cost[0], cost[1]); + vector f(n + 1); + f[0] = cost[0]; + f[1] = cost[1]; + for(int i = 2; i < n; i++){ + f[i] = min(f[i - 1], f[i - 2]) + cost[i]; + } + return min(f[n - 1], f[n - 2]); + } +}; + +// 方法2: (执行时间:0ms | 击败:100.00%) +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + int n = cost.size(); + vector f(n + 1); + f[0] = f[1] = 0; + for(int i = 2; i <= n; i++){ + f[i] = min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2]); + } + return f[n]; + } +}; + +// 方法3:(执行时间:0ms | 击败:100.00%) +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + int n = cost.size(); + vector f(n + 1); + f[0] = 0; + f[1] = min(cost[0], cost[1]); + for(int i = 2; i < n; i++){ + f[i] = min(f[i - 1] + cost[i], f[i - 2] + cost[i - 1]); + } + return f[n - 1]; + } +}; -- Gitee