Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)

#### Array

| ID | Description | Solution | Test | Difficulty |
|:---:|:----------------------------------------------------------------------------------------------|:-----------------------------------------------------------------:|:-----------------------------------------------------------------:|:----------:|
| ID | Description | Solution | Test | Difficulty |
|:---:|:----------------------------------------------------------------------------------------------|:--------------------------------------------------------------:|:--------------------------------------------------------------:|:----------:|
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |

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

Expand Down
36 changes: 36 additions & 0 deletions src/main/kotlin/exercise/easy/id977/Description977.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)

# Squares of a Sorted Array

Given an integer array `nums` sorted in non-decreasing order, return an array of the squares of each number sorted in
non-decreasing order.

![Difficulty](https://img.shields.io/badge/Difficulty-Easy-61904f)

Example 1:

```
Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
```

Example 2:

```
Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]
```

Constraints:

- `1 <= nums.length <= 104`
- `-104 <= nums[i] <= 104`
- `nums is sorted in non-decreasing order`

| ID | Description | Solution | Test | Difficulty |
|:---:|:--------------------------|:----------------------------:|:--------------------------------------------------------------------------------:|:----------:|
| 977 | Squares of a Sorted Array | [solution](./Solution977.kt) | [test](../../../../../../src/test/kotlin/exercise/easy/id977/Solution977Test.kt) | Easy |

:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
31 changes: 31 additions & 0 deletions src/main/kotlin/exercise/easy/id977/Solution977.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package exercise.easy.id977

class Solution977 {
fun sortedSquares(nums: IntArray): IntArray {
for (index in nums.indices) {
nums[index] *= nums[index]
}

var flag: Boolean
do {
flag = false
for (index in 1..<nums.size) {
val box = nums[index - 1]
if (nums[index - 1] > nums[index]) {
nums[index - 1] = nums[index]
nums[index] = box
flag = true
}
}
} while (flag)
return nums
}

/*

fun sortedSquares(nums: IntArray): IntArray {
return nums.toList().map { it * it }.sorted().toIntArray()
}

*/
}
26 changes: 26 additions & 0 deletions src/test/kotlin/exercise/easy/id977/Solution977Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package exercise.easy.id977

import io.kotest.assertions.print.print
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.WithDataTestName
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe

data class TestCase(
val nums: IntArray,
val expected: IntArray,
) : WithDataTestName {
override fun dataTestName(): String = expected.print().value
}

class Solution977Test : FunSpec({
context("shouldBe") {
withData(
nameFn = { "Input: ${it.nums.print().value}, Expected: ${it.expected.print().value}" },
TestCase(nums = intArrayOf(-4, -1, 0, 3, 10), expected = intArrayOf(0, 1, 9, 16, 100)),
TestCase(nums = intArrayOf(-7, -3, 2, 3, 11), expected = intArrayOf(4, 9, 9, 49, 121)),
) { (nums, expected) ->
Solution977().sortedSquares(nums) shouldBe expected
}
}
})