From 6e8a94ec7a5f6a9fb734b9771ff41ff3837ae419 Mon Sep 17 00:00:00 2001 From: JiyadAhammad Date: Mon, 17 Aug 2026 21:59:34 +0530 Subject: [PATCH 1/2] 18. 4Sum --- .../__pycache__/challenge_8.cpython-313.pyc | Bin 1432 -> 1398 bytes .../__pycache__/challenge_9.cpython-313.pyc | Bin 0 -> 1678 bytes emd_challenge/challenge_9.py | 70 ++++++++++++++++++ main.py | 3 +- 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 emd_challenge/__pycache__/challenge_9.cpython-313.pyc create mode 100644 emd_challenge/challenge_9.py diff --git a/emd_challenge/__pycache__/challenge_8.cpython-313.pyc b/emd_challenge/__pycache__/challenge_8.cpython-313.pyc index 680db61c59d14f0526c50690a3d16e3202c4d79b..8b620f838fdf7c217947d17e16d79bc81f408e40 100644 GIT binary patch delta 210 zcmbQi{f&$FGcPX}0}yO})tF_xk@o^4W7Fh&j2Vo@liit$Btlt(SV6L=Aew#hb0$mM zBnF01=3pkPJq!$qObiUZSwKFE22?3nq?myrm?el4RX2zi%{@7m*^q50 zvmrnGE#{omyj#qfc_p`43yLN;Fz?_J2da<+;^LE&omlF*o^bGXr1tPU;NY8lj3rc& l4=7UvQnHevNCLzb0}{75Y;yBcN^?@}ilisou$r-eWB>_;E(QPq 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 0000000000000000000000000000000000000000..0fcd3a5d33009736aa6c64c490114bc3c311b808 GIT binary patch literal 1678 zcmZWp&rcgi6rT02f3TGNuz?Uvm8s;Uaq64W?W9U04-co*Bmo;9<( zP25X~6t$KjRkqY9aHF?yOK!=3Q53bZRyiQG7j8r(=l0Fo3p9Q9&VDoRoA1qgGqXp1 zeKG?3@%^Lqe|!l2#XGJAy5MLUz*Ce$45fsWXGyr}VIGW9;;Ls=^nx!Xt%@lx^tydM zHsMS8hS9pjq@q6>Am=;?$%$)Ycp_z0WqG0ey+$#CKVzhhsZ}+wzJ@6-Q&THy%uq1X zOpBf3fSXh**eq$-qU8-slQ%4)GOJe6Oao`RM3&?fM=N{J1qBzVxg0jELJ0(jnUv#r zdIl2(-&1fw!784{R6gz!s=^Rq8dl4OS*8VZOz!IAt8M@n6>=_8b9iP3n}iiLQ@*QP zxC*It)8NY{)OesVND)vS>rB_*amb^i zjAw9MUa?Gvn$3=mb54R2IF7^5fqY(EN#t@T5yt-)0dzaR)9Tkwsk?4a`C6xj$p~za zk(3j>e7q!M6^$0n67P=38 zjKZyOHACpePY6-(DTX)Y83isdMTgm4B`~YN^rd|rKlKB{!x=P?d6*0$qylrfAEAtw zFDB#TEmz9;k5Rxyv?tR_W)LD@{FmS1W*pvxMCG&}V)Q`_e>wo@0*{s!Z}))gN0ye| zoG@BB2kO6qI^6n`c`M!%wa$tOU-4adbcA{lM$3)PJ1s^~RJwJ*OMSqrf53|n_1Yqw z2wOBX*il1grtM{f7B$;PYBi0j(OxD2kMSgJf%1Jetcqy|O2jDT`7EJq3+uL2(bo9f zaTe!6B)hG;1jc#Vrc_D8L{hAih$m^@Whp#t5WoT=*waCH6t#4a5oAdkq!_Ssm%R7-j`af?EeEMeK zO4ro_SC@PkMxhJOlp{Y1UVbN{GsA~h&?mvoZ z5pKOxkX^)vVSo+s(ZF<7SFMWn6}tpoeg+JFpAJPq5Z-wP1?fW^2}6gz5h2)&G>umD Z5CPmtIj`q;UPN9PFQdEp4}8>-{THx#s4@Tm literal 0 HcmV?d00001 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..365b1fa 100644 --- a/main.py +++ b/main.py @@ -22,6 +22,7 @@ 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 +45,7 @@ # sortColors_app2([2, 0, 2, 1, 1, 0]) -ans = addBinary(a="11", b="1") +ans = fourSum([2, 2, 2, 2, 2], 8) print(ans) # stockSpanner = StockSpanner2() From 4e61f01b5db2a109dfe431d5ecb2a1c90ab6633a Mon Sep 17 00:00:00 2001 From: JiyadAhammad Date: Tue, 18 Aug 2026 08:02:16 +0530 Subject: [PATCH 2/2] 54. Spiral Matrix --- .../__pycache__/challenge_10.cpython-313.pyc | Bin 0 -> 1614 bytes .../__pycache__/challenge_9.cpython-313.pyc | Bin 1678 -> 1678 bytes emd_challenge/challenge_10.py | 69 ++++++++++++++++++ main.py | 3 +- 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 emd_challenge/__pycache__/challenge_10.cpython-313.pyc create mode 100644 emd_challenge/challenge_10.py 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 0000000000000000000000000000000000000000..73195f3ef9b08dce6e5bb6b921191fa705228900 GIT binary patch literal 1614 zcma)5-*4Jh6u!phM+`K9By3Hq%u=!}EG~u=(k5CbF(uj3mR1?Dl!_tb1qYmh@obZY zH>ACQ@-X%=4Kz)oZ}e^dO83B!6-`LpOWvrWK5yq5!_rOJdMw|2?z!iD=YHqhd*Suc z2;^D(nl-82drT@7VOQDPX*LyQ zW9%Btci6k^dOA%%F6*Z2(1BfJ*J0xh3vTJZ5m&E7WTSuuh!AoYv~@NTW+M?cvifi6 zC;&%R&D}6$F@w_dIj(PzFsO%;rW=kSf5S24e*i@r3ob7S|HZNm_ku^u)|LSMMJR<& zV#IT@6=fsh+9h^GL{3(?di{H?b{ zHZghU706CNeg}}97MsW$9FAcZs+c5W7O($dVgFASb}N_Ns#oZZTzhYwWyr{J1tW)5 zD~H9%Iqna1A^|<($wd^T9;vu7=eYOhe}V)_gDi-$VOIq?CmIexQA9Z#oKPw7FbLTo ziun3z<$`XIdPy-HUzT*eR5Yl9c&I~|l*{V|A!!rf&y2))$nlz}YTS`j5wb#FC>Dh* zw<&5*0fH-5n6FFfQ!c9s8Kt7Ux5&NaSDc1Nv8W zSB?AwytS@x;j(A2z4c=5!>(rqyDs)!-iEJE*X(`BaPMq(t{Ln2o-KDi>CSQuqBj-z z!TX)JdDxOWyD#Rs?o=3}0^z>L-w^5#YTJGHpQj0!GcBQr^H|hdHmJH1=I*V3fc{4CX+24WHN?BuPBn7Q}LxXNJ-YsT0Tc=01WC3 zwz@tlma^r7_^}Fa5aC;*-G*U65(M$e<{_xpHxcpffN~Sm;N}wHZ&J;@_Co7PYjS{K Q+1dPM`uWalteT*I0Zp}L6aWAK literal 0 HcmV?d00001 diff --git a/emd_challenge/__pycache__/challenge_9.cpython-313.pyc b/emd_challenge/__pycache__/challenge_9.cpython-313.pyc index 0fcd3a5d33009736aa6c64c490114bc3c311b808..8dfd3eb750c81d547c7bdc43e2207ec5f4ec4b5f 100644 GIT binary patch delta 18 XcmeC~_ 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/main.py b/main.py index 365b1fa..2dd5156 100644 --- a/main.py +++ b/main.py @@ -17,6 +17,7 @@ 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 @@ -45,7 +46,7 @@ # sortColors_app2([2, 0, 2, 1, 1, 0]) -ans = fourSum([2, 2, 2, 2, 2], 8) +ans = spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(ans) # stockSpanner = StockSpanner2()