Skip to content

Commit ca20b36

Browse files
committed
test: 827 solution
py
1 parent dc0ae45 commit ca20b36

File tree

10 files changed

+251
-3
lines changed

10 files changed

+251
-3
lines changed

daily-problems.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"daily": "3227",
2+
"daily": "827",
33
"plans": ["3674", "problems", "3675", "problems", "3676", "problems"]
44
}

golang/solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golang
22

33
import (
4-
problem "leetCode/problems/problems_3227"
4+
problem "leetCode/problems/problems_827"
55
"testing"
66
)
77

88
func TestSolution(t *testing.T) {
9-
TestEach(t, "3227", "problems", problem.Solve)
9+
TestEach(t, "827", "problems", problem.Solve)
1010
}

problems/problems_827/Solution.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build ignore
2+
#include "cpp/common/Solution.h"
3+
4+
5+
using namespace std;
6+
using json = nlohmann::json;
7+
8+
class Solution {
9+
public:
10+
int largestIsland(vector<vector<int>>& grid) {
11+
12+
}
13+
};
14+
15+
json leetcode::qubh::Solve(string input_json_values) {
16+
vector<string> inputArray;
17+
size_t pos = input_json_values.find('\n');
18+
while (pos != string::npos) {
19+
inputArray.push_back(input_json_values.substr(0, pos));
20+
input_json_values = input_json_values.substr(pos + 1);
21+
pos = input_json_values.find('\n');
22+
}
23+
inputArray.push_back(input_json_values);
24+
25+
Solution solution;
26+
vector<vector<int>> grid = json::parse(inputArray.at(0));
27+
return solution.largestIsland(grid);
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package problems.problems_827;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import java.util.*;
5+
import qubhjava.BaseSolution;
6+
7+
8+
public class Solution extends BaseSolution {
9+
public int largestIsland(int[][] grid) {
10+
11+
}
12+
13+
@Override
14+
public Object solve(String[] inputJsonValues) {
15+
int[][] grid = jsonArrayToInt2DArray(inputJsonValues[0]);
16+
return JSON.toJSON(largestIsland(grid));
17+
}
18+
}

problems/problems_827/problem.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 827. Making A Large Island [Rating: 1933.96]
2+
3+
<p>You are given an <code>n x n</code> binary matrix <code>grid</code>. You are allowed to change <strong>at most one</strong> <code>0</code> to be <code>1</code>.</p>
4+
5+
<p>Return <em>the size of the largest <strong>island</strong> in</em> <code>grid</code> <em>after applying this operation</em>.</p>
6+
7+
<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>s.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> grid = [[1,0],[0,1]]
14+
<strong>Output:</strong> 3
15+
<strong>Explanation:</strong> Change one 0 to 1 and connect two 1s, then we get an island with area = 3.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> grid = [[1,1],[1,0]]
22+
<strong>Output:</strong> 4
23+
<strong>Explanation: </strong>Change the 0 to 1 and make the island bigger, only one island with area = 4.</pre>
24+
25+
<p><strong class="example">Example 3:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> grid = [[1,1],[1,1]]
29+
<strong>Output:</strong> 4
30+
<strong>Explanation:</strong> Can&#39;t change any 0 to 1, only one island with area = 4.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>n == grid.length</code></li>
38+
<li><code>n == grid[i].length</code></li>
39+
<li><code>1 &lt;= n &lt;= 500</code></li>
40+
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
41+
</ul>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 827. 最大人工岛 [难度分: 1933.96]
2+
3+
<p>给你一个大小为 <code>n x n</code> 二进制矩阵 <code>grid</code> 。<strong>最多</strong> 只能将一格&nbsp;<code>0</code> 变成&nbsp;<code>1</code> 。</p>
4+
5+
<p>返回执行此操作后,<code>grid</code> 中最大的岛屿面积是多少?</p>
6+
7+
<p><strong>岛屿</strong> 由一组上、下、左、右四个方向相连的&nbsp;<code>1</code> 形成。</p>
8+
9+
<p>&nbsp;</p>
10+
11+
<p><strong class="example">示例 1:</strong></p>
12+
13+
<pre>
14+
<strong>输入: </strong>grid = [[1, 0], [0, 1]]
15+
<strong>输出:</strong> 3
16+
<strong>解释:</strong> 将一格0变成1,最终连通两个小岛得到面积为 3 的岛屿。
17+
</pre>
18+
19+
<p><strong class="example">示例 2:</strong></p>
20+
21+
<pre>
22+
<strong>输入: </strong>grid =<strong> </strong>[[1, 1], [1, 0]]
23+
<strong>输出:</strong> 4
24+
<strong>解释:</strong> 将一格0变成1,岛屿的面积扩大为 4。</pre>
25+
26+
<p><strong class="example">示例 3:</strong></p>
27+
28+
<pre>
29+
<strong>输入: </strong>grid = [[1, 1], [1, 1]]
30+
<strong>输出:</strong> 4
31+
<strong>解释:</strong> 没有0可以让我们变成1,面积依然为 4。</pre>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><strong>提示:</strong></p>
36+
37+
<ul>
38+
<li><code>n == grid.length</code></li>
39+
<li><code>n == grid[i].length</code></li>
40+
<li><code>1 &lt;= n &lt;= 500</code></li>
41+
<li><code>grid[i][j]</code> 为 <code>0</code> 或 <code>1</code></li>
42+
</ul>

problems/problems_827/solution.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package problem827
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"strings"
7+
)
8+
9+
func largestIsland(grid [][]int) int {
10+
11+
}
12+
13+
func Solve(inputJsonValues string) any {
14+
inputValues := strings.Split(inputJsonValues, "\n")
15+
var grid [][]int
16+
17+
if err := json.Unmarshal([]byte(inputValues[0]), &grid); err != nil {
18+
log.Fatal(err)
19+
}
20+
21+
return largestIsland(grid)
22+
}

problems/problems_827/solution.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import copy
2+
3+
import solution
4+
from typing import *
5+
6+
7+
class Solution(solution.Solution):
8+
def solve(self, test_input=None):
9+
return self.largestIsland(test_input)
10+
11+
def largestIsland(self, grid: List[List[int]]) -> int:
12+
DIRS = [(-1, 0), (1, 0), (0, -1), (0, 1)]
13+
def point_to_idx(x, y):
14+
return x * n + y
15+
16+
17+
18+
n = len(grid)
19+
uf = UnionFind(n * n)
20+
for i, row in enumerate(grid):
21+
for j, val in enumerate(row):
22+
if val == 1:
23+
p = point_to_idx(i, j)
24+
for dx, dy in DIRS:
25+
if 0 <= (nx := i + dx) < n and 0 <= (ny := j + dy) < n and grid[nx][ny] == 1:
26+
uf.union(p, point_to_idx(nx, ny))
27+
ans = max(uf.count)
28+
for i, row in enumerate(grid):
29+
for j, val in enumerate(row):
30+
if val == 0:
31+
tot = 1
32+
explored = set()
33+
for dx, dy in DIRS:
34+
if 0 <= (nx := i + dx) < n and 0 <= (ny := j + dy) < n and grid[nx][ny] == 1:
35+
root = uf.find(point_to_idx(nx, ny))
36+
if root in explored:
37+
continue
38+
explored.add(root)
39+
tot += uf.count[root]
40+
ans = max(ans, tot)
41+
42+
return ans
43+
44+
class UnionFind:
45+
def __init__(self, size):
46+
self.parent = list(range(size))
47+
self.rank = [1] * size
48+
self.count = [1] * size
49+
self.size = size
50+
self.cc = size
51+
52+
def find(self, x):
53+
while self.parent[x] != x:
54+
self.parent[x] = self.parent[self.parent[x]] # 路径压缩
55+
x = self.parent[x]
56+
return x
57+
58+
def union(self, x, y):
59+
root_x = self.find(x)
60+
root_y = self.find(y)
61+
62+
if root_x == root_y:
63+
return False # 已经在同一集合
64+
65+
# 按秩合并
66+
if self.rank[root_x] > self.rank[root_y]:
67+
self.parent[root_y] = root_x
68+
self.count[root_x] += self.count[root_y]
69+
else:
70+
self.parent[root_x] = root_y
71+
if self.rank[root_x] == self.rank[root_y]:
72+
self.rank[root_y] += 1
73+
self.count[root_y] += self.count[root_x]
74+
self.cc -= 1
75+
return True
76+
77+
def is_connected(self, x, y):
78+
return self.find(x) == self.find(y)

problems/problems_827/testcase

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

problems/problems_827/testcase.py

Lines changed: 17 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)