Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #457

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
jenningsloy318 merged 7 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 70 additions & 3 deletions problems/0417.太平洋大西洋水流问题.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@ public:

// 记录从大西洋出发,可以遍历的节点
vector<vector<bool>> atlantic = vector<vector<bool>>(n, vector<bool>(m, false));

// 从最上最下行的节点出发,向高处遍历
// 从最左最右列的节点出发,向高处遍历
for (int i = 0; i < n; i++) {
dfs (heights, pacific, i, 0); // 遍历最左列,接触太平洋
dfs (heights, atlantic, i, m - 1); // 遍历最右列,接触大西
}

// 从最左最右列的节点出发,向高处遍历
// 从最上最下行的节点出发,向高处遍历
for (int j = 0; j < m; j++) {
dfs (heights, pacific, 0, j); // 遍历最上行,接触太平洋
dfs (heights, atlantic, n - 1, j); // 遍历最下行,接触大西洋
Expand Down Expand Up @@ -297,6 +297,73 @@ class Solution {
}
```

```Java
class Solution {

// 和Carl题解更加符合的Java DFS
private int[][] directions = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

/**
* @param heights 题目给定的二维数组
* @param m 当前位置的行号
* @param n 当前位置的列号
* @param visited 记录这个位置可以到哪条河
*/

public void dfs(int[][] heights, boolean[][] visited, int m, int n){
if(visited[m][n]) return;
visited[m][n] = true;

for(int[] dir: directions){
int nextm = m + dir[0];
int nextn = n + dir[1];
//出了2D array的边界,continue
if(nextm < 0||nextm == heights.length||nextn <0||nextn== heights[0].length) continue;
//下一个位置比当下位置还要低,跳过,继续找下一个更高的位置
if(heights[m][n] > heights[nextm][nextn]) continue;
dfs(heights, visited, nextm, nextn);
}
}


public List<List<Integer>> pacificAtlantic(int[][] heights) {
int m = heights.length;
int n = heights[0].length;

// 记录从太平洋边出发,可以遍历的节点
boolean[][] pacific = new boolean[m][n];
// 记录从大西洋出发,可以遍历的节点
boolean[][] atlantic = new boolean[m][n];

// 从最左最右列的节点出发,向高处遍历
for(int i = 0; i<m; i++){
dfs(heights, pacific, i, 0); //遍历pacific最左边
dfs(heights, atlantic, i, n-1); //遍历atlantic最右边
}

// 从最上最下行的节点出发,向高处遍历
for(int j = 0; j<n; j++){
dfs(heights, pacific, 0, j); //遍历pacific最上边
dfs(heights, atlantic, m-1, j); //遍历atlantic最下边
}

List<List<Integer>> result = new ArrayList<>();
for(int a = 0; a<m; a++){
for(int b = 0; b<n; b++){
// 如果这个节点,从太平洋和大西洋出发都遍历过,就是结果
if(pacific[a][b] && atlantic[a][b]){
List<Integer> pair = new ArrayList<>();
pair.add(a);
pair.add(b);
result.add(pair);
}
}
}
return result;
}
}
```

广度优先遍历:

```Java
Expand Down
82 changes: 82 additions & 0 deletions problems/0474.一和零.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,89 @@ public:
* 时间复杂度: O(kmn),k 为strs的长度
* 空间复杂度: O(mn)

C++:
使用三维数组的版本

```CPP
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
int num_of_str = strs.size();

vector<vector<vector<int>>> dp(num_of_str, vector<vector<int>>(m + 1,vector<int>(n + 1, 0)));

/* dp[i][j][k] represents, if choosing items among strs[0] to strs[i] to form a subset,
what is the maximum size of this subset such that there are no more than m 0's and n 1's in this subset.
Each entry of dp[i][j][k] is initialized with 0

transition formula:
using x[i] to indicates the number of 0's in strs[i]
using y[i] to indicates the number of 1's in strs[i]

dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - x[i]][k - y[i]] + 1)

*/


// num_of_zeros records the number of 0's for each str
// num_of_ones records the number of 1's for each str
// find the number of 0's and the number of 1's for each str in strs
vector<int> num_of_zeros;
vector<int> num_of_ones;
for (auto& str : strs){
int count_of_zero = 0;
int count_of_one = 0;
for (char &c : str){
if(c == '0') count_of_zero ++;
else count_of_one ++;
}
num_of_zeros.push_back(count_of_zero);
num_of_ones.push_back(count_of_one);

}


// num_of_zeros[0] indicates the number of 0's for str[0]
// num_of_ones[0] indiates the number of 1's for str[1]

// initialize the 1st plane of dp[i][j][k], i.e., dp[0][j][k]
// if num_of_zeros[0] > m or num_of_ones[0] > n, no need to further initialize dp[0][j][k],
// because they have been intialized to 0 previously
if(num_of_zeros[0] <= m && num_of_ones[0] <= n){
// for j < num_of_zeros[0] or k < num_of_ones[0], dp[0][j][k] = 0
for(int j = num_of_zeros[0]; j <= m; j++){
for(int k = num_of_ones[0]; k <= n; k++){
dp[0][j][k] = 1;
}
}
}

/* if j - num_of_zeros[i] >= 0 and k - num_of_ones[i] >= 0:
dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - num_of_zeros[i]][k - num_of_ones[i]] + 1)
else:
dp[i][j][k] = dp[i-1][j][k]
*/

for (int i = 1; i < num_of_str; i++){
int count_of_zeros = num_of_zeros[i];
int count_of_ones = num_of_ones[i];
for (int j = 0; j <= m; j++){
for (int k = 0; k <= n; k++){
if( j < count_of_zeros || k < count_of_ones){
dp[i][j][k] = dp[i-1][j][k];
}else{
dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j - count_of_zeros][k - count_of_ones] + 1);
}
}
}

}

return dp[num_of_str-1][m][n];

}
};
```

## 总结

Expand Down

AltStyle によって変換されたページ (->オリジナル) /