-
Notifications
You must be signed in to change notification settings - Fork 0
/
269.py
63 lines (49 loc) · 1.79 KB
/
269.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Problem:
You are given an string representing the initial conditions of some dominoes. Each
element can take one of three values:
L, meaning the domino has just been pushed to the left,
R, meaning the domino has just been pushed to the right, or
., meaning the domino is standing still.
Determine the orientation of each tile when the dominoes stop falling. Note that if a
domino receives a force from the left and right side simultaneously, it will remain
upright.
For example, given the string .L.R....L, you should return LL.RRRLLL.
Given the string ..R...L.L, you should return ..RR.LLLL.
"""
from typing import List
def get_config_helper(dominos: List[str], length: int) -> List[str]:
has_been_updated = False
updated_dominos = dominos.copy()
for i in range(length):
if (
dominos[i] == "L"
and i > 0
and dominos[i - 1] == "."
and ((i > 1 and dominos[i - 2] != "R") or i <= 1)
):
updated_dominos[i - 1] = "L"
has_been_updated = True
elif (
dominos[i] == "R"
and i < length - 1
and dominos[i + 1] == "."
and ((i < length - 2 and dominos[i + 2] != "L") or i >= length - 2)
):
updated_dominos[i + 1] = "R"
has_been_updated = True
if has_been_updated:
return get_config_helper(updated_dominos, length)
return dominos
def get_config(initial_state: str) -> str:
dominoes = list(initial_state)
return "".join(get_config_helper(dominoes, len(dominoes)))
if __name__ == "__main__":
print(get_config(".L.R....L"))
print(get_config("..R...L.L"))
"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(n ^ 2)
[each iteration takes O(n) in both time & space and there can be n such iterations]
"""