Skip to content

Commit

Permalink
add: 统计整数数目
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 16, 2024
1 parent 0c23459 commit 6ab8aeb
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
"DCMAKE",
"dearmour",
"deque",
"dlimit",
"dupenv",
"efcfe",
"egcfe",
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -850,3 +850,7 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文
- [被列覆盖的最多行数](src/math/maximum_rows_covered_by_columns.cpp) [位运算, 数组, 回溯, 枚举, 矩阵]

- LeetCode 2397. 被列覆盖的最多行数 <https://leetcode.cn/problems/maximum-rows-covered-by-columns>

- [统计整数数目](src/math/count_of_integers.cpp) [数学, 字符串, 动态规划]

- LeetCode 2719. 统计整数数目 <https://leetcode.cn/problems/count-of-integers>
Binary file added images/math/count_of_integers.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions src/math/count_of_integers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 统计整数数目
// https://leetcode.cn/problems/count-of-integers
// INLINE ../../images/math/count_of_integers.jpeg

#include <headers.hpp>

class Solution {
public:
int count(string num1, string num2, int min_sum, int max_sum) {
const int mod = 1000000007;
int m = num2.length();
std::vector<int> up_limit, down_limit;
for (char c : num2)
up_limit.push_back(c - '0');
num1 = std::string(std::max(0, m - static_cast<int>(num1.length())), '0') +
num1;
for (char c : num1)
down_limit.push_back(c - '0');

std::function<int(int, int, bool, bool, bool)> f;
std::unordered_map<long long, int> memo;

f = [&](int i, int s, bool valid, bool dlimit, bool ulimit) -> int {
if (i == m) {
return valid && min_sum <= s && s <= max_sum ? 1 : 0;
}
long long key =
i + 71LL * (s + 71LL * (valid + 2LL * (dlimit + 2LL * ulimit)));
if (memo.count(key))
return memo[key];
int down = dlimit ? down_limit[i] : 0;
int up = ulimit ? up_limit[i] : 9;
int ans = 0;
for (int d = down; d <= up; ++d) {
ans = (ans + f(i + 1, s + d, valid || d != 0, dlimit && d == down,
ulimit && d == up)) %
mod;
}
return memo[key] = ans;
};

return f(0, 0, false, true, true);
}
};
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 执行编译时间:2024-01-13 14:28:48
// 执行编译时间:2024-01-16 09:18:43
#include <gtest/gtest.h>
#include <lib.hpp>

Expand Down
26 changes: 26 additions & 0 deletions test/math/count_of_integers_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <math/count_of_integers.cpp>

TEST(统计整数数目, count) {
Solution solution;
// 示例 1:
// 输入:num1 = "1", num2 = "12", min_num = 1, max_num = 8
// 输出:11
// 解释:总共有 11 个整数的数位和在 1 到 8 之间,分别是 1,2,3,4,5,6,7,8,10,11
// 和 12 。所以我们返回 11 。
string num1 = "1";
string num2 = "12";
int min_sum = 1;
int max_sum = 8;
EXPECT_EQ(solution.count(num1, num2, min_sum, max_sum), 11);

// 示例 2:
// 输入:num1 = "1", num2 = "5", min_num = 1, max_num = 5
// 输出:5
// 解释:数位和在 1 到 5 之间的 5 个整数分别为 1,2,3,4 和 5 。所以我们返回 5
//
num1 = "1";
num2 = "5";
min_sum = 1;
max_sum = 5;
EXPECT_EQ(solution.count(num1, num2, min_sum, max_sum), 5);
}

0 comments on commit 6ab8aeb

Please sign in to comment.