Skip to content

Commit 0de2fcf

Browse files
committed
no message
1 parent 13fbec9 commit 0de2fcf

11 files changed

+246
-3
lines changed

problems/combinations.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ def dfs(n_min, path):
8383

8484

8585

86+
class Solution(object):
87+
def combine(self, N, K):
88+
def dfs(comb, start, N, K):
89+
if len(comb)==K: ans.append(comb[:])
90+
91+
for n in xrange(start, N+1):
92+
comb.append(n)
93+
dfs(comb, n+1, N, K)
94+
comb.pop()
95+
96+
ans = []
97+
dfs([], 1, N, K)
98+
return ans
99+
100+
86101

87102

88103

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution(object):
2+
def countOrders(self, n):
3+
d = 1
4+
for i in xrange(1, 2*n, 2): d*=i
5+
return math.factorial(n)*d % (10**9 + 7)

problems/logger-rate-limiter.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,27 @@ def shouldPrintMessage(self, timestamp, message):
66
if message not in self.log or self.log[message]+10<=timestamp:
77
self.log[message] = timestamp
88
return True
9+
else:
10+
return False
11+
12+
13+
14+
class Logger(object):
15+
16+
def __init__(self):
17+
#stores the messages within 10 seconds
18+
self.q = collections.deque()
19+
self.set = set()
20+
21+
22+
def shouldPrintMessage(self, timestamp, message):
23+
while self.q and timestamp-self.q[0][0]>=10:
24+
time, msg = self.q.popleft()
25+
self.set.remove(msg)
26+
27+
if message not in self.set:
28+
self.q.append((timestamp, message))
29+
self.set.add(message)
30+
return True
931
else:
1032
return False
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def minimumCost(self, n, highways, discounts):
3+
pq = [(0, discounts, 0)]
4+
visited = set()
5+
6+
adj = collections.defaultdict(list)
7+
for city1, city2, toll in highways:
8+
adj[city1].append((city2, toll))
9+
adj[city2].append((city1, toll))
10+
11+
12+
while pq:
13+
toll, d, city = heapq.heappop(pq)
14+
if (d, city) in visited: continue
15+
visited.add((d, city))
16+
17+
if city==n-1: return toll
18+
19+
for nei, toll2 in adj[city]:
20+
if (d, nei) not in visited:
21+
heapq.heappush(pq, (toll+toll2, d, nei))
22+
if d>0 and (d-1, nei) not in visited:
23+
heapq.heappush(pq, (toll+toll2/2, d-1, nei))
24+
25+
return -1
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution(object):
2+
def minFlips(self, mat):
3+
def flip(mat, m, n):
4+
mat[m][n] = 0 if mat[m][n]==1 else 1
5+
if m+1<len(mat): mat[m+1][n] = 0 if mat[m+1][n]==1 else 1
6+
if n+1<len(mat[0]): mat[m][n+1] = 0 if mat[m][n+1]==1 else 1
7+
if m-1>=0: mat[m-1][n] = 0 if mat[m-1][n]==1 else 1
8+
if n-1>=0: mat[m][n-1] = 0 if mat[m][n-1]==1 else 1
9+
10+
def check(mat, state):
11+
for i, b in enumerate(state):
12+
if b=='1':
13+
m = i/len(mat[0])
14+
n = i%len(mat[0])
15+
flip(mat, m, n)
16+
17+
for i in xrange(len(mat)):
18+
for j in xrange(len(mat[0])):
19+
if mat[i][j]==1: return False
20+
21+
return True
22+
23+
24+
M = len(mat)
25+
N = len(mat[0])
26+
q = collections.deque(['0'*(M*N)])
27+
visited = set()
28+
29+
while q:
30+
state = q.popleft()
31+
if state in visited: continue
32+
visited.add(state)
33+
34+
if check([row[:] for row in mat], state): return state.count('1')
35+
36+
for i in xrange(len(state)):
37+
if state[i]=='1': continue
38+
nextState = state[:i] + '1' +state[i+1:]
39+
q.append(nextState)
40+
41+
return -1
42+
43+
44+
45+
46+

problems/permutation-sequence.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def getPermutation(self, N, K):
3+
K = K-1 #make it 0-index
4+
nums = range(1, N+1)
5+
ans = ''
6+
7+
while N>0:
8+
a = K/math.factorial(N-1)
9+
ans += str(nums[a])
10+
nums.pop(a)
11+
12+
K -= math.factorial(N-1)*(a+1)
13+
N -= 1
14+
return ans

problems/permutations-ii.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,28 @@ def helper(path):
5353
counter = collections.Counter(nums)
5454
helper([])
5555
return ans
56+
57+
58+
"""
59+
差板法
60+
"""
61+
class Solution(object):
62+
def permuteUnique(self, nums):
63+
if not nums: return []
64+
65+
permutations = collections.deque([[nums[0]]])
66+
67+
for i in xrange(1, len(nums)):
68+
num = nums[i]
69+
l = len(permutations)
70+
71+
while l:
72+
permutation = permutations.popleft()
73+
for j in xrange(len(permutation)+1):
74+
if 0<j and num==permutation[j-1]: break
75+
newPermutaion = permutation[:]
76+
newPermutaion.insert(j, num)
77+
permutations.append(newPermutaion)
78+
l -= 1
79+
80+
return permutations

problems/permutations.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,28 @@ def helper(i):
7373
N = len(nums)
7474
ans = []
7575
helper(0)
76-
return ans
76+
return ans
77+
78+
79+
"""
80+
差板法
81+
"""
82+
class Solution(object):
83+
def permute(self, nums):
84+
if not nums: return []
85+
86+
permutations = collections.deque([[nums[0]]])
87+
88+
for i in xrange(1, len(nums)):
89+
num = nums[i]
90+
l = len(permutations)
91+
92+
while l:
93+
permutation = permutations.popleft()
94+
for j in xrange(len(permutation)+1):
95+
newPermutaion = permutation[:]
96+
newPermutaion.insert(j, num)
97+
permutations.append(newPermutaion)
98+
l -= 1
99+
100+
return permutations

problems/profitable-schemes.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,30 @@ def profitableSchemes(self, maxMember, minProfit, group, profit):
1616
dp[i][n][p] = (dp[i-1][n-group[i-1]][p-profit[i-1]] if p-profit[i-1]>=0 and n-group[i-1]>=0 else 0) + dp[i-1][n][p]
1717
if i==len(profit) and p>=minProfit and n<=maxMember: count += dp[i][n][p]
1818
return count
19-
19+
20+
21+
22+
"""
23+
dp[i][g][p] := consider only crime[:i] the scheme that can generate profit p using man power g.
24+
"""
25+
class Solution(object):
26+
def profitableSchemes(self, n, minProfit, group, profit):
27+
N = len(profit)
28+
29+
dp = [[[0]*(n+2) for _ in xrange(minProfit+1)] for _ in xrange(N+1)]
30+
dp[0][0][0] = 1
31+
32+
for i in xrange(1, N+1):
33+
for p in xrange(minProfit+1):
34+
for g in xrange(n+1):
35+
pi = profit[i-1]
36+
gi = group[i-1]
37+
38+
#considerting last round using p and g
39+
dp[i][p][g] += dp[i-1][p][g]
40+
dp[i][min(pi+p, minProfit)][min(gi+g, n+1)] += dp[i-1][p][g]
41+
42+
ans = 0
43+
for g in xrange(n+1):
44+
ans += dp[N][minProfit][g]
45+
return ans % (10**9 + 7)

problems/single-threaded-cpu.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def getOrder(self, tasks):
3+
ans = []
4+
tasks = sorted([(task[0], task[1], i) for i, task in enumerate(tasks)], reverse=True)
5+
pq = [] #tasks available
6+
now = 0
7+
8+
9+
while tasks or pq:
10+
#check if the task is availiable, if yes, add to pq
11+
while tasks and tasks[-1][0]<=now:
12+
startTime, processTime, i = tasks.pop()
13+
heapq.heappush(pq, (processTime, i))
14+
15+
if pq:
16+
processTime, i = heapq.heappop(pq)
17+
ans.append(i)
18+
now += processTime
19+
else:
20+
now = tasks[-1][0]
21+
22+
return ans

problems/target-sum.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,23 @@ def findTargetSumWays(self, nums, target):
5555
for t in xrange(minTarget, maxTarget+1):
5656
dp[i][t] = (dp[i-1][t-nums[i-1]] if t-nums[i-1]>=minTarget else 0) + (dp[i-1][t+nums[i-1]] if t+nums[i-1]<=maxTarget else 0)
5757

58-
return dp[N][target]
58+
return dp[N][target]
59+
60+
61+
"""
62+
dp[i][s] := considerting nums[:i] how many expressions can sum up to s.
63+
dp[0][0] = 1
64+
"""
65+
class Solution(object):
66+
def findTargetSumWays(self, nums, target):
67+
S = sum(nums)
68+
if not -S<=target<=S: return 0
69+
70+
dp = [{s:0 for s in xrange(-S, S+1)} for _ in xrange(len(nums)+1)]
71+
dp[0][0] = 1
72+
73+
for i in xrange(1, len(nums)+1):
74+
for s in xrange(-S, S+1):
75+
dp[i][s] = (dp[i-1][s+nums[i-1]] if s+nums[i-1]<=S else 0) + (dp[i-1][s-nums[i-1]] if s-nums[i-1]>=-S else 0)
76+
77+
return dp[-1][target]

0 commit comments

Comments
 (0)