forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #358
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
6 commits
Select commit
Hold shift + click to select a range
b7e9bcc
Create Readme.md
wisdompeak f16e119
Update 3670.Maximum-Product-of-Two-Integers-With-No-Common-Bits.cpp
wisdompeak 9990d34
Create 3669.Balanced-K-Factor-Decomposition.cpp
wisdompeak dd01d81
Update Readme.md
wisdompeak 668197e
Create Readme.md
wisdompeak 8668ca7
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
42 changes: 42 additions & 0 deletions
DFS/3669.Balanced-K-Factor-Decomposition/3669.Balanced-K-Factor-Decomposition.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,42 @@ | ||
class Solution { | ||
vector<int>divisors; | ||
vector<int>cur; | ||
vector<int>rets; | ||
int bestDiff = INT_MAX/2; | ||
public: | ||
void dfs(int idx, int n, int k) { | ||
if (k==1) { | ||
cur.push_back(n); | ||
int diff = cur.back()-cur[0]; | ||
if (diff < bestDiff) { | ||
bestDiff = diff; | ||
rets = cur; | ||
} | ||
cur.pop_back(); | ||
return; | ||
} | ||
|
||
for (int i = idx; i<divisors.size(); i++) { | ||
int d = divisors[i]; | ||
if (n%d!=0) continue; | ||
if (n/d < d) break; | ||
cur.push_back(d); | ||
dfs(i, n/d, k-1); | ||
cur.pop_back(); | ||
} | ||
} | ||
|
||
vector<int> minDifference(int n, int k) { | ||
for (int i=1; i*i<=n; i++) { | ||
if (n%i==0) { | ||
divisors.push_back(i); | ||
if (i*i!=n) divisors.push_back(n/i); | ||
} | ||
} | ||
sort(divisors.begin(), divisors.end()); | ||
|
||
dfs(0, n, k); | ||
|
||
return rets; | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
DFS/3669.Balanced-K-Factor-Decomposition/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 @@ | ||
### 3669.Balanced-K-Factor-Decomposition | ||
|
||
用sqrt(n)的时间将n的所有divisors求出来。本题转化为在divisors数组中寻找k个元素使得乘积恰好为n。因为k的个数较小,可以用暴力DFS解决。 | ||
|
||
递归函数`dfs(i,n,k)`表示要将n拆分为k个元素的乘积,当前可以从第i个divisor开始选择。选中某个约数d(编号为j)之后,即可递归处理`dfs(j,n/d,k-1)`.当k=1时,即可记录所选中的约数。 | ||
|
||
注意DFS过程中,当选择不同divisor时,已选中的数组需要有回溯操作。 |
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
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
15 changes: 15 additions & 0 deletions
Trie/3670.Maximum-Product-of-Two-Integers-With-No-Common-Bits/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,15 @@ | ||
### 3670.Maximum-Product-of-Two-Integers-With-No-Common-Bits | ||
|
||
#### 解法1:Trie | ||
比较容易想到的一个解法是将所有数字的二进制表达写入一张字典树。然后对每个num,我们从高往低遍历每个一位bit:如果bit等于1,那么我们在字典树里只能往子节点0走,如果子节点0不存在那么返回失败。如果bit等于0,那么我们优先往子节点1走;如果走下去最终返回失败,那么我们再回溯尝试往子节点0走。直至我们找到一条通往叶子节点的路径,必然是与num没有任何重合的set bits的最大数。 | ||
|
||
这样的做法容易TLE。而且记忆化需要的参数比较复杂(需要包含所在的节点以及后续的路径期望)。 | ||
|
||
#### 解法2:DP | ||
此题的DP做法不太常规。我们令dp[m]表示在数组里所能找到的最大的数、满足其是m的submask。因为数组元素最大值是1e6,不超过20个bit位,所以我们可以尝试计算1到(1<<k)所有数的DP值。k是数组里最大元素的二进制位数, | ||
|
||
怎么计算dp[m]呢?我们枚举每个bit位j,则有`dp[m]=max(dp[m], dp[m-(1<<j)]`.这样按m从小到大计算dp[m],满足无后效性。 | ||
|
||
初始条件是对于元素里的每一个元素x,都有`dp[x]=x`. | ||
|
||
在得到dp[m]之后,对于元素里的任意元素x,根据题意,它的最理想的搭配伙伴就是dp[x的补数]。其中"x的补数"就是其所有bit位的翻转,即`((1<<k)-1)^x`. |
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.