Skip to content

Commit cca41c2

Browse files
committed
feat: add solutions to lc problem: No.0838
No.0838.Push Dominoes
1 parent cb50fa4 commit cca41c2

File tree

6 files changed

+474
-2
lines changed

6 files changed

+474
-2
lines changed

solution/0800-0899/0838.Push Dominoes/README.md

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,184 @@
4646

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

49+
BFS。设 time 记录骨牌翻倒或者确定不翻倒的时间,翻倒的骨牌不会对正在翻倒或者已经翻倒的骨牌施加力,force 记录骨牌受到的力,骨牌仅在受到单侧的力时会翻倒。
50+
51+
初始时,将所有受力点 i (`dominoes[i] != '.'`)入队,并设置 `time[i] = 0`
52+
4953
<!-- tabs:start -->
5054

5155
### **Python3**
5256

5357
<!-- 这里可写当前语言的特殊实现逻辑 -->
5458

5559
```python
56-
60+
class Solution:
61+
def pushDominoes(self, dominoes: str) -> str:
62+
n = len(dominoes)
63+
q = deque()
64+
time = [-1] * n
65+
force = defaultdict(list)
66+
for i, f in enumerate(dominoes):
67+
if f != '.':
68+
q.append(i)
69+
time[i] = 0
70+
force[i].append(f)
71+
ans = ['.'] * n
72+
while q:
73+
i = q.popleft()
74+
if len(force[i]) == 1:
75+
ans[i] = f = force[i][0]
76+
j = i - 1 if f == 'L' else i + 1
77+
if 0 <= j < n:
78+
t = time[i]
79+
if time[j] == -1:
80+
q.append(j)
81+
time[j] = t + 1
82+
force[j].append(f)
83+
elif time[j] == t + 1:
84+
force[j].append(f)
85+
return ''.join(ans)
5786
```
5887

5988
### **Java**
6089

6190
<!-- 这里可写当前语言的特殊实现逻辑 -->
6291

6392
```java
93+
class Solution {
94+
public String pushDominoes(String dominoes) {
95+
int n = dominoes.length();
96+
Deque<Integer> q = new ArrayDeque<>();
97+
int[] time = new int[n];
98+
Arrays.fill(time, -1);
99+
List<Character>[] force = new List[n];
100+
for (int i = 0; i < n; ++i) {
101+
force[i] = new ArrayList<>();
102+
}
103+
for (int i = 0; i < n; ++i) {
104+
char f = dominoes.charAt(i);
105+
if (f != '.') {
106+
q.offer(i);
107+
time[i] = 0;
108+
force[i].add(f);
109+
}
110+
}
111+
char[] ans = new char[n];
112+
Arrays.fill(ans, '.');
113+
while (!q.isEmpty()) {
114+
int i = q.poll();
115+
if (force[i].size() == 1) {
116+
ans[i] = force[i].get(0);
117+
char f = ans[i];
118+
int j = f == 'L' ? i - 1 : i + 1;
119+
if (j >= 0 && j < n) {
120+
int t = time[i];
121+
if (time[j] == -1) {
122+
q.offer(j);
123+
time[j] = t + 1;
124+
force[j].add(f);
125+
} else if (time[j] == t + 1) {
126+
force[j].add(f);
127+
}
128+
}
129+
}
130+
}
131+
return new String(ans);
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
string pushDominoes(string dominoes) {
142+
int n = dominoes.size();
143+
queue<int> q;
144+
vector<int> time(n, - 1);
145+
vector<string> force(n);
146+
for (int i = 0; i < n; i++)
147+
{
148+
if (dominoes[i] == '.') continue;
149+
q.emplace(i);
150+
time[i] = 0;
151+
force[i].push_back(dominoes[i]);
152+
}
153+
154+
string ans(n, '.');
155+
while (!q.empty())
156+
{
157+
int i = q.front();
158+
q.pop();
159+
if (force[i].size() == 1)
160+
{
161+
char f = force[i][0];
162+
ans[i] = f;
163+
int j = (f == 'L') ? (i - 1) : (i + 1);
164+
if (j >= 0 && j < n)
165+
{
166+
int t = time[i];
167+
if (time[j] == -1)
168+
{
169+
q.emplace(j);
170+
time[j] = t + 1;
171+
force[j].push_back(f);
172+
}
173+
else if(time[j] == t + 1) force[j].push_back(f);
174+
}
175+
}
176+
}
177+
return ans;
178+
}
179+
};
180+
```
64181

182+
### **Go**
183+
184+
```go
185+
func pushDominoes(dominoes string) string {
186+
n := len(dominoes)
187+
q := []int{}
188+
time := make([]int, n)
189+
for i := range time {
190+
time[i] = -1
191+
}
192+
force := make([][]byte, n)
193+
for i, c := range dominoes {
194+
if c != '.' {
195+
q = append(q, i)
196+
time[i] = 0
197+
force[i] = append(force[i], byte(c))
198+
}
199+
}
200+
201+
ans := bytes.Repeat([]byte{'.'}, n)
202+
for len(q) > 0 {
203+
i := q[0]
204+
q = q[1:]
205+
if len(force[i]) > 1 {
206+
continue
207+
}
208+
f := force[i][0]
209+
ans[i] = f
210+
j := i - 1
211+
if f == 'R' {
212+
j = i + 1
213+
}
214+
if 0 <= j && j < n {
215+
t := time[i]
216+
if time[j] == -1 {
217+
q = append(q, j)
218+
time[j] = t + 1
219+
force[j] = append(force[j], f)
220+
} else if time[j] == t+1 {
221+
force[j] = append(force[j], f)
222+
}
223+
}
224+
}
225+
return string(ans)
226+
}
65227
```
66228

67229
### **...**

solution/0800-0899/0838.Push Dominoes/README_EN.md

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,171 @@
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def pushDominoes(self, dominoes: str) -> str:
63+
n = len(dominoes)
64+
q = deque()
65+
time = [-1] * n
66+
force = defaultdict(list)
67+
for i, f in enumerate(dominoes):
68+
if f != '.':
69+
q.append(i)
70+
time[i] = 0
71+
force[i].append(f)
72+
ans = ['.'] * n
73+
while q:
74+
i = q.popleft()
75+
if len(force[i]) == 1:
76+
ans[i] = f = force[i][0]
77+
j = i - 1 if f == 'L' else i + 1
78+
if 0 <= j < n:
79+
t = time[i]
80+
if time[j] == -1:
81+
q.append(j)
82+
time[j] = t + 1
83+
force[j].append(f)
84+
elif time[j] == t + 1:
85+
force[j].append(f)
86+
return ''.join(ans)
6287
```
6388

6489
### **Java**
6590

6691
```java
92+
class Solution {
93+
public String pushDominoes(String dominoes) {
94+
int n = dominoes.length();
95+
Deque<Integer> q = new ArrayDeque<>();
96+
int[] time = new int[n];
97+
Arrays.fill(time, -1);
98+
List<Character>[] force = new List[n];
99+
for (int i = 0; i < n; ++i) {
100+
force[i] = new ArrayList<>();
101+
}
102+
for (int i = 0; i < n; ++i) {
103+
char f = dominoes.charAt(i);
104+
if (f != '.') {
105+
q.offer(i);
106+
time[i] = 0;
107+
force[i].add(f);
108+
}
109+
}
110+
char[] ans = new char[n];
111+
Arrays.fill(ans, '.');
112+
while (!q.isEmpty()) {
113+
int i = q.poll();
114+
if (force[i].size() == 1) {
115+
ans[i] = force[i].get(0);
116+
char f = ans[i];
117+
int j = f == 'L' ? i - 1 : i + 1;
118+
if (j >= 0 && j < n) {
119+
int t = time[i];
120+
if (time[j] == -1) {
121+
q.offer(j);
122+
time[j] = t + 1;
123+
force[j].add(f);
124+
} else if (time[j] == t + 1) {
125+
force[j].add(f);
126+
}
127+
}
128+
}
129+
}
130+
return new String(ans);
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
string pushDominoes(string dominoes) {
141+
int n = dominoes.size();
142+
queue<int> q;
143+
vector<int> time(n, - 1);
144+
vector<string> force(n);
145+
for (int i = 0; i < n; i++)
146+
{
147+
if (dominoes[i] == '.') continue;
148+
q.emplace(i);
149+
time[i] = 0;
150+
force[i].push_back(dominoes[i]);
151+
}
152+
153+
string ans(n, '.');
154+
while (!q.empty())
155+
{
156+
int i = q.front();
157+
q.pop();
158+
if (force[i].size() == 1)
159+
{
160+
char f = force[i][0];
161+
ans[i] = f;
162+
int j = (f == 'L') ? (i - 1) : (i + 1);
163+
if (j >= 0 && j < n)
164+
{
165+
int t = time[i];
166+
if (time[j] == -1)
167+
{
168+
q.emplace(j);
169+
time[j] = t + 1;
170+
force[j].push_back(f);
171+
}
172+
else if(time[j] == t + 1) force[j].push_back(f);
173+
}
174+
}
175+
}
176+
return ans;
177+
}
178+
};
179+
```
67180

181+
### **Go**
182+
183+
```go
184+
func pushDominoes(dominoes string) string {
185+
n := len(dominoes)
186+
q := []int{}
187+
time := make([]int, n)
188+
for i := range time {
189+
time[i] = -1
190+
}
191+
force := make([][]byte, n)
192+
for i, c := range dominoes {
193+
if c != '.' {
194+
q = append(q, i)
195+
time[i] = 0
196+
force[i] = append(force[i], byte(c))
197+
}
198+
}
199+
200+
ans := bytes.Repeat([]byte{'.'}, n)
201+
for len(q) > 0 {
202+
i := q[0]
203+
q = q[1:]
204+
if len(force[i]) > 1 {
205+
continue
206+
}
207+
f := force[i][0]
208+
ans[i] = f
209+
j := i - 1
210+
if f == 'R' {
211+
j = i + 1
212+
}
213+
if 0 <= j && j < n {
214+
t := time[i]
215+
if time[j] == -1 {
216+
q = append(q, j)
217+
time[j] = t + 1
218+
force[j] = append(force[j], f)
219+
} else if time[j] == t+1 {
220+
force[j] = append(force[j], f)
221+
}
222+
}
223+
}
224+
return string(ans)
225+
}
68226
```
69227

70228
### **...**

0 commit comments

Comments
 (0)