Skip to content

Commit 37f900a

Browse files
authored
Merge pull request #9 from kotler-dev/exercise/id977
Exercise/id977
2 parents a33ede9 + f5e082b commit 37f900a

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)
4747

4848
#### Array
4949

50-
| ID | Description | Solution | Test | Difficulty |
51-
|:---:|:----------------------------------------------------------------------------------------------|:-----------------------------------------------------------------:|:-----------------------------------------------------------------:|:----------:|
50+
| ID | Description | Solution | Test | Difficulty |
51+
|:---:|:----------------------------------------------------------------------------------------------|:--------------------------------------------------------------:|:--------------------------------------------------------------:|:----------:|
5252
| 1 | [Two Sum](src/main/kotlin/exercise/easy/id1/Description1.md) | [solution](src/main/kotlin/exercise/easy/id1/Solution1.kt) | [test](src/test/kotlin/exercise/easy/id1/Solution1Test.kt) | Easy |
5353
| 121 | [Best Time to Buy and Sell stock Easy](src/main/kotlin/exercise/easy/id121/Description121.md) | [solution](src/main/kotlin/exercise/easy/id121/Solution121.kt) | [test](src/test/kotlin/exercise/easy/id121/Solution121Test.kt) | Easy |
5454
| 169 | [Majority Element](src/main/kotlin/exercise/easy/id169/Description169.md) | [solution](src/main/kotlin/exercise/easy/id169/Solution169.kt) | [test](src/test/kotlin/exercise/easy/id169/Solution169Test.kt) | Easy |
5555
| 217 | [Contains Duplicate](src/main/kotlin/exercise/easy/id217/Description217.md) | [solution](src/main/kotlin/exercise/easy/id217/Solution217.kt) | [test](src/test/kotlin/exercise/easy/id217/Solution217Test.kt) | Easy |
5656
| 283 | [Move Zeroes](src/main/kotlin/exercise/easy/id283/Description283.md) | [solution](src/main/kotlin/exercise/easy/id283/Solution283.kt) | [test](src/test/kotlin/exercise/easy/id283/Solution283Test.kt) | Easy |
57+
| 977 | [Squares of a Sorted Array](src/main/kotlin/exercise/easy/id977/Description977.md) | [solution](src/main/kotlin/exercise/easy/id977/Solution977.kt) | [test](src/test/kotlin/exercise/easy/id977/Solution977Test.kt) | Easy |
5758

5859
[//]: # (https://www.techinterviewhandbook.org/grind75?weeks=26&hours=40&grouping=topics)
5960

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)