Skip to content

Commit 3b4ecde

Browse files
author
Kohei Asai
authored
153. Find Minimum in Rotated Sorted Array (#85)
1 parent a02f57f commit 3b4ecde

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import findMin from "./findMinimumInRotatedSortedArray";
2+
3+
const TEST_CASES = new Map([
4+
[[3, 4, 5, 1, 2], 1],
5+
[[4, 5, 6, 7, 0, 1, 2], 0],
6+
[[3, 1, 2], 1],
7+
[[1, 2, 3, 4, 5, 6], 1],
8+
[[2, 1], 1],
9+
[[1], 1]
10+
]);
11+
12+
describe("153. Find Minimum in Rotated Sorted Array", () => {
13+
for (const [args, expected] of TEST_CASES.entries()) {
14+
test(`when [${args.join(", ")}]`, () => {
15+
expect(findMin(args)).toBe(expected);
16+
});
17+
}
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// 153. Find Minimum in Rotated Sorted Array
2+
// https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
3+
export default function findMin(nums: number[]) {
4+
let from = 0;
5+
let to = nums.length;
6+
7+
while (to - from >= 2) {
8+
const middle = Math.floor((from + to) / 2);
9+
10+
if (nums[middle - 1] >= nums[to - 1]) {
11+
from = middle;
12+
} else {
13+
to = middle;
14+
}
15+
}
16+
17+
return nums[from];
18+
}

0 commit comments

Comments
 (0)