Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 50857c7

Browse filesBrowse files
authored
Added tasks 818, 819, 820, 821
1 parent e5c24af commit 50857c7
Copy full SHA for 50857c7

File tree

Expand file treeCollapse file tree

13 files changed

+355
-0
lines changed
Filter options
Expand file treeCollapse file tree

13 files changed

+355
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17121712
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17131713
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
17141714
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
1715+
| 0821 |[Shortest Distance to a Character](src/main/kotlin/g0801_0900/s0821_shortest_distance_to_a_character/Solution.kt)| Easy | Array, String, Two_Pointers | 168 | 88.00
1716+
| 0820 |[Short Encoding of Words](src/main/kotlin/g0801_0900/s0820_short_encoding_of_words/Solution.kt)| Medium | Array, String, Hash_Table, Trie | 231 | 100.00
1717+
| 0819 |[Most Common Word](src/main/kotlin/g0801_0900/s0819_most_common_word/Solution.kt)| Easy | String, Hash_Table, Counting | 211 | 83.33
1718+
| 0818 |[Race Car](src/main/kotlin/g0801_0900/s0818_race_car/Solution.kt)| Hard | Dynamic_Programming | 123 | 100.00
17151719
| 0817 |[Linked List Components](src/main/kotlin/g0801_0900/s0817_linked_list_components/Solution.kt)| Medium | Hash_Table, Linked_List | 239 | 100.00
17161720
| 0816 |[Ambiguous Coordinates](src/main/kotlin/g0801_0900/s0816_ambiguous_coordinates/Solution.kt)| Medium | String, Backtracking | 231 | 100.00
17171721
| 0815 |[Bus Routes](src/main/kotlin/g0801_0900/s0815_bus_routes/Solution.kt)| Hard | Array, Hash_Table, Breadth_First_Search, Level_2_Day_11_Graph/BFS/DFS | 429 | 100.00
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0801_0900.s0818_race_car
2+
3+
// #Hard #Dynamic_Programming #2023_03_24_Time_123_ms_(100.00%)_Space_34.3_MB_(93.94%)
4+
5+
import java.util.LinkedList
6+
import java.util.Queue
7+
8+
class Solution {
9+
fun racecar(target: Int): Int {
10+
val queue: Queue<IntArray> = LinkedList()
11+
queue.add(intArrayOf(0, 1, 0))
12+
while (!queue.isEmpty()) {
13+
val arr = queue.poll()
14+
if (arr[0] == target) {
15+
return arr[2]
16+
}
17+
queue.add(intArrayOf(arr[0] + arr[1], 2 * arr[1], 1 + arr[2]))
18+
if (arr[0] + arr[1] > target && arr[1] > 0) {
19+
queue.add(intArrayOf(arr[0], -1, 1 + arr[2]))
20+
}
21+
if (arr[0] + arr[1] < target && arr[1] < 0) {
22+
queue.add(intArrayOf(arr[0], 1, 1 + arr[2]))
23+
}
24+
}
25+
return -1
26+
}
27+
}
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
818\. Race Car
2+
3+
Hard
4+
5+
Your car starts at position `0` and speed `+1` on an infinite number line. Your car can go into negative positions. Your car drives automatically according to a sequence of instructions `'A'` (accelerate) and `'R'` (reverse):
6+
7+
* When you get an instruction `'A'`, your car does the following:
8+
* `position += speed`
9+
* `speed *= 2`
10+
* When you get an instruction `'R'`, your car does the following:
11+
* If your speed is positive then `speed = -1`
12+
* otherwise `speed = 1`Your position stays the same.
13+
14+
For example, after commands `"AAR"`, your car goes to positions `0 --> 1 --> 3 --> 3`, and your speed goes to `1 --> 2 --> 4 --> -1`.
15+
16+
Given a target position `target`, return _the length of the shortest sequence of instructions to get there_.
17+
18+
**Example 1:**
19+
20+
**Input:** target = 3
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
The shortest instruction sequence is "AA".
27+
28+
Your position goes from 0 --> 1 --> 3.
29+
30+
**Example 2:**
31+
32+
**Input:** target = 6
33+
34+
**Output:** 5
35+
36+
**Explanation:**
37+
38+
The shortest instruction sequence is "AAARA".
39+
40+
Your position goes from 0 --> 1 --> 3 --> 7 --> 7 --> 6.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= target <= 10<sup>4</sup></code>
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0801_0900.s0819_most_common_word
2+
3+
// #Easy #String #Hash_Table #Counting #2023_03_24_Time_211_ms_(83.33%)_Space_36.9_MB_(88.89%)
4+
5+
import java.util.Locale
6+
7+
@Suppress("NAME_SHADOWING")
8+
class Solution {
9+
fun mostCommonWord(paragraph: String, banned: Array<String>): String {
10+
var paragraph = paragraph
11+
paragraph = paragraph.replace("\\p{Punct}".toRegex(), " ").lowercase(Locale.getDefault())
12+
val a = paragraph.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
13+
for (i in banned.indices) {
14+
banned[i] = banned[i].lowercase(Locale.getDefault())
15+
}
16+
val map: MutableMap<String, Int> = HashMap()
17+
for (s in a) {
18+
val x = map.getOrDefault(s, 0)
19+
map[s] = x + 1
20+
}
21+
for (s in banned) {
22+
map.remove(s)
23+
map.remove("")
24+
}
25+
return map.maxBy { it.value }.key
26+
}
27+
}
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
819\. Most Common Word
2+
3+
Easy
4+
5+
Given a string `paragraph` and a string array of the banned words `banned`, return _the most frequent word that is not banned_. It is **guaranteed** there is **at least one word** that is not banned, and that the answer is **unique**.
6+
7+
The words in `paragraph` are **case-insensitive** and the answer should be returned in **lowercase**.
8+
9+
**Example 1:**
10+
11+
**Input:** paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
12+
13+
**Output:** "ball"
14+
15+
**Explanation:** "hit" occurs 3 times, but it is a banned word. "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. Note that words in the paragraph are not case sensitive, that punctuation is ignored (even if adjacent to words, such as "ball,"), and that "hit" isn't the answer even though it occurs more because it is banned.
16+
17+
**Example 2:**
18+
19+
**Input:** paragraph = "a.", banned = []
20+
21+
**Output:** "a"
22+
23+
**Constraints:**
24+
25+
* `1 <= paragraph.length <= 1000`
26+
* paragraph consists of English letters, space `' '`, or one of the symbols: `"!?',;."`.
27+
* `0 <= banned.length <= 100`
28+
* `1 <= banned[i].length <= 10`
29+
* `banned[i]` consists of only lowercase English letters.
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0801_0900.s0820_short_encoding_of_words
2+
3+
// #Medium #Array #String #Hash_Table #Trie
4+
// #2023_03_24_Time_231_ms_(100.00%)_Space_41.2_MB_(100.00%)
5+
6+
class Solution {
7+
private class Node {
8+
var nodes = arrayOfNulls<Node>(26)
9+
}
10+
11+
private fun insert(node: Node, word: String): Boolean {
12+
var current: Node? = node
13+
val n = word.length
14+
var flag = false
15+
for (i in n - 1 downTo 0) {
16+
if (current!!.nodes[word[i].code - 'a'.code] == null) {
17+
current.nodes[word[i].code - 'a'.code] = Node()
18+
flag = true
19+
}
20+
current = current.nodes[word[i].code - 'a'.code]
21+
}
22+
return flag
23+
}
24+
25+
fun minimumLengthEncoding(words: Array<String>): Int {
26+
var out = 0
27+
words.sortWith { a: String, b: String -> b.length - a.length }
28+
val node = Node()
29+
for (word in words) {
30+
if (insert(node, word)) {
31+
out += word.length + 1
32+
}
33+
}
34+
return out
35+
}
36+
}
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
820\. Short Encoding of Words
2+
3+
Medium
4+
5+
A **valid encoding** of an array of `words` is any reference string `s` and array of indices `indices` such that:
6+
7+
* `words.length == indices.length`
8+
* The reference string `s` ends with the `'#'` character.
9+
* For each index `indices[i]`, the **substring** of `s` starting from `indices[i]` and up to (but not including) the next `'#'` character is equal to `words[i]`.
10+
11+
Given an array of `words`, return _the **length of the shortest reference string**_ `s` _possible of any **valid encoding** of_ `words`_._
12+
13+
**Example 1:**
14+
15+
**Input:** words = ["time", "me", "bell"]
16+
17+
**Output:** 10
18+
19+
**Explanation:** A valid encoding would be s = `"time#bell#" and indices = [0, 2, 5`].
20+
21+
words[0] = "time", the substring of s starting from indices[0] = 0 to the next '#' is underlined in "time#bell#"
22+
23+
words[1] = "me", the substring of s starting from indices[1] = 2 to the next '#' is underlined in "time#bell#"
24+
25+
words[2] = "bell", the substring of s starting from indices[2] = 5 to the next '#' is underlined in "time#bell#"
26+
27+
**Example 2:**
28+
29+
**Input:** words = ["t"]
30+
31+
**Output:** 2
32+
33+
**Explanation:** A valid encoding would be s = "t#" and indices = [0].
34+
35+
**Constraints:**
36+
37+
* `1 <= words.length <= 2000`
38+
* `1 <= words[i].length <= 7`
39+
* `words[i]` consists of only lowercase letters.
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0801_0900.s0821_shortest_distance_to_a_character
2+
3+
// #Easy #Array #String #Two_Pointers #2023_03_24_Time_168_ms_(88.00%)_Space_36.1_MB_(56.00%)
4+
5+
class Solution {
6+
fun shortestToChar(s: String, c: Char): IntArray {
7+
val result = IntArray(s.length)
8+
result.fill(Int.MAX_VALUE)
9+
for (i in s.indices) {
10+
if (s[i] == c) {
11+
result[i] = 0
12+
}
13+
}
14+
for (i in s.indices) {
15+
if (result[i] != 0) {
16+
var j = i - 1
17+
while (j >= 0 && result[j] != 0) {
18+
j--
19+
}
20+
if (j >= 0) {
21+
result[i] = i - j
22+
}
23+
j = i + 1
24+
while (j < s.length && result[j] != 0) {
25+
j++
26+
}
27+
if (j < s.length) {
28+
result[i] = result[i].coerceAtMost(j - i)
29+
}
30+
}
31+
}
32+
return result
33+
}
34+
}
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
821\. Shortest Distance to a Character
2+
3+
Easy
4+
5+
Given a string `s` and a character `c` that occurs in `s`, return _an array of integers_ `answer` _where_ `answer.length == s.length` _and_ `answer[i]` _is the **distance** from index_ `i` _to the **closest** occurrence of character_ `c` _in_ `s`.
6+
7+
The **distance** between two indices `i` and `j` is `abs(i - j)`, where `abs` is the absolute value function.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "loveleetcode", c = "e"
12+
13+
**Output:** [3,2,1,0,1,0,0,1,2,2,1,0]
14+
15+
**Explanation:** The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
16+
17+
The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(0 - 3) = 3.
18+
19+
The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(1 - 3) = 2.
20+
21+
For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5, but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
22+
23+
The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(8 - 6) = 2.
24+
25+
**Example 2:**
26+
27+
**Input:** s = "aaab", c = "b"
28+
29+
**Output:** [3,2,1,0]
30+
31+
**Constraints:**
32+
33+
* <code>1 <= s.length <= 10<sup>4</sup></code>
34+
* `s[i]` and `c` are lowercase English letters.
35+
* It is guaranteed that `c` occurs at least once in `s`.
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0801_0900.s0818_race_car
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 raceCar() {
10+
assertThat(Solution().racecar(3), equalTo(2))
11+
}
12+
13+
@Test
14+
fun raceCar2() {
15+
assertThat(Solution().racecar(6), equalTo(5))
16+
}
17+
}
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0801_0900.s0819_most_common_word
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 mostCommonWord() {
10+
assertThat(
11+
Solution()
12+
.mostCommonWord(
13+
"Bob hit a ball, the hit BALL flew far after it was hit.", arrayOf("hit")
14+
),
15+
equalTo("ball")
16+
)
17+
}
18+
19+
@Test
20+
fun mostCommonWord2() {
21+
assertThat(Solution().mostCommonWord("a.", arrayOf()), equalTo("a"))
22+
}
23+
}
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0801_0900.s0820_short_encoding_of_words
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 minimumLengthEncoding() {
10+
assertThat(
11+
Solution().minimumLengthEncoding(arrayOf("time", "me", "bell")),
12+
equalTo(10)
13+
)
14+
}
15+
16+
@Test
17+
fun minimumLengthEncoding2() {
18+
assertThat(Solution().minimumLengthEncoding(arrayOf("t")), equalTo(2))
19+
}
20+
}
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0801_0900.s0821_shortest_distance_to_a_character
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 shortestToChar() {
10+
assertThat(
11+
Solution().shortestToChar("loveleetcode", 'e'),
12+
equalTo(intArrayOf(3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0))
13+
)
14+
}
15+
16+
@Test
17+
fun shortestToChar2() {
18+
assertThat(Solution().shortestToChar("aaab", 'b'), equalTo(intArrayOf(3, 2, 1, 0)))
19+
}
20+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.