Skip to content

Commit 56f0332

Browse files
committed
solved stk + intervals questions
1 parent 1b03080 commit 56f0332

File tree

6 files changed

+102
-3
lines changed

6 files changed

+102
-3
lines changed

Easy/228.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def summaryRanges(self, nums: List[int]) -> List[str]:
6+
if not nums: # check for empty list
7+
return []
8+
res = []
9+
l = 0
10+
for r in range(1, len(nums)):
11+
if nums[r] != nums[r - 1] + 1:
12+
res.append(
13+
f"{nums[l]}->{nums[r - 1]}" if l != r - 1 else f"{nums[r - 1]}"
14+
)
15+
l = r
16+
res.append(f"{nums[l]}->{nums[-1]}" if l != len(nums) - 1 else f"{nums[-1]}")
17+
return res

Hard/29.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def calculate(self, s: str) -> int:
3+
stack = []
4+
result = 0
5+
sign = 1
6+
num = 0
7+
8+
for ch in s:
9+
if ch.isdigit():
10+
num = num * 10 + int(ch)
11+
elif ch in "+-":
12+
result += sign * num
13+
num = 0
14+
sign = 1 if ch == "+" else -1
15+
elif ch == "(":
16+
stack.append(result)
17+
stack.append(sign)
18+
result = 0
19+
sign = 1
20+
elif ch == ")":
21+
result += sign * num
22+
num = 0
23+
result *= stack.pop()
24+
result += stack.pop()
25+
26+
return result + sign * num

Medium/260.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def singleNumber(self, nums: List[int]) -> List[int]:
6+
res = 0
7+
for num in nums:
8+
res = res ^ num
9+
10+
diff_bit = 1
11+
while not (res & diff_bit):
12+
diff_bit <<= 1
13+
14+
a, b = 0, 0
15+
for n in nums:
16+
if diff_bit & n:
17+
a = a ^ n
18+
else:
19+
b = b ^ n
20+
return [a, b]

Medium/452.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def findMinArrowShots(self, points: List[List[int]]) -> int:
6+
if not points:
7+
return 0
8+
9+
# Sort balloons by their ending coordinate
10+
points.sort(key=lambda x: x[1])
11+
arrows = 1
12+
end = points[0][1]
13+
14+
for x_start, x_end in points[1:]:
15+
if x_start > end:
16+
# Need a new arrow
17+
arrows += 1
18+
end = x_end # update arrow position
19+
20+
return arrows

Medium/71.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def simplifyPath(self, path: str) -> str:
3+
components = path.split("/")
4+
st = []
5+
6+
for comp in components:
7+
if comp == "" or comp == ".":
8+
continue
9+
10+
if comp == "..":
11+
if st:
12+
st.pop()
13+
else:
14+
st.append(comp)
15+
16+
return "/" + "/".join(st)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ This repository contains my solutions to various LeetCode problems. Each solutio
1717

1818
### Easy Problems
1919

20-
- **Total Solved:** [45]
20+
- **Total Solved:** [46]
2121

2222
### Medium Problems
2323

24-
- **Total Solved:** [116]
24+
- **Total Solved:** [118]
2525

2626
### Hard Problems
2727

28-
- **Total Solved:** [28]
28+
- **Total Solved:** [29]
2929

3030
## 🛠️ Languages Used
3131

0 commit comments

Comments
 (0)