-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayOfDoubledPairs.py
More file actions
57 lines (50 loc) · 1.89 KB
/
arrayOfDoubledPairs.py
File metadata and controls
57 lines (50 loc) · 1.89 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
46
47
48
49
50
51
52
53
54
55
56
57
# problem: https://leetcode.com/problems/array-of-doubled-pairs/
# Runtime: 576 ms, faster than 88.17% of Python3 online submissions for Array of Doubled Pairs.
# Memory Usage: 16.6 MB, less than 57.71% of Python3 online submissions for Array of Doubled Pairs.
from typing import List
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
from collections import Counter
count = Counter(A)
keys = list(count.keys())
keys.sort()
for key in keys:
if count[key]:
if count[2 * key] >= count[key]:
count[2 * key] -= count[key]
count[key] = 0
elif count[(1/2) * key] >= count[key]:
count[(1/2) * key] -= count[key]
count[key] = 0
else:
return False
return True
class Solution:
def getConnections(self, element):
connections = []
for i in range(4):
el = [s for s in element]
el[i] = str(int(el[i]) + 1) if el[i] != "9" else "0"
connections.append("".join(el))
el[i] = str(int(el[i]) - 1) if el[i] != "1" else "0"
connections.append("".join(el))
return connections
def openLock(self, deadends: List[str], target: str) -> int:
from collections import deque
deadends = set(deadends)
queue = deque()
queue.append("0000")
layer = 0
while queue:
tmp = deque()
while queue:
element = queue.popleft()
if element == target:
return layer
connections = self.getConnections(element)
for connection in connections:
if not connection in deadends:
tmp.append(connection)
layer += 1
queue = tmp
return -1