Skip to content

Commit 58b988a

Browse files
authored
Added tasks 216, 217, 218, 219.
1 parent 9e51072 commit 58b988a

File tree

13 files changed

+395
-0
lines changed

13 files changed

+395
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
419419

420420
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
421421
|-|-|-|-|-|-
422+
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate/Solution.kt)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49
422423

423424
#### Day 12 Class and Object
424425

@@ -942,6 +943,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
942943
| 0121 |[Best Time to Buy and Sell Stock](src/main/kotlin/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 609 | 94.06
943944
| 0283 |[Move Zeroes](src/main/kotlin/g0201_0300/s0283_move_zeroes/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 516 | 79.07
944945
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 234 | 92.75
946+
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate/Solution.kt)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49
945947
| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word/Solution.kt)| Easy | String | 243 | 63.33
946948
| 0122 |[Best Time to Buy and Sell Stock II](src/main/kotlin/g0101_0200/s0122_best_time_to_buy_and_sell_stock_ii/Solution.kt)| Medium | Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 370 | 16.98
947949
| 0080 |[Remove Duplicates from Sorted Array II](src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/Solution.kt)| Medium | Array, Two_Pointers | 357 | 44.78
@@ -1059,6 +1061,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
10591061
|-|-|-|-|-|-
10601062
| 0022 |[Generate Parentheses](src/main/kotlin/g0001_0100/s0022_generate_parentheses/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking | 210 | 78.51
10611063
| 0039 |[Combination Sum](src/main/kotlin/g0001_0100/s0039_combination_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Backtracking | 317 | 86.85
1064+
| 0216 |[Combination Sum III](src/main/kotlin/g0201_0300/s0216_combination_sum_iii/Solution.kt)| Medium | Array, Backtracking | 175 | 90.91
10621065
| 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking | 353 | 30.14
10631066
| 0017 |[Letter Combinations of a Phone Number](src/main/kotlin/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking | 262 | 73.59
10641067
| 0046 |[Permutations](src/main/kotlin/g0001_0100/s0046_permutations/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking | 186 | 100.00
@@ -1084,6 +1087,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
10841087

10851088
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10861089
|-|-|-|-|-|-
1090+
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate/Solution.kt)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting | 719 | 73.49
10871091
| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer | 662 | 82.48
10881092

10891093
#### Day 2 Array
@@ -1578,6 +1582,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15781582
| 0230 |[Kth Smallest Element in a BST](src/main/kotlin/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_17_Tree, Level_2_Day_9_Binary_Search_Tree | 393 | 33.33
15791583
| 0226 |[Invert Binary Tree](src/main/kotlin/g0201_0300/s0226_invert_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue | 233 | 54.90
15801584
| 0221 |[Maximal Square](src/main/kotlin/g0201_0300/s0221_maximal_square/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16 | 614 | 44.00
1585+
| 0219 |[Contains Duplicate II](src/main/kotlin/g0201_0300/s0219_contains_duplicate_ii/Solution.kt)| Easy | Array, Hash_Table, Sliding_Window | 813 | 80.46
1586+
| 0218 |[The Skyline Problem](src/main/kotlin/g0201_0300/s0218_the_skyline_problem/Solution.kt)| Hard | Top_Interview_Questions, Array, Heap_Priority_Queue, Ordered_Set, Divide_and_Conquer, Segment_Tree, Binary_Indexed_Tree, Line_Sweep | 365 | 93.14
1587+
| 0217 |[Contains Duplicate](src/main/kotlin/g0201_0300/s0217_contains_duplicate/Solution.kt)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting, Data_Structure_I_Day_1_Array, Programming_Skills_I_Day_11_Containers_and_Libraries, Udemy_Arrays | 719 | 73.49
1588+
| 0216 |[Combination Sum III](src/main/kotlin/g0201_0300/s0216_combination_sum_iii/Solution.kt)| Medium | Array, Backtracking, Udemy_Backtracking/Recursion | 175 | 90.91
15811589
| 0215 |[Kth Largest Element in an Array](src/main/kotlin/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Data_Structure_II_Day_20_Heap_Priority_Queue | 839 | 34.43
15821590
| 0214 |[Shortest Palindrome](src/main/kotlin/g0201_0300/s0214_shortest_palindrome/Solution.kt)| Hard | String, Hash_Function, String_Matching, Rolling_Hash | 278 | 100.00
15831591
| 0213 |[House Robber II](src/main/kotlin/g0201_0300/s0213_house_robber_ii/Solution.kt)| Medium | Array, Dynamic_Programming, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Udemy_Dynamic_Programming | 257 | 59.62
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0201_0300.s0216_combination_sum_iii
2+
3+
// #Medium #Array #Backtracking #Udemy_Backtracking/Recursion
4+
// #2022_10_25_Time_175_ms_(90.91%)_Space_34.4_MB_(72.73%)
5+
6+
class Solution {
7+
fun combinationSum3(k: Int, n: Int): List<List<Int>> {
8+
val a = MutableList(0) { MutableList(0) { 0 } }
9+
comb(a, k, n, 1, MutableList<Int>(0) { 0 })
10+
return a
11+
}
12+
13+
fun comb(a: MutableList<MutableList<Int>>, k: Int, n: Int, index: Int, tmp: MutableList<Int>) {
14+
if (n < 0) {
15+
return
16+
}
17+
if (tmp.size > k) {
18+
return
19+
}
20+
if (n == 0 && tmp.size == k) {
21+
a.add(tmp.map { it -> it }.toMutableList())
22+
return
23+
}
24+
for (i in index until 10) {
25+
tmp.add(i)
26+
comb(a, k, n - i, i + 1, tmp)
27+
tmp.removeAt(tmp.size - 1)
28+
}
29+
}
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
216\. Combination Sum III
2+
3+
Medium
4+
5+
Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:
6+
7+
* Only numbers `1` through `9` are used.
8+
* Each number is used **at most once**.
9+
10+
Return _a list of all possible valid combinations_. The list must not contain the same combination twice, and the combinations may be returned in any order.
11+
12+
**Example 1:**
13+
14+
**Input:** k = 3, n = 7
15+
16+
**Output:** [[1,2,4]]
17+
18+
**Explanation:** 1 + 2 + 4 = 7 There are no other valid combinations.
19+
20+
**Example 2:**
21+
22+
**Input:** k = 3, n = 9
23+
24+
**Output:** [[1,2,6],[1,3,5],[2,3,4]]
25+
26+
**Explanation:**
27+
28+
1 + 2 + 6 = 9
29+
30+
1 + 3 + 5 = 9
31+
32+
2 + 3 + 4 = 9
33+
34+
There are no other valid combinations.
35+
36+
**Example 3:**
37+
38+
**Input:** k = 4, n = 1
39+
40+
**Output:** []
41+
42+
**Explanation:** There are no valid combinations. Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
43+
44+
**Constraints:**
45+
46+
* `2 <= k <= 9`
47+
* `1 <= n <= 60`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0201_0300.s0217_contains_duplicate
2+
3+
// #Easy #Top_Interview_Questions #Array #Hash_Table #Sorting #Data_Structure_I_Day_1_Array
4+
// #Programming_Skills_I_Day_11_Containers_and_Libraries #Udemy_Arrays
5+
// #2022_10_25_Time_719_ms_(73.49%)_Space_71.6_MB_(55.82%)
6+
7+
class Solution {
8+
fun containsDuplicate(nums: IntArray): Boolean {
9+
val set: MutableSet<Int> = HashSet()
10+
for (n in nums) {
11+
if (set.contains(n)) {
12+
return true
13+
}
14+
set.add(n)
15+
}
16+
return false
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
217\. Contains Duplicate
2+
3+
Easy
4+
5+
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.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,2,3,1]
10+
11+
**Output:** true
12+
13+
**Example 2:**
14+
15+
**Input:** nums = [1,2,3,4]
16+
17+
**Output:** false
18+
19+
**Example 3:**
20+
21+
**Input:** nums = [1,1,1,3,3,4,3,2,4,2]
22+
23+
**Output:** true
24+
25+
**Constraints:**
26+
27+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
28+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g0201_0300.s0218_the_skyline_problem
2+
3+
// #Hard #Top_Interview_Questions #Array #Heap_Priority_Queue #Ordered_Set #Divide_and_Conquer
4+
// #Segment_Tree #Binary_Indexed_Tree #Line_Sweep
5+
// #2022_10_25_Time_365_ms_(93.14%)_Space_45.7_MB_(93.71%)
6+
7+
import java.util.TreeMap
8+
9+
class Solution {
10+
fun getSkyline(blds: Array<IntArray>): List<List<Int>> {
11+
val ans = ArrayList<List<Int>>()
12+
if (blds.isEmpty()) {
13+
return ans
14+
}
15+
val totalBuildings = blds.size
16+
val buildings = Array<Building?>(totalBuildings * 2) { null }
17+
var idx = 0
18+
for (building in blds) {
19+
buildings[idx] = Building(building[0], building[2], true)
20+
buildings[idx + 1] = Building(building[1], building[2], false)
21+
idx += 2
22+
}
23+
buildings.sort()
24+
val skyline = TreeMap<Int, Int>()
25+
skyline[0] = 1
26+
var prevMaxHeight = 0
27+
for (building in buildings) {
28+
if (building == null) {
29+
continue
30+
}
31+
val height = building.height
32+
if (building.isStart) {
33+
skyline[height] = 1 + (skyline[height] ?: 0)
34+
} else {
35+
skyline[height] = (skyline[height] ?: 1) - 1
36+
if (skyline[height] == 0) skyline.remove(height)
37+
}
38+
val xCoor = building.xCoor
39+
val curMaxHeight = skyline.lastKey()
40+
if (prevMaxHeight != curMaxHeight) {
41+
ans.add(arrayListOf(xCoor, curMaxHeight))
42+
prevMaxHeight = curMaxHeight
43+
}
44+
}
45+
return ans
46+
}
47+
48+
private class Building(val xCoor: Int, val height: Int, val isStart: Boolean) : Comparable<Building> {
49+
override fun compareTo(other: Building): Int {
50+
return COMPARATOR.compare(this, other)
51+
}
52+
53+
private companion object {
54+
private val COMPARATOR = Comparator<Building> { a, b ->
55+
when {
56+
a.xCoor != b.xCoor -> a.xCoor.compareTo(b.xCoor)
57+
a.isStart && b.isStart -> b.height.compareTo(a.height)
58+
!(a.isStart || b.isStart) -> a.height.compareTo(b.height)
59+
else -> if (a.isStart) -1 else 1
60+
}
61+
}
62+
}
63+
}
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
218\. The Skyline Problem
2+
3+
Hard
4+
5+
A city's **skyline** is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Given the locations and heights of all the buildings, return _the **skyline** formed by these buildings collectively_.
6+
7+
The geometric information of each building is given in the array `buildings` where <code>buildings[i] = [left<sub>i</sub>, right<sub>i</sub>, height<sub>i</sub>]</code>:
8+
9+
* <code>left<sub>i</sub></code> is the x coordinate of the left edge of the <code>i<sup>th</sup></code> building.
10+
* <code>right<sub>i</sub></code> is the x coordinate of the right edge of the <code>i<sup>th</sup></code> building.
11+
* <code>height<sub>i</sub></code> is the height of the <code>i<sup>th</sup></code> building.
12+
13+
You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height `0`.
14+
15+
The **skyline** should be represented as a list of "key points" **sorted by their x-coordinate** in the form <code>[[x<sub>1</sub>,y<sub>1</sub>],[x<sub>2</sub>,y<sub>2</sub>],...]</code>. Each key point is the left endpoint of some horizontal segment in the skyline except the last point in the list, which always has a y-coordinate `0` and is used to mark the skyline's termination where the rightmost building ends. Any ground between the leftmost and rightmost buildings should be part of the skyline's contour.
16+
17+
**Note:** There must be no consecutive horizontal lines of equal height in the output skyline. For instance, `[...,[2 3],[4 5],[7 5],[11 5],[12 7],...]` is not acceptable; the three lines of height 5 should be merged into one in the final output as such: `[...,[2 3],[4 5],[12 7],...]`
18+
19+
**Example 1:**
20+
21+
![](https://assets.leetcode.com/uploads/2020/12/01/merged.jpg)
22+
23+
**Input:** buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
24+
25+
**Output:** [[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
26+
27+
**Explanation:** Figure A shows the buildings of the input. Figure B shows the skyline formed by those buildings. The red points in figure B represent the key points in the output list.
28+
29+
**Example 2:**
30+
31+
**Input:** buildings = [[0,2,3],[2,5,3]]
32+
33+
**Output:** [[0,3],[5,0]]
34+
35+
**Constraints:**
36+
37+
* <code>1 <= buildings.length <= 10<sup>4</sup></code>
38+
* <code>0 <= left<sub>i</sub> < right<sub>i</sub> <= 2<sup>31</sup> - 1</code>
39+
* <code>1 <= height<sub>i</sub> <= 2<sup>31</sup> - 1</code>
40+
* `buildings` is sorted by <code>left<sub>i</sub></code> in non-decreasing order.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0201_0300.s0219_contains_duplicate_ii
2+
3+
// #Easy #Array #Hash_Table #Sliding_Window #2022_10_25_Time_813_ms_(80.46%)_Space_71.1_MB_(81.22%)
4+
5+
class Solution {
6+
fun containsNearbyDuplicate(nums: IntArray, k: Int): Boolean {
7+
val map: MutableMap<Int, Int> = HashMap()
8+
val len = nums.size
9+
for (i in 0 until len) {
10+
val index = map.put(nums[i], i)
11+
if (index != null && Math.abs(index - i) <= k) {
12+
return true
13+
}
14+
}
15+
return false
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
219\. Contains Duplicate II
2+
3+
Easy
4+
5+
Given an integer array `nums` and an integer `k`, return `true` if there are two **distinct indices** `i` and `j` in the array such that `nums[i] == nums[j]` and `abs(i - j) <= k`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,2,3,1], k = 3
10+
11+
**Output:** true
12+
13+
**Example 2:**
14+
15+
**Input:** nums = [1,0,1,1], k = 1
16+
17+
**Output:** true
18+
19+
**Example 3:**
20+
21+
**Input:** nums = [1,2,3,1,2,3], k = 2
22+
23+
**Output:** false
24+
25+
**Constraints:**
26+
27+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
28+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
29+
* <code>0 <= k <= 10<sup>5</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0201_0300.s0216_combination_sum_iii
2+
3+
import com_github_leetcode.ArrayUtils
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun combinationSum3() {
11+
assertThat(
12+
Solution().combinationSum3(3, 7),
13+
equalTo(ArrayUtils.getLists(arrayOf(intArrayOf(1, 2, 4))))
14+
)
15+
}
16+
17+
@Test
18+
fun combinationSum32() {
19+
assertThat(
20+
Solution().combinationSum3(3, 9),
21+
equalTo(ArrayUtils.getLists(arrayOf(intArrayOf(1, 2, 6), intArrayOf(1, 3, 5), intArrayOf(2, 3, 4))))
22+
)
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0201_0300.s0217_contains_duplicate
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun containsDuplicate() {
10+
assertThat(Solution().containsDuplicate(intArrayOf(1, 2, 3, 1)), equalTo(true))
11+
}
12+
13+
@Test
14+
fun containsDuplicate2() {
15+
assertThat(Solution().containsDuplicate(intArrayOf(1, 2, 3, 4)), equalTo(false))
16+
}
17+
18+
@Test
19+
fun containsDuplicate3() {
20+
assertThat(
21+
Solution().containsDuplicate(intArrayOf(1, 1, 1, 3, 3, 4, 3, 2, 4, 2)),
22+
equalTo(true)
23+
)
24+
}
25+
}

0 commit comments

Comments
 (0)