Skip to content

Commit ca0c571

Browse files
ThanhNITjavadev
authored andcommitted
Added tasks 916, 917, 918, 919
1 parent 93fc4b1 commit ca0c571

File tree

13 files changed

+415
-0
lines changed

13 files changed

+415
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
13111311
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13121312
|-|-|-|-|-|-
13131313
| 0053 |[Maximum Subarray](src.save/main/kotlin/g0001_0100/s0053_maximum_subarray/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer | 662 | 82.48
1314+
| 0918 |[Maximum Sum Circular Subarray](src/main/kotlin/g0901_1000/s0918_maximum_sum_circular_subarray/Solution.kt)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue | 339 | 86.96
13141315

13151316
#### Day 6
13161317

@@ -1729,6 +1730,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17291730
|------|----------------|-------------|-------------|----------|---------
17301731
| 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
17311732
| 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
1733+
| 0919 |[Complete Binary Tree Inserter](src/main/kotlin/g0901_1000/s0919_complete_binary_tree_inserter/CBTInserter.kt)| Medium | Breadth_First_Search, Tree, Binary_Tree, Design | 225 | 100.00
1734+
| 0918 |[Maximum Sum Circular Subarray](src/main/kotlin/g0901_1000/s0918_maximum_sum_circular_subarray/Solution.kt)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue, Dynamic_Programming_I_Day_5 | 339 | 86.96
1735+
| 0917 |[Reverse Only Letters](src/main/kotlin/g0901_1000/s0917_reverse_only_letters/Solution.kt)| Easy | String, Two_Pointers | 126 | 100.00
1736+
| 0916 |[Word Subsets](src/main/kotlin/g0901_1000/s0916_word_subsets/Solution.kt)| Medium | Array, String, Hash_Table | 397 | 88.89
17321737
| 0915 |[Partition Array into Disjoint Intervals](src/main/kotlin/g0901_1000/s0915_partition_array_into_disjoint_intervals/Solution.kt)| Medium | Array | 510 | 76.92
17331738
| 0914 |[X of a Kind in a Deck of Cards](src/main/kotlin/g0901_1000/s0914_x_of_a_kind_in_a_deck_of_cards/Solution.kt)| Easy | Array, Hash_Table, Math, Counting, Number_Theory | 238 | 70.00
17341739
| 0913 |[Cat and Mouse](src/main/kotlin/g0901_1000/s0913_cat_and_mouse/Solution.kt)| Hard | Dynamic_Programming, Math, Graph, Memoization, Topological_Sort, Game_Theory | 211 | 100.00
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0901_1000.s0916_word_subsets
2+
3+
// #Medium #Array #String #Hash_Table #2023_04_16_Time_397_ms_(88.89%)_Space_45_MB_(55.56%)
4+
5+
class Solution {
6+
fun wordSubsets(words1: Array<String>, words2: Array<String>): List<String> {
7+
val l1: MutableList<String> = ArrayList()
8+
val target = IntArray(26)
9+
for (s1 in words2) {
10+
val temp = IntArray(26)
11+
for (ch1 in s1.toCharArray()) {
12+
temp[ch1.code - 'a'.code]++
13+
target[ch1.code - 'a'.code] =
14+
target[ch1.code - 'a'.code].coerceAtLeast(temp[ch1.code - 'a'.code])
15+
}
16+
}
17+
for (s1 in words1) {
18+
val count = IntArray(26)
19+
for (ch1 in s1.toCharArray()) {
20+
count[ch1.code - 'a'.code]++
21+
}
22+
if (checkIt(target, count)) {
23+
l1.add(s1)
24+
}
25+
}
26+
return l1
27+
}
28+
29+
private fun checkIt(target: IntArray, count: IntArray): Boolean {
30+
for (i in 0..25) {
31+
if (count[i] < target[i]) {
32+
return false
33+
}
34+
}
35+
return true
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
916\. Word Subsets
2+
3+
Medium
4+
5+
You are given two string arrays `words1` and `words2`.
6+
7+
A string `b` is a **subset** of string `a` if every letter in `b` occurs in `a` including multiplicity.
8+
9+
* For example, `"wrr"` is a subset of `"warrior"` but is not a subset of `"world"`.
10+
11+
A string `a` from `words1` is **universal** if for every string `b` in `words2`, `b` is a subset of `a`.
12+
13+
Return an array of all the **universal** strings in `words1`. You may return the answer in **any order**.
14+
15+
**Example 1:**
16+
17+
**Input:** words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["e","o"]
18+
19+
**Output:** ["facebook","google","leetcode"]
20+
21+
**Example 2:**
22+
23+
**Input:** words1 = ["amazon","apple","facebook","google","leetcode"], words2 = ["l","e"]
24+
25+
**Output:** ["apple","google","leetcode"]
26+
27+
**Constraints:**
28+
29+
* <code>1 <= words1.length, words2.length <= 10<sup>4</sup></code>
30+
* `1 <= words1[i].length, words2[i].length <= 10`
31+
* `words1[i]` and `words2[i]` consist only of lowercase English letters.
32+
* All the strings of `words1` are **unique**.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0901_1000.s0917_reverse_only_letters
2+
3+
// #Easy #String #Two_Pointers #2023_04_16_Time_126_ms_(100.00%)_Space_33.6_MB_(94.44%)
4+
5+
class Solution {
6+
fun reverseOnlyLetters(s: String): String {
7+
val array = s.toCharArray()
8+
var i = 0
9+
var j = array.size - 1
10+
while (i < j) {
11+
if (Character.isLetter(array[i]) && Character.isLetter(array[j])) {
12+
val temp = array[i]
13+
array[i++] = array[j]
14+
array[j--] = temp
15+
} else if (Character.isLetter(array[i])) {
16+
j--
17+
} else if (Character.isLetter(array[j])) {
18+
i++
19+
} else {
20+
i++
21+
j--
22+
}
23+
}
24+
return String(array)
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
917\. Reverse Only Letters
2+
3+
Easy
4+
5+
Given a string `s`, reverse the string according to the following rules:
6+
7+
* All the characters that are not English letters remain in the same position.
8+
* All the English letters (lowercase or uppercase) should be reversed.
9+
10+
Return `s` _after reversing it_.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "ab-cd"
15+
16+
**Output:** "dc-ba"
17+
18+
**Example 2:**
19+
20+
**Input:** s = "a-bC-dEf-ghIj"
21+
22+
**Output:** "j-Ih-gfE-dCba"
23+
24+
**Example 3:**
25+
26+
**Input:** s = "Test1ng-Leet=code-Q!"
27+
28+
**Output:** "Qedo1ct-eeLg=ntse-T!"
29+
30+
**Constraints:**
31+
32+
* `1 <= s.length <= 100`
33+
* `s` consists of characters with ASCII values in the range `[33, 122]`.
34+
* `s` does not contain `'\"'` or `'\\'`.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0901_1000.s0918_maximum_sum_circular_subarray
2+
3+
// #Medium #Array #Dynamic_Programming #Divide_and_Conquer #Queue #Monotonic_Queue
4+
// #Dynamic_Programming_I_Day_5 #2023_04_16_Time_339_ms_(86.96%)_Space_46.4_MB_(56.52%)
5+
6+
class Solution {
7+
private fun kadane(nums: IntArray, sign: Int): Int {
8+
var currSum = Int.MIN_VALUE
9+
var maxSum = Int.MIN_VALUE
10+
for (i in nums) {
11+
currSum = sign * i + currSum.coerceAtLeast(0)
12+
maxSum = maxSum.coerceAtLeast(currSum)
13+
}
14+
return maxSum
15+
}
16+
17+
fun maxSubarraySumCircular(nums: IntArray): Int {
18+
if (nums.size == 1) {
19+
return nums[0]
20+
}
21+
var sumOfArray = 0
22+
for (i in nums) {
23+
sumOfArray += i
24+
}
25+
val maxSumSubarray = kadane(nums, 1)
26+
val minSumSubarray = kadane(nums, -1) * -1
27+
return if (sumOfArray == minSumSubarray) {
28+
maxSumSubarray
29+
} else {
30+
maxSumSubarray.coerceAtLeast(sumOfArray - minSumSubarray)
31+
}
32+
}
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
918\. Maximum Sum Circular Subarray
2+
3+
Medium
4+
5+
Given a **circular integer array** `nums` of length `n`, return _the maximum possible sum of a non-empty **subarray** of_ `nums`.
6+
7+
A **circular array** means the end of the array connects to the beginning of the array. Formally, the next element of `nums[i]` is `nums[(i + 1) % n]` and the previous element of `nums[i]` is `nums[(i - 1 + n) % n]`.
8+
9+
A **subarray** may only include each element of the fixed buffer `nums` at most once. Formally, for a subarray `nums[i], nums[i + 1], ..., nums[j]`, there does not exist `i <= k1`, `k2 <= j` with `k1 % n == k2 % n`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,-2,3,-2]
14+
15+
**Output:** 3
16+
17+
**Explanation:** Subarray [3] has maximum sum 3.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [5,-3,5]
22+
23+
**Output:** 10
24+
25+
**Explanation:** Subarray [5,5] has maximum sum 5 + 5 = 10.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [-3,-2,-3]
30+
31+
**Output:** -2
32+
33+
**Explanation:** Subarray [-2] has maximum sum -2.
34+
35+
**Constraints:**
36+
37+
* `n == nums.length`
38+
* <code>1 <= n <= 3 * 10<sup>4</sup></code>
39+
* <code>-3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup></code>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package g0901_1000.s0919_complete_binary_tree_inserter
2+
3+
// #Medium #Breadth_First_Search #Tree #Binary_Tree #Design
4+
// #2023_04_16_Time_225_ms_(100.00%)_Space_37_MB_(100.00%)
5+
6+
import com_github_leetcode.TreeNode
7+
import java.util.LinkedList
8+
import java.util.Queue
9+
10+
/*
11+
* Example:
12+
* var ti = TreeNode(5)
13+
* var v = ti.`val`
14+
* Definition for a binary tree node.
15+
* class TreeNode(var `val`: Int) {
16+
* var left: TreeNode? = null
17+
* var right: TreeNode? = null
18+
* }
19+
*/
20+
class CBTInserter(root: TreeNode?) {
21+
private val q: Queue<TreeNode>
22+
private val head: TreeNode
23+
24+
init {
25+
q = LinkedList()
26+
head = root!!
27+
addToQueue()
28+
}
29+
30+
private fun addToQueue() {
31+
val hlq: Queue<TreeNode> = LinkedList()
32+
hlq.add(head)
33+
while (!hlq.isEmpty()) {
34+
var size = hlq.size
35+
while (size-- > 0) {
36+
val poll: TreeNode = hlq.poll()
37+
q.add(poll)
38+
if (poll.left != null) {
39+
hlq.add(poll.left)
40+
}
41+
if (poll.right != null) {
42+
hlq.add(poll.right)
43+
}
44+
}
45+
}
46+
}
47+
48+
fun insert(`val`: Int): Int {
49+
val nn = TreeNode(`val`)
50+
deleteFullNode()
51+
val peek: TreeNode = q.peek()
52+
if (peek.left == null) {
53+
peek.left = nn
54+
} else {
55+
peek.right = nn
56+
}
57+
q.add(nn)
58+
return peek.`val`
59+
}
60+
61+
private fun deleteFullNode() {
62+
while (!q.isEmpty()) {
63+
val peek: TreeNode = q.peek()
64+
if (peek.left != null && peek.right != null) {
65+
q.poll()
66+
} else {
67+
break
68+
}
69+
}
70+
}
71+
72+
// get_root()
73+
fun getRoot(): TreeNode {
74+
return head
75+
}
76+
}
77+
78+
/*
79+
* Your CBTInserter object will be instantiated and called as such:
80+
* var obj = CBTInserter(root)
81+
* var param_1 = obj.insert(`val`)
82+
* var param_2 = obj.get_root()
83+
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
919\. Complete Binary Tree Inserter
2+
3+
Medium
4+
5+
A **complete binary tree** is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
6+
7+
Design an algorithm to insert a new node to a complete binary tree keeping it complete after the insertion.
8+
9+
Implement the `CBTInserter` class:
10+
11+
* `CBTInserter(TreeNode root)` Initializes the data structure with the `root` of the complete binary tree.
12+
* `int insert(int v)` Inserts a `TreeNode` into the tree with value `Node.val == val` so that the tree remains complete, and returns the value of the parent of the inserted `TreeNode`.
13+
* `TreeNode get_root()` Returns the root node of the tree.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/08/03/lc-treeinsert.jpg)
18+
19+
**Input** ["CBTInserter", "insert", "insert", "get\_root"] [[[1, 2]], [3], [4], []]
20+
21+
**Output:** [null, 1, 2, [1, 2, 3, 4]]
22+
23+
**Explanation:**
24+
25+
CBTInserter cBTInserter = new CBTInserter([1, 2]);
26+
cBTInserter.insert(3); // return 1
27+
cBTInserter.insert(4); // return 2
28+
cBTInserter.get\_root(); // return [1, 2, 3, 4]
29+
30+
**Constraints:**
31+
32+
* The number of nodes in the tree will be in the range `[1, 1000]`.
33+
* `0 <= Node.val <= 5000`
34+
* `root` is a complete binary tree.
35+
* `0 <= val <= 5000`
36+
* At most <code>10<sup>4</sup></code> calls will be made to `insert` and `get_root`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0901_1000.s0916_word_subsets
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 wordSubsets() {
10+
assertThat(
11+
Solution()
12+
.wordSubsets(arrayOf("amazon", "apple", "facebook", "google", "leetcode"), arrayOf("e", "o")),
13+
equalTo(listOf("facebook", "google", "leetcode"))
14+
)
15+
}
16+
17+
@Test
18+
fun wordSubsets2() {
19+
assertThat(
20+
Solution()
21+
.wordSubsets(arrayOf("amazon", "apple", "facebook", "google", "leetcode"), arrayOf("l", "e")),
22+
equalTo(listOf("apple", "google", "leetcode"))
23+
)
24+
}
25+
}

0 commit comments

Comments
 (0)