Skip to content

Commit c9ed109

Browse files
committed
leetcode
1 parent f31972e commit c9ed109

File tree

4 files changed

+490
-0
lines changed

4 files changed

+490
-0
lines changed

PushDominoes/push_dominoes.dart

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
3+
-* Push Dominoes *-
4+
5+
6+
There are n dominoes in a line, and we place each domino vertically upright. In the beginning,
7+
we simultaneously push some of the dominoes either to the left or to the right.
8+
9+
After each second, each domino that is falling to the left pushes the adjacent domino on the left.
10+
Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
11+
12+
When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
13+
14+
For the purposes of this question, we will consider that a falling domino expends no additional
15+
force to a falling or already fallen domino.
16+
17+
You are given a string dominoes representing the initial state where:
18+
19+
dominoes[i] = 'L', if the ith domino has been pushed to the left,
20+
dominoes[i] = 'R', if the ith domino has been pushed to the right, and
21+
dominoes[i] = '.', if the ith domino has not been pushed.
22+
Return a string representing the final state.
23+
24+
25+
26+
Example 1:
27+
28+
Input: dominoes = "RR.L"
29+
Output: "RR.L"
30+
Explanation: The first domino expends no additional force on the second domino.
31+
Example 2:
32+
33+
34+
Input: dominoes = ".L.R...LR..L.."
35+
Output: "LL.RR.LLRRLL.."
36+
37+
38+
Constraints:
39+
40+
n == dominoes.length
41+
1 <= n <= 105
42+
dominoes[i] is either 'L', 'R', or '.'.
43+
44+
*/
45+
import 'dart:math';
46+
47+
class A {
48+
// Two Pointers
49+
// Time: O(n)O(n)
50+
// Space: O(n)O(n)
51+
// Runtime: 386 ms, faster than 100.00% of Dart online submissions for Push Dominoes.
52+
// Memory Usage: 156.8 MB, less than 100.00% of Dart online submissions for Push Dominoes.
53+
54+
String pushDominoes(String dominoes) {
55+
// splitting the the whole string into a list like each individual character
56+
List<String> finalString = dominoes.split("");
57+
// left falling domino
58+
int L = -1;
59+
// right falling domino
60+
int R = -1;
61+
// looping through the length off the dominoes
62+
for (int i = 0; i <= dominoes.length; ++i)
63+
// if the i same as the dominos length OR smae as the individual character or the splitting list
64+
// of spited list as above assuming from right size
65+
if (i == dominoes.length || finalString[i] == 'R') {
66+
// if the left is greater than the right while right side is less than the individual character
67+
if (L < R) while (R < i) finalString[R++] = 'R';
68+
// than we will give the order to push from the right side
69+
// and our right side is will be equal to the character because we pushed so it's value will be changing
70+
R = i;
71+
// else if the individual character is same aas from the left
72+
} else if (finalString[i] == 'L') {
73+
// assuming the right side is less than left or left and right is a negative value
74+
if (R < L || L == -1 && R == -1) {
75+
// we will push the trigger from the left side
76+
if (L == -1 && R == -1) ++L;
77+
// assuming the left is less than the each character
78+
// so we will increment it at the point of splitting string
79+
while (L < i) finalString[L++] = 'L';
80+
} else {
81+
// if it's not the case
82+
// than defining the left and right sides
83+
int l = R + 1;
84+
int r = i - 1;
85+
// assuming that the left is less than right
86+
while (l < r) {
87+
// increment from the left side by triggering from the right side
88+
// because the left value will increase as the right value decrease
89+
finalString[l++] = 'R';
90+
finalString[r--] = 'L';
91+
}
92+
}
93+
// assigning the left value to the character
94+
L = i;
95+
}
96+
// as a result we will return the resulting string and and join it to make it one single string
97+
return finalString.join();
98+
}
99+
}
100+
101+
class B {
102+
// Traversal solution in TC: O(n)
103+
// Runtime: 407 ms, faster than 100.00% of Dart online submissions for Push Dominoes.
104+
// Memory Usage: 157.4 MB, less than 100.00% of Dart online submissions for Push Dominoes.
105+
String pushDominoes(String dominoes) {
106+
// splitting the string into individual character
107+
List<String> s = dominoes.split("");
108+
// length of our whole string
109+
int n = dominoes.length;
110+
// lift side of the dominoes - holing the how many dominoes available on the left
111+
List<int> left = List.filled(n, 0);
112+
// right side of the dominoes - holing the how many dominoes available on the right
113+
List<int> right = List.filled(n, 0);
114+
// total sum value of both right and left - holing the how many dominoes available on both side
115+
List<int> sum = List.filled(n, 0);
116+
//R command from left to right
117+
int command = 0;
118+
// looping through the whole length - if the length is less than each character
119+
for (int i = 0; i < n; i++) {
120+
if (dominoes[i] == 'R')
121+
// our command will be from right side and assign the vle to i
122+
command = n;
123+
else if (dominoes[i] == 'L')
124+
command = 0;
125+
else if (dominoes[i] == '.') command = max(command - 1, 0);
126+
127+
right[i] = command;
128+
}
129+
int p = 0;
130+
for (int i = n - 1; i >= 0; i--, p++) {
131+
if (dominoes[i] == 'L')
132+
command = n;
133+
else if (dominoes[i] == 'R')
134+
command = 0;
135+
else if (dominoes[i] == '.') command = max(command - 1, 0);
136+
137+
left[i] = command * (-1);
138+
}
139+
for (int i = 0; i < n; i++) {
140+
sum[i] = right[i] + left[i];
141+
if (sum[i] == 0)
142+
s[i] = '.';
143+
else if (sum[i] > 0)
144+
s[i] = 'R';
145+
else
146+
s[i] = 'L';
147+
}
148+
return s.join();
149+
}
150+
}
151+
152+
// extension on String {
153+
// operator >(String other) {
154+
// return double.parse(this) > double.parse(other);
155+
// }
156+
157+
// operator >=(String other) {
158+
// return double.parse(this) >= double.parse(other);
159+
// }
160+
161+
// operator <(String other) {
162+
// return double.parse(this) < double.parse(other);
163+
// }
164+
165+
// operator <=(String other) {
166+
// return double.parse(this) <= double.parse(other);
167+
// }
168+
// }
169+
170+
class C {
171+
// Runtime: 538 ms, faster than 100.00% of Dart online submissions for Push Dominoes.
172+
// Memory Usage: 154.9 MB, less than 100.00% of Dart online submissions for Push Dominoes.
173+
String pushDominoes(String dominoes) {
174+
List<String> d = dominoes.split("");
175+
// l is the left pointer
176+
int l = 0, n = dominoes.length;
177+
// r is the right pointer
178+
for (int r = 0; r < n; r++) {
179+
if (d[r] == '.') {
180+
// case 1. meeting `.`, then skip it
181+
continue;
182+
} else if ((d[r] == d[l]) || (d[l] == '.' && d[r] == 'L')) {
183+
// case 2. both end is equal, i.e. d[r] == d[l]
184+
// then fill all the dots between both end
185+
// e.g. L....L -> LLLLLL
186+
// e.g. R....R -> RRRRRR
187+
// case 2.1 if the left end is . and the right end is L,
188+
// i.e. d[l] == '.' && d[r] == 'L'
189+
// then we need to fill them from `l` to `r` in this case
190+
for (int k = l; k < r; k++) d[k] = d[r];
191+
} else if (d[l] == 'L' && d[r] == 'R') {
192+
// case 3. left end is L and right end is R
193+
// e.g. L.....R
194+
// then do nothing
195+
} else if (d[l] == 'R' && d[r] == 'L') {
196+
// case 4. left end is R and right end is L
197+
// if we have odd number of dots between them (let's say m dots),
198+
// then we can only add (m / 2) Ls and (m / 2) Rs.
199+
// p.s `/` here is integer division. e.g. 3 / 2 = 1
200+
// e.g. R...L -> RR.LL
201+
// if we have even number of dots between them (let's say m dots),
202+
// then we can only add (m / 2) Ls and (m / 2) Rs.
203+
// e.g. R....L -> RRRLLL
204+
int m = (r - l - 1) ~/ 2;
205+
for (int k = 1; k <= m; k++) {
206+
d[r - k] = 'L';
207+
d[l + k] = 'R';
208+
}
209+
}
210+
// update left pointer
211+
l = r;
212+
}
213+
// case 5. if the left dominoe is `R`, then fill all 'R' till the end
214+
// e.g. LL.R. -> LL.RR
215+
if (d[l] == 'R') for (int k = l; k < n; k++) d[k] = 'R';
216+
return d.join();
217+
}
218+
}

PushDominoes/push_dominoes.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package main
2+
3+
// func fall(l, c, r rune) rune {
4+
// if c == '.' {
5+
// if l == 'R' && r == 'L' {
6+
// return '.'
7+
// }
8+
// if l == 'R' {
9+
// return 'R'
10+
// }
11+
// if r == 'L' {
12+
// return 'L'
13+
// }
14+
// }
15+
// return c
16+
// }
17+
18+
// func pushIteration(dominoes string) string {
19+
// result := make([]rune, len(dominoes))
20+
// if len(dominoes) == 1 {
21+
// return dominoes
22+
// }
23+
// dominoesR := []rune(dominoes)
24+
// for i := 0; i < len(dominoesR); i++ {
25+
// switch i {
26+
// case 0:
27+
// result[i] = fall('.', dominoesR[i], dominoesR[i+1])
28+
// case len(dominoes) - 1:
29+
// result[i] = fall(dominoesR[i-1], dominoesR[i], '.')
30+
// default:
31+
// result[i] = fall(dominoesR[i-1], dominoesR[i], dominoesR[i+1])
32+
// }
33+
// }
34+
// return string(result)
35+
// }
36+
37+
// func pushDominoes(dominoes string) string {
38+
// previousIteration := pushIteration(dominoes)
39+
// for previousIteration != dominoes {
40+
// previousIteration = dominoes
41+
// dominoes = pushIteration(dominoes)
42+
// }
43+
// return dominoes
44+
// }
45+
46+
func pushDominoes(dominoes string) string {
47+
var (
48+
buf = make([]int, len(dominoes))
49+
res = make([]byte, 0, len(buf))
50+
val = 0
51+
52+
dot byte = 46
53+
r byte = 82
54+
l byte = 76
55+
)
56+
57+
for i := 0; i < len(dominoes); i++ {
58+
if dominoes[i] == r {
59+
val = len(dominoes)
60+
} else if dominoes[i] == l {
61+
val = 0
62+
} else {
63+
val = max(val-1, 0)
64+
}
65+
66+
buf[i] += val
67+
}
68+
69+
val = 0
70+
for i := len(dominoes) - 1; i >= 0; i-- {
71+
if dominoes[i] == l {
72+
val = len(dominoes)
73+
} else if dominoes[i] == r {
74+
val = 0
75+
} else {
76+
val = max(val-1, 0)
77+
}
78+
79+
buf[i] -= val
80+
}
81+
82+
for i := 0; i < len(buf); i++ {
83+
if buf[i] < 0 {
84+
res = append(res, l)
85+
} else if buf[i] > 0 {
86+
res = append(res, r)
87+
} else {
88+
res = append(res, dot)
89+
}
90+
}
91+
92+
return string(res)
93+
}
94+
95+
func max(a, b int) int {
96+
if a > b {
97+
return a
98+
}
99+
100+
return b
101+
}

0 commit comments

Comments
 (0)