Skip to content

Commit b16b9b8

Browse files
authored
Added tasks 643, 645, 646, 648
1 parent f64a80a commit b16b9b8

File tree

13 files changed

+356
-0
lines changed

13 files changed

+356
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16731673
| 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
16741674
| 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
16751675
| 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
1676+
| 0648 |[Replace Words](src/main/kotlin/g0601_0700/s0648_replace_words/Solution.kt)| Medium | Array, String, Hash_Table, Trie | 392 | 100.00
16761677
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1678+
| 0646 |[Maximum Length of Pair Chain](src/main/kotlin/g0601_0700/s0646_maximum_length_of_pair_chain/Solution.kt)| Medium | Array, Dynamic_Programming, Sorting, Greedy | 249 | 100.00
1679+
| 0645 |[Set Mismatch](src/main/kotlin/g0601_0700/s0645_set_mismatch/Solution.kt)| Easy | Array, Hash_Table, Sorting, Bit_Manipulation | 246 | 88.46
1680+
| 0643 |[Maximum Average Subarray I](src/main/kotlin/g0601_0700/s0643_maximum_average_subarray_i/Solution.kt)| Easy | Array, Sliding_Window | 494 | 98.65
16771681
| 0641 |[Design Circular Deque](src/main/kotlin/g0601_0700/s0641_design_circular_deque/MyCircularDeque.kt)| Medium | Array, Design, Linked_List, Queue | 232 | 100.00
16781682
| 0640 |[Solve the Equation](src/main/kotlin/g0601_0700/s0640_solve_the_equation/Solution.kt)| Medium | String, Math, Simulation | 170 | 66.67
16791683
| 0639 |[Decode Ways II](src/main/kotlin/g0601_0700/s0639_decode_ways_ii/Solution.kt)| Hard | String, Dynamic_Programming | 259 | 100.00
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0601_0700.s0643_maximum_average_subarray_i
2+
3+
// #Easy #Array #Sliding_Window #2023_02_11_Time_494_ms_(98.65%)_Space_45.8_MB_(95.95%)
4+
5+
class Solution {
6+
fun findMaxAverage(nums: IntArray, k: Int): Double {
7+
var windowSum = 0.0
8+
var windowStart = 0
9+
var max = Int.MIN_VALUE.toDouble()
10+
for (windowEnd in nums.indices) {
11+
windowSum += nums[windowEnd].toDouble()
12+
if (windowEnd >= k - 1) {
13+
val candidate = windowSum / k
14+
max = Math.max(candidate, max)
15+
windowSum -= nums[windowStart].toDouble()
16+
windowStart++
17+
}
18+
}
19+
return max
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
643\. Maximum Average Subarray I
2+
3+
Easy
4+
5+
You are given an integer array `nums` consisting of `n` elements, and an integer `k`.
6+
7+
Find a contiguous subarray whose **length is equal to** `k` that has the maximum average value and return _this value_. Any answer with a calculation error less than <code>10<sup>-5</sup></code> will be accepted.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,12,-5,-6,50,3], k = 4
12+
13+
**Output:** 12.75000
14+
15+
**Explanation:** Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [5], k = 1
20+
21+
**Output:** 5.00000
22+
23+
**Constraints:**
24+
25+
* `n == nums.length`
26+
* <code>1 <= k <= n <= 10<sup>5</sup></code>
27+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0601_0700.s0645_set_mismatch
2+
3+
// #Easy #Array #Hash_Table #Sorting #Bit_Manipulation
4+
// #2023_02_11_Time_246_ms_(88.46%)_Space_38.1_MB_(80.77%)
5+
6+
class Solution {
7+
fun findErrorNums(nums: IntArray): IntArray {
8+
val ans = IntArray(2)
9+
var i = 0
10+
while (i < nums.size) {
11+
val correct = nums[i] - 1
12+
if (nums[i] != nums[correct]) {
13+
val temp = nums[i]
14+
nums[i] = nums[correct]
15+
nums[correct] = temp
16+
} else {
17+
i++
18+
}
19+
}
20+
for (a in nums.indices) {
21+
if (nums[a] != a + 1) {
22+
ans[0] = nums[a]
23+
ans[1] = a + 1
24+
break
25+
}
26+
}
27+
return ans
28+
}
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
645\. Set Mismatch
2+
3+
Easy
4+
5+
You have a set of integers `s`, which originally contains all the numbers from `1` to `n`. Unfortunately, due to some error, one of the numbers in `s` got duplicated to another number in the set, which results in **repetition of one** number and **loss of another** number.
6+
7+
You are given an integer array `nums` representing the data status of this set after the error.
8+
9+
Find the number that occurs twice and the number that is missing and return _them in the form of an array_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,2,4]
14+
15+
**Output:** [2,3]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1,1]
20+
21+
**Output:** [1,2]
22+
23+
**Constraints:**
24+
25+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
26+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0601_0700.s0646_maximum_length_of_pair_chain
2+
3+
// #Medium #Array #Dynamic_Programming #Sorting #Greedy
4+
// #2023_02_11_Time_249_ms_(100.00%)_Space_38.2_MB_(100.00%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
fun findLongestChain(pairs: Array<IntArray>): Int {
10+
if (pairs.size == 1) {
11+
return 1
12+
}
13+
Arrays.sort(pairs) { a: IntArray, b: IntArray ->
14+
a[1] - b[1]
15+
}
16+
var min = pairs[0][1]
17+
var max = 1
18+
for (i in 1 until pairs.size) {
19+
if (pairs[i][0] > min) {
20+
max++
21+
min = pairs[i][1]
22+
}
23+
}
24+
return max
25+
}
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
646\. Maximum Length of Pair Chain
2+
3+
Medium
4+
5+
You are given an array of `n` pairs `pairs` where <code>pairs[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> and <code>left<sub>i</sub> < right<sub>i</sub></code>.
6+
7+
A pair `p2 = [c, d]` **follows** a pair `p1 = [a, b]` if `b < c`. A **chain** of pairs can be formed in this fashion.
8+
9+
Return _the length longest chain which can be formed_.
10+
11+
You do not need to use up all the given intervals. You can select pairs in any order.
12+
13+
**Example 1:**
14+
15+
**Input:** pairs = [[1,2],[2,3],[3,4]]
16+
17+
**Output:** 2
18+
19+
**Explanation:** The longest chain is [1,2] -> [3,4].
20+
21+
**Example 2:**
22+
23+
**Input:** pairs = [[1,2],[7,8],[4,5]]
24+
25+
**Output:** 3
26+
27+
**Explanation:** The longest chain is [1,2] -> [4,5] -> [7,8].
28+
29+
**Constraints:**
30+
31+
* `n == pairs.length`
32+
* `1 <= n <= 1000`
33+
* <code>-1000 <= left<sub>i</sub> < right<sub>i</sub> <= 1000</code>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g0601_0700.s0648_replace_words
2+
3+
// #Medium #Array #String #Hash_Table #Trie #2023_02_11_Time_392_ms_(100.00%)_Space_62.4_MB_(25.00%)
4+
5+
import java.util.function.Consumer
6+
7+
class Solution {
8+
fun replaceWords(dictionary: List<String>, sentence: String): String {
9+
val trie = Trie()
10+
dictionary.forEach(Consumer { word: String -> trie.insert(word) })
11+
val allWords = sentence.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
12+
for (i in allWords.indices) {
13+
allWords[i] = trie.getRootForWord(allWords[i])
14+
}
15+
return java.lang.String.join(" ", *allWords)
16+
}
17+
18+
internal class Node {
19+
var links = arrayOfNulls<Node>(26)
20+
var isWordCompleted = false
21+
22+
fun containsKey(ch: Char): Boolean {
23+
return links[ch.code - 'a'.code] != null
24+
}
25+
26+
fun put(ch: Char, node: Node?) {
27+
links[ch.code - 'a'.code] = node
28+
}
29+
30+
operator fun get(ch: Char): Node? {
31+
return links[ch.code - 'a'.code]
32+
}
33+
}
34+
35+
internal class Trie {
36+
var root: Node = Node()
37+
38+
fun insert(word: String) {
39+
var node: Node? = root
40+
for (i in word.indices) {
41+
if (!node!!.containsKey(word[i])) {
42+
node.put(word[i], Node())
43+
}
44+
node = node[word[i]]
45+
}
46+
node!!.isWordCompleted = true
47+
}
48+
49+
fun getRootForWord(word: String): String {
50+
var node: Node? = root
51+
val rootWord = StringBuilder()
52+
for (i in word.indices) {
53+
if (node!!.containsKey(word[i])) {
54+
rootWord.append(word[i])
55+
node = node[word[i]]
56+
if (node!!.isWordCompleted) {
57+
return rootWord.toString()
58+
}
59+
} else {
60+
return word
61+
}
62+
}
63+
return word
64+
}
65+
}
66+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
648\. Replace Words
2+
3+
Medium
4+
5+
In English, we have a concept called **root**, which can be followed by some other word to form another longer word - let's call this word **successor**. For example, when the **root** `"an"` is followed by the **successor** word `"other"`, we can form a new word `"another"`.
6+
7+
Given a `dictionary` consisting of many **roots** and a `sentence` consisting of words separated by spaces, replace all the **successors** in the sentence with the **root** forming it. If a **successor** can be replaced by more than one **root**, replace it with the **root** that has **the shortest length**.
8+
9+
Return _the `sentence`_ after the replacement.
10+
11+
**Example 1:**
12+
13+
**Input:** dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
14+
15+
**Output:** "the cat was rat by the bat"
16+
17+
**Example 2:**
18+
19+
**Input:** dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
20+
21+
**Output:** "a a b c"
22+
23+
**Constraints:**
24+
25+
* `1 <= dictionary.length <= 1000`
26+
* `1 <= dictionary[i].length <= 100`
27+
* `dictionary[i]` consists of only lower-case letters.
28+
* <code>1 <= sentence.length <= 10<sup>6</sup></code>
29+
* `sentence` consists of only lower-case letters and spaces.
30+
* The number of words in `sentence` is in the range `[1, 1000]`
31+
* The length of each word in `sentence` is in the range `[1, 1000]`
32+
* Every two consecutive words in `sentence` will be separated by exactly one space.
33+
* `sentence` does not have leading or trailing spaces.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0601_0700.s0643_maximum_average_subarray_i
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 findMaxAverage() {
10+
assertThat(
11+
Solution().findMaxAverage(intArrayOf(1, 12, -5, -6, 50, 3), 4),
12+
equalTo(12.75000)
13+
)
14+
}
15+
16+
@Test
17+
fun findMaxAverage2() {
18+
assertThat(Solution().findMaxAverage(intArrayOf(5), 1), equalTo(5.00000))
19+
}
20+
}

0 commit comments

Comments
 (0)