Skip to content

Commit eca038f

Browse files
committed
add 3 new easy litcode algos yandex
1 parent f06b207 commit eca038f

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# https://leetcode.com/problems/intersection-of-two-arrays-ii/description/
2+
3+
# Given two integer arrays nums1 and nums2, return an array of their intersection.
4+
# Each element in the result must appear as many times
5+
# as it shows in both arrays and you may return the result in any order.
6+
7+
from typing import List
8+
9+
def intersect(nums1: List[int], nums2: List[int]) -> List[int]:
10+
length1 = len(nums1)
11+
length2 = len(nums2)
12+
intersection = []
13+
14+
if length1 < length2:
15+
nums1, nums2 = nums2, nums1
16+
17+
dict_of_values = dict()
18+
19+
for val in nums2:
20+
if dict_of_values.get(val):
21+
dict_of_values[val] += 1
22+
else:
23+
dict_of_values[val] = 1
24+
25+
for val in nums1:
26+
if dict_of_values.get(val):
27+
counter_tmp = dict_of_values.get(val)
28+
if counter_tmp > 0:
29+
intersection.append(val)
30+
dict_of_values[val] -= 1
31+
32+
return intersection
33+
34+
if __name__ == "__main__":
35+
print(intersect(nums1=[1,2,3,4,3,4,3], nums2=[3,4,3]))

InterviewAlgorithms/inPlaceSort.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://leetcode.com/problems/merge-sorted-array/
2+
# You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n,
3+
# representing the number of elements in nums1 and nums2 respectively.
4+
5+
from typing import List
6+
7+
def merge(nums1: List[int], m: int, nums2: List[int], n: int) -> None:
8+
"""
9+
Do not return anything, modify nums1 in-place instead.
10+
"""
11+
p1 = m - 1
12+
p2 = n - 1
13+
p = m+n -1
14+
15+
while p1 >= 0 and p2 >= 0:
16+
if nums1[p1] > nums2[p2]:
17+
nums1[p1], nums1[p] = nums1[p], nums1[p1]
18+
p1 -= 1
19+
else:
20+
nums1[p] = nums2[p2]
21+
p2 -= 1
22+
p -= 1
23+
24+
while p2 >= 0 and p>=0:
25+
nums1[p] = nums2[p2]
26+
p -= 1
27+
p2 -= 1
28+
29+
if __name__ == "__main__":
30+
merge(nums1=[4,5,6,0,0,0], m=3, nums2=[1,2,3], n=3)
31+
print("STOP")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import Optional
2+
3+
class ListNode:
4+
def __init__(self, val=0, next=None):
5+
self.val = val
6+
self.next = next
7+
8+
9+
def mergeTwoLists(list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
10+
if list1 is None:
11+
return list2
12+
13+
if list2 is None:
14+
return list1
15+
16+
if list1.val <= list2.val:
17+
list1.next = mergeTwoLists(list1.next, list2)
18+
return list1
19+
else:
20+
list2.next = mergeTwoLists(list1, list2.next)
21+
return list2
22+
23+
24+
25+
26+
if __name__ == "__main__":
27+
list1 = ListNode(4, ListNode(5, ListNode(6)))
28+
list2 = ListNode(1, ListNode(2, ListNode(3)))
29+
test = mergeTwoLists(list1, list2)
30+
print("stop")

0 commit comments

Comments
 (0)