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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)

#### Array

| ID | Description | Solution | Test | Difficulty |
|:----:|:-------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------:|:-----------------------------------------------------------------:|:----------:|
| 1 | [Two Sum](src/main/kotlin/exercise100/easy/id1/Description1.md) | [solution](src/main/kotlin/exercise100/easy/id1/Solution1.kt) | [test](src/test/kotlin/exercise100/easy/id1/Solution1Test.kt) | Easy |
| 121 | [Best Time to Buy and Sell stock Easy](src/main/kotlin/exercise100/easy/id121/Description121.md) | [solution](src/main/kotlin/exercise100/easy/id121/Solution121.kt) | [test](src/test/kotlin/exercise100/easy/id121/Solution121Test.kt) | Easy |
| 169 | [Majority Element](src/main/kotlin/exercise100/easy/id169/Description169.md) | [solution](src/main/kotlin/exercise100/easy/id169/Solution169.kt) | [test](src/test/kotlin/exercise100/easy/id169/Solution169Test.kt) | Easy |
| 1000 | [Contains Duplicate]() | [solution]() | [test]() | Easy |
| 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 |

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ Constraints:

| ID | Description | Solution | Test | Difficulty |
|:--:|:------------|:--------------------------:|:-------------------------------------------------------------------------------:|:----------:|
| 1 | Two Sum | [solution](./Solution1.kt) | [test](../../../../../../src/test/kotlin/exercise100/easy/id1/Solution1Test.kt) | Easy |
| 1 | Two Sum | [solution](./Solution1.kt) | [test](../../../../../../src/test/kotlin/exercise/easy/id1/Solution1Test.kt) | Easy |

:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exercise100.easy.id1
package exercise.easy.id1

class Solution1 {
fun twoSum(nums: IntArray, target: Int): IntArray {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ Constraints:

| ID | Description | Solution | Test | Difficulty |
|:---:|:--------------------------------|:----------------------------:|:-----------------------------------------------------------------------------------:|:----------:|
| 121 | Best Time to Buy and Sell Stock | [solution](./Solution121.kt) | [test](../../../../../../src/test/kotlin/exercise100/easy/id121/Solution121Test.kt) | Easy |
| 121 | Best Time to Buy and Sell Stock | [solution](./Solution121.kt) | [test](../../../../../../src/test/kotlin/exercise/easy/id121/Solution121Test.kt) | Easy |

:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exercise100.easy.id121
package exercise.easy.id121

class Solution121 {
fun maxProfit(prices: IntArray): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ Follow-up: Could you solve the problem in linear time and in `O(1)` space?

| ID | Description | Solution | Test | Difficulty |
|:---:|:-----------------|:----------------------------:|:-----------------------------------------------------------------------------------:|:----------:|
| 169 | Majority Element | [solution](./Solution169.kt) | [test](../../../../../../src/test/kotlin/exercise100/easy/id169/Solution169Test.kt) | Easy |
| 169 | Majority Element | [solution](./Solution169.kt) | [test](../../../../../test/kotlin/exercise/easy/id169/Solution169Test.kt) | Easy |

:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exercise100.easy.id169
package exercise.easy.id169

class Solution169 {
fun majorityElement(nums: IntArray): Int {
Expand Down
40 changes: 40 additions & 0 deletions src/main/kotlin/exercise/easy/id217/Description217.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)

# Contains Duplicates

Given an integer array `nums`, return `true` if any value appears at least twice in the array, and return `false` if
every element is distinct.

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

Example 1:

```
Input: nums = [1,2,3,1]
Output: true
```

Example 2:

```
Input: nums = [1,2,3,4]
Output: false
```

Example 3:

```
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
```

Constraints:

- `1 <= nums.legth <= 10^5`
- `-10^9 <= nums[i] <= 10^9`

| ID | Description | Solution | Test | Difficulty |
|:---:|:--------------------|:----------------------------:|:--------------------------------------------------------------------------------:|:----------:|
| 217 | Contains Duplicates | [solution](./Solution217.kt) | [test](../../../../../../src/test/kotlin/exercise/easy/id217/Solution217Test.kt) | Easy |

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

class Solution217 {
fun containsDuplicate(nums: IntArray) = nums.size > nums.toSet().size

/*

fun containsDuplicate(nums: IntArray): Boolean {
val hashSet = HashSet<Int>()
nums.forEach {
if (!hashSet.add(it))
return true
}
return false
}

fun containsDuplicate(nums: IntArray): Boolean {
if (nums.size < 2) return false
val hs = HashSet<Int>()
var end = nums.size - 1
var index = 0
var middle = nums.size / 2

if (end % 2 == 0) {
hs.add(nums[index])
middle++
index++
}

for (start in index..<end) {
if (!hs.contains(nums[start])) hs.add(nums[start]) else return true
if (!hs.contains(nums[end])) hs.add(nums[end]) else return true
if (end == middle) return false
end--
}
return false
}

fun containsDuplicate(nums: IntArray): Boolean {
if (nums.size < 2) return false
val hs = HashSet<Int>()
var end = nums.size - 1
val middle = nums.size / 2

if (end >= 4) hs.add(nums[middle])

for (start in 0..middle) {
if (!hs.contains(nums[start])) hs.add(nums[start]) else return true
if (!hs.contains(nums[end])) hs.add(nums[end]) else return true
if (end == middle) return false
end--
}
return false
}

fun containsDuplicate(nums: IntArray): Boolean {
val hs = HashSet<Int>()
var end = nums.size - 1
var backStep = nums.size / 2
var forwardStep = backStep + 1

if ((backStep - 1) % 2 != 0) {
hs.add(nums[backStep])
backStep -= 1
forwardStep += 1
}

for (start in 0..<nums.size / 2) {
if (!hs.contains(nums[start])) hs.add(nums[start]) else return true
if (!hs.contains(nums[end])) hs.add(nums[end--]) else return true
if (nums.size >= 42) {
if (!hs.contains(nums[backStep])) hs.add(nums[backStep--]) else return true
if (!hs.contains(nums[forwardStep])) hs.add(nums[forwardStep++]) else return true
}
}
return false
}

fun containsDuplicate(nums: IntArray): Boolean {
val hs = HashSet<Int>()
val first = 0
var end = nums.size - 1
var backStep = nums.size / 2 - 1
var forwardStep = backStep + 1

for (start in 0..nums.size / 2 - 1) {
if (!hs.contains(nums[start])) hs.add(nums[start]) else return true
if (!hs.contains(nums[end])) hs.add(nums[end]) else return true
if (nums.size > 3) {
if (!hs.contains(nums[backStep])) hs.add(nums[backStep]) else return true
if (!hs.contains(nums[forwardStep])) hs.add(nums[forwardStep]) else return true
if (backStep - 1 != start) backStep--
if (forwardStep + 1 != end) forwardStep++
}
if (end - 1 != forwardStep) end--
}
return false
}

fun containsDuplicate(nums: IntArray): Boolean {
var flag = false
var dupl = -1
for ((index, value) in nums.withIndex()) {
if (flag == false) {
if (index == nums.size - 1) return false
flag = searchDuplicate(nums.slice(index+1..nums.size - 1), value)
} else {
return flag
}
}
return false
}

fun searchDuplicate(arr: List<Int>, dupl: Int): Boolean {
arr.forEach {
if (it == dupl) {
println("arr:$arr, dupl$dupl")
return true
}
}
return false
}

*/
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exercise100.easy.id1
package exercise.easy.id1

import io.kotest.assertions.print.print
import io.kotest.core.spec.style.FunSpec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exercise100.easy.id121
package exercise.easy.id121

import io.kotest.assertions.print.print
import io.kotest.core.spec.style.FunSpec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exercise100.easy.id169
package exercise.easy.id169

import io.kotest.assertions.print.print
import io.kotest.core.spec.style.FunSpec
Expand Down
36 changes: 36 additions & 0 deletions src/test/kotlin/exercise/easy/id217/Solution217Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package exercise.easy.id217

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: Boolean,
) : WithDataTestName {
override fun dataTestName(): String = expected.print().value
}

class Solution217Test : FunSpec({
context("shouldBe") {
withData(
nameFn = { "Input: ${it.nums.print().value}, Expected: ${it.expected.print().value}" },
TestCase(nums = intArrayOf(0), expected = false),
TestCase(nums = intArrayOf(0, 0), expected = true),
TestCase(nums = intArrayOf(3, 1), expected = false),
TestCase(nums = intArrayOf(3, 2, 3), expected = true),
TestCase(nums = intArrayOf(1, 2, 3, 1), expected = true),
TestCase(nums = intArrayOf(1, 2, 3, 4), expected = false),
TestCase(nums = intArrayOf(0, 1, 2, 3, 7, 4, 5, 42, 101, 6, 8, 7, 11, 12, 13, 14), expected = true),
TestCase(nums = intArrayOf(1, 1, 1, 3, 3, 4, 3, 2, 4, 2), expected = true),
TestCase(nums = intArrayOf(13, 18, 22, 22), expected = true),
TestCase(nums = intArrayOf(2, 14, 18, 22, 22), expected = true),
TestCase(nums = intArrayOf(1, 5, -2, -4, 0), expected = false),
TestCase(nums = intArrayOf(1000000000, 1000000000, 11), expected = true),
) { (prices, expected) ->
Solution217().containsDuplicate(prices) shouldBe expected
}
}
})