You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/content/part02/array.asc
+122-8
Original file line number
Diff line number
Diff line change
@@ -298,13 +298,13 @@ To sum up, the time complexity of an array is:
298
298
299
299
==== Array Patterns for Solving Interview Questions
300
300
301
-
Many programming problems involves manipulating arrays. Here are some patterns that can help you improve your problemsolving skills.
301
+
Many programming problems involve manipulating arrays. Here are some patterns that can help you improve your problem-solving skills.
302
302
303
303
===== Two Pointers Pattern
304
304
305
-
Usually we use one pointer to navigate each element in an array. However, there are times when having two pointers (left/right, low/high) comes handy. Let's do examples.
305
+
Usually, we use one pointer to navigate each element in an array. However, there are times when having two pointers (left/right, low/high) comes in handy. Let's do some examples.
306
306
307
-
*AR-A*) _Given a sorted array of integers, find two numbers that add up to target t and return their values._
307
+
*AR-A*) _Given a sorted `array` of integers, find two numbers that add up to a `target` and return their values._
One naive solution would be use two pointers in a nested loop:
319
+
One naive solution would be to use two pointers in a nested loop:
320
320
321
321
.Solution 1: Brute force
322
322
[source, javascript]
@@ -331,9 +331,9 @@ function twoSum(arr, target) {
331
331
332
332
The runtime of this solution would be `O(n^2)`. Because of the nested loops. Can we do better? We are not using the fact that the array is SORTED!
333
333
334
-
We can use two pointers but this time we will traverse the array only once. One starting from the left side and the other from the right side.
334
+
We can use two pointers: one pointer starting from the left side and the other from the right side.
335
335
336
-
Depending on if the the sum is bigger or smaller than target, we move right or left pointer. If the sum is equal to target we return the values at the current left or right pointer.
336
+
Depending on whether the sum is bigger or smaller than the target, we move right or left. If the sum is equal to the target, we return the current left and right pointer's values.
337
337
338
338
.Solution 1: Two Pointers
339
339
[source, javascript]
@@ -352,11 +352,125 @@ function twoSum(arr, target) {
352
352
353
353
These two pointers have a runtime of `O(n)`.
354
354
355
-
REMEMBER: This technique only works for sorted arrays. If the array was not sorted, you would have to sort it first or choose another approach.
355
+
WARNING: This technique only works for sorted arrays. If the array was not sorted, you would have to sort it first or choose another approach.
356
356
357
357
===== Sliding Windows Pattern
358
358
359
-
TBD
359
+
The sliding window pattern is similar to the two pointers. The difference is that the distance between the left and right pointer is always the same. Also, the numbers don't need to be sorted. Let's do an example!
360
+
361
+
*AR-B*) _Find the max sum of an array of integers, only taking `k` items from the right and left side sequentially._
362
+
_*Constraints*: `k` won't exceed the number of elements `n`: `1 <= k <= n`._
Notice that many elements on the middle branches (in red color) have the same numbers but in a different order, so the sums oscillate between 104 and 14. That's why this algorithm is not very optimal for this problem.
442
+
443
+
*Sliding window algorithm*
444
+
445
+
Another approach is using sliding windows. Since the sum always has `k` elements, we can compute the cumulative sum for k first elements from the left. Then, we slide the "window" to the right and remove one from the left until we cover all the right items. In the end, we would have all the possible combinations without duplicated work.
446
+
447
+
Check out the following illustration:
448
+
449
+
image::max-sum-sliding-window.png[sliding window for arrays]
450
+
451
+
Here's the implementation:
452
+
453
+
[source, javascript]
454
+
----
455
+
function maxSum(arr, k) {
456
+
let left = k - 1;
457
+
let right = arr.length -1;
458
+
let sum = 0;
459
+
for (let i = 0; i < k; i++) sum += arr[i];
460
+
let max = sum;
461
+
462
+
for (let i = 0; i < k; i++) {
463
+
sum += arr[right--] - arr[left--];
464
+
max = Math.max(max, sum);
465
+
}
466
+
467
+
return max;
468
+
};
469
+
----
470
+
471
+
The difference between the two pointers pattern and the sliding windows, it's that we move both pointers at the same time to keep the length of the window the same.
472
+
473
+
The runtime for this code is: `O(k)`. We move the window k times. Since `k <= n`, the final runtime is `O(n)`.
0 commit comments