Skip to content

Commit cbd02f2

Browse files
added 056 merge interval & 100 same tree
1 parent 8be1c47 commit cbd02f2

File tree

4 files changed

+106
-4
lines changed

4 files changed

+106
-4
lines changed

README.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,43 @@
44
https://sebinsua.com/algorithmic-bathwater
55
https://sebinsua.com/assets/posts/algorithmic-bathwater/algorithmic-bathwater-1.svg
66

7+
8+
9+
10+
# 2405: Optimal Partition of String
11+
12+
Intuition: map to store the string char, if char already exists in map means with hit a non unique char, increase the count and reset the map
13+
14+
Approach:
15+
1. Map to track the unique char, count to track the count, set it to 1
16+
2. for each char
17+
1. if char in map, count increment ans reset the map
18+
2. for all, set char in map with True or index
19+
20+
21+
# 056 Merge Interval
22+
Intuition: Sort the intervals on first element. For each interval, if last's[1] is less then curr[0] merge the intervals
23+
24+
Approach:
25+
26+
1. Sort the intervals on first element
27+
2. for each interval
28+
1. if no merge interval in result so far or last interval in result is out of current interval
29+
1. add into result
30+
2. else merge interval // edge case - current interval might be within boundary of first interval
31+
3. return the result
32+
33+
----
34+
735
# 763 Partition Labels
836
Intuition: store last occurrence index of the char in map and two pointer to find the break points to get the partitions
937

1038
Approach:
1139
1. map to store the last occurrence of the each index in pam
12-
2. Two variables break point, count and result array
13-
3. for loop till len of string
14-
1. get the index of char from map
15-
2. store the max of index and break point //this will help us to find the appropriate break point
40+
2. Variables break point, count and result array
41+
3. for loop till the len of the string
42+
1. get the last index of char from map
43+
2. store the max of index and break point in break point //this will help us to find the appropriate break point
1644
3. count++ for current partition
1745
4. if index is equal to break point
1846
1. store count value in result array

leetcode-py/056-merge-interval.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
3+
res = []
4+
5+
intervals.sort(key=lambda x:x[0])
6+
7+
for interval in intervals:
8+
if not res or res[-1][1]<interval[0]:
9+
res.append(interval)
10+
else:
11+
res[-1][1] = max(res[-1][1], interval[1])
12+
13+
return res

leetcode-py/100-same-tree.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
9+
if not p and not q:
10+
return True
11+
12+
if not p or not q:
13+
return False
14+
15+
if p.val == q.val:
16+
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
17+
else:
18+
return False

leetcode-py/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,46 @@
1010
output `{'w': [], 'r': [], 't': [], 'f': [], 'e': []}`
1111

1212
----
13+
14+
Loop over list and get the index and value both
15+
`for i, interval in enumerate(intervals):`
16+
17+
18+
Loop over list from non zero starting point
19+
`for j in range(i+1, len(intervals)):`
20+
21+
----
22+
Sorting
23+
- `sorted()` return the new sorted list without modifying existing
24+
- `my_list.sort()` modify existing list
25+
- Reverse sorting pass `reverse=True`
26+
- custom sorting pass `key=lambda x:x[0]`
27+
```
28+
my_list = [(1, 'apple'), (3, 'banana'), (2, 'orange')]
29+
30+
# Sort based on the second element of each tuple (the fruit name)
31+
sorted_list = sorted(my_list, key=lambda x: x[1])
32+
print(sorted_list)
33+
# Output: [(1, 'apple'), (3, 'banana'), (2, 'orange')]
34+
35+
# Sort based on the first element of each tuple (the number)
36+
my_list.sort(key=lambda x: x[0])
37+
print(my_list)
38+
# Output: [(1, 'apple'), (2, 'orange'), (3, 'banana')]
39+
```
40+
41+
-----
42+
maps
43+
44+
- How to check keys in maps
45+
`key in map`
46+
47+
- How to check values in map
48+
`value in map.values()`
49+
50+
----
51+
List
52+
53+
- How to define an array of 26 size with all the element `0`
54+
`unique = [0] * 26`
55+

0 commit comments

Comments
 (0)