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 fcd6150

Browse files
3254: Find the Power of K-Size Subarrays I
1 parent 92be855 commit fcd6150

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ My solutions to LeetCode problems in Kotlin.
3131
| [2914](https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/) | [Minimum Number of Changes to Make Binary String Beautiful](src/main/kotlin/com/schmoczer/leetcode/_2914/MinChanges.kt) | Medium |
3232
| [3011](https://leetcode.com/problems/find-if-array-can-be-sorted/) | [Find if Array Can Be Sorted](src/main/kotlin/com/schmoczer/leetcode/_3011/FindIfArrayCanBeSorted.kt) | Medium |
3333
| [3163](https://leetcode.com/problems/string-compression-iii/) | [String Compression III](src/main/kotlin/com/schmoczer/leetcode/_3163/StringCompression3.kt) | Medium |
34+
| [3254](https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/) | [Find the Power of K-Size Subarrays I](src/main/kotlin/com/schmoczer/leetcode/_3254/FindThePowerOfKSizeSubarrays.kt) | Medium |
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.schmoczer.leetcode._3254
2+
3+
class FindThePowerOfKSizeSubarrays {
4+
// Approach 1: Runtime 1ms Beats 100.00%
5+
fun resultsArray(nums: IntArray, k: Int): IntArray {
6+
if (k == 1) {
7+
return nums
8+
}
9+
10+
val powers = IntArray(nums.size - k + 1) { -1 }
11+
var begin = 0
12+
var previousValue = nums[0]
13+
for (i in 1 until nums.size) {
14+
val currentValue = nums[i]
15+
if (currentValue == previousValue + 1) {
16+
if (i - k + 1 == begin) {
17+
powers[i - k + 1] = currentValue
18+
begin++
19+
}
20+
} else {
21+
begin = i
22+
}
23+
previousValue = currentValue
24+
}
25+
return powers
26+
}
27+
28+
// Approach 2: using a ArrayDeque
29+
fun resultsArray2(nums: IntArray, k: Int): IntArray {
30+
if (k == 1) {
31+
return nums
32+
}
33+
34+
val powers = IntArray(nums.size - k + 1) { -1 }
35+
val queue = ArrayDeque<Int>()
36+
for (i in 0 until nums.size) {
37+
val current = nums[i]
38+
if (queue.isEmpty()) {
39+
queue.add(current)
40+
} else {
41+
val previous = queue.last()
42+
if (current != previous + 1) {
43+
queue.clear()
44+
}
45+
queue.add(current)
46+
if (queue.size == k) {
47+
powers[i - k + 1] = current
48+
queue.removeFirst()
49+
}
50+
}
51+
}
52+
53+
return powers
54+
}
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Find the Power of K-Size Subarrays I
2+
3+
You are given an array of integers `nums` of length `n` and a positive integer `k`.
4+
5+
The power of an array is defined as:
6+
7+
- Its maximum element if all of its elements are consecutive and sorted in ascending order.
8+
- -1 otherwise.
9+
10+
You need to find the power of all subarrays of `nums` of size `k`.
11+
12+
Return an integer array `results` of size `n - k + 1`, where `results[i]` is the power of `nums[i..(i + k - 1)]`.
13+
14+
Example 1:
15+
16+
> Input: nums = [1,2,3,4,3,2,5], k = 3
17+
>
18+
> Output: [3,4,-1,-1,-1]
19+
20+
Example 2:
21+
22+
> Input: nums = [2,2,2,2,2], k = 4
23+
>
24+
> Output: [-1,-1]
25+
26+
Example 3:
27+
28+
> Input: nums = [3,2,3,2,3,2], k = 2
29+
>
30+
> Output: [-1,3,-1,3,-1]
31+
32+
Constraints:
33+
34+
- `1 <= n == nums.length <= 500`
35+
- `1 <= nums[i] <= 10^5`
36+
- `1 <= k <= n`
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.schmoczer.leetcode._3254
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertContentEquals
6+
7+
class FindThePowerOfKSizeSubarraysTest {
8+
private lateinit var sut: FindThePowerOfKSizeSubarrays
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = FindThePowerOfKSizeSubarrays()
13+
}
14+
15+
@Test
16+
fun `power of all sub-arrays of size 3 of 1,2,3,4,3,2,5 is 3,4,-1,-1,-1`() {
17+
val input = intArrayOf(1, 2, 3, 4, 3, 2, 5)
18+
val k = 3
19+
var expected = intArrayOf(3, 4, -1, -1, -1)
20+
21+
val result = sut.resultsArray(input, k)
22+
23+
assertContentEquals(expected, result)
24+
}
25+
26+
@Test
27+
fun `power of all sub-arrays of size 4 of 2,2,2,2,2 is -1,-1`() {
28+
val input = intArrayOf(2, 2, 2, 2, 2)
29+
val k = 4
30+
var expected = intArrayOf(-1, -1)
31+
32+
val result = sut.resultsArray(input, k)
33+
34+
assertContentEquals(expected, result)
35+
}
36+
37+
@Test
38+
fun `power of all sub-arrays of size 2 of 3,2,3,2,3,2 is -1,3,-1,3,-1`() {
39+
val input = intArrayOf(3, 2, 3, 2, 3, 2)
40+
val k = 2
41+
var expected = intArrayOf(-1, 3, -1, 3, -1)
42+
43+
val result = sut.resultsArray(input, k)
44+
45+
assertContentEquals(expected, result)
46+
}
47+
}

0 commit comments

Comments
(0)

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