File tree

10 files changed

+146
-1
lines changed

10 files changed

+146
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"python.pythonPath": "/Users/alvee/.pyenv/versions/3.8.3/bin/python",
2+
"python.pythonPath": "/usr/local/bin/python3",
33
"python.linting.pylintEnabled": true,
44
"python.linting.enabled": true,
55
"python.linting.mypyEnabled": false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def canJump(self, nums: List[int]) -> bool:
3+
max_reach = 0
4+
5+
for i in range(len(nums)):
6+
max_reach = max(nums[i] + i, max_reach)
7+
if max_reach == i:
8+
break
9+
10+
return max_reach >= len(nums) - 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def uniquePaths(self, m: int, n: int) -> int:
3+
dp = [[0] * n for _ in range(m)]
4+
5+
for r in range(m):
6+
dp[r][0] = 1
7+
8+
for c in range(n):
9+
dp[0][c] = 1
10+
11+
# list comprehension for above code
12+
# dp = [[1 if (c == 0 or r == 0) else 0 for c in range(n)] for r in range(m)]
13+
14+
for r in range(1, m):
15+
for c in range(1, n):
16+
dp[r][c] = dp[r-1][c] + dp[r][c-1]
17+
18+
return dp[m-1][n-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
# dp table:
4+
# index denotes the current amount
5+
# value denotes the minimum number of coins requried to reach amount
6+
dp = [float('inf') for _ in range(amount + 1)]
7+
# 0 amount is always possible with 0 coins
8+
dp[0] = 0
9+
10+
for coin in coins:
11+
for i in range(amount - coin + 1): # coin + i must be < amount
12+
cur_amount = i + coin # skips amounts that are less than current denomination
13+
cur_min = dp[i + coin]
14+
pos_min = dp[i] + 1 # current minimum coins + 1 coin of current denomination
15+
dp[cur_amount] = min(pos_min, cur_min)
16+
17+
if dp[amount] == float('inf'):
18+
return -1
19+
20+
return dp[amount]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def lengthOfLIS(self, nums: List[int]) -> int:
3+
dp = [1 for _ in range(len(nums))]
4+
max_len = 1
5+
6+
# i: current index
7+
# j: previous index
8+
for i in range(len(dp)):
9+
for j in range(i):
10+
# cond 1: nums[j] < nums[i] as we want an increasing sequence
11+
# cond 2: dp[j] >= dp[i] to choose LIS if it's bigger
12+
# without including the num at current index
13+
if nums[j] < nums[i] and dp[j] >= dp[i]:
14+
dp[i] = dp[j] + 1
15+
max_len = max(dp[i], max_len)
16+
17+
return max_len
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def isHappy(self, n: int) -> bool:
3+
seen = set()
4+
5+
while n not in seen:
6+
seen.add(n)
7+
n = self.findSquare(n)
8+
9+
return n == 1
10+
11+
def findSquare(self, n: int) -> int:
12+
square = 0
13+
14+
while n != 0:
15+
square += (n % 10) ** 2
16+
n = n // 10
17+
18+
return square
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def trailingZeroes(self, n: int) -> int:
3+
num_fives = 0
4+
5+
while n >= 5:
6+
num_fives += n // 5
7+
n = n // 5
8+
9+
return num_fives
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def titleToNumber(self, columnTitle: str) -> int:
3+
res = 0
4+
for char in columnTitle:
5+
diff = ord(char) - ord('A') + 1
6+
res = res * 26 + diff
7+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def myPow(self, x: float, n: int) -> float:
3+
if n < 0:
4+
return 1 / self.powRec(x, -n)
5+
return self.powRec(x, n)
6+
7+
def powRec(self, x, n):
8+
if n == 0:
9+
return 1
10+
elif n == 1:
11+
return x
12+
elif n % 2 == 0:
13+
return self.powRec(x * x, n / 2)
14+
else:
15+
return x * self.powRec(x, n - 1)
16+
17+
class Solution2:
18+
def myPow(self, x, n):
19+
if n < 0:
20+
x = 1 / x
21+
n = -n
22+
23+
res = 1
24+
while n:
25+
if n % 2 != 0:
26+
res *= x
27+
x *= x
28+
n //= 2
29+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def mySqrt(self, x: int) -> int:
3+
l = 1
4+
r = x
5+
6+
while l <= r:
7+
mid = (l + r) // 2
8+
9+
x_temp = mid * mid
10+
if x_temp == x:
11+
return mid
12+
elif x_temp > x:
13+
r = mid - 1
14+
else:
15+
l = mid + 1
16+
17+
return r

0 commit comments

Comments
 (0)