Skip to content

Commit e924972

Browse files
authored
Added tasks 204, 205, 209, 210.
1 parent 7db4e18 commit e924972

File tree

13 files changed

+383
-2
lines changed

13 files changed

+383
-2
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
124124

125125
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
126126
|-|-|-|-|-|-
127+
| 0209 |[Minimum Size Subarray Sum](src/main/kotlin/g0201_0300/s0209_minimum_size_subarray_sum/Solution.kt)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window | 315 | 96.73
127128

128129
#### Day 2
129130

@@ -652,7 +653,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
652653

653654
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
654655
|-|-|-|-|-|-
655-
| 0197 |[Rising Temperature](src/main/kotlin/g0101_0200/s0197_rising_temperature/script.sql)| |||
656+
| 0197 |[Rising Temperature](src/main/kotlin/g0101_0200/s0197_rising_temperature/script.sql)| Easy | Database | 394 | 94.15
656657

657658
#### Day 7 Function
658659

@@ -686,6 +687,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
686687

687688
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
688689
|-|-|-|-|-|-
690+
| 0205 |[Isomorphic Strings](src/main/kotlin/g0201_0300/s0205_isomorphic_strings/Solution.kt)| Easy | String, Hash_Table | 278 | 79.96
689691

690692
#### Day 3 Linked List
691693

@@ -837,6 +839,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
837839

838840
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
839841
|-|-|-|-|-|-
842+
| 0210 |[Course Schedule II](src/main/kotlin/g0201_0300/s0210_course_schedule_ii/Solution.kt)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 266 | 96.32
840843

841844
#### Day 12 Dynamic Programming
842845

@@ -1433,6 +1436,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
14331436
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14341437
|-|-|-|-|-|-
14351438
| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window | 561 | 54.68
1439+
| 0209 |[Minimum Size Subarray Sum](src/main/kotlin/g0201_0300/s0209_minimum_size_subarray_sum/Solution.kt)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window | 315 | 96.73
14361440

14371441
#### Day 6 Breadth First Search Depth First Search
14381442

@@ -1572,16 +1576,20 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15721576
| 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
15731577
| 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
15741578
| 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
1579+
| 0210 |[Course Schedule II](src/main/kotlin/g0201_0300/s0210_course_schedule_ii/Solution.kt)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Level_2_Day_11_Graph/BFS/DFS | 266 | 96.32
1580+
| 0209 |[Minimum Size Subarray Sum](src/main/kotlin/g0201_0300/s0209_minimum_size_subarray_sum/Solution.kt)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Binary_Search_II_Day_1 | 315 | 96.73
15751581
| 0208 |[Implement Trie (Prefix Tree)](src/main/kotlin/g0201_0300/s0208_implement_trie_prefix_tree/Trie.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap | 689 | 61.00
15761582
| 0207 |[Course Schedule](src/main/kotlin/g0201_0300/s0207_course_schedule/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 416 | 40.10
15771583
| 0206 |[Reverse Linked List](src/main/kotlin/g0201_0300/s0206_reverse_linked_list/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List | 279 | 45.78
1584+
| 0205 |[Isomorphic Strings](src/main/kotlin/g0201_0300/s0205_isomorphic_strings/Solution.kt)| Easy | String, Hash_Table, Level_1_Day_2_String | 278 | 79.96
1585+
| 0204 |[Count Primes](src/main/kotlin/g0201_0300/s0204_count_primes/Solution.kt)| Medium | Top_Interview_Questions, Array, Math, Enumeration, Number_Theory | 360 | 96.61
15781586
| 0203 |[Remove Linked List Elements](src/main/kotlin/g0201_0300/s0203_remove_linked_list_elements/Solution.kt)| Easy | Linked_List, Recursion, Data_Structure_I_Day_7_Linked_List | 233 | 91.22
15791587
| 0202 |[Happy Number](src/main/kotlin/g0201_0300/s0202_happy_number/Solution.kt)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers, Algorithm_II_Day_21_Others, Programming_Skills_I_Day_4_Loop, Level_2_Day_1_Implementation/Simulation | 261 | 45.08
15801588
| 0201 |[Bitwise AND of Numbers Range](src/main/kotlin/g0201_0300/s0201_bitwise_and_of_numbers_range/Solution.kt)| Medium | Bit_Manipulation, Algorithm_II_Day_19_Bit_Manipulation | 368 | 80.00
15811589
| 0200 |[Number of Islands](src/main/kotlin/g0101_0200/s0200_number_of_islands/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_1_Matrix_Related_Problems, Level_1_Day_9_Graph/BFS/DFS, Udemy_Graph | 252 | 95.41
15821590
| 0199 |[Binary Tree Right Side View](src/main/kotlin/g0101_0200/s0199_binary_tree_right_side_view/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_16_Tree, Level_2_Day_15_Tree | 194 | 92.89
15831591
| 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming | 156 | 92.24
1584-
| 0197 |[Rising Temperature](src/main/kotlin/g0101_0200/s0197_rising_temperature/script.sql)| |||
1592+
| 0197 |[Rising Temperature](src/main/kotlin/g0101_0200/s0197_rising_temperature/script.sql)| Easy | Database, SQL_I_Day_6_Union | 394 | 94.15
15851593
| 0196 |[Delete Duplicate Emails](src/main/kotlin/g0101_0200/s0196_delete_duplicate_emails/script.sql)| Easy | Database, SQL_I_Day_2_Select_and_Order | 593 | 94.17
15861594
| 0195 |[Tenth Line](src/main/kotlin/g0101_0200/s0195_tenth_line/script.sh)| Easy | Shell | 36 | 87.50
15871595
| 0194 |[Transpose File](src/main/kotlin/g0101_0200/s0194_transpose_file/script.sh)| Medium | Shell | 477 | 28.60
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0201_0300.s0204_count_primes
2+
3+
// #Medium #Top_Interview_Questions #Array #Math #Enumeration #Number_Theory
4+
// #2022_10_19_Time_360_ms_(96.61%)_Space_61.2_MB_(71.19%)
5+
6+
class Solution {
7+
fun countPrimes(n: Int): Int {
8+
val isprime = BooleanArray(n)
9+
var count = 0
10+
run {
11+
var i = 2
12+
while (i * i <= n) {
13+
if (!isprime[i]) {
14+
var j = i * 2
15+
while (j < n) {
16+
isprime[j] = true
17+
j += i
18+
}
19+
}
20+
i++
21+
}
22+
}
23+
for (i in 2 until isprime.size) {
24+
if (!isprime[i]) {
25+
count++
26+
}
27+
}
28+
return count
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
204\. Count Primes
2+
3+
Medium
4+
5+
Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 10
10+
11+
**Output:** 4
12+
13+
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
14+
15+
**Example 2:**
16+
17+
**Input:** n = 0
18+
19+
**Output:** 0
20+
21+
**Example 3:**
22+
23+
**Input:** n = 1
24+
25+
**Output:** 0
26+
27+
**Constraints:**
28+
29+
* <code>0 <= n <= 5 * 10<sup>6</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0201_0300.s0205_isomorphic_strings
2+
3+
// #Easy #String #Hash_Table #Level_1_Day_2_String
4+
// #2022_10_20_Time_278_ms_(79.96%)_Space_37.7_MB_(72.52%)
5+
6+
class Solution {
7+
fun isIsomorphic(s: String, t: String): Boolean {
8+
val map = IntArray(128)
9+
val str = s.toCharArray()
10+
val tar = t.toCharArray()
11+
val n = str.size
12+
for (i in 0 until n) {
13+
if (map[tar[i].code] == 0) {
14+
if (search(map, str[i].code, tar[i].code) != -1) {
15+
return false
16+
}
17+
map[tar[i].code] = str[i].code
18+
} else {
19+
if (map[tar[i].code] != str[i].code) {
20+
return false
21+
}
22+
}
23+
}
24+
return true
25+
}
26+
27+
private fun search(map: IntArray, tar: Int, skip: Int): Int {
28+
for (i in 0..127) {
29+
if (i == skip) {
30+
continue
31+
}
32+
if (map[i] != 0 && map[i] == tar) {
33+
return i
34+
}
35+
}
36+
return -1
37+
}
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
205\. Isomorphic Strings
2+
3+
Easy
4+
5+
Given two strings `s` and `t`, _determine if they are isomorphic_.
6+
7+
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
8+
9+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "egg", t = "add"
14+
15+
**Output:** true
16+
17+
**Example 2:**
18+
19+
**Input:** s = "foo", t = "bar"
20+
21+
**Output:** false
22+
23+
**Example 3:**
24+
25+
**Input:** s = "paper", t = "title"
26+
27+
**Output:** true
28+
29+
**Constraints:**
30+
31+
* <code>1 <= s.length <= 5 * 10<sup>4</sup></code>
32+
* `t.length == s.length`
33+
* `s` and `t` consist of any valid ascii character.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0201_0300.s0209_minimum_size_subarray_sum
2+
3+
// #Medium #Array #Binary_Search #Prefix_Sum #Sliding_Window #Algorithm_II_Day_5_Sliding_Window
4+
// #Binary_Search_II_Day_1 #2022_10_20_Time_315_ms_(96.73%)_Space_46.7_MB_(90.20%)
5+
6+
class Solution {
7+
fun minSubArrayLen(s: Int, nums: IntArray): Int {
8+
var sum = 0
9+
var start = 0
10+
var minLength = Integer.MAX_VALUE
11+
var end = 0
12+
if (nums.size < 1) {
13+
return 0
14+
}
15+
while (end < nums.size) {
16+
sum += nums[end]
17+
while (sum >= s) {
18+
minLength = Math.min(minLength, end - start + 1)
19+
sum -= nums[start++]
20+
}
21+
end++
22+
}
23+
return if (start > 0) minLength else 0
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
209\. Minimum Size Subarray Sum
2+
3+
Medium
4+
5+
Given an array of positive integers `nums` and a positive integer `target`, return the minimal length of a **contiguous subarray** <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> of which the sum is greater than or equal to `target`. If there is no such subarray, return `0` instead.
6+
7+
**Example 1:**
8+
9+
**Input:** target = 7, nums = [2,3,1,2,4,3]
10+
11+
**Output:** 2
12+
13+
**Explanation:** The subarray [4,3] has the minimal length under the problem constraint.
14+
15+
**Example 2:**
16+
17+
**Input:** target = 4, nums = [1,4,4]
18+
19+
**Output:** 1
20+
21+
**Example 3:**
22+
23+
**Input:** target = 11, nums = [1,1,1,1,1,1,1,1]
24+
25+
**Output:** 0
26+
27+
**Constraints:**
28+
29+
* <code>1 <= target <= 10<sup>9</sup></code>
30+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
31+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
32+
33+
**Follow up:** If you have figured out the `O(n)` solution, try coding another solution of which the time complexity is `O(n log(n))`.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0201_0300.s0210_course_schedule_ii
2+
3+
// #Medium #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search #Graph
4+
// #Topological_Sort #Level_2_Day_11_Graph/BFS/DFS
5+
// #2022_10_20_Time_266_ms_(96.32%)_Space_45.9_MB_(92.65%)
6+
7+
class Solution {
8+
fun findOrder(numCourses: Int, prerequisites: Array<IntArray>): IntArray {
9+
val indegrees = IntArray(numCourses) { 0 }
10+
val graph = buildGraph(numCourses, prerequisites, indegrees)
11+
val queue = ArrayDeque<Int>()
12+
for ((idx, indegree) in indegrees.withIndex()) {
13+
if (indegree == 0) {
14+
queue.addLast(idx)
15+
}
16+
}
17+
val ans = IntArray(numCourses) { 0 }
18+
var idx = 0
19+
while (queue.isNotEmpty()) {
20+
val cur = queue.removeFirst()
21+
ans[idx++] = cur
22+
for (pre in graph[cur]) {
23+
if (--indegrees[pre] == 0) {
24+
queue.addLast(pre)
25+
}
26+
}
27+
}
28+
if (idx < numCourses) {
29+
return intArrayOf()
30+
}
31+
return ans
32+
}
33+
34+
private fun buildGraph(
35+
numCourses: Int,
36+
prerequisites: Array<IntArray>,
37+
indegrees: IntArray
38+
): List<List<Int>> {
39+
val graph = List(numCourses) { mutableListOf<Int>() }
40+
for ((cur, prev) in prerequisites) {
41+
graph[prev].add(cur)
42+
++indegrees[cur]
43+
}
44+
return graph
45+
}
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
210\. Course Schedule II
2+
3+
Medium
4+
5+
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you **must** take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.
6+
7+
* For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.
8+
9+
Return _the ordering of courses you should take to finish all courses_. If there are many valid answers, return **any** of them. If it is impossible to finish all courses, return **an empty array**.
10+
11+
**Example 1:**
12+
13+
**Input:** numCourses = 2, prerequisites = [[1,0]]
14+
15+
**Output:** [0,1]
16+
17+
**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
18+
19+
**Example 2:**
20+
21+
**Input:** numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
22+
23+
**Output:** [0,2,1,3]
24+
25+
**Explanation:** There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
26+
27+
**Example 3:**
28+
29+
**Input:** numCourses = 1, prerequisites = []
30+
31+
**Output:** [0]
32+
33+
**Constraints:**
34+
35+
* `1 <= numCourses <= 2000`
36+
* `0 <= prerequisites.length <= numCourses * (numCourses - 1)`
37+
* `prerequisites[i].length == 2`
38+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code>
39+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
40+
* All the pairs <code>[a<sub>i</sub>, b<sub>i</sub>]</code> are **distinct**.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0201_0300.s0204_count_primes
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 countPrimes() {
10+
assertThat(Solution().countPrimes(10), equalTo(4))
11+
}
12+
13+
@Test
14+
fun countPrimes2() {
15+
assertThat(Solution().countPrimes(0), equalTo(0))
16+
}
17+
18+
@Test
19+
fun countPrimes3() {
20+
assertThat(Solution().countPrimes(1), equalTo(0))
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0201_0300.s0205_isomorphic_strings
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 isIsomorphic() {
10+
assertThat(Solution().isIsomorphic("egg", "add"), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isIsomorphic2() {
15+
assertThat(Solution().isIsomorphic("foo", "bar"), equalTo(false))
16+
}
17+
18+
@Test
19+
fun isIsomorphic3() {
20+
assertThat(Solution().isIsomorphic("paper", "title"), equalTo(true))
21+
}
22+
}

0 commit comments

Comments
 (0)