Skip to content

Commit 73b7281

Browse files
refactor 941
1 parent 392c22f commit 73b7281

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32+
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | O(n) | O(1) | |Easy|
3233
|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | O(n) | O(n) | |Easy|
3334
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_929.java) | O(n) | O(n) | |Easy|
3435
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | O(n) | O(1) | |Easy|
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 941. Valid Mountain Array
5+
*
6+
* Given an array A of integers, return true if and only if it is a valid mountain array.
7+
*
8+
* Recall that A is a mountain array if and only if:
9+
*
10+
* A.length >= 3
11+
* There exists some i with 0 < i < A.length - 1 such that:
12+
* A[0] < A[1] < ... A[i-1] < A[i]
13+
* A[i] > A[i+1] > ... > A[B.length - 1]
14+
*
15+
*
16+
* Example 1:
17+
*
18+
* Input: [2,1]
19+
* Output: false
20+
* Example 2:
21+
*
22+
* Input: [3,5,5]
23+
* Output: false
24+
* Example 3:
25+
*
26+
* Input: [0,3,2,1]
27+
* Output: true
28+
*
29+
*
30+
* Note:
31+
*
32+
* 0 <= A.length <= 10000
33+
* 0 <= A[i] <= 10000
34+
* */
35+
public class _941 {
36+
public static class Solution1 {
37+
public boolean validMountainArray(int[] A) {
38+
int i = 0;
39+
for (; i < A.length - 1; i++) {
40+
if (A[i] < A[i+1]) {
41+
continue;
42+
} else if (A[i] == A[i+1]) {
43+
return false;
44+
} else {
45+
break;
46+
}
47+
}
48+
if (i == 0 || i >= A.length - 1) {
49+
return false;
50+
}
51+
for (; i < A.length-1; i++) {
52+
if (A[i] > A[i+1]) {
53+
continue;
54+
} else {
55+
return false;
56+
}
57+
}
58+
return i == A.length - 1;
59+
}
60+
}
61+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._941;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _941Test {
10+
private static _941.Solution1 solution1;
11+
private static int[] A;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _941.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[]{0, 3, 2, 1};
21+
assertEquals(true, solution1.validMountainArray(A));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
A = new int[]{2, 1};
27+
assertEquals(false, solution1.validMountainArray(A));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
A = new int[]{3, 5, 5};
33+
assertEquals(false, solution1.validMountainArray(A));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
A = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
39+
assertEquals(false, solution1.validMountainArray(A));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)