-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday24.java
More file actions
45 lines (37 loc) · 1.21 KB
/
day24.java
File metadata and controls
45 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//ques1:838. Push Dominoes
//link:https://leetcode.com/problems/push-dominoes/description/?envType=problem-list-v2&envId=string
class Solution {
public String pushDominoes(String dominoes) {
int n = dominoes.length();
int[] forces = new int[n];
int force = 0;
for (int i = 0; i < n; i++) {
if (dominoes.charAt(i) == 'R') {
force = n;
} else if (dominoes.charAt(i) == 'L') {
force = 0;
} else {
force = Math.max(0, force - 1);
}
forces[i] += force;
}
force = 0;
for (int i = n - 1; i >= 0; i--) {
if (dominoes.charAt(i) == 'L') {
force = n;
} else if (dominoes.charAt(i) == 'R') {
force = 0;
} else {
force = Math.max(0, force - 1);
}
forces[i] -= force;
}
StringBuilder result = new StringBuilder();
for (int f : forces) {
if (f > 0) result.append('R');
else if (f < 0) result.append('L');
else result.append('.');
}
return result.toString();
}
}