File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
find-minimum-in-rotated-sorted-array Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ [๋ฌธ์ ํ์ด]
3
+ - ์ผ์ชฝ์ ์ ๋ณด๋ค ์ค๋ฅธ์ชฝ์ ์๊ฐ ์์ ์๊ฐ์ ์ฐพ์.
4
+ - ๊ฐ์ฅ ์ผ์ชฝ์ ์ ๋ณด๋ค ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ ์๊ฐ ํฌ๋ฉด ํ์ ์ด ์ผ์ด๋์ง ์์ ์ํ์ด๋ฏ๋ก, ์ฒซ๋ฒ์งธ๊ฐ ์ต์๊ฐ์ด๋ค.
5
+ - ํ์ด1
6
+ time: O(N), space: O(1);
7
+ class Solution {
8
+ public int findMin(int[] nums) {
9
+ if (nums.length == 1) {
10
+ return nums[0];
11
+ }
12
+
13
+ for (int i = 1; i < nums.length; i++) {
14
+ if (nums[i - 1] > nums[i]) {
15
+ return nums[i];
16
+ }
17
+ }
18
+ return nums[0];
19
+ }
20
+ }
21
+ - ํ์ด2 <์ด์งํ์>
22
+ time: O(log N), space: O(1)
23
+
24
+ [ํ๊ณ ]
25
+ ์ด์งํ์์ผ๋ก ํธ๋ ๋ฅ๋ ฅ์ด ๋ถ์กฑํ ๊ฒ ๊ฐ๋ค.
26
+ */
27
+
28
+ class Solution {
29
+ public int findMin (int [] nums ) {
30
+ int left = 0 ;
31
+ int right = nums .length - 1 ;
32
+
33
+ if (nums [left ] < nums [right ]) {
34
+ return nums [left ];
35
+ }
36
+
37
+ while (left < right ) {
38
+ int mid = (left + right ) / 2 ;
39
+
40
+ if (nums [mid ] <= nums [right ]) {
41
+ right = mid ;
42
+ } else {
43
+ left = mid + 1 ;
44
+ }
45
+ }
46
+ return nums [left ];
47
+ }
48
+ }
49
+
You canโt perform that action at this time.
0 commit comments