Skip to content

Commit 5adf9e1

Browse files
committed
test: 827 solution
py, c++, go, java
1 parent 06ca535 commit 5adf9e1

File tree

4 files changed

+365
-79
lines changed

4 files changed

+365
-79
lines changed

problems/problems_827/Solution.cpp

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,104 @@
11
//go:build ignore
22
#include "cpp/common/Solution.h"
33

4-
54
using namespace std;
65
using json = nlohmann::json;
76

7+
class UnionFind {
8+
vector<int> fa;
9+
vector<int> size;
10+
11+
public:
12+
int cc;
13+
explicit UnionFind(int n) : fa(n), size(n, 1), cc(n) {
14+
for (int i = 0; i < n; i++) {
15+
fa[i] = i;
16+
}
17+
}
18+
19+
int find(int x) {
20+
if (fa[x] != x) {
21+
fa[x] = find(fa[x]);
22+
}
23+
return fa[x];
24+
}
25+
26+
bool merge(int x, int y) {
27+
int px = find(x), py = find(y);
28+
if (px == py) {
29+
return false;
30+
}
31+
fa[px] = py;
32+
size[py] += size[px];
33+
cc--;
34+
return true;
35+
}
36+
37+
int get_size(int x) { return size[find(x)]; }
38+
};
39+
40+
constexpr array<array<int, 2>, 4> DIRS({{0, 1}, {1, 0}, {0, -1}, {-1, 0}});
41+
842
class Solution {
943
public:
10-
int largestIsland(vector<vector<int>>& grid) {
11-
44+
int largestIsland(const vector<vector<int>> &grid) {
45+
int n = grid.size();
46+
auto pointToIdx = [&n](int x, int y) -> int { return x * n + y; };
47+
UnionFind uf(n * n);
48+
int ans = 0;
49+
for (int i = 0; i < n; ++i) {
50+
for (int j = 0; j < n; ++j) {
51+
if (grid[i][j] == 0) {
52+
continue;
53+
}
54+
int p = pointToIdx(i, j);
55+
for (const auto &dir : DIRS) {
56+
int nx = i + dir[0], ny = j + dir[1];
57+
if (nx < 0 || nx >= n || ny < 0 || ny >= n || grid[nx][ny] != 1) {
58+
continue;
59+
}
60+
uf.merge(p, pointToIdx(nx, ny));
61+
}
62+
ans = max(ans, uf.get_size(uf.find(p)));
63+
}
64+
}
65+
for (int i = 0; i < n; ++i) {
66+
for (int j = 0; j < n; ++j) {
67+
if (grid[i][j] != 0) {
68+
continue;
69+
}
70+
int tot = 1;
71+
unordered_set<int> explored;
72+
for (const auto &dir : DIRS) {
73+
int nx = i + dir[0], ny = j + dir[1];
74+
if (nx < 0 || nx >= n || ny < 0 || ny >= n || grid[nx][ny] != 1) {
75+
continue;
76+
}
77+
int root = uf.find(pointToIdx(nx, ny));
78+
if (explored.contains(root)) {
79+
continue;
80+
}
81+
explored.insert(root);
82+
tot += uf.get_size(root);
83+
}
84+
ans = max(ans, tot);
85+
}
1286
}
87+
return ans;
88+
}
1389
};
1490

1591
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);
92+
vector<string> inputArray;
93+
size_t pos = input_json_values.find('\n');
94+
while (pos != string::npos) {
95+
inputArray.push_back(input_json_values.substr(0, pos));
96+
input_json_values = input_json_values.substr(pos + 1);
97+
pos = input_json_values.find('\n');
98+
}
99+
inputArray.push_back(input_json_values);
100+
101+
Solution solution;
102+
vector<vector<int>> grid = json::parse(inputArray.at(0));
103+
return solution.largestIsland(grid);
28104
}

problems/problems_827/Solution.java

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,99 @@
66

77

88
public class Solution extends BaseSolution {
9+
class UnionFind {
10+
private int[] parent;
11+
private int[] size;
12+
private int count;
13+
14+
public UnionFind(int n) {
15+
parent = new int[n];
16+
size = new int[n];
17+
count = n;
18+
for (int i = 0; i < n; i++) {
19+
parent[i] = i;
20+
size[i] = 1;
21+
}
22+
}
23+
24+
public int find(int x) {
25+
if (parent[x] != x) {
26+
parent[x] = find(parent[x]); // Path compression
27+
}
28+
return parent[x];
29+
}
30+
31+
public boolean union(int x, int y) {
32+
int px = find(x);
33+
int py = find(y);
34+
if (px == py) {
35+
return false; // Already in the same set
36+
}
37+
if (size[px] < size[py]) {
38+
parent[px] = py;
39+
size[py] += size[px];
40+
} else {
41+
parent[py] = px;
42+
size[px] += size[py];
43+
}
44+
count--;
45+
return true; // Union successful
46+
}
47+
48+
public int getCount() {
49+
return count;
50+
}
51+
52+
public int getSize(int x) {
53+
return size[find(x)];
54+
}
55+
}
56+
57+
private static final int[][] DIRS = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
58+
959
public int largestIsland(int[][] grid) {
10-
60+
int n = grid.length;
61+
int ans = 0;
62+
UnionFind uf = new UnionFind(n * n);
63+
for (int i = 0; i < n; ++i) {
64+
for (int j = 0; j < n; ++j) {
65+
if (grid[i][j] == 0) {
66+
continue;
67+
}
68+
int p = i * n + j;
69+
for (int[] dir: DIRS) {
70+
int nx = i + dir[0], ny = j + dir[1];
71+
if (nx < 0 || nx == n || ny < 0 || ny == n || grid[nx][ny] == 0) {
72+
continue;
73+
}
74+
uf.union(p, nx * n + ny);
75+
}
76+
ans = Math.max(ans, uf.getSize(p));
77+
}
78+
}
79+
for (int i = 0; i < n; ++i) {
80+
for (int j = 0; j < n; ++j) {
81+
if (grid[i][j] != 0) {
82+
continue;
83+
}
84+
int tot = 1;
85+
Set<Integer> s = new HashSet<>();
86+
for (int[] dir: DIRS) {
87+
int nx = i + dir[0], ny = j + dir[1];
88+
if (nx < 0 || nx == n || ny < 0 || ny == n || grid[nx][ny] == 0) {
89+
continue;
90+
}
91+
int root = uf.find(nx * n + ny);
92+
if (s.contains(root)) {
93+
continue;
94+
}
95+
s.add(root);
96+
tot += uf.getSize(root);
97+
}
98+
ans = Math.max(ans, tot);
99+
}
100+
}
101+
return ans;
11102
}
12103

13104
@Override

problems/problems_827/solution.go

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,115 @@ import (
66
"strings"
77
)
88

9-
func largestIsland(grid [][]int) int {
10-
9+
type UnionFind struct {
10+
parent []int
11+
rank []int
12+
size []int
13+
cc int
14+
}
15+
16+
func NewUnionFind(n int) *UnionFind {
17+
uf := &UnionFind{
18+
parent: make([]int, n),
19+
rank: make([]int, n),
20+
size: make([]int, n),
21+
cc: n,
22+
}
23+
for i := range uf.parent {
24+
uf.parent[i] = i
25+
uf.rank[i] = 1
26+
uf.size[i] = 1
27+
}
28+
return uf
29+
}
30+
31+
func (uf *UnionFind) Find(x int) int {
32+
for uf.parent[x] != x {
33+
uf.parent[x] = uf.parent[uf.parent[x]] // 路径压缩
34+
x = uf.parent[x]
35+
}
36+
return x
37+
}
38+
39+
func (uf *UnionFind) Union(x, y int) bool {
40+
rootX := uf.Find(x)
41+
rootY := uf.Find(y)
42+
43+
if rootX == rootY {
44+
return false // 已经在同一集合
45+
}
46+
47+
// 按秩合并
48+
if uf.rank[rootX] > uf.rank[rootY] {
49+
uf.parent[rootY] = rootX
50+
uf.size[rootX] += uf.size[rootY]
51+
} else {
52+
uf.parent[rootX] = rootY
53+
if uf.rank[rootX] == uf.rank[rootY] {
54+
uf.rank[rootY]++
55+
}
56+
uf.size[rootY] += uf.size[rootX]
57+
}
58+
uf.cc-- // 合并后集合数减少
59+
return true
60+
}
61+
62+
func (uf *UnionFind) IsConnected(x, y int) bool {
63+
return uf.Find(x) == uf.Find(y)
64+
}
65+
66+
func (uf *UnionFind) GetSize(x int) int {
67+
return uf.size[uf.Find(x)]
68+
}
69+
70+
var DIRS = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
71+
72+
func largestIsland(grid [][]int) (ans int) {
73+
n := len(grid)
74+
pointToIdx := func(x int, y int) int {
75+
return x*n + y
76+
}
77+
78+
uf := NewUnionFind(n * n)
79+
for i := range n {
80+
for j := range n {
81+
if grid[i][j] == 0 {
82+
continue
83+
}
84+
p := pointToIdx(i, j)
85+
for _, dir := range DIRS {
86+
nx, ny := i+dir[0], j+dir[1]
87+
if nx < 0 || nx == n || ny < 0 || ny == n || grid[nx][ny] == 0 {
88+
continue
89+
}
90+
uf.Union(p, uf.Find(pointToIdx(nx, ny)))
91+
}
92+
ans = max(ans, uf.GetSize(p))
93+
}
94+
}
95+
for i := range n {
96+
for j := range n {
97+
if grid[i][j] != 0 {
98+
continue
99+
}
100+
tot := 1
101+
explored := map[int]bool{}
102+
for _, dir := range DIRS {
103+
nx, ny := i+dir[0], j+dir[1]
104+
if nx < 0 || nx == n || ny < 0 || ny == n || grid[nx][ny] == 0 {
105+
continue
106+
}
107+
root := uf.Find(pointToIdx(nx, ny))
108+
if _, ok := explored[root]; ok {
109+
continue
110+
}
111+
explored[root] = true
112+
tot += uf.GetSize(root)
113+
}
114+
ans = max(ans, tot)
115+
}
116+
}
117+
return
11118
}
12119

13120
func Solve(inputJsonValues string) any {

0 commit comments

Comments
 (0)