forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
finding-3-digit-even-numbers.py
143 lines (125 loc) · 4.26 KB
/
finding-3-digit-even-numbers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Time: O(1) ~ O(n), n is 10^3
# Space: O(1)
class Solution(object):
def findEvenNumbers(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
k = 3
def backtracking(curr, cnt, result):
if len(curr) == k:
result.append(reduce(lambda x, y: x*10+y, curr))
return
for i, c in enumerate(cnt):
if c == 0 or (not curr and i == 0) or (len(curr) == k-1 and i%2 != 0):
continue
cnt[i] -= 1
curr.append(i)
backtracking(curr, cnt, result)
curr.pop()
cnt[i] += 1
cnt = [0]*10
for d in digits:
cnt[d] += 1
result = []
backtracking([], cnt, result)
return result
# Time: O(n), n is 10^3
# Space: O(1)
import collections
class Solution2(object):
def findEvenNumbers(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
result, cnt = [], collections.Counter(digits)
for i in xrange(1, 10):
for j in xrange(10):
for k in xrange(0, 10, 2):
if cnt[i] > 0 and cnt[j] > (j == i) and cnt[k] > (k == i) + (k == j):
result.append(i*100 + j*10 + k)
return result
# Time: O(1) ~ O(n), n is 10^3
# Space: O(1)
import collections
class Node(object):
def __init__(self, val=None, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution3(object):
def findEvenNumbers(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
k = 3
def backtracking(curr, dummy, result):
if len(curr) == k:
result.append(reduce(lambda x, y: x*10+y, curr))
return
node = dummy.right
while node:
if (not curr and node.val[0] == 0) or (len(curr) == k-1 and node.val[0]%2 != 0):
node = node.right
continue
node.val[1] -= 1
if node.val[1] == 0:
if node.left:
node.left.right = node.right
if node.right:
node.right.left = node.left
curr.append(node.val[0])
backtracking(curr, dummy, result)
curr.pop()
if node.val[1] == 0:
if node.left:
node.left.right = node
if node.right:
node.right.left = node
node.val[1] += 1
node = node.right
prev = dummy = Node()
for digit, cnt in sorted(map(list, collections.Counter(digits).iteritems())):
prev.right = Node(val=[digit, cnt], left=prev)
prev = prev.right
result = []
backtracking([], dummy, result)
return result
# Time: O(1) ~ O(nlogn), n is 10^3
# Space: O(1)
import collections
class Solution4(object):
def findEvenNumbers(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
k = 3
def backtracking(curr, digit_cnt, result):
if len(curr) == k:
result.append(reduce(lambda x, y: x*10+y, curr))
return
for i, (digit, cnt) in enumerate(digit_cnt):
if (not curr and digit == 0) or (len(curr) == k-1 and digit%2 != 0):
continue
digit_cnt[i][1] -= 1
digit_cnt[i], digit_cnt[-1] = digit_cnt[-1], digit_cnt[i]
removed = []
if digit_cnt[-1][1] == 0:
removed = digit_cnt.pop()
curr.append(digit)
backtracking(curr, digit_cnt, result)
curr.pop()
if removed:
digit_cnt.append(removed)
digit_cnt[i], digit_cnt[-1] = digit_cnt[-1], digit_cnt[i]
digit_cnt[i][1] += 1
cnt = collections.Counter(digits)
digit_cnt = map(list, cnt.iteritems())
result = []
backtracking([], digit_cnt, result)
result.sort()
return result