-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,7 @@ | |
"DCMAKE", | ||
"dearmour", | ||
"deque", | ||
"dlimit", | ||
"dupenv", | ||
"efcfe", | ||
"egcfe", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |