Skip to content

Commit f77488e

Browse files
add 852
1 parent 1bf9146 commit f77488e

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Your ideas/fixes/algorithms are more than welcome!
3434
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Easy|
3535
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
3636
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
37+
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | O(n) | O(1) | |Easy|
3738
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
3839
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | O(n) | O(1) | |Easy|
3940
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java) | O(n) | O(n) | |Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder.solutions;
2+
3+
/**852. Peak Index in a Mountain Array
4+
*
5+
* Let's call an array A a mountain if the following properties hold:
6+
*
7+
* A.length >= 3
8+
* There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
9+
* Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
10+
*
11+
* Example 1:
12+
*
13+
* Input: [0,1,0]
14+
* Output: 1
15+
* Example 2:
16+
*
17+
* Input: [0,2,1,0]
18+
* Output: 1
19+
* Note:
20+
*
21+
* 3 <= A.length <= 10000
22+
* 0 <= A[i] <= 10^6
23+
* A is a mountain, as defined above.*/
24+
public class _852 {
25+
public static class Solution1 {
26+
public int peakIndexInMountainArray(int[] A) {
27+
for (int i = 1; i < A.length - 1; i++) {
28+
if (A[i] > A[i+1]) {
29+
return i;
30+
}
31+
}
32+
return 0;
33+
}
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._852;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _852Test {
10+
private static _852.Solution1 solution1;
11+
private static int[] A;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _852.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[]{0, 1, 0};
21+
assertEquals(1, solution1.peakIndexInMountainArray(A));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
A = new int[]{0, 2, 1, 0};
27+
assertEquals(1, solution1.peakIndexInMountainArray(A));
28+
}
29+
}

0 commit comments

Comments
 (0)