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 33db3be

Browse files
977: Squares of a Sorted Array
1 parent 4369a68 commit 33db3be

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ My solutions to LeetCode problems in Kotlin.
2727
| [773](https://leetcode.com/problems/sliding-puzzle/) | [Sliding Puzzle](src/main/kotlin/com/schmoczer/leetcode/_0773/SlidingPuzzle.kt) | Hard |
2828
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
2929
| [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 |
30+
| [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 |
3031
| [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 |
3132
| [1342](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [Number of Steps to Reduce a Number to Zero](src/main/kotlin/com/schmoczer/leetcode/_1342/NumberOfStepsToReduceANumberToZero.kt) | Easy |
3233
| [1480](https://leetcode.com/problems/running-sum-of-1d-array/) | [Running Sum of 1d Array](src/main/kotlin/com/schmoczer/leetcode/_1480/RunningSumOf1dArray.kt) | Easy |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Squares of a Sorted Array
2+
3+
Given an integer array `nums` sorted in non-decreasing order, return an array of the squares of each number sorted in
4+
non-decreasing order.
5+
6+
Example 1:
7+
8+
> Input: nums = [-4,-1,0,3,10]
9+
>
10+
> Output: [0,1,9,16,100]
11+
12+
Example 2:
13+
14+
> Input: nums = [-7,-3,2,3,11]
15+
>
16+
> Output: [4,9,9,49,121]
17+
18+
19+
Constraints:
20+
21+
- `1 <= nums.length <= 10^4`
22+
- `-10^4 <= nums[i] <= 10^4`
23+
- `nums` is sorted in non-decreasing order
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.schmoczer.leetcode._0977
2+
3+
import kotlin.math.abs
4+
5+
class SquaresOfASortedArray {
6+
// Approach 1: two passes, first add all squares, then sort
7+
fun sortedSquares1(nums: IntArray): IntArray {
8+
val result = mutableListOf<Int>()
9+
for (number in nums) {
10+
result.add(number * number)
11+
}
12+
return result.sorted().toIntArray()
13+
}
14+
15+
// Approach 2: one pass with two pointers
16+
// Runtime 1ms Beats 100.00%
17+
fun sortedSquares(nums: IntArray): IntArray {
18+
val result = IntArray(nums.size)
19+
var left = 0
20+
var right = nums.size - 1
21+
var index = right
22+
while (left <= right) {
23+
val leftNumber = abs(nums[left])
24+
val rightNumber = nums[right]
25+
if (leftNumber > rightNumber) {
26+
result[index] = leftNumber * leftNumber
27+
left++
28+
} else {
29+
result[index] = rightNumber * rightNumber
30+
right--
31+
}
32+
index--
33+
}
34+
return result
35+
}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.schmoczer.leetcode._0977
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.Arguments
6+
import org.junit.jupiter.params.provider.MethodSource
7+
import kotlin.test.assertContentEquals
8+
9+
class SquaresOfASortedArrayTest {
10+
private companion object {
11+
@JvmStatic
12+
fun numbersAndSquares() = listOf(
13+
Arguments.of(intArrayOf(-4, -1, 0, 3, 10), intArrayOf(0, 1, 9, 16, 100)),
14+
Arguments.of(intArrayOf(-7, -3, 2, 3, 11), intArrayOf(4, 9, 9, 49, 121)),
15+
)
16+
}
17+
18+
private lateinit var sut: SquaresOfASortedArray
19+
20+
@BeforeEach
21+
fun setUp() {
22+
sut = SquaresOfASortedArray()
23+
}
24+
25+
@ParameterizedTest(name = "the sorted squares of {0} are {1}")
26+
@MethodSource("numbersAndSquares")
27+
fun `returns the squares of the input array sorted in non-decreasing order`(input: IntArray, expected: IntArray) {
28+
val result = sut.sortedSquares(input)
29+
30+
assertContentEquals(expected, result)
31+
}
32+
}

0 commit comments

Comments
(0)

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