File tree Expand file tree Collapse file tree 3 files changed +67
-0
lines changed
find-minimum-in-rotated-sorted-array Expand file tree Collapse file tree 3 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ // 시간복잡도: O(n)
3+ // 공간복잡도: O(1)
4+ public int findMin (int [] nums ) {
5+ for (int i = 1 ; i < nums .length ; i ++) {
6+ if (nums [i - 1 ] > nums [i ]) {
7+ return nums [i ];
8+ }
9+ }
10+
11+ return nums [0 ];
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * class ListNode {
4+ * int val;
5+ * ListNode next;
6+ * ListNode(int x) {
7+ * val = x;
8+ * next = null;
9+ * }
10+ * }
11+ */
12+ //시간복잡도: O(n)
13+ //공간복잡도: O(1)
14+ public class Solution {
15+ public boolean hasCycle (ListNode head ) {
16+ ListNode slow = head ;
17+ ListNode fast = head ;
18+
19+ while (fast != null && fast .next != null ) {
20+ slow = slow .next ;
21+ fast = fast .next .next ;
22+
23+ if (slow == fast ) return true ;
24+ }
25+
26+ return false ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ //시간복잡도: O(n)
3+ //공간복잡도: O(n)
4+ public int maxProduct (int [] nums ) {
5+ int [] maxValue = new int [nums .length ];
6+ int [] minValue = new int [nums .length ];
7+
8+ maxValue [0 ] = nums [0 ];
9+ minValue [0 ] = nums [0 ];
10+
11+ for (int i = 1 ; i < nums .length ; i ++) {
12+ int value1 = maxValue [i - 1 ] * nums [i ];
13+ int value2 = minValue [i - 1 ] * nums [i ];
14+
15+ maxValue [i ] = Math .max (Math .max (value1 , value2 ), nums [i ]);
16+ minValue [i ] = Math .min (Math .min (value1 , value2 ), nums [i ]);
17+ }
18+
19+ int result = Integer .MIN_VALUE ;
20+ for (int i = 0 ; i < nums .length ; i ++) {
21+ result = Math .max (result , maxValue [i ]);
22+ }
23+
24+ return result ;
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments