Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bbf4d95
Exercise/id1 (#path)
kotler-dev Oct 21, 2023
2747db9
exercise/id121
kotler-dev Oct 22, 2023
6549a05
Merge pull request #3 from kotler-dev/exercise/id121
kotler-dev Oct 22, 2023
a812d99
exercise/id169
kotler-dev Oct 22, 2023
485fafe
Merge pull request #4 from kotler-dev/exercise/id169
kotler-dev Oct 22, 2023
8f5edf5
exercise/id169
kotler-dev Oct 22, 2023
2ab925c
Merge pull request #5 from kotler-dev/exercise/id169
kotler-dev Oct 22, 2023
aa6251f
Update path
kotler-dev Oct 22, 2023
1c708db
exercise/id217
kotler-dev Oct 22, 2023
0fb8637
Merge pull request #6 from kotler-dev/exercise/id217
kotler-dev Oct 22, 2023
f820e34
exercise/id283
kotler-dev Oct 22, 2023
ebe1e67
Merge pull request #7 from kotler-dev/exercise/id283
kotler-dev Oct 22, 2023
d52afa8
exercise/id283
kotler-dev Oct 23, 2023
a33ede9
Merge pull request #8 from kotler-dev/exercise/id283
kotler-dev Oct 23, 2023
817719f
exercise/id977
kotler-dev Oct 23, 2023
ad64e79
Merge remote-tracking branch 'origin/master' into exercise/id977
kotler-dev Oct 23, 2023
f5e082b
exercise/id977
kotler-dev Oct 23, 2023
37f900a
Merge pull request #9 from kotler-dev/exercise/id977
kotler-dev Oct 23, 2023
6fc09cd
exercise/id57
kotler-dev Oct 25, 2023
c7fc5d2
exercise/id57
kotler-dev Oct 25, 2023
7ec6e1b
Merge pull request #10 from kotler-dev/exercice/id57
kotler-dev Oct 25, 2023
00f23e9
exercise/id3
kotler-dev Oct 25, 2023
3975d76
exercise/id3
kotler-dev Oct 26, 2023
7e715e2
Merge pull request #11 from kotler-dev/exercise/id3
kotler-dev Oct 26, 2023
b45c728
exercise/id15
kotler-dev Oct 27, 2023
029794a
Merge pull request #12 from kotler-dev/exercise/id15
kotler-dev Oct 27, 2023
729f38d
exercise/id238
kotler-dev Oct 28, 2023
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
37 changes: 18 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)

[//]: # (- [Linked List](#linked-list))

[//]: # (- [String](#string))
- [String](#string)

[//]: # (- [Binary Tree](#binary-tree))

Expand Down Expand Up @@ -47,24 +47,23 @@ 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]() | [solution]() | [test]() | Easy |
| 169 | [Majority Element]() | [solution]() | [test]() | Easy |
| 1000 | [Contains Duplicate]() | [solution]() | [test]() | Easy |

### Template

| ID | Description | Solution | Test | Difficulty |
|:----:|:--------------------------------------------------------------|:-------------------------------------------------------------:|:-------------------------------------------------------------:|:----------:|
| 0000 | [Title](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 |
| 0000 | [Title](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) | Medium |
| 0000 | [Title](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) | Hard |

![GitHub license](https://img.shields.io/badge/Difficulty-Easy-61904f)
![GitHub license](https://img.shields.io/badge/Difficulty-Medium-548af7)
![GitHub license](https://img.shields.io/badge/Difficulty-Hard-a571e6)
| 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 |
| 15 | [3Sum](src/main/kotlin/exercise/medium/id15/Description15.md) | [solution](src/main/kotlin/exercise/medium/id15/Solution15.kt) | [test](src/test/kotlin/exercise/medium/id15/Solution15Test.kt) | Medium |
| 57 | [Insert Internal](src/main/kotlin/exercise/medium/id57/Description57.md) | [solution](src/main/kotlin/exercise/medium/id57/Solution57.kt) | [test](src/test/kotlin/exercise/medium/id57/Solution57Test.kt) | Medium |
| 238 | [Product of Array Except Self](src/main/kotlin/exercise/medium/id238/Description238.md) | [solution](src/main/kotlin/exercise/medium/id238/Solution238.kt) | [test](src/test/kotlin/exercise/medium/id238/Solution238Test.kt) | Medium |

#### String

| ID | Description | Solution | Test | Difficulty |
|:--:|:------------------------------------------------------------------------------------------------------|:------------------------------------------------------------:|:------------------------------------------------------------:|:----------:|
| 3 | [Longest Substring Without Repeating Characters](src/main/kotlin/exercise/medium/id3/Description3.md) | [solution](src/main/kotlin/exercise/medium/id3/Solution3.kt) | [test](src/test/kotlin/exercise/medium/id3/Solution3Test.kt) | Medium |

[//]: # (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 @@ -38,8 +38,8 @@ Constraints:
- `-10^9 <= target <= 10^9`
- Only one valid answer exist.

| Id | Description | Solution | Test | Difficulty |
| 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](../../../../../../../../../blob/master/README.md)
: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
37 changes: 37 additions & 0 deletions src/main/kotlin/exercise/easy/id121/Description121.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)

# Best Time to Buy and Sell Stock

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

You are given an array `prices` where `prices[i]` is the price of a given stock on the `i'th` day.
You want to maximize your profit by choosing a singl to buy one stock and choosing a different day in the future to sell
that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return `0`.

Example 1:

```
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (prices = 1) and sell on day 5 (price = 6), profit = 6 - 1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
```

Example 2:

```
Input: prices = [7,6,4,3,1]
Output: 0
```

Constraints:

- `1 <= prices.length <= 10^5`
- `0 <= prices[i] <= 10^4`

| ID | Description | Solution | Test | Difficulty |
|:---:|:--------------------------------|:----------------------------:|:-----------------------------------------------------------------------------------:|:----------:|
| 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)
115 changes: 115 additions & 0 deletions src/main/kotlin/exercise/easy/id121/Solution121.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package exercise.easy.id121

class Solution121 {
fun maxProfit(prices: IntArray): Int {
if (prices.size < 2) return 0
var profit = 0
var min = Int.MAX_VALUE
var max = 0

for (i in 0..<prices.size) {
if (prices[i] < min) {
min = prices[i]
max = 0
}
if (prices[i] > max) max = prices[i]
if (max - min > profit) profit = max - min
}
return profit
}

/*

fun maxProfit(prices: IntArray): Int {
if (prices.size < 2) return 0
var profit = 0
var min = Int.MAX_VALUE
var max = 0

val minn = prices.slice(1..<prices.size).minBy { it }
val m = prices.slice(1..<prices.size).maxBy { it }
val mIndex = prices.lastIndexOf(m)

for (i in 0..mIndex) {
if (prices[i] < min) {
min = prices[i]
max = m
}
if (prices[i] > max) max = prices[i]
if (max - min > profit) profit = max - min
}
return profit
}

fun maxProfit(prices: IntArray): Int {
if (prices.size < 2) return 0
var profit = 0
for (i in prices.indices) {
for (j in i + 1..<prices.size) {
if (prices[j] - prices[i] > profit) {
profit = prices[j] - prices[i]
}
}
}
return profit
}

fun maxProfit(prices: IntArray): Int {
if (prices.size < 2) return 0
var profit = 0

val min = prices.slice(0..prices.size - 1).indices.minBy { prices[it] }
val minIndex = prices.lastIndexOf(prices[min])
val p = prices.slice(minIndex..prices.size - 1)

val max = p.indices.maxBy { p[it] }
val maxIndex = p.lastIndexOf(p[max])

val result = p[maxIndex] - prices[minIndex]
if (result > profit) profit = result
return profit
}

fun maxProfit(prices: IntArray): Int {
var profit = 0
for ((index, value) in prices.withIndex()) {
val i1 = index + 1
if (i1 < prices.size) {
if (prices[i1] > value) {
val max = prices.slice(i1..prices.size - 1).max()
if (profit < max - value) {
profit = max - value
}
}
}
}
return profit
}

fun maxProfit(prices: IntArray): Int {
var profit = 0

val minIndex = prices.indices.minBy { prices[it] } ?: 0
val slice = prices.slice(minIndex..prices.size - 1)
val maxIndex = slice.indices.maxBy { slice[it] } ?: 0
val result = slice[maxIndex] - prices[minIndex]

if (result > profit) profit = result

return profit
}

fun maxProfit(prices: IntArray): Int {
if (prices.size < 2) return 0
var profit = 0
val maxSlice = prices.slice(1..prices.size - 1)
val maxIndex = maxSlice.indices.maxBy { maxSlice[it] } ?: 0
val minSlice = prices.slice(0..maxSlice.lastIndexOf(maxSlice[maxIndex]))
val minIndex = minSlice.indices.minBy { minSlice[it] } ?: 0
val result = maxSlice[maxIndex] - minSlice[minIndex]
if (result > profit) profit = result
return profit
}

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

# Majority Element

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

Given an array `nums` of size `n`, return the majority element.
The majority element is the element that appears more than `|n / 2|` times.
You may assume that the majority element always exists in the array.

Example 1:

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

Example 2:

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

Constraints:

- `n == nums.length`
- `1 <= n <= 5 * 10^4`
- `-10^9 <= nums[i] <= 10^9`

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](../../../../../test/kotlin/exercise/easy/id169/Solution169Test.kt) | Easy |

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

class Solution169 {
fun majorityElement(nums: IntArray): Int {
val hm = HashMap<Int, Int>()
nums.forEach {
val n = hm[it]
if (n == null) hm[it] = 0
if (n != null) hm[it] = n + 1
}
return hm.maxBy { n -> n.value }.key
}

/*



*/
}
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)
Loading