Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a306ed2

Browse files
941: Valid Mountain Array
1 parent 08194ef commit a306ed2

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ My solutions to LeetCode problems in Kotlin.
3030
| [773](https://leetcode.com/problems/sliding-puzzle/) | [Sliding Puzzle](src/main/kotlin/com/schmoczer/leetcode/_0773/SlidingPuzzle.kt) | Hard |
3131
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
3232
| [876](https://leetcode.com/problems/middle-of-the-linked-list/) | [Middle of the Linked List](src/main/kotlin/com/schmoczer/leetcode/_0876/MiddleOfTheLinkedList.kt) | Easy |
33+
| [941](https://leetcode.com/problems/valid-mountain-array/) | [Valid Mountain Array](src/main/kotlin/com/schmoczer/leetcode/_0941/ValidMountainArray.kt) | Easy |
3334
| [977](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Squares of a Sorted Array](src/main/kotlin/com/schmoczer/leetcode/_0977/SquaresOfASortedArray.kt) | Easy |
3435
| [1089](https://leetcode.com/problems/duplicate-zeros/) | [Duplicate Zeros](src/main/kotlin/com/schmoczer/leetcode/_1089/DuplicateZeros.kt) | Easy |
3536
| [1295](https://leetcode.com/problems/find-numbers-with-even-number-of-digits/) | [Find Numbers with Even Number of Digits](src/main/kotlin/com/schmoczer/leetcode/_1295/FindNumbersWithEvenNumberOfDigits.kt) | Easy |
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Valid Mountain Array
2+
3+
Given an array of integers `arr`, return `true` if and only if it is a valid mountain array.
4+
5+
Recall that arr is a mountain array if and only if:
6+
7+
- `arr.length >= 3`
8+
- There exists some `i` with `0 < i < arr.length - 1` such that:
9+
- `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
10+
- `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
11+
12+
Example 1:
13+
14+
> Input: arr = [2,1]
15+
>
16+
> Output: false
17+
18+
Example 2:
19+
20+
> Input: arr = [3,5,5]
21+
>
22+
> Output: false
23+
24+
Example 3:
25+
26+
> Input: arr = [0,3,2,1]
27+
>
28+
> Output: true
29+
30+
31+
Constraints:
32+
33+
- `1 <= arr.length <= 10^4`
34+
- `0 <= arr[i] <= 10^4`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.schmoczer.leetcode._0941
2+
3+
class ValidMountainArray {
4+
// Runtime 184ms Beats 89.11%
5+
fun validMountainArray(arr: IntArray): Boolean {
6+
if (arr.size < 3) {
7+
return false
8+
}
9+
10+
var max = -1
11+
var maxInd = 0
12+
// find first local max
13+
for (index in 0 until arr.size) {
14+
val num = arr[index]
15+
if (num > max) {
16+
max = num
17+
maxInd = index
18+
} else {
19+
break
20+
}
21+
}
22+
23+
if (arr[0] == max || arr.last() == max) {
24+
return false
25+
}
26+
27+
// check if all elements after our first max are really descending
28+
for (i in maxInd until arr.lastIndex) {
29+
if (arr[i] <= arr[i + 1]) {
30+
return false
31+
}
32+
}
33+
34+
return true
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.schmoczer.leetcode._0941
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.BeforeEach
5+
import org.junit.jupiter.params.ParameterizedTest
6+
import org.junit.jupiter.params.provider.Arguments
7+
import org.junit.jupiter.params.provider.MethodSource
8+
9+
class ValidMountainArrayTest {
10+
private companion object {
11+
@JvmStatic
12+
fun mountainArrays() = listOf(
13+
Arguments.of(intArrayOf(2, 1), false),
14+
Arguments.of(intArrayOf(3, 5, 5), false),
15+
Arguments.of(intArrayOf(0, 3, 2, 1), true),
16+
)
17+
}
18+
19+
private lateinit var sut: ValidMountainArray
20+
21+
@BeforeEach
22+
fun setUp() {
23+
sut = ValidMountainArray()
24+
}
25+
26+
@ParameterizedTest(name = "{0} is a mountain array: {1}")
27+
@MethodSource("mountainArrays")
28+
fun `returns true if it is a valid mountain array`(input: IntArray, expected: Boolean) {
29+
val result = sut.validMountainArray(input)
30+
31+
assertEquals(expected, result)
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /