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)) 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) diff --git a/LeetCode/intersection_two_array_two.py b/LeetCode/intersection_two_array_two.py new file mode 100644 index 0000000..5cc03cd --- /dev/null +++ b/LeetCode/intersection_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) 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)