forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
722e789
Create 3447.Assign-Elements-to-Groups-with-Constraints.cpp
wisdompeak dd363d6
Update Readme.md
wisdompeak c71d7fc
Create Readme.md
wisdompeak 2318d0e
Create 3448.Count-Substrings-Divisible-By-Last-Digit.cpp
wisdompeak 2916a52
Update Readme.md
wisdompeak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
...ount-Substrings-Divisible-By-Last-Digit/3448.Count-Substrings-Divisible-By-Last-Digit.cpp
This file contains hidden or 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,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; | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
...n-Elements-to-Groups-with-Constraints/3447.Assign-Elements-to-Groups-with-Constraints.cpp
This file contains hidden or 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,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; | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
Others/3447.Assign-Elements-to-Groups-with-Constraints/Readme.md
This file contains hidden or 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,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里把答案拷贝过去即可。 |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.