Skip to content

Commit 9979ec9

Browse files
committed
First Upload at 3/8
1 parent 9aec337 commit 9979ec9

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

11. Container With Most Water.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
3+
def maxArea(self, height: List[int]) -> int:
4+
l = 0
5+
r = len(height) - 1
6+
width = len(height) - 1
7+
max_area = 0
8+
for w in range(width, 0, -1):
9+
max_area = max(max_area, w * min(height[l], height[r]))
10+
if height[l] < height[r]:
11+
l += 1
12+
else:
13+
r -= 1
14+
return max_area

12. Integer to Roman.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class Solution:
2+
3+
def intToRoman(self, num):
4+
"""
5+
:type num: int
6+
:rtype: str
7+
"""
8+
lst = []
9+
while num > 0:
10+
if num - 1000 >= 0:
11+
lst.append("M")
12+
num -= 1000
13+
continue
14+
if num - 900 >= 0:
15+
lst.append("CM")
16+
num -= 900
17+
continue
18+
if num - 500 >= 0:
19+
lst.append("D")
20+
num -= 500
21+
continue
22+
if num - 400 >= 0:
23+
lst.append("CD")
24+
num -= 400
25+
continue
26+
if num - 100 >= 0:
27+
lst.append("C")
28+
num -= 100
29+
continue
30+
if num - 90 >= 0:
31+
lst.append("XC")
32+
num -= 90
33+
continue
34+
if num - 50 >= 0:
35+
lst.append("L")
36+
num -= 50
37+
continue
38+
if num - 40 >= 0:
39+
lst.append("XL")
40+
num -= 40
41+
continue
42+
if num - 10 >= 0:
43+
lst.append("X")
44+
num -= 10
45+
continue
46+
if num - 9 >= 0:
47+
lst.append("IX")
48+
num -= 9
49+
continue
50+
if num - 5 >= 0:
51+
lst.append("V")
52+
num -= 5
53+
continue
54+
if num - 4 >= 0:
55+
lst.append("IV")
56+
num -= 4
57+
continue
58+
if num - 3 >= 0:
59+
lst.append("III")
60+
num -= 3
61+
continue
62+
if num - 2 >= 0:
63+
lst.append("II")
64+
num -= 2
65+
continue
66+
if num - 1 >= 0:
67+
lst.append("I")
68+
num -= 1
69+
continue
70+
return "".join(lst)

51. N-Queens.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
3+
def solveNQueens(self, n: int) -> List[List[str]]:
4+
res = []
5+
self.dfs([-1] * n, 0, [], res)
6+
return res
7+
8+
def dfs(self, nums, index, path, res):
9+
if index == len(nums):
10+
res.append(path)
11+
return
12+
for i in range(len(nums)):
13+
nums[index] = i
14+
if self.isValid(nums, index):
15+
temp = "." * len(nums)
16+
self.dfs(nums, index + 1, path + [temp[:i] + "Q" + temp[i + 1:]], res)
17+
18+
def isValid(self, nums, index):
19+
for i in range(index):
20+
if abs(nums[i] - nums[index]) == index - i or nums[i] == nums[index]:
21+
return False
22+
return True

52. N-Queens II.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
3+
def totalNQueens(self, n: int) -> int:
4+
res = [0]
5+
self.dfs([-1] * n, 0, [], res)
6+
return res[0]
7+
8+
def dfs(self, nums, index, path, res):
9+
if index == len(nums):
10+
res[0] += 1
11+
return
12+
for i in range(len(nums)):
13+
nums[index] = i
14+
if self.isValid(nums, index):
15+
temp = "." * len(nums)
16+
self.dfs(nums, index + 1, path + [temp[:i] + "Q" + temp[i + 1:]], res)
17+
18+
def isValid(self, nums, index):
19+
for i in range(index):
20+
if abs(nums[i] - nums[index]) == index - i or nums[i] == nums[index]:
21+
return False
22+
return True

9. Palindrome Number.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
3+
def isPalindrome(self, x: int) -> bool:
4+
if x < 0:
5+
return False
6+
s = str(x)
7+
return s == s[::-1]

0 commit comments

Comments
 (0)