Skip to content

Commit f24b1ba

Browse files
committed
852_Peak_Index_in_a_Mountain_Array
1 parent ef13e43 commit f24b1ba

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Also, there are open source implementations for basic data structs and algorithm
168168
| 811 | [Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/811_Subdomain_Visit_Count.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/811_Subdomain_Visit_Count.java) | String split and HashMap, O(n) and O(n) |
169169
| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/819_Most_Common_Word.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/819_Most_Common_Word.java) | String processing, be careful about 'b,b,b'. regex is recommended. |
170170
| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/844_Backspace_String_Compare.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/844_Backspace_String_Compare.java) | 1. Stack pop when encounters #, O(n) and O(n)<br>2. Compare string from end to start, O(n) and O(1) |
171+
| 852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/852_Peak_Index_in_a_Mountain_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/852_Peak_Index_in_a_Mountain_Array.java) | 1. Scan the array until encountering decline, O(n) and O(1)<br>2. Binary seach with additional check for [i + 1], O(logn) and O(1)|
171172
| 904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/904_Fruit_Into_Baskets.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/904_Fruit_Into_Baskets.java) | 1. Scan through blocks of tree, O(n) and O(n)<br>2. Mainten a sliding window with start and curr point, O(n) and O(n). |
172173
| 922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/922_Sort_Array_By_Parity_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/922_Sort_Array_By_Parity_II.java) | 1. Place odd and even number in odd and even place, not sort is needed. O(n) and O(1)<br>2. Two points with quick sort swap idea, O(n) and O(1). |
173174
| 929 | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/929_Unique_Email_Addresses.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/929_Unique_Email_Addresses.java) | String handle and hash (or set) |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
// public int peakIndexInMountainArray(int[] A) {
3+
// int i = 0;
4+
// for (; A[i] < A[i + 1]; i++);
5+
// return i;
6+
// }
7+
8+
public int peakIndexInMountainArray(int[] A) {
9+
int lo = 0, hi = A.length - 1;
10+
while (lo < hi) {
11+
int mid = (lo + hi) / 2;
12+
if (A[mid] < A[mid + 1]) lo = mid + 1;
13+
else hi = mid;
14+
}
15+
return lo;
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
# def peakIndexInMountainArray(self, A):
3+
# """
4+
# :type A: List[int]
5+
# :rtype: int
6+
# """
7+
# i = 0
8+
# while A[i + 1] >= A[i]:
9+
# i += 1
10+
# return i
11+
12+
def peakIndexInMountainArray(self, A):
13+
lo, hi = 0, len(A) - 1
14+
while lo < hi:
15+
mid = (lo + hi) / 2
16+
if A[mid] < A[mid + 1]:
17+
lo = mid + 1
18+
else:
19+
hi = mid
20+
return lo

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy