Skip to content

Commit 67978f6

Browse files
author
yennj12
committed
update 852 java
1 parent 2666909 commit 67978f6

File tree

3 files changed

+93
-9
lines changed

3 files changed

+93
-9
lines changed

data/progress.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# 2025-01-08
44
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
- TODO: go through `neetcode`, note/summary knowledges (by topic) at cheatsheet
56

67
# 2025-01-07
78
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

leetcode_java/src/main/java/LeetCodeJava/BinarySearch/PeakIndexInAMountainArray.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,82 @@
6161
public class PeakIndexInAMountainArray {
6262

6363
// V0
64+
// TODO: fix below:
6465
// public int peakIndexInMountainArray(int[] arr) {
6566
//
67+
// // edge
68+
// int maxIdx = -1;
69+
// int maxVal = -1;
70+
// if (arr.length == 3){
71+
// for(int i = 0; i < arr.length; i++){
72+
// if(arr[i] > maxVal){
73+
// maxVal = arr[i];
74+
// maxIdx = i;
75+
// }
76+
// }
77+
// return maxIdx;
78+
// }
79+
//
80+
// // binary search
81+
// int l = 0;
82+
// int r = arr.length - 1;
83+
// int mid = (l + r) / 2;
84+
// while (r >= l && r >= 0){
85+
//
86+
// mid = (l + r) / 2;
87+
//
88+
// // case 1) cur > left and cur > right (find peak)
89+
// if (arr[mid] > arr[mid-1] && arr[mid] > arr[mid+1]){
90+
// return mid;
91+
// }
92+
// // Exp 1 : [0,0,0, 3,2,1,0] -> 1
93+
// // case 2) cur < left && cur < left most (left is increasing order)
94+
// else if (arr[mid] >= arr[mid-1] && arr[mid] >= arr[l]){
95+
// l = mid + 1;
96+
// }
97+
// // case 3) cur < right and cur > right most (right is decreasing order ?)
98+
// else if (arr[mid] >= arr[mid+1] && arr[mid] >= arr[r]){
99+
// r = mid - 1;
100+
// }
101+
// }
102+
//
103+
// return mid;
66104
// }
105+
106+
// V0-1
107+
// IDEA: BINARY SEARCH
108+
// TODO: validate & fix
109+
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Binary_Search/peak-index-in-a-mountain-array.py#L55
110+
public int peakIndexInMountainArray_0_1(int[] arr) {
111+
if (arr.length < 3) {
112+
return -1; // Return -1 if the array length is less than 3
113+
}
114+
115+
// Binary search
116+
int l = 0;
117+
int r = arr.length - 1;
118+
119+
while (r >= l) {
120+
int mid = l + (r - l) / 2;
121+
122+
// Check if mid is the peak
123+
if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) {
124+
return mid;
125+
}
126+
// If the element at mid is smaller than the next element, peak is on the right
127+
else if (arr[mid] < arr[mid + 1]) {
128+
l = mid + 1;
129+
}
130+
// Otherwise, peak is on the left
131+
else {
132+
r = mid - 1;
133+
}
134+
}
135+
return -1; // Return -1 if no peak is found (though this case shouldn't happen with a valid
136+
// mountain array)
137+
}
138+
67139
// V1
140+
68141
// V2
69142
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,16 @@ private boolean isStrobogrammatic(String x){
22462246

22472247
// LC 852
22482248
// https://leetcode.com/problems/peak-index-in-a-mountain-array/
2249+
/**
2250+
* You are given an integer mountain array arr
2251+
* of length n where the values increase to a peak element and then decrease.
2252+
*
2253+
* Return the index of the peak element.
2254+
*
2255+
* Your task is to solve it in O(log(n)) time complexity.
2256+
*
2257+
*
2258+
*/
22492259
/**
22502260
*
22512261
*
@@ -2293,27 +2303,27 @@ public int peakIndexInMountainArray(int[] arr) {
22932303
// binary search
22942304
int l = 0;
22952305
int r = arr.length - 1;
2296-
while (r >= l){
2297-
int mid = (l + r) / 2;
2306+
int mid = (l + r) / 2;
2307+
while (r >= l && r >= 0){
2308+
2309+
mid = (l + r) / 2;
22982310

22992311
// case 1) cur > left and cur > right (find peak)
23002312
if (arr[mid] > arr[mid-1] && arr[mid] > arr[mid+1]){
23012313
return mid;
23022314
}
23032315
// Exp 1 : [0,0,0, 3,2,1,0] -> 1
2304-
// case 2) cur < left && cur > left most
2305-
else if (arr[mid] < arr[mid-1] && arr[mid] >= arr[l]){
2316+
// case 2) cur < left && cur < left most (left is increasing order)
2317+
else if (arr[mid] >= arr[mid-1] && arr[mid] >= arr[l]){
23062318
l = mid + 1;
23072319
}
2308-
// case 3) cur < right and cur > right most
2309-
else if (arr[mid] < arr[mid-1] && arr[mid] >= arr[r]){
2320+
// case 3) cur < right and cur > right most (right is decreasing order ?)
2321+
else if (arr[mid] >= arr[mid+1] && arr[mid] >= arr[r]){
23102322
r = mid - 1;
23112323
}
2312-
2313-
return 0;
23142324
}
23152325

2316-
return -1;
2326+
return mid;
23172327
}
23182328

23192329
}

0 commit comments

Comments
 (0)