Skip to content

Commit 23fed7a

Browse files
committed
leetcode
1 parent ca404aa commit 23fed7a

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
3+
-* 852. Peak Index in a Mountain Array *-
4+
5+
An array arr a mountain if the following properties hold:
6+
7+
arr.length >= 3
8+
There exists some i with 0 < i < arr.length - 1 such that:
9+
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
10+
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
11+
Given a mountain array arr, return the index i such that arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].
12+
13+
You must solve it in O(log(arr.length)) time complexity.
14+
15+
16+
17+
Example 1:
18+
19+
Input: arr = [0,1,0]
20+
Output: 1
21+
Example 2:
22+
23+
Input: arr = [0,2,1,0]
24+
Output: 1
25+
Example 3:
26+
27+
Input: arr = [0,10,5,2]
28+
Output: 1
29+
30+
31+
Constraints:
32+
33+
3 <= arr.length <= 105
34+
0 <= arr[i] <= 106
35+
arr is guaranteed to be a mountain array.
36+
37+
*/
38+
39+
class A {
40+
int peakIndexInMountainArray(List<int> arr) {
41+
int i = arr.length ~/ 2;
42+
int arrLen = i;
43+
while (true) {
44+
if (arr[i] > arr[i + 1] && arr[i] > arr[i - 1]) break;
45+
if (arr[i] < arr[i + 1]) {
46+
arrLen ~/= 2;
47+
i += arrLen;
48+
} else {
49+
i -= arrLen ~/ 2;
50+
}
51+
}
52+
return i;
53+
}
54+
}
55+
56+
class B {
57+
int peakIndexInMountainArray(List<int> arr) {
58+
int left = 0;
59+
int right = arr.length - 1;
60+
int mid;
61+
while (left < right) {
62+
mid = left + (right - left) ~/ 2;
63+
if (arr[mid] > arr[mid + 1]) {
64+
right = mid;
65+
} else {
66+
left = mid + 1;
67+
}
68+
}
69+
return left;
70+
}
71+
}
72+
73+
class Solution {
74+
int peakIndexInMountainArray(List<int> arr) {
75+
return getPeak(arr.length ~/ 2, arr, 0, arr.length - 1);
76+
}
77+
78+
int getPeak(int cur, List<int> arr, int start, int end) {
79+
if (arr[cur] < arr[cur + 1]) {
80+
start = cur;
81+
cur = (start + end) ~/ 2;
82+
return getPeak(cur, arr, start, end);
83+
}
84+
if (arr[cur] < arr[cur - 1]) {
85+
end = cur;
86+
cur = (end - start) ~/ 2;
87+
return getPeak(cur ~/ 2, arr, start, end);
88+
}
89+
return cur;
90+
}
91+
}
92+
93+
/*
94+
95+
This Solution need to Work On
96+
97+
98+
class PeakIndexFinder {
99+
int peakIndexInMountainArray(List<int> arr) {
100+
List<MapEntry<int, int>> pq = arr.asMap().entries.map((entry) => MapEntry(-entry.value, entry.key)).toList();
101+
heapify(pq);
102+
heapPop(pq);
103+
return pq[pq.length - 1].value; // Return the peak index from the last element of the heap
104+
}
105+
}
106+
107+
void heapify(List<MapEntry<int, int>> arr) {
108+
int n = arr.length;
109+
for (int i = (n ~/ 2) - 1; i >= 0; i--) {
110+
_sink(arr, i, n);
111+
}
112+
}
113+
114+
void heapPop(List<MapEntry<int, int>> arr) {
115+
int n = arr.length;
116+
if (n == 0) return;
117+
118+
// Swap root with the last element
119+
MapEntry<int, int> temp = arr[0];
120+
arr[0] = arr[n - 1];
121+
arr[n - 1] = temp;
122+
123+
// Remove the last element (previously the root)
124+
arr.removeLast();
125+
126+
// Heapify the array again
127+
_sink(arr, 0, n - 1);
128+
}
129+
130+
void _sink(List<MapEntry<int, int>> arr, int k, int n) {
131+
while (2 * k + 1 < n) {
132+
int j = 2 * k + 1; // Left child
133+
if (j + 1 < n && arr[j].key > arr[j + 1].key) {
134+
j++; // Right child
135+
}
136+
if (arr[k].key <= arr[j].key) {
137+
break;
138+
}
139+
// Swap parent with the smallest child
140+
MapEntry<int, int> temp = arr[k];
141+
arr[k] = arr[j];
142+
arr[j] = temp;
143+
k = j;
144+
}
145+
}
146+
147+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
func peakIndexInMountainArray(arr []int) int {
4+
return getPeak(len(arr)/2, arr, 0, len(arr)-1)
5+
}
6+
7+
func getPeak(current int, arr []int, start int, end int) int {
8+
if arr[current] < arr[current+1] {
9+
start = current
10+
current = (start + end) / 2
11+
return getPeak(current, arr, start, end)
12+
}
13+
if arr[current] < arr[current-1] {
14+
end = current
15+
current = (end - start) / 2
16+
return getPeak(current/2, arr, start, end)
17+
}
18+
return current
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#
2+
3+
## **Intuition:**
4+
5+
The code is designed to find the peak index in a mountain array. A mountain array is an array that increases up to a certain index (peak) and then decreases afterward. The code uses a binary search-based approach to efficiently locate the peak element.
6+
7+
## **Approach:**
8+
9+
The `peakIndexInMountainArray` function initiates the binary search by calling the `getPeak` function. The `getPeak` function performs a binary search to find the peak index in the array.
10+
11+
1. The `peakIndexInMountainArray` function starts the binary search by calling `getPeak` with the initial values of `cur` (the middle index of the array), `start` (the start index of the array), and `end` (the end index of the array).
12+
2. The `getPeak` function checks if the value at `cur` is less than the value at the next index (`cur + 1`). If it is, it means the peak is on the right side, so it updates `start` to `cur` and calculates a new value of `cur` as the middle index between `start` and `end`.
13+
3. If the value at `cur` is not less than the value at the next index, it means the peak is on the left side. The function updates `end` to `cur` and calculates a new value of `cur` as the middle index between `start` and `end`.
14+
4. The binary search continues until it finds the peak index when the condition `arr[cur] < arr[cur + 1]` or `arr[cur] < arr[cur - 1]` is false, and the function returns the current index as the peak index.
15+
16+
## **Time Complexity:**
17+
18+
The time complexity of this algorithm is O(log n), where n is the number of elements in the input array. This is because the binary search approach divides the search space in half with each recursive call.
19+
20+
## **Space Complexity:**
21+
22+
The space complexity of this algorithm is O(log n) as well. This is because the binary search approach uses recursive calls, and the maximum depth of the recursion is log n (where n is the number of elements in the input array) because the search space is halved with each recursive call.
23+
24+
```dart
25+
class Solution {
26+
int peakIndexInMountainArray(List<int> arr) {
27+
return getPeak(arr.length ~/ 2, arr, 0, arr.length - 1);
28+
}
29+
30+
int getPeak(int cur, List<int> arr, int start, int end) {
31+
if (arr[cur] < arr[cur + 1]) {
32+
start = cur;
33+
cur = (start + end) ~/ 2;
34+
return getPeak(cur, arr, start, end);
35+
}
36+
if (arr[cur] < arr[cur - 1]) {
37+
end = cur;
38+
cur = (end - start) ~/ 2;
39+
return getPeak(cur ~/ 2, arr, start, end);
40+
}
41+
return cur;
42+
}
43+
}
44+
```

0 commit comments

Comments
 (0)