Skip to content

Commit aaffb4f

Browse files
solves valid mountain array
1 parent 528d448 commit aaffb4f

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@
254254
| 933 | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls) | [![Java](assets/java.png)](src/NumberOfRecentCalls.java) |
255255
| 937 | [Reorder Data In Log Files](https://leetcode.com/problems/reorder-data-in-log-files) | [![Java](assets/java.png)](src/ReorderDataInLogFiles.java) |
256256
| 938 | [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst) | [![Java](assets/java.png)](src/RangeSumOfBST.java) |
257-
| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array) | |
257+
| 941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array) | [![Java](assets/java.png)](src/ValidMountainArray.java) [![Python](assets/python.png)](python/valid_mountain_array.py) |
258258
| 942 | [DI String Match](https://leetcode.com/problems/di-string-match) | [![Java](assets/java.png)](src/DIStringMatch.java) |
259259
| 944 | [Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted) | |
260260
| 949 | [Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits) | |

python/valid_mountain_array.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def validMountainArray(self, numbers: List[int]) -> bool:
6+
isIncreasing = True
7+
index = 1
8+
while index < len(numbers):
9+
if numbers[index] == numbers[index - 1]: return False
10+
if numbers[index] < numbers[index - 1] and isIncreasing:
11+
if index == 1: return False
12+
isIncreasing = False
13+
if numbers[index] > numbers[index - 1] and not isIncreasing:
14+
return False
15+
index += 1
16+
return not isIncreasing

src/ValidMountainArray.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class ValidMountainArray {
2+
public boolean validMountainArray(int[] arr) {
3+
boolean isIncreasing = true;
4+
for (int index = 1 ; index < arr.length ; index++) {
5+
if (arr[index] == arr[index - 1]) return false;
6+
if (arr[index] < arr[index - 1] && isIncreasing) {
7+
if (index == 1) return false;
8+
isIncreasing = false;
9+
} else if (arr[index] > arr[index - 1] && !isIncreasing) {
10+
return false;
11+
}
12+
}
13+
return !isIncreasing;
14+
}
15+
}

0 commit comments

Comments
 (0)