Skip to content

Commit 394e2e0

Browse files
authored
Added tasks 197, 201, 202, 203.
1 parent bcc1424 commit 394e2e0

File tree

13 files changed

+393
-0
lines changed

13 files changed

+393
-0
lines changed

README.md

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

380380
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
381381
|-|-|-|-|-|-
382+
| 0202 |[Happy Number](src/main/kotlin/g0201_0300/s0202_happy_number/Solution.kt)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 261 | 45.08
382383

383384
#### Day 5 Function
384385

@@ -651,6 +652,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
651652

652653
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
653654
|-|-|-|-|-|-
655+
| 0197 |[Rising Temperature](src/main/kotlin/g0101_0200/s0197_rising_temperature/script.sql)| |||
654656

655657
#### Day 7 Function
656658

@@ -768,6 +770,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
768770

769771
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
770772
|-|-|-|-|-|-
773+
| 0202 |[Happy Number](src/main/kotlin/g0201_0300/s0202_happy_number/Solution.kt)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 261 | 45.08
771774
| 0054 |[Spiral Matrix](src/main/kotlin/g0001_0100/s0054_spiral_matrix/Solution.kt)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 224 | 62.50
772775

773776
#### Day 2 String
@@ -1115,6 +1118,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
11151118
|-|-|-|-|-|-
11161119
| 0141 |[Linked List Cycle](src/main/kotlin/g0101_0200/s0141_linked_list_cycle/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List | 223 | 91.85
11171120
| 0021 |[Merge Two Sorted Lists](src/main/kotlin/g0001_0100/s0021_merge_two_sorted_lists/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion | 176 | 96.25
1121+
| 0203 |[Remove Linked List Elements](src/main/kotlin/g0201_0300/s0203_remove_linked_list_elements/Solution.kt)| Easy | Linked_List, Recursion | 233 | 91.22
11181122

11191123
#### Day 8 Linked List
11201124

@@ -1520,6 +1524,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15201524

15211525
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15221526
|-|-|-|-|-|-
1527+
| 0201 |[Bitwise AND of Numbers Range](src/main/kotlin/g0201_0300/s0201_bitwise_and_of_numbers_range/Solution.kt)| Medium | Bit_Manipulation | 368 | 80.00
15231528

15241529
#### Day 20 Others
15251530

@@ -1530,6 +1535,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15301535

15311536
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15321537
|-|-|-|-|-|-
1538+
| 0202 |[Happy Number](src/main/kotlin/g0201_0300/s0202_happy_number/Solution.kt)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 261 | 45.08
15331539
| 0149 |[Max Points on a Line](src/main/kotlin/g0101_0200/s0149_max_points_on_a_line/Solution.kt)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry | 307 | 83.33
15341540

15351541
## Algorithms
@@ -1569,9 +1575,13 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15691575
| 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
15701576
| 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
15711577
| 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
1578+
| 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
1579+
| 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
1580+
| 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
15721581
| 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
15731582
| 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
15741583
| 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)| |||
15751585
| 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
15761586
| 0195 |[Tenth Line](src/main/kotlin/g0101_0200/s0195_tenth_line/script.sh)| Easy | Shell | 36 | 87.50
15771587
| 0194 |[Transpose File](src/main/kotlin/g0101_0200/s0194_transpose_file/script.sh)| Medium | Shell | 477 | 28.60
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
197\. Rising Temperature
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Weather`
8+
9+
+---------------+---------+
10+
| Column Name | Type |
11+
+---------------+---------+
12+
| id | int |
13+
| recordDate | date |
14+
| temperature | int |
15+
+---------------+---------+
16+
id is the primary key for this table.
17+
This table contains information about the temperature on a certain day.
18+
19+
Write an SQL query to find all dates' `Id` with higher temperatures compared to its previous dates (yesterday).
20+
21+
Return the result table in **any order**.
22+
23+
The query result format is in the following example.
24+
25+
**Example 1:**
26+
27+
**Input:**
28+
29+
Weather table:
30+
+----+------------+-------------+
31+
| id | recordDate | temperature |
32+
+----+------------+-------------+
33+
| 1 | 2015-01-01 | 10 |
34+
| 2 | 2015-01-02 | 25 |
35+
| 3 | 2015-01-03 | 20 |
36+
| 4 | 2015-01-04 | 30 |
37+
+----+------------+-------------+
38+
39+
**Output:**
40+
41+
+----+
42+
| id |
43+
+----+
44+
| 2 |
45+
| 4 |
46+
+----+
47+
48+
**Explanation:**
49+
50+
In 2015-01-02, the temperature was higher than the previous day (10 -> 25).
51+
In 2015-01-04, the temperature was higher than the previous day (20 -> 30).
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Write your MySQL query statement below
2+
#Easy #Database #SQL_I_Day_6_Union #2022_10_19_Time_394_ms_(94.15%)_Space_0B_(100.00%)
3+
SELECT SecondDate.id as Id
4+
FROM Weather SecondDate JOIN Weather FirstDate
5+
ON ADDDATE(FirstDate.recordDate,1) = SecondDate.recordDate
6+
AND SecondDate.temperature > FirstDate.temperature;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g0201_0300.s0201_bitwise_and_of_numbers_range
2+
3+
// #Medium #Bit_Manipulation #Algorithm_II_Day_19_Bit_Manipulation
4+
// #2022_10_19_Time_368_ms_(80.00%)_Space_37.1_MB_(76.67%)
5+
6+
class Solution {
7+
fun rangeBitwiseAnd(left: Int, right: Int): Int {
8+
return if (left == right) left else right and MASKS[Integer.numberOfLeadingZeros(left xor right)]
9+
}
10+
11+
companion object {
12+
private val MASKS = intArrayOf(
13+
0,
14+
-0x80000000,
15+
-0x40000000,
16+
-0x20000000,
17+
-0x10000000,
18+
-0x8000000,
19+
-0x4000000,
20+
-0x2000000,
21+
-0x1000000,
22+
-0x800000,
23+
-0x400000,
24+
-0x200000,
25+
-0x100000,
26+
-0x80000,
27+
-0x40000,
28+
-0x20000,
29+
-0x10000,
30+
-0x8000,
31+
-0x4000,
32+
-0x2000,
33+
-0x1000,
34+
-0x800,
35+
-0x400,
36+
-0x200,
37+
-0x100,
38+
-0x80,
39+
-0x40,
40+
-0x20,
41+
-0x10,
42+
-0x8,
43+
-0x4,
44+
-0x2
45+
)
46+
}
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
201\. Bitwise AND of Numbers Range
2+
3+
Medium
4+
5+
Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.
6+
7+
**Example 1:**
8+
9+
**Input:** left = 5, right = 7
10+
11+
**Output:** 4
12+
13+
**Example 2:**
14+
15+
**Input:** left = 0, right = 0
16+
17+
**Output:** 0
18+
19+
**Example 3:**
20+
21+
**Input:** left = 1, right = 2147483647
22+
23+
**Output:** 0
24+
25+
**Constraints:**
26+
27+
* <code>0 <= left <= right <= 2<sup>31</sup> - 1</code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0201_0300.s0202_happy_number
2+
3+
// #Easy #Top_Interview_Questions #Hash_Table #Math #Two_Pointers #Algorithm_II_Day_21_Others
4+
// #Programming_Skills_I_Day_4_Loop #Level_2_Day_1_Implementation/Simulation
5+
// #2022_10_19_Time_261_ms_(45.08%)_Space_33.7_MB_(78.51%)
6+
7+
class Solution {
8+
private val set = mutableSetOf<Int>()
9+
10+
tailrec fun isHappy(n: Int): Boolean {
11+
var num = n
12+
var squareSum = 0
13+
while (num > 0) {
14+
val digit = num % 10
15+
squareSum += digit * digit
16+
num /= 10
17+
}
18+
if (squareSum == 1) {
19+
return true
20+
}
21+
if (set.contains(squareSum)) {
22+
return false
23+
}
24+
set.add(squareSum)
25+
return isHappy(squareSum)
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
202\. Happy Number
2+
3+
Easy
4+
5+
Write an algorithm to determine if a number `n` is happy.
6+
7+
A **happy number** is a number defined by the following process:
8+
9+
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
10+
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly in a cycle** which does not include 1.
11+
* Those numbers for which this process **ends in 1** are happy.
12+
13+
Return `true` _if_ `n` _is a happy number, and_ `false` _if not_.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 19
18+
19+
**Output:** true
20+
21+
**Explanation:** 1<sup>2</sup> + 9<sup>2</sup> = 82 8<sup>2</sup> + 2<sup>2</sup> = 68 6<sup>2</sup> + 8<sup>2</sup> = 100 1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1
22+
23+
**Example 2:**
24+
25+
**Input:** n = 2
26+
27+
**Output:** false
28+
29+
**Constraints:**
30+
31+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0201_0300.s0203_remove_linked_list_elements
2+
3+
// #Easy #Linked_List #Recursion #Data_Structure_I_Day_7_Linked_List
4+
// #2022_10_19_Time_233_ms_(91.22%)_Space_37_MB_(100.00%)
5+
6+
import com_github_leetcode.ListNode
7+
8+
/*
9+
* Example:
10+
* var li = ListNode(5)
11+
* var v = li.`val`
12+
* Definition for singly-linked list.
13+
* class ListNode(var `val`: Int) {
14+
* var next: ListNode? = null
15+
* }
16+
*/
17+
class Solution {
18+
fun removeElements(head: ListNode?, `val`: Int): ListNode? {
19+
val sentinel = ListNode(-1)
20+
sentinel.next = head
21+
var next = head
22+
var prev = sentinel
23+
while (next != null) {
24+
if (next.`val` == `val`) {
25+
prev.next = next.next
26+
} else {
27+
prev = next
28+
}
29+
next = next.next
30+
}
31+
return sentinel.next
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
203\. Remove Linked List Elements
2+
3+
Easy
4+
5+
Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/06/removelinked-list.jpg)
10+
11+
**Input:** head = [1,2,6,3,4,5,6], val = 6
12+
13+
**Output:** [1,2,3,4,5]
14+
15+
**Example 2:**
16+
17+
**Input:** head = [], val = 1
18+
19+
**Output:** []
20+
21+
**Example 3:**
22+
23+
**Input:** head = [7,7,7,7], val = 7
24+
25+
**Output:** []
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.
30+
* `1 <= Node.val <= 50`
31+
* `0 <= val <= 50`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g0101_0200.s0197_rising_temperature
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import org.zapodot.junit.db.annotations.EmbeddedDatabase
7+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest
8+
import org.zapodot.junit.db.common.CompatibilityMode
9+
import java.io.BufferedReader
10+
import java.io.FileNotFoundException
11+
import java.io.FileReader
12+
import java.sql.SQLException
13+
import java.util.stream.Collectors.joining
14+
import javax.sql.DataSource
15+
16+
@EmbeddedDatabaseTest(
17+
compatibilityMode = CompatibilityMode.MySQL,
18+
initialSqls = [
19+
"CREATE TABLE Weather(id INTEGER PRIMARY KEY, recordDate DATE, temperature INTEGER); " +
20+
"INSERT INTO Weather(id, recordDate, temperature) VALUES (1, '2015-01-01', 10); " +
21+
"INSERT INTO Weather(id, recordDate, temperature) VALUES (2, '2015-01-02', 25); " +
22+
"INSERT INTO Weather(id, recordDate, temperature) VALUES (3, '2015-01-03', 20); " +
23+
"INSERT INTO Weather(id, recordDate, temperature) VALUES (4, '2015-01-04', 30); "
24+
]
25+
)
26+
internal class MysqlTest {
27+
@Test
28+
@Throws(SQLException::class, FileNotFoundException::class)
29+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
30+
dataSource.connection.use { connection ->
31+
connection.createStatement().use { statement ->
32+
statement.executeQuery(
33+
BufferedReader(
34+
FileReader(
35+
"src/main/kotlin/g0101_0200/s0197_rising_temperature/script.sql"
36+
)
37+
)
38+
.lines()
39+
.collect(joining("\n"))
40+
.replace("#.*?\\r?\\n".toRegex(), "")
41+
.replace(
42+
"ADDDATE(FirstDate.recordDate,1)",
43+
"TIMESTAMPADD(DAY, 1, FirstDate.recordDate)"
44+
)
45+
).use { resultSet ->
46+
assertThat(resultSet.next(), equalTo(true))
47+
assertThat(resultSet.getInt(1), equalTo(2))
48+
assertThat(resultSet.next(), equalTo(true))
49+
assertThat(resultSet.getInt(1), equalTo(4))
50+
assertThat(resultSet.next(), equalTo(false))
51+
}
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)