Skip to content

Commit 1133d7d

Browse files
authored
153. Find Minimum in Rotated Sorted Array (#49)
* 153 solved. * add 153 question link
1 parent 8f5c4b9 commit 1133d7d

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ continually updating 😃.
103103
* [34. Find First and Last Position of Element in Sorted Array](./src/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)   *`array;`*  *`binary search`*
104104
* [35. Search Insert Position](src/0035_search_insert_position/search_insert_position.go)   *`array;`*  *`binary search`*
105105
* [69. Sqrt(x)](./src/0069_sqrtx/sqrtx.go)   *`math;`*  *`binary search`*
106+
* [153. Find Minimum in Rotated Sorted Array](src/0153_find_minimum_in_rotated_sorted_array/fmirsa.go)
106107
* [704. Binary Search](./src/0704_binary_search/binary_search.go)
107108

108109
### Math
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
153. Find Minimum in Rotated Sorted Array
3+
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
4+
5+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
6+
7+
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
8+
9+
Find the minimum element.
10+
11+
You may assume no duplicate exists in the array.
12+
*/
13+
// time: 2019-01-07
14+
15+
package fmirsa
16+
17+
// binary search
18+
// time complexity: O( log n)
19+
// space complexity: O(1)
20+
func findMin(nums []int) int {
21+
var (
22+
low int
23+
high = len(nums) - 1
24+
mid int
25+
)
26+
for low < high {
27+
mid = low + (high-low)>>1
28+
if nums[high] < nums[mid] {
29+
low = mid + 1
30+
} else {
31+
high = mid
32+
}
33+
}
34+
return nums[low]
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package fmirsa
2+
3+
import "testing"
4+
5+
func TestFindMin(t *testing.T) {
6+
nums := []int{3, 4, 5, 1, 2}
7+
expected := 1
8+
if res := findMin(nums); res != expected {
9+
t.Errorf("expected %d, got %d", expected, res)
10+
}
11+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
6060
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
6161
|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|
62+
|0153|[153. Find Minimum in Rotated Sorted Array](0153_find_minimum_in_rotated_sorted_array/fmirsa.go)|Medium|*`binary search`*|
6263
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
6364
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
6465
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|

0 commit comments

Comments
 (0)