forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #343
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
e992d0c
Create 3640.Trionic-Array-II.cpp
wisdompeak cc2dcb7
Create Readme.md
wisdompeak bb94cc3
Update Readme.md
wisdompeak 5ce863c
Create 3639.Minimum-Time-to-Activate-String.cpp
wisdompeak 70f07b9
Update Readme.md
wisdompeak c1adc87
Create 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
Binary_Search/3639.Minimum-Time-to-Activate-String/3639.Minimum-Time-to-Activate-String.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 { | ||
public: | ||
int minTime(string s, vector<int>& order, int k) { | ||
int n = s.size(); | ||
vector<bool>isStar(n, false); | ||
ll T = ll(n)*(n+1)/2; | ||
if (k>T) return -1; | ||
|
||
auto check = [&](int mid) { | ||
fill(isStar.begin(), isStar.end(), false); | ||
for (int i=0; i<=mid; i++) | ||
isStar[order[i]] = true; | ||
|
||
ll sumNon = 0; | ||
for (int i=0; i<n; ){ | ||
if (isStar[i]) { | ||
i++; | ||
continue; | ||
} | ||
int j = i; | ||
while (j<n && !isStar[j]) | ||
j++; | ||
ll len = j-i; | ||
sumNon += len*(len+1)/2; | ||
i = j; | ||
} | ||
return T - sumNon >= k; | ||
}; | ||
|
||
int lo = 0, hi = n-1, ans = -1; | ||
while (lo < hi) { | ||
int mid = lo + (hi-lo)/2; | ||
if (check(mid)) { | ||
hi = mid; | ||
} else { | ||
lo = mid+1; | ||
} | ||
} | ||
if (check(hi)) return hi; | ||
else return -1; | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
Binary_Search/3639.Minimum-Time-to-Activate-String/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,5 @@ | ||
### 3639.Minimum-Time-to-Activate-String | ||
|
||
非常明显的二分搜值。假设运行到某个时刻t,那么我们就得到一个包含若干星号的字符串。我们需要考察该字符串里至少包含一个星号的substring的个数是否超过k。超过的话,就可以尝试减少k,否则需要增加k。 | ||
|
||
计算"至少包含一个星号的substring的个数",等效于反向计算"没有任何星号的substring的个数",并且后者更容易计算。对于任何一段连续的、不包含任何星号的子串长度p,那么就有p*(p+1)/2个子串符合条件。我们分割原始字符串为若干段"没有任何星号的区间",分别计算再相加即可。 |
54 changes: 54 additions & 0 deletions
Others/3640.Trionic-Array-II/3640.Trionic-Array-II.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,54 @@ | ||
using ll = long long; | ||
class Solution { | ||
public: | ||
vector<pair<int, int>>split(vector<int>&arr) { | ||
int n = arr.size(); | ||
vector<pair<int, int>> result; | ||
int i = 0; | ||
while (i < n) { | ||
int j = i; | ||
while (j + 1 < n && arr[j + 1] < arr[j]) { | ||
j++; | ||
} | ||
if (j > i) { | ||
result.push_back({i, j}); | ||
} | ||
i = j+1; | ||
} | ||
return result; | ||
} | ||
|
||
long long maxSumTrionic(vector<int>& nums) { | ||
int n = nums.size(); | ||
vector<pair<int,int>>arr = split(nums); | ||
|
||
ll ret = LLONG_MIN/2; | ||
|
||
for (int i=0; i<arr.size(); i++) { | ||
int x = arr[i].first, y = arr[i].second; | ||
if (x-1<0) continue; | ||
if (y+1>=n) continue; | ||
|
||
ll sum = nums[x-1]; | ||
ll maxSum1 = nums[x-1]; | ||
for (int j=x-2; j>=0; j--) { | ||
if (nums[j]>=nums[j+1]) break; | ||
sum += nums[j]; | ||
maxSum1 = max(maxSum1, sum); | ||
} | ||
|
||
sum = nums[y+1]; | ||
ll maxSum2 = nums[y+1]; | ||
for (int j=y+2; j<n; j++) { | ||
if (nums[j]<=nums[j-1]) break; | ||
sum += nums[j]; | ||
maxSum2 = max(maxSum2, sum); | ||
} | ||
|
||
sum = accumulate(nums.begin()+x, nums.begin()+y+1, 0ll); | ||
ret = max(ret, maxSum1 + maxSum2 + sum); | ||
} | ||
|
||
return ret; | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
Others/3640.Trionic-Array-II/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,5 @@ | ||
### 3640.Trionic-Array-II | ||
|
||
因为符合条件的区间必然包含一段完整的递减区间,所以入手点就是拆解nums,遍历其中所有的递减区间。假设其中一个递减区间是[x,y],然后从x-1分别往前、从y+1往后找隔壁递增区间的最大前缀和即可。 | ||
|
||
最终返回全局最大的三段和。 |
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.