Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 5 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using LL = long long;
class Solution {
int n;
public:
long long countSubstrings(string s)
{
n = s.size();
vector<int>nums;
for (auto ch: s)
nums.push_back(ch-'0');
nums.insert(nums.begin(), 0);

LL ret = 0;
for (int k=1; k<=9; k++)
ret += helper(nums, k);
return ret;
}

LL helper(vector<int>&nums, int k)
{
vector<LL>count(k, 0);
vector<LL>count2(k,0);
LL ret = 0;

int r = 0;
count[0] = 1;
for (int i=1; i<=n; i++)
{
for (int d=0; d<k; d++)
count2[d] = 0;
for (int d=0; d<k; d++)
count2[(d*10)%k]+=count[d];
count = count2;

r = (r*10+nums[i])%k;
if (nums[i]==k)
ret += count[r];
count[r]+=1;
}

return ret;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
vector<int> assignElements(vector<int>& groups, vector<int>& elements)
{
int n = *max_element(groups.begin(), groups.end());
vector<int>arr(n+1, -1);

for (int j=0; j<elements.size(); j++)
{
int x0 = elements[j];
if (x0>n) continue;

if (arr[x0]!=-1) continue;

int x = x0;
while (x<=n)
{
if (arr[x]==-1)
arr[x] = j;
x+=x0;
}
}

vector<int>rets;
for (int g: groups)
rets.push_back(arr[g]);
return rets;
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### 3447.Assign-Elements-to-Groups-with-Constraints

突破点在于groups里的元素的数值不超过1e5.在这个范围是,如果枚举所有1的倍数,然后枚举所有2的倍数,然后枚举所有3的倍数,直至枚举n的倍数,那么总共的时间复杂度是`n+n/2+n/3+...n/n = n*(1+1/2+1/3+...1/n)`.这个级数虽然不收敛,但是它是趋近于nlog(n)的。所以本题可以用暴力枚举。

所以本题的算法很简单。我们开一个长度为1e5的数组assign,来记录每个自然数最早能被哪个element所assign。我们依次考察element里的每个元素,比如说elements[j]=x,然后枚举x的所有倍数(直至1e5),比如说kx,那样就有`assign[kx] = j`,当然根据题意,我们对于每个assign我们只更新一次。

最后根据groups的数值,从assgin里把答案拷贝过去即可。
2 changes: 2 additions & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
[2875.Minimum-Size-Subarray-in-Infinite-Array](https://github.com/wisdompeak/LeetCode/tree/master/Hash/2875.Minimum-Size-Subarray-in-Infinite-Array) (H-)
[2949.Count-Beautiful-Substrings-II](https://github.com/wisdompeak/LeetCode/tree/master/Hash/2949.Count-Beautiful-Substrings-II) (H-)
[2950.Number-of-Divisible-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Hash/2950.Number-of-Divisible-Substrings) (H-)
[3448.Count-Substrings-Divisible-By-Last-Digit](https://github.com/wisdompeak/LeetCode/tree/master/Hash/3448.Count-Substrings-Divisible-By-Last-Digit) (H-)

#### [Sorted Container](https://github.com/wisdompeak/LeetCode/tree/master/Sorted_Container)
[220.Contains-Duplicate-III](https://github.com/wisdompeak/LeetCode/tree/master/Sorted_Container/220.Contains-Duplicate-III) (M)
Expand Down Expand Up @@ -1631,6 +1632,7 @@
[2768.Number-of-Black-Blocks](https://github.com/wisdompeak/LeetCode/tree/master/Others/2768.Number-of-Black-Blocks) (M+)
[2857.Count-Pairs-of-Points-With-Distance-k](https://github.com/wisdompeak/LeetCode/tree/master/Others/2857.Count-Pairs-of-Points-With-Distance-k) (M+)
[3404.Count-Special-Subsequences](https://github.com/wisdompeak/LeetCode/tree/master/Others/3404.Count-Special-Subsequences) (H)
[3447.Assign-Elements-to-Groups-with-Constraints](https://github.com/wisdompeak/LeetCode/tree/master/Others/3447.Assign-Elements-to-Groups-with-Constraints) (M+)
* ``Presum``
[1878.Get-Biggest-Three-Rhombus-Sums-in-a-Grid](https://github.com/wisdompeak/LeetCode/tree/master/Others/1878.Get-Biggest-Three-Rhombus-Sums-in-a-Grid) (M+)
[1906.Minimum-Absolute-Difference-Queries](https://github.com/wisdompeak/LeetCode/tree/master/Others/1906.Minimum-Absolute-Difference-Queries) (M+)
Expand Down

AltStyle によって変換されたページ (->オリジナル) /