diff --git a/README.md b/README.md index 6094ede..16f3746 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/main/kotlin/exercise/easy/id977/Description977.md b/src/main/kotlin/exercise/easy/id977/Description977.md new file mode 100644 index 0000000..4a95039 --- /dev/null +++ b/src/main/kotlin/exercise/easy/id977/Description977.md @@ -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) diff --git a/src/main/kotlin/exercise/easy/id977/Solution977.kt b/src/main/kotlin/exercise/easy/id977/Solution977.kt new file mode 100644 index 0000000..254bb65 --- /dev/null +++ b/src/main/kotlin/exercise/easy/id977/Solution977.kt @@ -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[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() + } + + */ +} \ No newline at end of file diff --git a/src/test/kotlin/exercise/easy/id977/Solution977Test.kt b/src/test/kotlin/exercise/easy/id977/Solution977Test.kt new file mode 100644 index 0000000..8b3188a --- /dev/null +++ b/src/test/kotlin/exercise/easy/id977/Solution977Test.kt @@ -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 + } + } +}) \ No newline at end of file