Skip to content

Commit 54905cf

Browse files
committed
Added 4Sum to Two Pointers
1 parent f14eba5 commit 54905cf

File tree

2 files changed

+270
-2
lines changed

2 files changed

+270
-2
lines changed

.ipynb_checkpoints/Two Pointers-checkpoint.ipynb

+135-1
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,144 @@
258258
"};"
259259
]
260260
},
261+
{
262+
"cell_type": "markdown",
263+
"id": "cf21e695",
264+
"metadata": {},
265+
"source": [
266+
"## 4Sum\n",
267+
"\n",
268+
"* https://leetcode.com/problems/4sum/description/\n",
269+
"***\n",
270+
"* Time Complexity: O($n^{k - 1}$) = O($n^{3}$), where k = the number of sums we have\n",
271+
" - have 2 for loops that handle find us our a and b, O(n$^{2}$)\n",
272+
" - we then do normal Two sum with the rest of the variables c and d ,O(n)\n",
273+
" - multiply them to get O($n^{3}$)\n",
274+
"* Space Complexity: O(n)\n",
275+
" - requires space for the res array\n",
276+
"***\n",
277+
"* similar to 3Sum, we can reduce this problem down to just picking 2 variables and doing Two sum with the rest\n",
278+
" - 3Sum = pick a and do TwoSum(b, c)\n",
279+
" - 4Sum = pick a, and do TwoSum(c, d)\n",
280+
"* since 3Sum uses only 1 loop to find a, we need 2 loops to find a and b\n",
281+
" - there is a bit of nuance with how we handle duplicates though\n",
282+
" - in this problem, we are allowed to have all values of a, b, c, and d be the same\n",
283+
" * so if we have nums = [2, 2, 2, 2, 2], target = 8, we can have our res arr be [[2, 2, 2, 2]]\n",
284+
" - the condition of nums[a] === nums[a - 1] is the same. we want to skip that\n",
285+
" - but the 2nd condition we want to consider is when nums[b] === nums[b - 1]\n",
286+
" * since a === b is ok for the first iteration, we check if b - a > 1\n",
287+
" * so if they're right next to each other at the start of the loop, that's ok but subsequent values of b after that should not be equal"
288+
]
289+
},
290+
{
291+
"cell_type": "code",
292+
"execution_count": 1,
293+
"id": "6876328e",
294+
"metadata": {},
295+
"outputs": [],
296+
"source": [
297+
"/**\n",
298+
" * @param {number[]} nums\n",
299+
" * @param {number} target\n",
300+
" * @return {number[][]}\n",
301+
" */\n",
302+
"var fourSum = function(nums, target) {\n",
303+
" const res = [];\n",
304+
"\n",
305+
" if (nums.length < 4) return res;\n",
306+
"\n",
307+
" // easier when sorted\n",
308+
" // O(nlogn)\n",
309+
" nums.sort((a, b) => a - b);\n",
310+
"\n",
311+
" for (let a = 0; a < nums.length - 3; a++) {\n",
312+
" \n",
313+
" // don't want duplicate sets\n",
314+
" if (a > 0 && nums[a] === nums[a - 1]) continue;\n",
315+
"\n",
316+
" for (let b = a + 1; b < nums.length - 2; b++) {\n",
317+
" \n",
318+
" // a, b, c, d have to be distinct for each set\n",
319+
" // but not in a set. so you can have an arr with [2, 2, 2, 2] for target = 8\n",
320+
" // so it is fine that we allow duplicate values once but for further values of b\n",
321+
" // it should NOT match with a\n",
322+
" // if we just did if b > 1, then we miss out on that dupe\n",
323+
" if ((b - a > 1) && nums[b] === nums[b - 1]) continue;\n",
324+
" let c = b + 1;\n",
325+
" let d = nums.length - 1;\n",
326+
"\n",
327+
" while (c < d) {\n",
328+
" let sum = nums[a] + nums[b] + nums[c] + nums[d];\n",
329+
"\n",
330+
" if (sum === target) {\n",
331+
" res.push([nums[a], nums[b], nums[c], nums[d]]);\n",
332+
"\n",
333+
" // gets rid of duplicates\n",
334+
" while (c < d && nums[c] === nums[c + 1]) c++\n",
335+
" while (c < d && nums[d] === nums[d - 1]) d--;\n",
336+
"\n",
337+
" c++;\n",
338+
" d--;\n",
339+
" }\n",
340+
" else if (sum < target) {\n",
341+
" c++;\n",
342+
" }\n",
343+
" else {\n",
344+
" d--;\n",
345+
" }\n",
346+
" }\n",
347+
" }\n",
348+
"\n",
349+
" }\n",
350+
"\n",
351+
" return res;\n",
352+
"};"
353+
]
354+
},
355+
{
356+
"cell_type": "code",
357+
"execution_count": null,
358+
"id": "b1b91a15",
359+
"metadata": {},
360+
"outputs": [],
361+
"source": []
362+
},
363+
{
364+
"cell_type": "code",
365+
"execution_count": null,
366+
"id": "7a1a819a",
367+
"metadata": {},
368+
"outputs": [],
369+
"source": []
370+
},
371+
{
372+
"cell_type": "code",
373+
"execution_count": null,
374+
"id": "d27e9157",
375+
"metadata": {},
376+
"outputs": [],
377+
"source": []
378+
},
379+
{
380+
"cell_type": "code",
381+
"execution_count": null,
382+
"id": "6c1239bb",
383+
"metadata": {},
384+
"outputs": [],
385+
"source": []
386+
},
387+
{
388+
"cell_type": "code",
389+
"execution_count": null,
390+
"id": "9137758a",
391+
"metadata": {},
392+
"outputs": [],
393+
"source": []
394+
},
261395
{
262396
"cell_type": "code",
263397
"execution_count": null,
264-
"id": "e2e3ca8a",
398+
"id": "c6f820e7",
265399
"metadata": {},
266400
"outputs": [],
267401
"source": []

Two Pointers.ipynb

+135-1
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,144 @@
258258
"};"
259259
]
260260
},
261+
{
262+
"cell_type": "markdown",
263+
"id": "cf21e695",
264+
"metadata": {},
265+
"source": [
266+
"## 4Sum\n",
267+
"\n",
268+
"* https://leetcode.com/problems/4sum/description/\n",
269+
"***\n",
270+
"* Time Complexity: O($n^{k - 1}$) = O($n^{3}$), where k = the number of sums we have\n",
271+
" - have 2 for loops that handle find us our a and b, O(n$^{2}$)\n",
272+
" - we then do normal Two sum with the rest of the variables c and d ,O(n)\n",
273+
" - multiply them to get O($n^{3}$)\n",
274+
"* Space Complexity: O(n)\n",
275+
" - requires space for the res array\n",
276+
"***\n",
277+
"* similar to 3Sum, we can reduce this problem down to just picking 2 variables and doing Two sum with the rest\n",
278+
" - 3Sum = pick a and do TwoSum(b, c)\n",
279+
" - 4Sum = pick a, and do TwoSum(c, d)\n",
280+
"* since 3Sum uses only 1 loop to find a, we need 2 loops to find a and b\n",
281+
" - there is a bit of nuance with how we handle duplicates though\n",
282+
" - in this problem, we are allowed to have all values of a, b, c, and d be the same\n",
283+
" * so if we have nums = [2, 2, 2, 2, 2], target = 8, we can have our res arr be [[2, 2, 2, 2]]\n",
284+
" - the condition of nums[a] === nums[a - 1] is the same. we want to skip that\n",
285+
" - but the 2nd condition we want to consider is when nums[b] === nums[b - 1]\n",
286+
" * since a === b is ok for the first iteration, we check if b - a > 1\n",
287+
" * so if they're right next to each other at the start of the loop, that's ok but subsequent values of b after that should not be equal"
288+
]
289+
},
290+
{
291+
"cell_type": "code",
292+
"execution_count": 1,
293+
"id": "6876328e",
294+
"metadata": {},
295+
"outputs": [],
296+
"source": [
297+
"/**\n",
298+
" * @param {number[]} nums\n",
299+
" * @param {number} target\n",
300+
" * @return {number[][]}\n",
301+
" */\n",
302+
"var fourSum = function(nums, target) {\n",
303+
" const res = [];\n",
304+
"\n",
305+
" if (nums.length < 4) return res;\n",
306+
"\n",
307+
" // easier when sorted\n",
308+
" // O(nlogn)\n",
309+
" nums.sort((a, b) => a - b);\n",
310+
"\n",
311+
" for (let a = 0; a < nums.length - 3; a++) {\n",
312+
" \n",
313+
" // don't want duplicate sets\n",
314+
" if (a > 0 && nums[a] === nums[a - 1]) continue;\n",
315+
"\n",
316+
" for (let b = a + 1; b < nums.length - 2; b++) {\n",
317+
" \n",
318+
" // a, b, c, d have to be distinct for each set\n",
319+
" // but not in a set. so you can have an arr with [2, 2, 2, 2] for target = 8\n",
320+
" // so it is fine that we allow duplicate values once but for further values of b\n",
321+
" // it should NOT match with a\n",
322+
" // if we just did if b > 1, then we miss out on that dupe\n",
323+
" if ((b - a > 1) && nums[b] === nums[b - 1]) continue;\n",
324+
" let c = b + 1;\n",
325+
" let d = nums.length - 1;\n",
326+
"\n",
327+
" while (c < d) {\n",
328+
" let sum = nums[a] + nums[b] + nums[c] + nums[d];\n",
329+
"\n",
330+
" if (sum === target) {\n",
331+
" res.push([nums[a], nums[b], nums[c], nums[d]]);\n",
332+
"\n",
333+
" // gets rid of duplicates\n",
334+
" while (c < d && nums[c] === nums[c + 1]) c++\n",
335+
" while (c < d && nums[d] === nums[d - 1]) d--;\n",
336+
"\n",
337+
" c++;\n",
338+
" d--;\n",
339+
" }\n",
340+
" else if (sum < target) {\n",
341+
" c++;\n",
342+
" }\n",
343+
" else {\n",
344+
" d--;\n",
345+
" }\n",
346+
" }\n",
347+
" }\n",
348+
"\n",
349+
" }\n",
350+
"\n",
351+
" return res;\n",
352+
"};"
353+
]
354+
},
355+
{
356+
"cell_type": "code",
357+
"execution_count": null,
358+
"id": "b1b91a15",
359+
"metadata": {},
360+
"outputs": [],
361+
"source": []
362+
},
363+
{
364+
"cell_type": "code",
365+
"execution_count": null,
366+
"id": "7a1a819a",
367+
"metadata": {},
368+
"outputs": [],
369+
"source": []
370+
},
371+
{
372+
"cell_type": "code",
373+
"execution_count": null,
374+
"id": "d27e9157",
375+
"metadata": {},
376+
"outputs": [],
377+
"source": []
378+
},
379+
{
380+
"cell_type": "code",
381+
"execution_count": null,
382+
"id": "6c1239bb",
383+
"metadata": {},
384+
"outputs": [],
385+
"source": []
386+
},
387+
{
388+
"cell_type": "code",
389+
"execution_count": null,
390+
"id": "9137758a",
391+
"metadata": {},
392+
"outputs": [],
393+
"source": []
394+
},
261395
{
262396
"cell_type": "code",
263397
"execution_count": null,
264-
"id": "e2e3ca8a",
398+
"id": "c6f820e7",
265399
"metadata": {},
266400
"outputs": [],
267401
"source": []

0 commit comments

Comments
 (0)