mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
removing image
This commit is contained in:
parent
97a8249a99
commit
01c924567a
1320
package-lock.json
generated
1320
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -43,6 +43,7 @@
|
||||
"eslint-plugin-import": "2.27.5",
|
||||
"eslint-plugin-jest": "27.2.1",
|
||||
"eslint-plugin-jsx-a11y": "6.7.1",
|
||||
"eslint-plugin-react": "^7.33.2",
|
||||
"husky": "8.0.3",
|
||||
"jest": "29.4.1",
|
||||
"pngjs": "^7.0.0"
|
||||
|
@ -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
|
||||
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
|
||||
|
||||
**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).
|
||||
|
@ -1,18 +1,30 @@
|
||||
import { twinPointerSorted, twinPointerUnsorted } from '../twinPointers';
|
||||
|
||||
describe('twinPointerSorted', () => {
|
||||
it('should search for a specific combination sum', () => {
|
||||
expect(twinPointerSorted([], 1)).toBe([0, 0]);
|
||||
expect(twinPointerSorted([0, 1, 2], 3)).toBe([1, 2]);
|
||||
expect(twinPointerSorted([0, 1, 2], 1)).toBe([0, 1]);
|
||||
expect(twinPointerSorted([1, 2, 5, 7, 9], 4)).toBe([0, 0]);
|
||||
expect(twinPointerSorted([1, 2, 5, 7, 9], 14)).toBe([2, 4]);
|
||||
expect(twinPointerSorted([3, 5, 7, 9], 1)).toBe([0, 0]);
|
||||
expect(twinPointerSorted([4, 6, 10, 15, 16, 18, 20], 10)).toBe([0, 1]);
|
||||
expect(twinPointerSorted([4, 6, 10, 15, 16, 18, 20], 38)).toBe([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], 100)).toBe([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], 5000)).toBe([0, 7]);
|
||||
});
|
||||
it('should search for a specific combination sum', () => {
|
||||
expect(twinPointerSorted([], 1)).toBe(-1);
|
||||
expect(twinPointerSorted([0, 1, 2], 3)).toStrictEqual([1, 2]);
|
||||
expect(twinPointerSorted([0, 1, 2], 1)).toStrictEqual([0, 1]);
|
||||
expect(twinPointerSorted([1, 2, 5, 7, 9], 4)).toBe(-1);
|
||||
expect(twinPointerSorted([1, 2, 5, 7, 9], 14)).toStrictEqual([2, 4]);
|
||||
expect(twinPointerSorted([3, 5, 7, 9], 1)).toBe(-1);
|
||||
expect(twinPointerSorted([4, 6, 10, 15, 16, 18, 20], 10)).toStrictEqual([0, 1]);
|
||||
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(-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)).toStrictEqual([0, 5]);
|
||||
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);
|
||||
});
|
||||
});
|
@ -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;
|
||||
* 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++;
|
||||
|
||||
// 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--;
|
||||
|
||||
// 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 [0, 0];
|
||||
// Return -1 if we haven't found any combination of numbers that works.
|
||||
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
|
||||
@ -55,6 +55,13 @@ export function twinPointerSorted(sortedArray, seekElement, comparatorCallback)
|
||||
export function twinPointerUnsorted(unsortedArray, 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.
|
||||
let left = 0;
|
||||
let right = unsortedArray.length - 1;
|
||||
|
Loading…
Reference in New Issue
Block a user