removing image

This commit is contained in:
Francis Yang 2024-01-09 09:22:02 -08:00
parent 97a8249a99
commit 01c924567a
5 changed files with 919 additions and 461 deletions

1320
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -43,6 +43,7 @@
"eslint-plugin-import": "2.27.5", "eslint-plugin-import": "2.27.5",
"eslint-plugin-jest": "27.2.1", "eslint-plugin-jest": "27.2.1",
"eslint-plugin-jsx-a11y": "6.7.1", "eslint-plugin-jsx-a11y": "6.7.1",
"eslint-plugin-react": "^7.33.2",
"husky": "8.0.3", "husky": "8.0.3",
"jest": "29.4.1", "jest": "29.4.1",
"pngjs": "^7.0.0" "pngjs": "^7.0.0"

View File

@ -15,8 +15,6 @@ Note that any array can be sorted to easily use the twin pointer method by using
However, the Array.sort method inherently has a time complexity of O(n log n), which can be undesirable However, the Array.sort method inherently has a time complexity of O(n log n), which can be undesirable
in many cases when the desired time complexity of your solution is simply O(n). in many cases when the desired time complexity of your solution is simply O(n).
![Binary Search](https://upload.wikimedia.org/wikipedia/commons/8/83/Binary_Search_Depiction.svg)
## Complexity ## Complexity
**Time Complexity**: `O(n)` - since we only need to look over every element of our array a single time when comparing, time complexity is O(n). **Time Complexity**: `O(n)` - since we only need to look over every element of our array a single time when comparing, time complexity is O(n).

View File

@ -2,17 +2,29 @@ import { twinPointerSorted, twinPointerUnsorted } from '../twinPointers';
describe('twinPointerSorted', () => { describe('twinPointerSorted', () => {
it('should search for a specific combination sum', () => { it('should search for a specific combination sum', () => {
expect(twinPointerSorted([], 1)).toBe([0, 0]); expect(twinPointerSorted([], 1)).toBe(-1);
expect(twinPointerSorted([0, 1, 2], 3)).toBe([1, 2]); expect(twinPointerSorted([0, 1, 2], 3)).toStrictEqual([1, 2]);
expect(twinPointerSorted([0, 1, 2], 1)).toBe([0, 1]); expect(twinPointerSorted([0, 1, 2], 1)).toStrictEqual([0, 1]);
expect(twinPointerSorted([1, 2, 5, 7, 9], 4)).toBe([0, 0]); expect(twinPointerSorted([1, 2, 5, 7, 9], 4)).toBe(-1);
expect(twinPointerSorted([1, 2, 5, 7, 9], 14)).toBe([2, 4]); expect(twinPointerSorted([1, 2, 5, 7, 9], 14)).toStrictEqual([2, 4]);
expect(twinPointerSorted([3, 5, 7, 9], 1)).toBe([0, 0]); expect(twinPointerSorted([3, 5, 7, 9], 1)).toBe(-1);
expect(twinPointerSorted([4, 6, 10, 15, 16, 18, 20], 10)).toBe([0, 1]); expect(twinPointerSorted([4, 6, 10, 15, 16, 18, 20], 10)).toStrictEqual([0, 1]);
expect(twinPointerSorted([4, 6, 10, 15, 16, 18, 20], 38)).toBe([5, 6]); expect(twinPointerSorted([4, 6, 10, 15, 16, 18, 20], 38)).toStrictEqual([5, 6]);
expect(twinPointerSorted([0, 100, 300, 500, 700, 1000, 2000, 5000], 50)).toBe([0, 0]); expect(twinPointerSorted([0, 100, 300, 500, 700, 1000, 2000, 5000], 50)).toBe(-1);
expect(twinPointerSorted([0, 100, 300, 500, 700, 1000, 2000, 5000], 100)).toBe([0, 1]); expect(twinPointerSorted([0, 100, 300, 500, 700, 1000, 2000, 5000], 100)).toStrictEqual([0, 1]);
expect(twinPointerSorted([0, 100, 300, 500, 700, 1000, 2000, 5000], 1000)).toBe([0, 5]); expect(twinPointerSorted([0, 100, 300, 500, 700, 1000, 2000, 5000], 1000)).toStrictEqual([0, 5]);
expect(twinPointerSorted([0, 100, 300, 500, 700, 1000, 2000, 5000], 5000)).toBe([0, 7]); expect(twinPointerSorted([0, 100, 300, 500, 700, 1000, 2000, 5000], 5000)).toStrictEqual([0, 7]);
});
});
describe('twinPointerUnsorted', () => {
it('should search for the highest possible area', () => {
expect(twinPointerUnsorted([])).toBe(0);
expect(twinPointerUnsorted([2])).toBe(2);
expect(twinPointerUnsorted([0, 1, 2])).toBe(1);
expect(twinPointerUnsorted([1, 2, 5, 7, 9])).toBe(10);
expect(twinPointerUnsorted([3, 5, 7, 9])).toBe(10);
expect(twinPointerUnsorted([4, 6, 10, 15, 16, 18, 20])).toBe(45);
expect(twinPointerUnsorted([0, 100, 300, 500, 700, 1000, 2000, 5000])).toBe(2100);
}); });
}); });

View File

@ -24,11 +24,11 @@ export function twinPointerSorted(sortedArray, seekElement, comparatorCallback)
* If our sum is less than the target then we can increase said sum but by increasing the left value; * If our sum is less than the target then we can increase said sum but by increasing the left value;
* since the array is sorted, this will always result in array[left] becoming a larger number. * since the array is sorted, this will always result in array[left] becoming a larger number.
*/ */
if (comparator.lessThan(sortedArray[left] + sortedarray[right], seekElement)) { if (comparator.lessThan(sortedArray[left] + sortedArray[right], seekElement)) {
left++; left++;
// Same concept as before, only now we decrease our sum because it's greater than the target. // Same concept as before, only now we decrease our sum because it's greater than the target.
} else if (comparator.greaterThan(sortedArray[left] + sortedarray[right], seekElement)) { } else if (comparator.greaterThan(sortedArray[left] + sortedArray[right], seekElement)) {
right--; right--;
// Assuming we have found our target, return left and right since they represent the indices that our correct sum is located at. // Assuming we have found our target, return left and right since they represent the indices that our correct sum is located at.
@ -37,8 +37,8 @@ export function twinPointerSorted(sortedArray, seekElement, comparatorCallback)
} }
} }
// Return [0, 0] (an impossible answer due to our while loop) if we haven't found any combination of numbers that works. // Return -1 if we haven't found any combination of numbers that works.
return [0, 0]; return -1;
} }
/* An example of a twin pointer method on an unsorted array. In this problem, we aim to get the heighest possible area from two numbers by using the /* An example of a twin pointer method on an unsorted array. In this problem, we aim to get the heighest possible area from two numbers by using the
@ -55,6 +55,13 @@ export function twinPointerSorted(sortedArray, seekElement, comparatorCallback)
export function twinPointerUnsorted(unsortedArray, comparatorCallback) { export function twinPointerUnsorted(unsortedArray, comparatorCallback) {
const comparator = new Comparator(comparatorCallback); const comparator = new Comparator(comparatorCallback);
// Edge cases; not relevant to the pointer method.
if (unsortedArray.length === 0) {
return 0
} else if (unsortedArray.length === 1) {
return unsortedArray[0]
}
// Again, we set our two pointers to the left and rightmost elements of the array. // Again, we set our two pointers to the left and rightmost elements of the array.
let left = 0; let left = 0;
let right = unsortedArray.length - 1; let right = unsortedArray.length - 1;