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

Commit 6dbb534

Browse files
20200830
1 parent 3d36b1e commit 6dbb534

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

‎Java/489.robot-room-cleaner.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @lc app=leetcode id=489 lang=java
3+
*
4+
* [489] Concatenated Words
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
dfs: 记录相对坐标, dfs时还要记录当前朝向(在dirs中的位置), 加上i就是新的朝向
11+
backtracking的时候要注意:除了要转180度后退之后,还要转180度回到原来的朝向
12+
time: O(size of room)
13+
space: O(size of room)
14+
*/
15+
int[] dirs = new int[]{1, 0, -1, 0, 1};
16+
public void cleanRoom(Robot robot) {
17+
Set<String> visited = new HashSet<>();
18+
visited.add("0 - 0");
19+
dfs(robot, visited, 0, 0, 0);
20+
}
21+
22+
private void dfs(Robot robot, Set<String> visited, int r, int c, int cur) {
23+
robot.clean();
24+
for (int i = 0; i < 4; ++i) {
25+
int dir = (i + cur) % 4;
26+
int nextR = r + dirs[dir];
27+
int nextC = c + dirs[dir + 1];
28+
if (visited.add(nextR + " - " + nextC) && robot.move()) {
29+
dfs(robot, visited, nextR, nextC, dir);
30+
// backtrack
31+
robot.turnRight();
32+
robot.turnRight();
33+
robot.move();
34+
robot.turnRight();
35+
robot.turnRight();
36+
}
37+
robot.turnRight();
38+
}
39+
}
40+
}
41+
// @lc code=end
42+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @lc app=leetcode id=952 lang=java
3+
*
4+
* [952] Largest Component Size by Common Factor
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
对于存在公因数,可以将这些数和公因数利用union find连接起来
11+
最后遍历这些数,找到相同parent出现最多的次数
12+
time: O(n * sqrt(max(A)))
13+
space: O(max(A))
14+
*/
15+
class UF {
16+
int[] parent;
17+
int[] rank;
18+
public UF(int n) {
19+
parent = new int[n];
20+
rank = new int[n];
21+
for (int i = 0; i < n; ++i) {
22+
parent[i] = i;
23+
}
24+
}
25+
26+
public void union(int x, int y) {
27+
int px = find(x);
28+
int py = find(y);
29+
if (px == py) return;
30+
if (rank[px] > rank[py]) {
31+
parent[py] = px;
32+
} else if (rank[px] < rank[py]) {
33+
parent[px] = py;
34+
} else {
35+
parent[py] = px;
36+
rank[px]++;
37+
}
38+
}
39+
40+
public int find(int x) {
41+
while (parent[x] != x) {
42+
parent[x] = parent[parent[x]];
43+
x = parent[x];
44+
}
45+
return x;
46+
}
47+
}
48+
public int largestComponentSize(int[] A) {
49+
if (A == null || A.length == 0) return 0;
50+
int max = findMax(A);
51+
UF uf = new UF(max + 1);
52+
int res = 0;
53+
for (int a : A) {
54+
for (int i = 2; i * i <= a; ++i) {
55+
if (a % i == 0) {
56+
uf.union(a, i);
57+
uf.union(a, a / i);
58+
}
59+
}
60+
}
61+
// 找到所有A中元素中parent出现次数最多的
62+
Map<Integer, Integer> count = new HashMap<>();
63+
for (int a : A) {
64+
int parent = uf.find(a);
65+
count.put(parent, count.getOrDefault(parent, 0) + 1);
66+
res = Math.max(res, count.get(parent));
67+
}
68+
return res;
69+
}
70+
71+
private int findMax(int[] A) {
72+
int res = Integer.MIN_VALUE;
73+
for (int a : A) {
74+
res = Math.max(res, a);
75+
}
76+
return res;
77+
}
78+
}
79+
// @lc code=end
80+

‎README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
[140. Word Break II](https://leetcode.com/problems/word-break-ii/) | [DFS + memo](https://github.com/Yukinichi/leetcode/blob/master/Java/140.word-break-ii.java)
136136
[329. Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/) | [DFS + memo](https://github.com/Yukinichi/leetcode/blob/master/Java/329.longest-increasing-path-in-a-matrix.java)
137137
[472. Concatenated Words](https://leetcode.com/problems/concatenated-words/) | [DFS + memo Solution(C++)](https://github.com/Yukinichi/leetcode/blob/master/Cpp/472.concatenated-words.cpp) \| [Trie Solution(Java)](https://github.com/Yukinichi/leetcode/blob/master/Java/472.concatenated-words.java)
138+
[489. Robot Room Cleaner](https://leetcode.com/problems/robot-room-cleaner/) | [模拟dfs, 注意当前robot朝向](https://github.com/Yukinichi/leetcode/blob/master/Java/489.robot-room-cleaner.java)
138139

139140

140141
## Sliding Window
@@ -178,4 +179,5 @@
178179
## Union Find
179180
| Problem | Solution
180181
:- | :-:
181-
[695. Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | [Union Find](https://github.com/Yukinichi/leetcode/blob/master/Java/695.max-area-of-island.java) \| [DFS](https://github.com/Yukinichi/leetcode/blob/master/Cpp/695.max-area-of-island.cpp)
182+
[695. Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | [Union Find](https://github.com/Yukinichi/leetcode/blob/master/Java/695.max-area-of-island.java) \| [DFS](https://github.com/Yukinichi/leetcode/blob/master/Cpp/695.max-area-of-island.cpp)
183+
[952. Largest Component Size by Common Factor](https://leetcode.com/problems/largest-component-size-by-common-factor/) | [连接公因数的UF](https://github.com/Yukinichi/leetcode/blob/master/Java/952.largest-component-size-by-common-factor.java)

0 commit comments

Comments
(0)

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