diff --git a/emd_challenge/__pycache__/challenge_10.cpython-313.pyc b/emd_challenge/__pycache__/challenge_10.cpython-313.pyc new file mode 100644 index 0000000..73195f3 Binary files /dev/null and b/emd_challenge/__pycache__/challenge_10.cpython-313.pyc differ diff --git a/emd_challenge/__pycache__/challenge_8.cpython-313.pyc b/emd_challenge/__pycache__/challenge_8.cpython-313.pyc index 680db61..8b620f8 100644 Binary files a/emd_challenge/__pycache__/challenge_8.cpython-313.pyc and b/emd_challenge/__pycache__/challenge_8.cpython-313.pyc differ diff --git a/emd_challenge/__pycache__/challenge_9.cpython-313.pyc b/emd_challenge/__pycache__/challenge_9.cpython-313.pyc new file mode 100644 index 0000000..8dfd3eb Binary files /dev/null and b/emd_challenge/__pycache__/challenge_9.cpython-313.pyc differ diff --git a/emd_challenge/challenge_10.py b/emd_challenge/challenge_10.py new file mode 100644 index 0000000..fc93b4e --- /dev/null +++ b/emd_challenge/challenge_10.py @@ -0,0 +1,69 @@ +""" +54. Spiral Matrix + +Given an m x n matrix, return all elements of the matrix in spiral order. + +Example 1: +Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] +Output: [1,2,3,6,9,8,7,4,5] + +Example 2: +Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] +Output: [1,2,3,4,8,12,11,10,9,5,6,7] +""" + +from typing import List + +""" +[ + [1,2,3], + [4,5,6], + [7,8,9] +] + +00,01,02 +10,11,12 +20,21,22 + +""" + + +def spiralOrder(matrix: List[List[int]]) -> List[int]: + if not matrix or not matrix[0]: + return [] + n = len(matrix) + m = len(matrix[0]) + result = [] + top, bottom = 0, n - 1 + left, right = 0, m - 1 + + """ + [ + [1,2,3], + [4,5,6], + [7,8,9] + ] + + """ + + # left -> right -> bottom -> left -> top + while top <= bottom and left <= right: + for i in range(left, right + 1): + result.append(matrix[top][i]) + top += 1 + + for i in range(top, bottom + 1): + result.append(matrix[i][right]) + right -= 1 + + if top <= bottom: + for i in range(right, left - 1, -1): + result.append(matrix[bottom][i]) + bottom -= 1 + + if left <= right: + for i in range(bottom, top - 1, -1): + result.append(matrix[i][left]) + left += 1 + + return result diff --git a/emd_challenge/challenge_9.py b/emd_challenge/challenge_9.py new file mode 100644 index 0000000..7fd4f63 --- /dev/null +++ b/emd_challenge/challenge_9.py @@ -0,0 +1,70 @@ +""" +18. 4Sum + +Given an array nums of n integers, return an array of all the unique +quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: +0 <= a, b, c, d < n +a, b, c, and d are distinct. +nums[a] + nums[b] + nums[c] + nums[d] == target +You may return the answer in any order. + +Example 1: +Input: nums = [1,0,-1,0,-2,2], target = 0 +Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]] + +Example 2: +Input: nums = [2,2,2,2,2], target = 8 +Output: [[2,2,2,2]] + +Constraints: +1 <= nums.length <= 200 +-109 <= nums[i] <= 109 +-109 <= target <= 109 +""" + +from typing import List + + +def fourSum(nums: List[int], target: int) -> List[List[int]]: + + n = len(nums) + result = [] + hash_set = set() + + nums.sort() + # [1,0,-1,0,-2,2] + # [-2, -1, 0, 0, 1, 2] + for i in range(n - 3): + for j in range(i + 1, n - 2): + left = j + 1 + right = n - 1 + while left < right: + four_sum = nums[i] + nums[j] + nums[left] + nums[right] + + if four_sum == target: + + sort = (nums[i], nums[j], nums[left], nums[right]) + left += 1 + right -= 1 + + result.append(sort) + elif four_sum > target: + right -= 1 + else: + left += 1 + + return result + + # n = len(nums) + # result = [] + + # for i in range(n - 3): + # for j in range(i + 1, n - 2): + # for k in range(j + 1, n - 1): + # for x in range(k + 1, n): + # if nums[i] + nums[j] + nums[k] + nums[x] == target: + # sort = sorted([nums[i], nums[j], nums[k], nums[x]]) + # if sort not in result: + # result.append(sort) + + # return result diff --git a/main.py b/main.py index aa4e41e..2dd5156 100644 --- a/main.py +++ b/main.py @@ -17,11 +17,13 @@ from arr.trapping_rain_water import trap_app from arr.two_sum_11 import two_sum_brute, two_sum_two_pointer from emd_challenge.challenge_1 import longestCommonPrefix +from emd_challenge.challenge_10 import spiralOrder from emd_challenge.challenge_2 import missingInteger from emd_challenge.challenge_3 import removeDuplicates from emd_challenge.challenge_5 import lengthOfLastWord from emd_challenge.challenge_6 import moveZeroes from emd_challenge.challenge_8 import addBinary +from emd_challenge.challenge_9 import fourSum from stack.asteroid_collision_735 import asteroidCollision from stack.daily_temparature_739 import dailyTemperatures_2 from stack.first_greater_element_2 import nextGreaterElements @@ -44,7 +46,7 @@ # sortColors_app2([2, 0, 2, 1, 1, 0]) -ans = addBinary(a="11", b="1") +ans = spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(ans) # stockSpanner = StockSpanner2()