Skip to content

Commit 217d4c6

Browse files
committed
feat: add solutions to lc problems: No.1095, 1122
* No.1095: Find in Mountain Array * No.1122: Relative Sort Array
1 parent b2fa8a2 commit 217d4c6

File tree

14 files changed

+742
-192
lines changed

14 files changed

+742
-192
lines changed

solution/1000-1099/1095.Find in Mountain Array/README.md

Lines changed: 208 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,229 @@
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77+
**方法一:二分查找**
78+
79+
我们先通过二分查找,找到数组中的最大值所在的下标 $l$,那么数组就可以被分成两段,前半段是递增的,后半段是递减的。
80+
81+
然后我们在前半段中使用二分查找,查找目标值所在的下标,如果找不到,再在后半段中使用二分查找,查找目标值所在的下标。
82+
83+
时间复杂度 $O(\log n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
84+
7785
<!-- tabs:start -->
7886

7987
### **Python3**
8088

8189
<!-- 这里可写当前语言的特殊实现逻辑 -->
8290

8391
```python
84-
92+
# """
93+
# This is MountainArray's API interface.
94+
# You should not implement it, or speculate about its implementation
95+
# """
96+
# class MountainArray:
97+
# def get(self, index: int) -> int:
98+
# def length(self) -> int:
99+
100+
class Solution:
101+
def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int:
102+
def search(l: int, r: int, k: int) -> int:
103+
while l < r:
104+
mid = (l + r) >> 1
105+
if k * mountain_arr.get(mid) >= k * target:
106+
r = mid
107+
else:
108+
l = mid + 1
109+
return -1 if mountain_arr.get(l) != target else l
110+
111+
n = mountain_arr.length()
112+
l, r = 0, n - 1
113+
while l < r:
114+
mid = (l + r) >> 1
115+
if mountain_arr.get(mid) > mountain_arr.get(mid + 1):
116+
r = mid
117+
else:
118+
l = mid + 1
119+
ans = search(0, l, 1)
120+
return search(l + 1, n - 1, -1) if ans == -1 else ans
85121
```
86122

87123
### **Java**
88124

89125
<!-- 这里可写当前语言的特殊实现逻辑 -->
90126

91127
```java
128+
/**
129+
* // This is MountainArray's API interface.
130+
* // You should not implement it, or speculate about its implementation
131+
* interface MountainArray {
132+
* public int get(int index) {}
133+
* public int length() {}
134+
* }
135+
*/
136+
137+
class Solution {
138+
private MountainArray mountainArr;
139+
private int target;
140+
141+
public int findInMountainArray(int target, MountainArray mountainArr) {
142+
int n = mountainArr.length();
143+
int l = 0, r = n - 1;
144+
while (l < r) {
145+
int mid = (l + r) >>> 1;
146+
if (mountainArr.get(mid) > mountainArr.get(mid + 1)) {
147+
r = mid;
148+
} else {
149+
l = mid + 1;
150+
}
151+
}
152+
this.mountainArr = mountainArr;
153+
this.target = target;
154+
int ans = search(0, l, 1);
155+
return ans == -1 ? search(l + 1, n - 1, -1) : ans;
156+
}
157+
158+
private int search(int l, int r, int k) {
159+
while (l < r) {
160+
int mid = (l + r) >>> 1;
161+
if (k * mountainArr.get(mid) >= k * target) {
162+
r = mid;
163+
} else {
164+
l = mid + 1;
165+
}
166+
}
167+
return mountainArr.get(l) == target ? l : -1;
168+
}
169+
}
170+
```
171+
172+
### **C++**
173+
174+
```cpp
175+
/**
176+
* // This is the MountainArray's API interface.
177+
* // You should not implement it, or speculate about its implementation
178+
* class MountainArray {
179+
* public:
180+
* int get(int index);
181+
* int length();
182+
* };
183+
*/
184+
185+
class Solution {
186+
public:
187+
int findInMountainArray(int target, MountainArray &mountainArr) {
188+
int n = mountainArr.length();
189+
int l = 0, r = n - 1;
190+
while (l < r) {
191+
int mid = (l + r) >> 1;
192+
if (mountainArr.get(mid) > mountainArr.get(mid + 1)) {
193+
r = mid;
194+
} else {
195+
l = mid + 1;
196+
}
197+
}
198+
auto search = [&](int l, int r, int k) -> int {
199+
while (l < r) {
200+
int mid = (l + r) >> 1;
201+
if (k * mountainArr.get(mid) >= k * target) {
202+
r = mid;
203+
} else {
204+
l = mid + 1;
205+
}
206+
}
207+
return mountainArr.get(l) == target ? l : -1;
208+
};
209+
int ans = search(0, l, 1);
210+
return ans == -1 ? search(l + 1, n - 1, -1) : ans;
211+
}
212+
};
213+
```
214+
215+
### **Go**
216+
217+
```go
218+
/**
219+
* // This is the MountainArray's API interface.
220+
* // You should not implement it, or speculate about its implementation
221+
* type MountainArray struct {
222+
* }
223+
*
224+
* func (this *MountainArray) get(index int) int {}
225+
* func (this *MountainArray) length() int {}
226+
*/
227+
228+
func findInMountainArray(target int, mountainArr *MountainArray) int {
229+
n := mountainArr.length()
230+
l, r := 0, n-1
231+
for l < r {
232+
mid := (l + r) >> 1
233+
if mountainArr.get(mid) > mountainArr.get(mid+1) {
234+
r = mid
235+
} else {
236+
l = mid + 1
237+
}
238+
}
239+
search := func(l, r, k int) int {
240+
for l < r {
241+
mid := (l + r) >> 1
242+
if k*mountainArr.get(mid) >= k*target {
243+
r = mid
244+
} else {
245+
l = mid + 1
246+
}
247+
}
248+
if mountainArr.get(l) == target {
249+
return l
250+
}
251+
return -1
252+
}
253+
ans := search(0, l, 1)
254+
if ans == -1 {
255+
return search(l+1, n-1, -1)
256+
}
257+
return ans
258+
}
259+
```
92260

261+
### **TypeScript**
262+
263+
```ts
264+
/**
265+
* // This is the MountainArray's API interface.
266+
* // You should not implement it, or speculate about its implementation
267+
* class Master {
268+
* get(index: number): number {}
269+
*
270+
* length(): number {}
271+
* }
272+
*/
273+
274+
function findInMountainArray(target: number, mountainArr: MountainArray) {
275+
const n = mountainArr.length();
276+
let l = 0;
277+
let r = n - 1;
278+
while (l < r) {
279+
const mid = (l + r) >> 1;
280+
if (mountainArr.get(mid) > mountainArr.get(mid + 1)) {
281+
r = mid;
282+
} else {
283+
l = mid + 1;
284+
}
285+
}
286+
const search = (l: number, r: number, k: number): number => {
287+
while (l < r) {
288+
const mid = (l + r) >> 1;
289+
if (k * mountainArr.get(mid) >= k * target) {
290+
r = mid;
291+
} else {
292+
l = mid + 1;
293+
}
294+
}
295+
return mountainArr.get(l) === target ? l : -1;
296+
};
297+
const ans = search(0, l, 1);
298+
return ans === -1 ? search(l + 1, n - 1, -1) : ans;
299+
}
93300
```
94301

95302
### **...**

0 commit comments

Comments
 (0)