Skip to content

Commit 6ab8aeb

Browse files
committed
add: 统计整数数目
1 parent 0c23459 commit 6ab8aeb

File tree

6 files changed

+76
-1
lines changed

6 files changed

+76
-1
lines changed

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
"DCMAKE",
197197
"dearmour",
198198
"deque",
199+
"dlimit",
199200
"dupenv",
200201
"efcfe",
201202
"egcfe",

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -850,3 +850,7 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文
850850
- [被列覆盖的最多行数](src/math/maximum_rows_covered_by_columns.cpp) [位运算, 数组, 回溯, 枚举, 矩阵]
851851

852852
- LeetCode 2397. 被列覆盖的最多行数 <https://leetcode.cn/problems/maximum-rows-covered-by-columns>
853+
854+
- [统计整数数目](src/math/count_of_integers.cpp) [数学, 字符串, 动态规划]
855+
856+
- LeetCode 2719. 统计整数数目 <https://leetcode.cn/problems/count-of-integers>

images/math/count_of_integers.jpeg

87.4 KB
Loading

src/math/count_of_integers.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// 统计整数数目
2+
// https://leetcode.cn/problems/count-of-integers
3+
// INLINE ../../images/math/count_of_integers.jpeg
4+
5+
#include <headers.hpp>
6+
7+
class Solution {
8+
public:
9+
int count(string num1, string num2, int min_sum, int max_sum) {
10+
const int mod = 1000000007;
11+
int m = num2.length();
12+
std::vector<int> up_limit, down_limit;
13+
for (char c : num2)
14+
up_limit.push_back(c - '0');
15+
num1 = std::string(std::max(0, m - static_cast<int>(num1.length())), '0') +
16+
num1;
17+
for (char c : num1)
18+
down_limit.push_back(c - '0');
19+
20+
std::function<int(int, int, bool, bool, bool)> f;
21+
std::unordered_map<long long, int> memo;
22+
23+
f = [&](int i, int s, bool valid, bool dlimit, bool ulimit) -> int {
24+
if (i == m) {
25+
return valid && min_sum <= s && s <= max_sum ? 1 : 0;
26+
}
27+
long long key =
28+
i + 71LL * (s + 71LL * (valid + 2LL * (dlimit + 2LL * ulimit)));
29+
if (memo.count(key))
30+
return memo[key];
31+
int down = dlimit ? down_limit[i] : 0;
32+
int up = ulimit ? up_limit[i] : 9;
33+
int ans = 0;
34+
for (int d = down; d <= up; ++d) {
35+
ans = (ans + f(i + 1, s + d, valid || d != 0, dlimit && d == down,
36+
ulimit && d == up)) %
37+
mod;
38+
}
39+
return memo[key] = ans;
40+
};
41+
42+
return f(0, 0, false, true, true);
43+
}
44+
};

test/lib/lib_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// 执行编译时间:2024-01-13 14:28:48
1+
// 执行编译时间:2024-01-16 09:18:43
22
#include <gtest/gtest.h>
33
#include <lib.hpp>
44

test/math/count_of_integers_test.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <math/count_of_integers.cpp>
2+
3+
TEST(统计整数数目, count) {
4+
Solution solution;
5+
// 示例 1:
6+
// 输入:num1 = "1", num2 = "12", min_num = 1, max_num = 8
7+
// 输出:11
8+
// 解释:总共有 11 个整数的数位和在 1 到 8 之间,分别是 1,2,3,4,5,6,7,8,10,11
9+
// 和 12 。所以我们返回 11 。
10+
string num1 = "1";
11+
string num2 = "12";
12+
int min_sum = 1;
13+
int max_sum = 8;
14+
EXPECT_EQ(solution.count(num1, num2, min_sum, max_sum), 11);
15+
16+
// 示例 2:
17+
// 输入:num1 = "1", num2 = "5", min_num = 1, max_num = 5
18+
// 输出:5
19+
// 解释:数位和在 1 到 5 之间的 5 个整数分别为 1,2,3,4 和 5 。所以我们返回 5
20+
//
21+
num1 = "1";
22+
num2 = "5";
23+
min_sum = 1;
24+
max_sum = 5;
25+
EXPECT_EQ(solution.count(num1, num2, min_sum, max_sum), 5);
26+
}

0 commit comments

Comments
 (0)