Skip to content

Commit 0fb8637

Browse files
authored
Merge pull request #6 from kotler-dev/exercise/id217
exercise/id217
2 parents 2ab925c + 1c708db commit 0fb8637

File tree

13 files changed

+216
-15
lines changed

13 files changed

+216
-15
lines changed

README.md

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

4848
#### Array
4949

50-
| ID | Description | Solution | Test | Difficulty |
51-
|:----:|:-------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------:|:-----------------------------------------------------------------:|:----------:|
52-
| 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 |
53-
| 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 |
54-
| 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 |
55-
| 1000 | [Contains Duplicate]() | [solution]() | [test]() | Easy |
50+
| ID | Description | Solution | Test | Difficulty |
51+
|:---:|:-------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------:|:-----------------------------------------------------------------:|:----------:|
52+
| 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 |
53+
| 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 |
54+
| 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 |
55+
| 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

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

src/main/kotlin/exercise100/easy/id1/Description1.md renamed to src/main/kotlin/exercise/easy/id1/Description1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ Constraints:
4040

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

4545
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)

src/main/kotlin/exercise100/easy/id1/Solution1.kt renamed to src/main/kotlin/exercise/easy/id1/Solution1.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package exercise100.easy.id1
1+
package exercise.easy.id1
22

33
class Solution1 {
44
fun twoSum(nums: IntArray, target: Int): IntArray {

src/main/kotlin/exercise100/easy/id121/Description121.md renamed to src/main/kotlin/exercise/easy/id121/Description121.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ Constraints:
3232

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

3737
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)

src/main/kotlin/exercise100/easy/id121/Solution121.kt renamed to src/main/kotlin/exercise/easy/id121/Solution121.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package exercise100.easy.id121
1+
package exercise.easy.id121
22

33
class Solution121 {
44
fun maxProfit(prices: IntArray): Int {

src/main/kotlin/exercise100/easy/id169/Description169.md renamed to src/main/kotlin/exercise/easy/id169/Description169.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ Follow-up: Could you solve the problem in linear time and in `O(1)` space?
3232

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

3737
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)

src/main/kotlin/exercise100/easy/id169/Solution169.kt renamed to src/main/kotlin/exercise/easy/id169/Solution169.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package exercise100.easy.id169
1+
package exercise.easy.id169
22

33
class Solution169 {
44
fun majorityElement(nums: IntArray): Int {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)
2+
3+
# Contains Duplicates
4+
5+
Given an integer array `nums`, return `true` if any value appears at least twice in the array, and return `false` if
6+
every element is distinct.
7+
8+
![Difficulty](https://img.shields.io/badge/Difficulty-Easy-61904f)
9+
10+
Example 1:
11+
12+
```
13+
Input: nums = [1,2,3,1]
14+
Output: true
15+
```
16+
17+
Example 2:
18+
19+
```
20+
Input: nums = [1,2,3,4]
21+
Output: false
22+
```
23+
24+
Example 3:
25+
26+
```
27+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
28+
Output: true
29+
```
30+
31+
Constraints:
32+
33+
- `1 <= nums.legth <= 10^5`
34+
- `-10^9 <= nums[i] <= 10^9`
35+
36+
| ID | Description | Solution | Test | Difficulty |
37+
|:---:|:--------------------|:----------------------------:|:--------------------------------------------------------------------------------:|:----------:|
38+
| 217 | Contains Duplicates | [solution](./Solution217.kt) | [test](../../../../../../src/test/kotlin/exercise/easy/id217/Solution217Test.kt) | Easy |
39+
40+
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package exercise.easy.id217
2+
3+
class Solution217 {
4+
fun containsDuplicate(nums: IntArray) = nums.size > nums.toSet().size
5+
6+
/*
7+
8+
fun containsDuplicate(nums: IntArray): Boolean {
9+
val hashSet = HashSet<Int>()
10+
nums.forEach {
11+
if (!hashSet.add(it))
12+
return true
13+
}
14+
return false
15+
}
16+
17+
fun containsDuplicate(nums: IntArray): Boolean {
18+
if (nums.size < 2) return false
19+
val hs = HashSet<Int>()
20+
var end = nums.size - 1
21+
var index = 0
22+
var middle = nums.size / 2
23+
24+
if (end % 2 == 0) {
25+
hs.add(nums[index])
26+
middle++
27+
index++
28+
}
29+
30+
for (start in index..<end) {
31+
if (!hs.contains(nums[start])) hs.add(nums[start]) else return true
32+
if (!hs.contains(nums[end])) hs.add(nums[end]) else return true
33+
if (end == middle) return false
34+
end--
35+
}
36+
return false
37+
}
38+
39+
fun containsDuplicate(nums: IntArray): Boolean {
40+
if (nums.size < 2) return false
41+
val hs = HashSet<Int>()
42+
var end = nums.size - 1
43+
val middle = nums.size / 2
44+
45+
if (end >= 4) hs.add(nums[middle])
46+
47+
for (start in 0..middle) {
48+
if (!hs.contains(nums[start])) hs.add(nums[start]) else return true
49+
if (!hs.contains(nums[end])) hs.add(nums[end]) else return true
50+
if (end == middle) return false
51+
end--
52+
}
53+
return false
54+
}
55+
56+
fun containsDuplicate(nums: IntArray): Boolean {
57+
val hs = HashSet<Int>()
58+
var end = nums.size - 1
59+
var backStep = nums.size / 2
60+
var forwardStep = backStep + 1
61+
62+
if ((backStep - 1) % 2 != 0) {
63+
hs.add(nums[backStep])
64+
backStep -= 1
65+
forwardStep += 1
66+
}
67+
68+
for (start in 0..<nums.size / 2) {
69+
if (!hs.contains(nums[start])) hs.add(nums[start]) else return true
70+
if (!hs.contains(nums[end])) hs.add(nums[end--]) else return true
71+
if (nums.size >= 42) {
72+
if (!hs.contains(nums[backStep])) hs.add(nums[backStep--]) else return true
73+
if (!hs.contains(nums[forwardStep])) hs.add(nums[forwardStep++]) else return true
74+
}
75+
}
76+
return false
77+
}
78+
79+
fun containsDuplicate(nums: IntArray): Boolean {
80+
val hs = HashSet<Int>()
81+
val first = 0
82+
var end = nums.size - 1
83+
var backStep = nums.size / 2 - 1
84+
var forwardStep = backStep + 1
85+
86+
for (start in 0..nums.size / 2 - 1) {
87+
if (!hs.contains(nums[start])) hs.add(nums[start]) else return true
88+
if (!hs.contains(nums[end])) hs.add(nums[end]) else return true
89+
if (nums.size > 3) {
90+
if (!hs.contains(nums[backStep])) hs.add(nums[backStep]) else return true
91+
if (!hs.contains(nums[forwardStep])) hs.add(nums[forwardStep]) else return true
92+
if (backStep - 1 != start) backStep--
93+
if (forwardStep + 1 != end) forwardStep++
94+
}
95+
if (end - 1 != forwardStep) end--
96+
}
97+
return false
98+
}
99+
100+
fun containsDuplicate(nums: IntArray): Boolean {
101+
var flag = false
102+
var dupl = -1
103+
for ((index, value) in nums.withIndex()) {
104+
if (flag == false) {
105+
if (index == nums.size - 1) return false
106+
flag = searchDuplicate(nums.slice(index+1..nums.size - 1), value)
107+
} else {
108+
return flag
109+
}
110+
}
111+
return false
112+
}
113+
114+
fun searchDuplicate(arr: List<Int>, dupl: Int): Boolean {
115+
arr.forEach {
116+
if (it == dupl) {
117+
println("arr:$arr, dupl$dupl")
118+
return true
119+
}
120+
}
121+
return false
122+
}
123+
124+
*/
125+
}

src/test/kotlin/exercise100/easy/id1/Solution1Test.kt renamed to src/test/kotlin/exercise/easy/id1/Solution1Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package exercise100.easy.id1
1+
package exercise.easy.id1
22

33
import io.kotest.assertions.print.print
44
import io.kotest.core.spec.style.FunSpec

0 commit comments

Comments
 (0)