Skip to content

Commit 7fe3df4

Browse files
committed
find minumum in rotated sorted array(v2)
1 parent 5f4f2e5 commit 7fe3df4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

โ€Žfind-minimum-in-rotated-sorted-array/se6816.javaโ€Ž

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,30 @@ public int findMin(int[] nums) {
1313
return result;
1414
}
1515
}
16+
17+
/**
18+
์ด๋ถ„ ํƒ์ƒ‰์„ ํ†ตํ•ด, ์ตœ์†Œ ์š”์†Œ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹
19+
nums์˜ ๊ธธ์ด -> N
20+
์‹œ๊ฐ„ ๋ณต์žก๋„ : O(logN)
21+
๊ณต๊ฐ„ ๋ณต์žก๋„ : O(1)
22+
*/
23+
class Solution {
24+
public int findMin(int[] nums) {
25+
return nums[binarySearch(nums)];
26+
}
27+
28+
public int binarySearch(int[] nums) {
29+
int start = 0;
30+
int end = nums.length -1;
31+
while(start < end) {
32+
int mid = (start + end) / 2;
33+
if(nums[mid] < nums[end]) {
34+
end = mid;
35+
} else {
36+
start = mid + 1;
37+
}
38+
}
39+
return start;
40+
}
41+
}
42+

0 commit comments

Comments
ย (0)