Skip to content

Commit f37ad63

Browse files
committed
First commit. Added problems 4, 6, 13, 20, 42.
0 parents  commit f37ad63

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

easy/13.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def romanToInt(self, s: str) -> int:
3+
nums = {
4+
'I':1, 'V':5, 'X':10, 'L':50,
5+
'C':100, 'D':500, 'M':1000
6+
}
7+
integer = 0
8+
for index in range(len(s)):
9+
try:
10+
if (n := nums[s[index]]) < nums[s[index+1]]:
11+
integer -= n
12+
else:
13+
integer += n
14+
except IndexError:
15+
integer += n
16+
return integer

easy/20.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
close = {'(':')', '[':']', '{':'}'}
4+
left = close.keys()
5+
queue = []
6+
for par in s:
7+
if par in left:
8+
queue.append(close[par])
9+
elif not queue or par != queue.pop():
10+
return False
11+
return True if not queue else False

hard/4.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
3+
nums = sorted(nums1 + nums2)
4+
size = len(nums)
5+
half = nums[(size-1)//2]
6+
if not size%2:
7+
return (half + nums[(size)//2])/2
8+
return half

hard/42.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def trap(self, height: List[int]) -> int:
3+
if len(height) == 1: return 0
4+
trapped = 0
5+
maxLeft = height[0]
6+
maxRight = max(height[1:])
7+
for idx in range(1, len(height) - 1):
8+
if height[idx-1] > maxLeft: maxLeft = height[idx-1]
9+
if height[idx] == maxRight: maxRight = max(height[(idx+1):])
10+
y = min(maxLeft, maxRight) - height[idx]
11+
if y > 0:
12+
trapped += y
13+
return trapped

medium/6.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def convert(self, s: str, numRows: int) -> str:
3+
if numRows == 1: return s
4+
rows = ['' for i in range(numRows)]
5+
x, it = 0, -1
6+
for char in s:
7+
rows[x] += char
8+
if not x%(numRows - 1): it = -it
9+
x += it
10+
return ''.join(rows)

0 commit comments

Comments
 (0)