Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1219
Browse files Browse the repository at this point in the history
No.1219.Path with Maximum Gold
  • Loading branch information
yanglbme committed Mar 3, 2022
1 parent 9531bfb commit 4c9533c
Show file tree
Hide file tree
Showing 9 changed files with 495 additions and 405 deletions.
334 changes: 167 additions & 167 deletions README.md

Large diffs are not rendered by default.

318 changes: 159 additions & 159 deletions README_EN.md

Large diffs are not rendered by default.

87 changes: 60 additions & 27 deletions solution/1200-1299/1219.Path with Maximum Gold/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@

<!-- 这里可写通用的实现逻辑 -->

DFS。
枚举每个单元格作为起点,进行 DFS 搜索,找到收益最大的一条路径。

由于每个单元格只能被开采一次,因此当搜索到单元格 `(i, j)` 时,可以将 `grid[i][j]` 置为 0,搜索结束后再恢复 `grid[i][j]` 为原来的值。

<!-- tabs:start -->

Expand All @@ -70,22 +72,17 @@ DFS。
class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def dfs(i, j):
if not (0 <= i < m and 0 <= j < n and grid[i][j]):
if not(0 <= i < m and 0 <= j < n and grid[i][j]):
return 0
t = grid[i][j]
grid[i][j] = 0
res = 0
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
res = max(res, t + dfs(i + a, j + b))
ans = t + max(dfs(i + a, j + b)
for a, b in [[0, 1], [0, -1], [-1, 0], [1, 0]])
grid[i][j] = t
return res
return ans

m, n = len(grid), len(grid[0])
ans = 0
for i in range(m):
for j in range(n):
ans = max(ans, dfs(i, j))
return ans
return max(dfs(i, j) for i in range(m) for j in range(n))
```

### **Java**
Expand All @@ -95,31 +92,35 @@ class Solution:
```java
class Solution {
private int[][] grid;
private int m;
private int n;

public int getMaximumGold(int[][] grid) {
m = grid.length;
n = grid[0].length;
this.grid = grid;
int ans = 0;
for (int i = 0; i < grid.length; ++i) {
for (int j = 0; j < grid[0].length; ++j) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans = Math.max(ans, dfs(i, j));
}
}
return ans;
}

private int dfs(int i, int j) {
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
return 0;
}
int t = grid[i][j];
grid[i][j] = 0;
int[] dirs = {-1, 0, 1, 0, -1};
int res = 0;
int ans = 0;
for (int k = 0; k < 4; ++k) {
res = Math.max(res, t + dfs(i + dirs[k], j + dirs[k + 1]));
ans = Math.max(ans, t + dfs(i + dirs[k], j + dirs[k + 1]));
}
grid[i][j] = t;
return res;
return ans;
}
}
```
Expand All @@ -129,26 +130,24 @@ class Solution {
```cpp
class Solution {
public:
vector<vector<int>> grid;
vector<int> dirs = {-1, 0, 1, 0, -1};

int getMaximumGold(vector<vector<int>>& grid) {
this->grid = grid;
int ans = 0;
for (int i = 0; i < grid.size(); ++i)
for (int j = 0; j < grid[0].size(); ++j)
ans = max(ans, dfs(i, j));
ans = max(ans, dfs(i, j, grid));
return ans;
}

int dfs(int i, int j) {
int dfs(int i, int j, vector<vector<int>>& grid) {
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() || grid[i][j] == 0) return 0;
int t = grid[i][j];
grid[i][j] = 0;
int res = 0;
for (int k = 0; k < 4; ++k) res = max(res, t + dfs(i + dirs[k], j + dirs[k + 1]));
int ans = 0;
for (int k = 0; k < 4; ++k) ans = max(ans, t + dfs(i + dirs[k], j + dirs[k + 1], grid));
grid[i][j] = t;
return res;
return ans;
}
};
```
Expand All @@ -165,13 +164,13 @@ func getMaximumGold(grid [][]int) int {
}
t := grid[i][j]
grid[i][j] = 0
res := 0
ans := 0
dirs := []int{-1, 0, 1, 0, -1}
for k := 0; k < 4; k++ {
res = max(res, t+dfs(i+dirs[k], j+dirs[k+1]))
ans = max(ans, t+dfs(i+dirs[k], j+dirs[k+1]))
}
grid[i][j] = t
return res
return ans
}
ans := 0
for i := 0; i < m; i++ {
Expand All @@ -190,6 +189,40 @@ func max(a, b int) int {
}
```

### **JavaScript**

```js
/**
* @param {number[][]} grid
* @return {number}
*/
var getMaximumGold = function (grid) {
const m = grid.length;
const n = grid[0].length;
function dfs(i, j) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
return 0;
}
const t = grid[i][j];
grid[i][j] = 0;
let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
for (let k = 0; k < 4; ++k) {
ans = Math.max(ans, t + dfs(i + dirs[k], j + dirs[k + 1]));
}
grid[i][j] = t;
return ans;
}
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
ans = Math.max(ans, dfs(i, j));
}
}
return ans;
};
```

### **...**

```
Expand Down
83 changes: 57 additions & 26 deletions solution/1200-1299/1219.Path with Maximum Gold/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,54 +66,53 @@ DFS.
class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def dfs(i, j):
if not (0 <= i < m and 0 <= j < n and grid[i][j]):
if not(0 <= i < m and 0 <= j < n and grid[i][j]):
return 0
t = grid[i][j]
grid[i][j] = 0
res = 0
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
res = max(res, t + dfs(i + a, j + b))
ans = t + max(dfs(i + a, j + b)
for a, b in [[0, 1], [0, -1], [-1, 0], [1, 0]])
grid[i][j] = t
return res
return ans

m, n = len(grid), len(grid[0])
ans = 0
for i in range(m):
for j in range(n):
ans = max(ans, dfs(i, j))
return ans
return max(dfs(i, j) for i in range(m) for j in range(n))
```

### **Java**

```java
class Solution {
private int[][] grid;
private int m;
private int n;

public int getMaximumGold(int[][] grid) {
m = grid.length;
n = grid[0].length;
this.grid = grid;
int ans = 0;
for (int i = 0; i < grid.length; ++i) {
for (int j = 0; j < grid[0].length; ++j) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans = Math.max(ans, dfs(i, j));
}
}
return ans;
}

private int dfs(int i, int j) {
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
return 0;
}
int t = grid[i][j];
grid[i][j] = 0;
int[] dirs = {-1, 0, 1, 0, -1};
int res = 0;
int ans = 0;
for (int k = 0; k < 4; ++k) {
res = Math.max(res, t + dfs(i + dirs[k], j + dirs[k + 1]));
ans = Math.max(ans, t + dfs(i + dirs[k], j + dirs[k + 1]));
}
grid[i][j] = t;
return res;
return ans;
}
}
```
Expand All @@ -123,26 +122,24 @@ class Solution {
```cpp
class Solution {
public:
vector<vector<int>> grid;
vector<int> dirs = {-1, 0, 1, 0, -1};

int getMaximumGold(vector<vector<int>>& grid) {
this->grid = grid;
int ans = 0;
for (int i = 0; i < grid.size(); ++i)
for (int j = 0; j < grid[0].size(); ++j)
ans = max(ans, dfs(i, j));
ans = max(ans, dfs(i, j, grid));
return ans;
}

int dfs(int i, int j) {
int dfs(int i, int j, vector<vector<int>>& grid) {
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() || grid[i][j] == 0) return 0;
int t = grid[i][j];
grid[i][j] = 0;
int res = 0;
for (int k = 0; k < 4; ++k) res = max(res, t + dfs(i + dirs[k], j + dirs[k + 1]));
int ans = 0;
for (int k = 0; k < 4; ++k) ans = max(ans, t + dfs(i + dirs[k], j + dirs[k + 1], grid));
grid[i][j] = t;
return res;
return ans;
}
};
```
Expand All @@ -159,13 +156,13 @@ func getMaximumGold(grid [][]int) int {
}
t := grid[i][j]
grid[i][j] = 0
res := 0
ans := 0
dirs := []int{-1, 0, 1, 0, -1}
for k := 0; k < 4; k++ {
res = max(res, t+dfs(i+dirs[k], j+dirs[k+1]))
ans = max(ans, t+dfs(i+dirs[k], j+dirs[k+1]))
}
grid[i][j] = t
return res
return ans
}
ans := 0
for i := 0; i < m; i++ {
Expand All @@ -184,6 +181,40 @@ func max(a, b int) int {
}
```

### **JavaScript**

```js
/**
* @param {number[][]} grid
* @return {number}
*/
var getMaximumGold = function (grid) {
const m = grid.length;
const n = grid[0].length;
function dfs(i, j) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
return 0;
}
const t = grid[i][j];
grid[i][j] = 0;
let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
for (let k = 0; k < 4; ++k) {
ans = Math.max(ans, t + dfs(i + dirs[k], j + dirs[k + 1]));
}
grid[i][j] = t;
return ans;
}
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
ans = Math.max(ans, dfs(i, j));
}
}
return ans;
};
```

### **...**

```
Expand Down
12 changes: 5 additions & 7 deletions solution/1200-1299/1219.Path with Maximum Gold/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
class Solution {
public:
vector<vector<int>> grid;
vector<int> dirs = {-1, 0, 1, 0, -1};

int getMaximumGold(vector<vector<int>>& grid) {
this->grid = grid;
int ans = 0;
for (int i = 0; i < grid.size(); ++i)
for (int j = 0; j < grid[0].size(); ++j)
ans = max(ans, dfs(i, j));
ans = max(ans, dfs(i, j, grid));
return ans;
}

int dfs(int i, int j) {
int dfs(int i, int j, vector<vector<int>>& grid) {
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() || grid[i][j] == 0) return 0;
int t = grid[i][j];
grid[i][j] = 0;
int res = 0;
for (int k = 0; k < 4; ++k) res = max(res, t + dfs(i + dirs[k], j + dirs[k + 1]));
int ans = 0;
for (int k = 0; k < 4; ++k) ans = max(ans, t + dfs(i + dirs[k], j + dirs[k + 1], grid));
grid[i][j] = t;
return res;
return ans;
}
};
6 changes: 3 additions & 3 deletions solution/1200-1299/1219.Path with Maximum Gold/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ func getMaximumGold(grid [][]int) int {
}
t := grid[i][j]
grid[i][j] = 0
res := 0
ans := 0
dirs := []int{-1, 0, 1, 0, -1}
for k := 0; k < 4; k++ {
res = max(res, t+dfs(i+dirs[k], j+dirs[k+1]))
ans = max(ans, t+dfs(i+dirs[k], j+dirs[k+1]))
}
grid[i][j] = t
return res
return ans
}
ans := 0
for i := 0; i < m; i++ {
Expand Down
Loading

0 comments on commit 4c9533c

Please sign in to comment.