forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #316
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
7 commits
Select commit
Hold shift + click to select a range
15cfdf2
Create 3489.Zero-Array-Transformation-IV.cpp
wisdompeak 962c080
Create Readme.md
wisdompeak 56cb89e
Delete Others/3489.Zero-Array-Transformation-IV/3489.Zero-Array-Trans...
wisdompeak cc9deb1
Update Readme.md
wisdompeak 6c2efd5
Update 3489.Zero-Array-Transformation-IV.cpp
wisdompeak f938d29
Update 3489.Zero-Array-Transformation-IV.cpp
wisdompeak 83a42a3
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
34 changes: 34 additions & 0 deletions
Dynamic_Programming/3489.Zero-Array-Transformation-IV/3489.Zero-Array-Transformation-IV.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,34 @@ | ||
class Solution { | ||
int dp[10][1001]; | ||
public: | ||
bool isOK(vector<int>& nums) | ||
{ | ||
for (int i=0; i<nums.size(); i++) | ||
{ | ||
if (dp[i][nums[i]] == false) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int minZeroArray(vector<int>& nums, vector<vector<int>>& queries) | ||
{ | ||
for (int i=0; i<nums.size(); i++) | ||
dp[i][0] = true; | ||
|
||
if (isOK(nums)) return 0; | ||
|
||
for (int k=0; k<queries.size(); k++) | ||
{ | ||
int a = queries[k][0], b = queries[k][1], d = queries[k][2]; | ||
for (int i=a; i<=b; i++) | ||
{ | ||
for (int v=1000; v>=0; v--) | ||
if (v>=d && dp[i][v-d] == true) | ||
dp[i][v] = true; | ||
} | ||
if (isOK(nums)) return k+1; | ||
} | ||
return -1; | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
Dynamic_Programming/3489.Zero-Array-Transformation-IV/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,17 @@ | ||
### 3489.Zero-Array-Transformation-IV | ||
|
||
这本质是一个背包问题。每处理一个query,在对应区间内的nums[i]就多得了一次删减的操作。 | ||
|
||
我们需要查看这些nums[i]在获得这个额外的删减机会之后,是否能连同之前的删减操作,实现置零?很显然,如果该query能够让nums[i]再削减d,那么就取决于nums[i]之前能否削减至d。 | ||
|
||
我们令dp[i][v]表示如果nums[i]的数值是v,能否最终削减成为零。就有 | ||
``` | ||
for q: queries | ||
a = q[0], b = q[1], d = q[2]; | ||
for (i=a; i=b; i++) { | ||
for (int v=0; v<=1000; v++) { | ||
dp[i][v] = dp[i][v] || d[i][v-d]; | ||
} | ||
} | ||
``` | ||
最终查看所有的dp[i][nums[i]]是否为true。 |
56 changes: 0 additions & 56 deletions
Others/3489.Zero-Array-Transformation-IV/3489.Zero-Array-Transformation-IV.cpp
Oops, something went wrong.
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
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.