Skip to content

Commit 817719f

Browse files
committed
exercise/id977
1 parent 0fb8637 commit 817719f

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)
2+
3+
# Squares of a Sorted Array
4+
5+
Given an integer array `nums` sorted in non-decreasing order, return an array of the squares of each number sorted in
6+
non-decreasing order.
7+
8+
![Difficulty](https://img.shields.io/badge/Difficulty-Easy-61904f)
9+
10+
Example 1:
11+
12+
```
13+
Input: nums = [-4,-1,0,3,10]
14+
Output: [0,1,9,16,100]
15+
Explanation: After squaring, the array becomes [16,1,0,9,100].
16+
After sorting, it becomes [0,1,9,16,100].
17+
```
18+
19+
Example 2:
20+
21+
```
22+
Input: nums = [-7,-3,2,3,11]
23+
Output: [4,9,9,49,121]
24+
```
25+
26+
Constraints:
27+
28+
- `1 <= nums.length <= 104`
29+
- `-104 <= nums[i] <= 104`
30+
- `nums is sorted in non-decreasing order`
31+
32+
| ID | Description | Solution | Test | Difficulty |
33+
|:---:|:--------------------------|:----------------------------:|:--------------------------------------------------------------------------------:|:----------:|
34+
| 977 | Squares of a Sorted Array | [solution](./Solution977.kt) | [test](../../../../../../src/test/kotlin/exercise/easy/id977/Solution977Test.kt) | Easy |
35+
36+
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package exercise.easy.id977
2+
3+
class Solution977 {
4+
fun sortedSquares(nums: IntArray): IntArray {
5+
for (index in nums.indices) {
6+
nums[index] *= nums[index]
7+
}
8+
9+
var flag: Boolean
10+
do {
11+
flag = false
12+
for (index in 1..<nums.size) {
13+
val box = nums[index - 1]
14+
if (nums[index - 1] > nums[index]) {
15+
nums[index - 1] = nums[index]
16+
nums[index] = box
17+
flag = true
18+
}
19+
}
20+
} while (flag)
21+
return nums
22+
}
23+
24+
/*
25+
26+
fun sortedSquares(nums: IntArray): IntArray {
27+
return nums.toList().map { it * it }.sorted().toIntArray()
28+
}
29+
30+
*/
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package exercise.easy.id977
2+
3+
import io.kotest.assertions.print.print
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.datatest.WithDataTestName
6+
import io.kotest.datatest.withData
7+
import io.kotest.matchers.shouldBe
8+
9+
data class TestCase(
10+
val nums: IntArray,
11+
val expected: IntArray,
12+
) : WithDataTestName {
13+
override fun dataTestName(): String = expected.print().value
14+
}
15+
16+
class Solution977Test : FunSpec({
17+
context("shouldBe") {
18+
withData(
19+
nameFn = { "Input: ${it.nums.print().value}, Expected: ${it.expected.print().value}" },
20+
TestCase(nums = intArrayOf(-4, -1, 0, 3, 10), expected = intArrayOf(0, 1, 9, 16, 100)),
21+
TestCase(nums = intArrayOf(-7, -3, 2, 3, 11), expected = intArrayOf(4, 9, 9, 49, 121)),
22+
) { (nums, expected) ->
23+
Solution977().sortedSquares(nums) shouldBe expected
24+
}
25+
}
26+
})

0 commit comments

Comments
 (0)