From 2da891d90a76aa6e203cd057a28863d71dd413f9 Mon Sep 17 00:00:00 2001 From: WazedKhan Date: Tue, 2 Jul 2024 08:08:10 +0600 Subject: [PATCH 1/5] first solution without any Follow up --- LeetCode/insertion_two_array_two.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 LeetCode/insertion_two_array_two.py diff --git a/LeetCode/insertion_two_array_two.py b/LeetCode/insertion_two_array_two.py new file mode 100644 index 0000000..5cc03cd --- /dev/null +++ b/LeetCode/insertion_two_array_two.py @@ -0,0 +1,20 @@ +from typing import List +from collections import Counter + + +class Solution: + def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: + counter = Counter(nums1) + result = [] + for item in nums2: + if counter[item] > 0: + result.append(item) + counter[item] -= 1 + return result + + +nums1 = [2, 2] +nums2 = [9, 4, 9, 2, 2] + +solution = Solution().intersect(nums1, nums2) +print(solution) From d40a4796b9d182fa6ce79f02b949c618cfdd965a Mon Sep 17 00:00:00 2001 From: WazedKhan Date: Tue, 2 Jul 2024 08:11:53 +0600 Subject: [PATCH 2/5] fix: file name --- .../{insertion_two_array_two.py => intersection_two_array_two.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename LeetCode/{insertion_two_array_two.py => intersection_two_array_two.py} (100%) diff --git a/LeetCode/insertion_two_array_two.py b/LeetCode/intersection_two_array_two.py similarity index 100% rename from LeetCode/insertion_two_array_two.py rename to LeetCode/intersection_two_array_two.py From f77e2c5bcbbd46fdfedf1446b12bf217c59847ca Mon Sep 17 00:00:00 2001 From: WazedKhan Date: Tue, 2 Jul 2024 08:15:33 +0600 Subject: [PATCH 3/5] intersection of two array I --- LeetCode/intersection_two_array.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 LeetCode/intersection_two_array.py diff --git a/LeetCode/intersection_two_array.py b/LeetCode/intersection_two_array.py new file mode 100644 index 0000000..cf07c37 --- /dev/null +++ b/LeetCode/intersection_two_array.py @@ -0,0 +1,19 @@ +from typing import List + + +class Solution: + def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: + hash_set = set(nums1) + result = [] + for i in nums2: + if i in hash_set: + result.append(i) + hash_set.remove(i) + return result + + +nums1 = [2, 2] +nums2 = [9, 4, 9, 2, 2] + +solution = Solution().intersect(nums1, nums2) +print(solution) From e81e7f8787a170bc87f1495b22043ed2507d7e2e Mon Sep 17 00:00:00 2001 From: WazedKhan Date: Tue, 2 Jul 2024 08:51:30 +0600 Subject: [PATCH 4/5] follow up: if arrays are sorted -> two pointer --- LeetCode/intersection_two_array_two_sorted.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 LeetCode/intersection_two_array_two_sorted.py diff --git a/LeetCode/intersection_two_array_two_sorted.py b/LeetCode/intersection_two_array_two_sorted.py new file mode 100644 index 0000000..8eaff85 --- /dev/null +++ b/LeetCode/intersection_two_array_two_sorted.py @@ -0,0 +1,34 @@ +from typing import List +from collections import Counter + + +class Solution: + def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: + nums1.sort() + nums2.sort() + + pointer_one = 0 + pointer_two = 0 + result = [] + + while pointer_one < len(nums1) and pointer_two < len(nums2): + + if nums1[pointer_one] < nums2[pointer_two]: + pointer_two += 1 + + elif nums1[pointer_two] > nums2[pointer_one]: + pointer_one += 1 + + else: + result.append(nums1[pointer_one]) + pointer_two += 1 + pointer_one += 1 + + return result + + +nums1 = [4, 9, 5] +nums2 = [9, 4, 9, 8, 4] + +solution = Solution().intersect(nums1, nums2) +print(solution) From 50d72bc71cc9785918d2f39855a54cab4d32f77e Mon Sep 17 00:00:00 2001 From: WazedKhan Date: Sun, 7 Jul 2024 10:51:40 +0600 Subject: [PATCH 5/5] GFG easy one --- GeeksforGeeks/move_all_zeros.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 GeeksforGeeks/move_all_zeros.py diff --git a/GeeksforGeeks/move_all_zeros.py b/GeeksforGeeks/move_all_zeros.py new file mode 100644 index 0000000..17ece56 --- /dev/null +++ b/GeeksforGeeks/move_all_zeros.py @@ -0,0 +1,16 @@ +class Solution: + def pushZerosToEnd(self, arr, n): + count = 0 + for i in arr: + if i != 0: + arr[count] = i + count += 1 + while count < n: + arr[count] = 0 + count += 1 + return arr + + +arr = [3, 5, 0, 0, 4] +n = 5 +print(Solution().pushZerosToEnd(arr=arr, n=n))