forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #5
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
dc41a3c
Create 473.Matchsticks-to-Square_v2.cpp
Bill0412 60dd792
Create 2290.Minimum-Obstacle-Removal-to-Reach-Corner.cpp
wisdompeak 39070c7
Merge pull request #38 from Bill0412/patch-1
wisdompeak 2677ece
Update Readme.md
wisdompeak ce47ebc
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
88 changes: 88 additions & 0 deletions
...inimum-Obstacle-Removal-to-Reach-Corner/2290.Minimum-Obstacle-Removal-to-Reach-Corner.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,88 @@ | ||
class Solution { | ||
vector<pair<int,int>>dir = {{1,0},{-1,0},{0,1},{0,-1}}; | ||
public: | ||
int minimumObstacles(vector<vector<int>>& grid) | ||
{ | ||
int m = grid.size(); | ||
int n = grid[0].size(); | ||
if (m==1 && n==1) return 0; | ||
vector<vector<int>>visited(m, vector<int>(n,0)); | ||
|
||
queue<pair<int,int>>q; | ||
q.push({0,0}); | ||
visited[0][0] = 1; | ||
|
||
int step = 0; | ||
while (!q.empty()) | ||
{ | ||
int len = q.size(); | ||
while (len--) | ||
{ | ||
auto [x, y] = q.front(); | ||
q.pop(); | ||
|
||
for (auto [dx, dy]: dir) | ||
{ | ||
int i = x+dx; | ||
int j = y+dy; | ||
|
||
if (i<0||i>=m||j<0||j>=n) continue; | ||
if (visited[i][j]==1) continue; | ||
if (grid[i][j] == 1) | ||
{ | ||
visited[i][j] = 1; | ||
q.push({i,j}); | ||
} | ||
else | ||
{ | ||
for (auto [ii, jj]: travel(grid, visited, i, j)) | ||
{ | ||
if (ii==m-1 && jj==n-1) | ||
return step; | ||
q.push({ii,jj}); | ||
} | ||
} | ||
} | ||
} | ||
step++; | ||
} | ||
return 0; | ||
} | ||
|
||
vector<pair<int,int>>travel(vector<vector<int>>& grid, vector<vector<int>>& visited, int x0, int y0) | ||
{ | ||
int m = grid.size(); | ||
int n = grid[0].size(); | ||
|
||
if (x0==m-1 && y0==n-1) | ||
return {{x0, y0}}; | ||
|
||
queue<pair<int,int>>q; | ||
q.push({x0,y0}); | ||
visited[x0][y0] = 1; | ||
|
||
vector<pair<int,int>>rets; | ||
while (!q.empty()) | ||
{ | ||
auto [x, y] = q.front(); | ||
q.pop(); | ||
|
||
for (auto [dx, dy] : dir) | ||
{ | ||
int i = x+dx; | ||
int j = y+dy; | ||
if (i<0||i>=m||j<0||j>=n) continue; | ||
if (visited[i][j]==1) continue; | ||
visited[i][j] = 1; | ||
if (i==m-1 && j==n-1) | ||
rets.push_back({i,j}); | ||
else if (grid[i][j]==1) | ||
rets.push_back({i,j}); | ||
else | ||
q.push({i,j}); | ||
} | ||
} | ||
|
||
return rets; | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/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 @@ | ||
### 2290.Minimum-Obstacle-Removal-to-Reach-Corner | ||
|
||
本题的本质就是从起点到终点,采用层级BFS,最少需要穿越几个回合的障碍。而障碍与障碍之间的空气,可以忽略不计。也就是说,某个障碍与空气相邻的话,下一个回合可以通过空气到达其他的障碍。 | ||
|
||
在实现过程中,除了常规的层级BFS之外,我们还需要有一个travelAir的函数。travelAir以某个空格子为起点,遍历所有能"隔空"访问的障碍物。这些障碍物需要加入下一回合BFS的队列中去。 |
38 changes: 38 additions & 0 deletions
DFS/473.Matchsticks-to-Square/473.Matchsticks-to-Square_v2.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,38 @@ | ||
class Solution { | ||
bool divide2(vector<int>& sticks, int target) { | ||
int n = sticks.size(); | ||
for(int state = 0; state < (1<<n); ++state) { | ||
int cur = 0; | ||
for(int i = 0; i < n; ++i) { | ||
if((state>>i)&1) cur += sticks[i]; | ||
} | ||
if(cur == target) return true; | ||
} | ||
return false; | ||
} | ||
public: | ||
bool makesquare(vector<int>& matchsticks) { | ||
int total = accumulate(matchsticks.begin(), matchsticks.end(), 0); | ||
if(total % 4 != 0) return false; | ||
|
||
int n = matchsticks.size(); | ||
|
||
for(int state = 0; state < (1<<n); ++state) { | ||
int cur = 0; | ||
for(int i = 0; i < n; ++i) { | ||
if((state>>i)&1) cur += matchsticks[i]; | ||
} | ||
if(cur == total / 2) { | ||
vector<int> v1, v2; | ||
for(int i = 0; i < n; ++i) { | ||
if((state>>i)&1) v1.push_back(matchsticks[i]); | ||
else v2.push_back(matchsticks[i]); | ||
} | ||
|
||
if(divide2(v1, total / 4) && divide2(v2, total / 4)) return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
}; |
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.