Skip to content

Commit 02f5d21

Browse files
authored
Added tasks 842, 843, 844, 845
1 parent 8636dc7 commit 02f5d21

File tree

13 files changed

+439
-0
lines changed

13 files changed

+439
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
295295

296296
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
297297
|-|-|-|-|-|-
298+
| 0844 |[Backspace String Compare](src/main/kotlin/g0801_0900/s0844_backspace_string_compare/Solution.kt)| Easy | String, Two_Pointers, Stack, Simulation | 126 | 98.31
298299
| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Stack, Recursion | 224 | 64.86
299300

300301
#### Day 15 Heap
@@ -1036,6 +1037,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
10361037

10371038
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10381039
|-|-|-|-|-|-
1040+
| 0844 |[Backspace String Compare](src/main/kotlin/g0801_0900/s0844_backspace_string_compare/Solution.kt)| Easy | String, Two_Pointers, Stack, Simulation | 126 | 98.31
10391041
| 0011 |[Container With Most Water](src/main/kotlin/g0001_0100/s0011_container_with_most_water/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers | 474 | 89.18
10401042

10411043
#### Day 5 Sliding Window
@@ -1715,6 +1717,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17151717
| 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
17161718
| 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
17171719
| 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
1720+
| 0845 |[Longest Mountain in Array](src/main/kotlin/g0801_0900/s0845_longest_mountain_in_array/Solution.kt)| Medium | Array, Dynamic_Programming, Two_Pointers, Enumeration | 222 | 100.00
1721+
| 0844 |[Backspace String Compare](src/main/kotlin/g0801_0900/s0844_backspace_string_compare/Solution.kt)| Easy | String, Two_Pointers, Stack, Simulation, Algorithm_II_Day_4_Two_Pointers, Level_1_Day_14_Stack | 126 | 98.31
1722+
| 0843 |[Guess the Word](src/main/kotlin/g0801_0900/s0843_guess_the_word/Solution.kt)| Hard | Array, String, Math, Game_Theory, Interactive | 75 | 100.00
1723+
| 0842 |[Split Array into Fibonacci Sequence](src/main/kotlin/g0801_0900/s0842_split_array_into_fibonacci_sequence/Solution.kt)| Medium | String, Backtracking | 142 | 100.00
17181724
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Data_Structure_II_Day_19_Graph, Graph_Theory_I_Day_7_Standard_Traversal | 189 | 69.23
17191725
| 0840 |[Magic Squares In Grid](src/main/kotlin/g0801_0900/s0840_magic_squares_in_grid/Solution.kt)| Medium | Array, Math, Matrix | 149 | 100.00
17201726
| 0839 |[Similar String Groups](src/main/kotlin/g0801_0900/s0839_similar_string_groups/Solution.kt)| Hard | Array, String, Depth_First_Search, Breadth_First_Search, Union_Find | 205 | 100.00
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0801_0900.s0842_split_array_into_fibonacci_sequence
2+
3+
// #Medium #String #Backtracking #2023_03_29_Time_142_ms_(100.00%)_Space_35_MB_(100.00%)
4+
5+
class Solution {
6+
fun splitIntoFibonacci(num: String): List<Int> {
7+
val res: MutableList<Int> = ArrayList()
8+
solve(num, res, 0)
9+
return res
10+
}
11+
12+
private fun solve(s: String, res: MutableList<Int>, idx: Int): Boolean {
13+
if (idx == s.length && res.size >= 3) {
14+
return true
15+
}
16+
for (i in idx until s.length) {
17+
if (s[idx] == '0' && i > idx) {
18+
return false
19+
}
20+
val num = s.substring(idx, i + 1).toLong()
21+
if (num > Int.MAX_VALUE) {
22+
return false
23+
}
24+
val size = res.size
25+
if (size >= 2 && num > res[size - 1] + res[size - 2]) {
26+
return false
27+
}
28+
if (size <= 1 || num == (res[size - 1] + res[size - 2]).toLong()) {
29+
res.add(num.toInt())
30+
if (solve(s, res, i + 1)) {
31+
return true
32+
}
33+
res.removeAt(res.size - 1)
34+
}
35+
}
36+
return false
37+
}
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
842\. Split Array into Fibonacci Sequence
2+
3+
Medium
4+
5+
You are given a string of digits `num`, such as `"123456579"`. We can split it into a Fibonacci-like sequence `[123, 456, 579]`.
6+
7+
Formally, a **Fibonacci-like** sequence is a list `f` of non-negative integers such that:
8+
9+
* <code>0 <= f[i] < 2<sup>31</sup></code>, (that is, each integer fits in a **32-bit** signed integer type),
10+
* `f.length >= 3`, and
11+
* `f[i] + f[i + 1] == f[i + 2]` for all `0 <= i < f.length - 2`.
12+
13+
Note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number `0` itself.
14+
15+
Return any Fibonacci-like sequence split from `num`, or return `[]` if it cannot be done.
16+
17+
**Example 1:**
18+
19+
**Input:** num = "1101111"
20+
21+
**Output:** [11,0,11,11]
22+
23+
**Explanation:** The output [110, 1, 111] would also be accepted.
24+
25+
**Example 2:**
26+
27+
**Input:** num = "112358130"
28+
29+
**Output:** []
30+
31+
**Explanation:** The task is impossible.
32+
33+
**Example 3:**
34+
35+
**Input:** num = "0123"
36+
37+
**Output:** []
38+
39+
**Explanation:** Leading zeroes are not allowed, so "01", "2", "3" is not valid.
40+
41+
**Constraints:**
42+
43+
* `1 <= num.length <= 200`
44+
* `num` contains only digits.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g0801_0900.s0843_guess_the_word
2+
3+
// #Hard #Array #String #Math #Game_Theory #Interactive
4+
// #2023_03_29_Time_75_ms_(100.00%)_Space_31.5_MB_(100.00%)
5+
6+
import java.util.Collections
7+
8+
/*
9+
* // This is the Master's API interface.
10+
* // You should not implement it, or speculate about its implementation
11+
* interface Master {
12+
* fun guess(word: String): Int {}
13+
* }
14+
*/
15+
class Solution {
16+
interface Master {
17+
fun guess(word: String): Int
18+
}
19+
20+
private var next = 0
21+
22+
fun findSecretWord(wordlist: Array<String>, master: Master) {
23+
val list = listOf(*wordlist)
24+
Collections.shuffle(list)
25+
val test = BooleanArray(wordlist.size)
26+
while (true) {
27+
val num = master.guess(list[next])
28+
if (num == 6) {
29+
break
30+
}
31+
updateList(list, test, num)
32+
}
33+
}
34+
35+
private fun updateList(list: List<String?>, test: BooleanArray, num: Int) {
36+
val index = next
37+
for (i in index + 1 until test.size) {
38+
if (test[i]) {
39+
continue
40+
}
41+
val samePart = getSame(list[index], list[i])
42+
if (samePart != num) {
43+
test[i] = true
44+
} else if (next == index) {
45+
next = i
46+
}
47+
}
48+
}
49+
50+
private fun getSame(word1: String?, word2: String?): Int {
51+
var ret = 0
52+
for (i in 0..5) {
53+
if (word1!![i] == word2!![i]) {
54+
ret++
55+
}
56+
}
57+
return ret
58+
}
59+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
843\. Guess the Word
2+
3+
Hard
4+
5+
This is an **_interactive problem_**.
6+
7+
You are given an array of **unique** strings `wordlist` where `wordlist[i]` is `6` letters long, and one word in this list is chosen as `secret`.
8+
9+
You may call `Master.guess(word)` to guess a word. The guessed word should have type `string` and must be from the original list with `6` lowercase letters.
10+
11+
This function returns an `integer` type, representing the number of exact matches (value and position) of your guess to the `secret` word. Also, if your guess is not in the given wordlist, it will return `-1` instead.
12+
13+
For each test case, you have exactly `10` guesses to guess the word. At the end of any number of calls, if you have made `10` or fewer calls to `Master.guess` and at least one of these guesses was `secret`, then you pass the test case.
14+
15+
**Example 1:**
16+
17+
**Input:** secret = "acckzz", wordlist = ["acckzz","ccbazz","eiowzz","abcczz"], numguesses = 10
18+
19+
**Output:** You guessed the secret word correctly.
20+
21+
**Explanation:**
22+
23+
master.guess("aaaaaa") returns -1, because "aaaaaa" is not in wordlist.
24+
master.guess("acckzz") returns 6, because "acckzz" is secret and has all 6 matches.
25+
master.guess("ccbazz") returns 3, because "ccbazz" has 3 matches.
26+
master.guess("eiowzz") returns 2, because "eiowzz" has 2 matches.
27+
master.guess("abcczz") returns 4, because "abcczz" has 4 matches.
28+
We made 5 calls to master.guess and one of them was the secret, so we pass the test case.
29+
30+
**Example 2:**
31+
32+
**Input:** secret = "hamada", wordlist = ["hamada","khaled"], numguesses = 10
33+
34+
**Output:** You guessed the secret word correctly.
35+
36+
**Constraints:**
37+
38+
* `1 <= wordlist.length <= 100`
39+
* `wordlist[i].length == 6`
40+
* `wordlist[i]` consist of lowercase English letters.
41+
* All the strings of `wordlist` are **unique**.
42+
* `secret` exists in `wordlist`.
43+
* `numguesses == 10`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0801_0900.s0844_backspace_string_compare
2+
3+
// #Easy #String #Two_Pointers #Stack #Simulation #Algorithm_II_Day_4_Two_Pointers
4+
// #Level_1_Day_14_Stack #2023_03_29_Time_126_ms_(98.31%)_Space_33.5_MB_(93.22%)
5+
6+
class Solution {
7+
fun backspaceCompare(s: String, t: String): Boolean {
8+
return cmprStr(s) == cmprStr(t)
9+
}
10+
11+
private fun cmprStr(str: String): String {
12+
val res = StringBuilder()
13+
var count = 0
14+
for (i in str.length - 1 downTo 0) {
15+
val currChar = str[i]
16+
if (currChar == '#') {
17+
count++
18+
} else {
19+
if (count > 0) {
20+
count--
21+
} else {
22+
res.append(currChar)
23+
}
24+
}
25+
}
26+
return res.toString()
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
844\. Backspace String Compare
2+
3+
Easy
4+
5+
Given two strings `s` and `t`, return `true` _if they are equal when both are typed into empty text editors_. `'#'` means a backspace character.
6+
7+
Note that after backspacing an empty text, the text will continue empty.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "ab#c", t = "ad#c"
12+
13+
**Output:** true
14+
15+
**Explanation:** Both s and t become "ac".
16+
17+
**Example 2:**
18+
19+
**Input:** s = "ab##", t = "c#d#"
20+
21+
**Output:** true
22+
23+
**Explanation:** Both s and t become "".
24+
25+
**Example 3:**
26+
27+
**Input:** s = "a#c", t = "b"
28+
29+
**Output:** false
30+
31+
**Explanation:** s becomes "c" while t becomes "b".
32+
33+
**Constraints:**
34+
35+
* `1 <= s.length, t.length <= 200`
36+
* `s` and `t` only contain lowercase letters and `'#'` characters.
37+
38+
**Follow up:** Can you solve it in `O(n)` time and `O(1)` space?
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0801_0900.s0845_longest_mountain_in_array
2+
3+
// #Medium #Array #Dynamic_Programming #Two_Pointers #Enumeration
4+
// #2023_03_29_Time_222_ms_(100.00%)_Space_38.4_MB_(16.67%)
5+
6+
class Solution {
7+
fun longestMountain(arr: IntArray): Int {
8+
var s = 0
9+
var i = 1
10+
while (i < arr.size - 1) {
11+
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
12+
var j = i
13+
var tem = 1
14+
while (j > 0 && arr[j] > arr[j - 1]) {
15+
j--
16+
tem++
17+
}
18+
j = i
19+
while (j < arr.size - 1 && arr[j] > arr[j + 1]) {
20+
j++
21+
tem++
22+
}
23+
s = s.coerceAtLeast(tem)
24+
i = j
25+
}
26+
i++
27+
}
28+
return s
29+
}
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
845\. Longest Mountain in Array
2+
3+
Medium
4+
5+
You may recall that an array `arr` is a **mountain array** if and only if:
6+
7+
* `arr.length >= 3`
8+
* There exists some index `i` (**0-indexed**) with `0 < i < arr.length - 1` such that:
9+
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
10+
* `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
11+
12+
Given an integer array `arr`, return _the length of the longest subarray, which is a mountain_. Return `0` if there is no mountain subarray.
13+
14+
**Example 1:**
15+
16+
**Input:** arr = [2,1,4,7,3,2,5]
17+
18+
**Output:** 5
19+
20+
**Explanation:** The largest mountain is [1,4,7,3,2] which has length 5.
21+
22+
**Example 2:**
23+
24+
**Input:** arr = [2,2,2]
25+
26+
**Output:** 0
27+
28+
**Explanation:** There is no mountain.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= arr.length <= 10<sup>4</sup></code>
33+
* <code>0 <= arr[i] <= 10<sup>4</sup></code>
34+
35+
**Follow up:**
36+
37+
* Can you solve it using only one pass?
38+
* Can you solve it in `O(1)` space?
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0801_0900.s0842_split_array_into_fibonacci_sequence
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 splitIntoFibonacci() {
10+
assertThat(
11+
Solution().splitIntoFibonacci("1101111"),
12+
equalTo(listOf(11, 0, 11, 11))
13+
)
14+
}
15+
16+
@Test
17+
fun splitIntoFibonacci2() {
18+
assertThat(
19+
Solution().splitIntoFibonacci("112358130"), equalTo(emptyList())
20+
)
21+
}
22+
23+
@Test
24+
fun splitIntoFibonacci3() {
25+
assertThat(Solution().splitIntoFibonacci("0123"), equalTo(emptyList()))
26+
}
27+
}

0 commit comments

Comments
 (0)