Skip to content

Commit 7535558

Browse files
committed
8.3 Add Magic Index
1 parent 3c8a8b0 commit 7535558

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

__tests__/8-3-magic-index.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { magicIndex } from '../src/cracking-the-coding-interview/8-recursion-and-dynamic-programming/8-3-magic-index';
2+
3+
describe(magicIndex.name, () => {
4+
[
5+
{ array: [ 0, 2, 4, 5, 6 ], expectedIndex: 0 },
6+
{ array: [ -1, 1, 4, 5, 6 ], expectedIndex: 1 },
7+
{ array: [ -1, 0, 2, 5, 6 ], expectedIndex: 2 },
8+
{ array: [ -1, 0, 1, 3, 6 ], expectedIndex: 3 },
9+
{ array: [ -1, 0, 1, 2, 4 ], expectedIndex: 4 },
10+
{ array: [ -1, 0, 1, 2, 3 ], expectedIndex: null },
11+
{ array: [], expectedIndex: null },
12+
].forEach(({ array, expectedIndex }) => {
13+
it(`Should return ${expectedIndex} for ${JSON.stringify(array)}`, () => {
14+
expect(magicIndex(array)).toEqual(expectedIndex);
15+
});
16+
});
17+
});

src/cracking-the-coding-interview/8-recursion-and-dynamic-programming/8-2-robot-in-a-grid.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ export type Move = 'down' | 'right';
33
export type X = 'x';
44
export type _ = ' ';
55

6-
export function findPathRecursively(grid: Grid, r?: number, c?: number): Move[] | null {
6+
export function findPathRecursively(grid: Grid, rowIndex?: number, columnIndex?: number): Move[] | null {
77
if (!grid || !grid.length) return null;
88

9-
r = r != null ? r : grid.length - 1;
10-
c = c != null ? c : grid[0].length - 1;
9+
rowIndex = rowIndex != null ? rowIndex : grid.length - 1;
10+
columnIndex = columnIndex != null ? columnIndex : grid[0].length - 1;
1111

12-
const canGoUp = r > 0 && grid[r - 1][c] === ' ';
13-
const canGoLeft = c > 0 && grid[r][c - 1] === ' ';
14-
if (r === 0 && c === 0) {
15-
return grid[r][c] === 'x' ? null : [];
12+
const canGoUp = rowIndex > 0 && grid[rowIndex - 1][columnIndex] === ' ';
13+
const canGoLeft = columnIndex > 0 && grid[rowIndex][columnIndex - 1] === ' ';
14+
if (rowIndex === 0 && columnIndex === 0) {
15+
return grid[rowIndex][columnIndex] === 'x' ? null : [];
1616
}
1717

1818
if (canGoUp) {
19-
const pathToUpper = findPathRecursively(grid, r - 1, c);
19+
const pathToUpper = findPathRecursively(grid, rowIndex - 1, columnIndex);
2020
if (pathToUpper) return [...pathToUpper, 'down'];
2121
}
2222

2323
if (canGoLeft) {
24-
const pathToLeft = findPathRecursively(grid, r, c - 1);
24+
const pathToLeft = findPathRecursively(grid, rowIndex, columnIndex - 1);
2525
if (pathToLeft) return [...pathToLeft, 'right'];
2626
}
2727

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function magicIndex(
2+
array: number[],
3+
lowIndex: number = 0,
4+
highIndex = array.length - 1,
5+
): number | null {
6+
if (!array.length) return null;
7+
8+
if (lowIndex === highIndex) return array[lowIndex] === lowIndex ? lowIndex : null;
9+
10+
const index = lowIndex + Math.floor((highIndex - lowIndex) / 2);
11+
const value = array[index];
12+
13+
if (value === index) return index;
14+
else if (value < index) return magicIndex(array, index + 1, highIndex);
15+
else return magicIndex(array, lowIndex, index - 1);
16+
}

0 commit comments

Comments
 (0)