Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file modified emd_challenge/__pycache__/challenge_8.cpython-313.pyc
Binary file not shown.
Binary file not shown.
69 changes: 69 additions & 0 deletions emd_challenge/challenge_10.py
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions emd_challenge/challenge_9.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down