Skip to content

Commit 48e14b0

Browse files
author
Chris Wu
committed
no message
1 parent f3534db commit 48e14b0

5 files changed

+100
-0
lines changed

problems/delete-and-earn.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import collections
2+
3+
class Solution(object):
4+
def deleteAndEarn(self, nums):
5+
if not nums: return 0
6+
m = min(nums)
7+
M = max(nums)
8+
c = collections.Counter(nums)
9+
10+
prev = 0
11+
curr = 0
12+
for n in xrange(m, M+1):
13+
prev, curr = curr, max(prev+n*c[n], curr)
14+
return curr

problems/domino-and-tromino-tiling.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def numTilings(self, N):
3+
H = [[0, 0] for _ in xrange(N+1)]
4+
H[0][0] = 1
5+
H[1][0] = 1
6+
7+
for i in xrange(2, N+1):
8+
H[i][0] = (H[i-1][0] + H[i-2][0] + H[i-1][1]*2) % 1000000007
9+
H[i][1] = (H[i-2][0] + H[i-1][1]) % 1000000007
10+
11+
return H[N][0]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def minSwap(self, A, B):
3+
keep = [float('inf') for _ in xrange(len(A))]
4+
swap = [float('inf') for _ in xrange(len(A))]
5+
6+
keep[0] = 0
7+
swap[0] = 1
8+
9+
for i in xrange(1, len(A)):
10+
11+
if A[i]>A[i-1] and B[i]>B[i-1]:
12+
keep[i] = keep[i-1]
13+
swap[i] = swap[i-1]+1
14+
15+
if A[i]>B[i-1] and B[i]>A[i-1]:
16+
keep[i] = min(keep[i], swap[i-1])
17+
swap[i] = min(swap[i], keep[i-1]+1)
18+
19+
return min(keep[-1], swap[-1])

problems/perfect-squares.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#Recursive
2+
class Solution(object):
3+
def numSquares(self, n):
4+
def helper(n):
5+
if not n: return 0
6+
if n in history: return history[n]
7+
8+
elements = [e**2 for e in range(2, int(n**0.5)+1)]
9+
ans = n
10+
11+
for e in reversed(elements):
12+
count_of_element = int(n/e)
13+
n_approximate = count_of_element*e
14+
ans = min(ans, count_of_element+helper(n-n_approximate))
15+
16+
history[n] = ans
17+
return history[n]
18+
19+
history = {1:1, 2:2, 3:3}
20+
return helper(n)
21+
22+
#DP
23+
class Solution(object):
24+
def numSquares(self, N):
25+
squares = [s**2 for s in range(2, int(N**0.5)+1)]
26+
27+
dp = [n for n in xrange(N+1)]
28+
29+
for n in xrange(N+1):
30+
for square in squares:
31+
if square>n:
32+
break
33+
elif square==n:
34+
dp[n] = 1
35+
break
36+
else:
37+
dp[n] = min(dp[n], dp[square]+dp[n-square])
38+
return dp[N]
39+

problems/word-break.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def wordBreak(self, s, wordDict):
3+
def helper(s_left):
4+
if not s_left: return True
5+
if s_left in history: return history[s_left]
6+
7+
for word in wordDict:
8+
if len(s_left)<len(word): continue
9+
if s_left[:len(word)]==word and helper(s_left[len(word):]):
10+
history[s_left] = True
11+
return history[s_left]
12+
13+
history[s_left] = False
14+
return history[s_left]
15+
16+
history = {}
17+
return helper(s)

0 commit comments

Comments
 (0)