Skip to content

Commit 6aa50a9

Browse files
authored
Added tasks 717, 718, 719, 720
1 parent 9e140e1 commit 6aa50a9

File tree

13 files changed

+339
-0
lines changed

13 files changed

+339
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16961696
| 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
16971697
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16981698
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
1699+
| 0720 |[Longest Word in Dictionary](src/main/kotlin/g0701_0800/s0720_longest_word_in_dictionary/Solution.kt)| Medium | Array, String, Hash_Table, Sorting, Trie | 209 | 100.00
1700+
| 0719 |[Find K-th Smallest Pair Distance](src/main/kotlin/g0701_0800/s0719_find_k_th_smallest_pair_distance/Solution.kt)| Hard | Array, Sorting, Binary_Search, Two_Pointers | 172 | 100.00
1701+
| 0718 |[Maximum Length of Repeated Subarray](src/main/kotlin/g0701_0800/s0718_maximum_length_of_repeated_subarray/Solution.kt)| Medium | Array, Dynamic_Programming, Binary_Search, Sliding_Window, Hash_Function, Rolling_Hash | 270 | 91.43
1702+
| 0717 |[1-bit and 2-bit Characters](src/main/kotlin/g0701_0800/s0717_1_bit_and_2_bit_characters/Solution.kt)| Easy | Array | 165 | 100.00
16991703
| 0715 |[Range Module](src/main/kotlin/g0701_0800/s0715_range_module/RangeModule.kt)| Hard | Design, Ordered_Set, Segment_Tree | 638 | 58.33
17001704
| 0714 |[Best Time to Buy and Sell Stock with Transaction Fee](src/main/kotlin/g0701_0800/s0714_best_time_to_buy_and_sell_stock_with_transaction_fee/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy, Dynamic_Programming_I_Day_8 | 417 | 90.91
17011705
| 0713 |[Subarray Product Less Than K](src/main/kotlin/g0701_0800/s0713_subarray_product_less_than_k/Solution.kt)| Medium | Array, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Udemy_Arrays | 336 | 92.11
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g0701_0800.s0717_1_bit_and_2_bit_characters
2+
3+
// #Easy #Array #2023_02_27_Time_165_ms_(100.00%)_Space_35.2_MB_(100.00%)
4+
5+
class Solution {
6+
fun isOneBitCharacter(arr: IntArray): Boolean {
7+
var i = 0
8+
while (i < arr.size - 1) {
9+
i = if (arr[i] == 1) i + 2 else i + 1
10+
}
11+
return i == arr.size - 1
12+
}
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
717\. 1-bit and 2-bit Characters
2+
3+
Easy
4+
5+
We have two special characters:
6+
7+
* The first character can be represented by one bit `0`.
8+
* The second character can be represented by two bits (`10` or `11`).
9+
10+
Given a binary array `bits` that ends with `0`, return `true` if the last character must be a one-bit character.
11+
12+
**Example 1:**
13+
14+
**Input:** bits = [1,0,0]
15+
16+
**Output:** true
17+
18+
**Explanation:** The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
19+
20+
**Example 2:**
21+
22+
**Input:** bits = [1,1,1,0]
23+
24+
**Output:** false
25+
26+
**Explanation:** The only way to decode it is two-bit character and two-bit character. So the last character is not one-bit character.
27+
28+
**Constraints:**
29+
30+
* `1 <= bits.length <= 1000`
31+
* `bits[i]` is either `0` or `1`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0701_0800.s0718_maximum_length_of_repeated_subarray
2+
3+
// #Medium #Array #Dynamic_Programming #Binary_Search #Sliding_Window #Hash_Function #Rolling_Hash
4+
// #2023_02_27_Time_270_ms_(91.43%)_Space_45.6_MB_(48.57%)
5+
6+
class Solution {
7+
fun findLength(nums1: IntArray, nums2: IntArray): Int {
8+
val m = nums1.size
9+
val n = nums2.size
10+
var max = 0
11+
val dp = Array(m + 1) { IntArray(n + 1) }
12+
for (i in 1..m) {
13+
for (j in 1..n) {
14+
if (nums1[i - 1] == nums2[j - 1]) {
15+
dp[i][j] = 1 + dp[i - 1][j - 1]
16+
max = Math.max(max, dp[i][j])
17+
}
18+
}
19+
}
20+
return max
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
718\. Maximum Length of Repeated Subarray
2+
3+
Medium
4+
5+
Given two integer arrays `nums1` and `nums2`, return _the maximum length of a subarray that appears in **both** arrays_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
10+
11+
**Output:** 3
12+
13+
**Explanation:** The repeated subarray with maximum length is [3,2,1].
14+
15+
**Example 2:**
16+
17+
**Input:** nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
18+
19+
**Output:** 5
20+
21+
**Explanation:** The repeated subarray with maximum length is [0,0,0,0,0].
22+
23+
**Constraints:**
24+
25+
* `1 <= nums1.length, nums2.length <= 1000`
26+
* `0 <= nums1[i], nums2[i] <= 100`
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0701_0800.s0719_find_k_th_smallest_pair_distance
2+
3+
// #Hard #Array #Sorting #Binary_Search #Two_Pointers
4+
// #2023_02_27_Time_172_ms_(100.00%)_Space_37.2_MB_(100.00%)
5+
6+
class Solution {
7+
fun smallestDistancePair(nums: IntArray, k: Int): Int {
8+
nums.sort()
9+
val length = nums.size
10+
val maxDiff = nums[length - 1] - nums[0]
11+
var start = 0
12+
var end = maxDiff
13+
while (start < end) {
14+
val mid = start + (end - start) / 2
15+
if (isPair(nums, mid, k)) {
16+
end = mid
17+
} else {
18+
start = mid + 1
19+
}
20+
}
21+
return start
22+
}
23+
24+
private fun isPair(nums: IntArray, mid: Int, k: Int): Boolean {
25+
var count = 0
26+
var i = 0
27+
for (j in 1 until nums.size) {
28+
while (nums[j] - nums[i] > mid) {
29+
i++
30+
}
31+
count += j - i
32+
}
33+
return count >= k
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
719\. Find K-th Smallest Pair Distance
2+
3+
Hard
4+
5+
The **distance of a pair** of integers `a` and `b` is defined as the absolute difference between `a` and `b`.
6+
7+
Given an integer array `nums` and an integer `k`, return _the_ <code>k<sup>th</sup></code> _smallest **distance among all the pairs**_ `nums[i]` _and_ `nums[j]` _where_ `0 <= i < j < nums.length`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,1], k = 1
12+
13+
**Output:** 0
14+
15+
**Explanation:** Here are all the pairs:
16+
17+
(1,3) -> 2
18+
19+
(1,1) -> 0
20+
21+
(3,1) -> 2
22+
23+
Then the 1<sup>st</sup> smallest distance pair is (1,1), and its distance is 0.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1,1,1], k = 2
28+
29+
**Output:** 0
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [1,6,1], k = 3
34+
35+
**Output:** 5
36+
37+
**Constraints:**
38+
39+
* `n == nums.length`
40+
* <code>2 <= n <= 10<sup>4</sup></code>
41+
* <code>0 <= nums[i] <= 10<sup>6</sup></code>
42+
* `1 <= k <= n * (n - 1) / 2`
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g0701_0800.s0720_longest_word_in_dictionary
2+
3+
// #Medium #Array #String #Hash_Table #Sorting #Trie
4+
// #2023_02_27_Time_209_ms_(100.00%)_Space_37_MB_(83.33%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
private val root = Node()
9+
private var longestWord: String? = ""
10+
11+
private class Node {
12+
var children: Array<Node?> = arrayOfNulls(26)
13+
var str: String? = null
14+
15+
fun insert(curr: Node?, word: String) {
16+
var curr = curr
17+
for (element in word) {
18+
if (curr!!.children[element.code - 'a'.code] == null) {
19+
curr.children[element.code - 'a'.code] = Node()
20+
}
21+
curr = curr.children[element.code - 'a'.code]
22+
}
23+
curr!!.str = word
24+
}
25+
}
26+
27+
fun longestWord(words: Array<String>): String? {
28+
for (word in words) {
29+
root.insert(root, word)
30+
}
31+
dfs(root)
32+
return longestWord
33+
}
34+
35+
private fun dfs(curr: Node?) {
36+
for (i in 0..25) {
37+
if (curr!!.children[i] != null && curr.children[i]!!.str != null) {
38+
if (curr.children[i]!!.str!!.length > longestWord!!.length) {
39+
longestWord = curr.children[i]!!.str
40+
}
41+
dfs(curr.children[i])
42+
}
43+
}
44+
}
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
720\. Longest Word in Dictionary
2+
3+
Medium
4+
5+
Given an array of strings `words` representing an English Dictionary, return _the longest word in_ `words` _that can be built one character at a time by other words in_ `words`.
6+
7+
If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.
8+
9+
Note that the word should be built from left to right with each additional character being added to the end of a previous word.
10+
11+
**Example 1:**
12+
13+
**Input:** words = ["w","wo","wor","worl","world"]
14+
15+
**Output:** "world"
16+
17+
**Explanation:** The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
18+
19+
**Example 2:**
20+
21+
**Input:** words = ["a","banana","app","appl","ap","apply","apple"]
22+
23+
**Output:** "apple"
24+
25+
**Explanation:** Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
26+
27+
**Constraints:**
28+
29+
* `1 <= words.length <= 1000`
30+
* `1 <= words[i].length <= 30`
31+
* `words[i]` consists of lowercase English letters.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0701_0800.s0717_1_bit_and_2_bit_characters
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 isOneBitCharacter() {
10+
assertThat(Solution().isOneBitCharacter(intArrayOf(1, 0, 0)), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isOneBitCharacter2() {
15+
assertThat(Solution().isOneBitCharacter(intArrayOf(1, 1, 1, 0)), equalTo(false))
16+
}
17+
}

0 commit comments

Comments
 (0)