Skip to content

Commit 456c72b

Browse files
authored
Added tasks 649, 650, 652, 653
1 parent 580bbfb commit 456c72b

File tree

13 files changed

+378
-0
lines changed

13 files changed

+378
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
859859
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
860860
|-|-|-|-|-|-
861861
| 0098 |[Validate Binary Search Tree](src.save/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38
862+
| 0653 |[Two Sum IV - Input is a BST](src/main/kotlin/g0601_0700/s0653_two_sum_iv_input_is_a_bst/Solution.kt)| Easy | Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Two_Pointers, Binary_Search_Tree | 231 | 96.08
862863
| 0235 |[Lowest Common Ancestor of a Binary Search Tree](src.save/main/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 404 | 75.59
863864

864865
### Data Structure II
@@ -1673,6 +1674,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16731674
| 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
16741675
| 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
16751676
| 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
1677+
| 0653 |[Two Sum IV - Input is a BST](src/main/kotlin/g0601_0700/s0653_two_sum_iv_input_is_a_bst/Solution.kt)| Easy | Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Two_Pointers, Binary_Search_Tree, Data_Structure_I_Day_14_Tree | 231 | 96.08
1678+
| 0652 |[Find Duplicate Subtrees](src/main/kotlin/g0601_0700/s0652_find_duplicate_subtrees/Solution.kt)| Medium | Hash_Table, Depth_First_Search, Tree, Binary_Tree | 266 | 76.00
1679+
| 0650 |[2 Keys Keyboard](src/main/kotlin/g0601_0700/s0650_2_keys_keyboard/Solution.kt)| Medium | Dynamic_Programming, Math | 115 | 100.00
1680+
| 0649 |[Dota2 Senate](src/main/kotlin/g0601_0700/s0649_dota2_senate/Solution.kt)| Medium | String, Greedy, Queue | 217 | 100.00
16761681
| 0648 |[Replace Words](src/main/kotlin/g0601_0700/s0648_replace_words/Solution.kt)| Medium | Array, String, Hash_Table, Trie | 392 | 100.00
16771682
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16781683
| 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
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g0601_0700.s0649_dota2_senate
2+
3+
// #Medium #String #Greedy #Queue #2023_02_12_Time_217_ms_(100.00%)_Space_36.1_MB_(100.00%)
4+
5+
class Solution {
6+
fun predictPartyVictory(senate: String): String {
7+
val blocks = IntArray(2)
8+
val status = BooleanArray(senate.length)
9+
var changes = true
10+
while (changes) {
11+
changes = false
12+
for (i in senate.indices) {
13+
if (status[i]) {
14+
continue
15+
}
16+
val curr = senate[i]
17+
val block: Int = if (curr == 'R') {
18+
0
19+
} else {
20+
1
21+
}
22+
if (blocks[1 - block] > 0) {
23+
status[i] = true
24+
blocks[1 - block]--
25+
changes = true
26+
} else {
27+
blocks[block]++
28+
}
29+
}
30+
}
31+
for (i in senate.indices) {
32+
if (!status[i]) {
33+
return if (senate[i] == 'R') {
34+
"Radiant"
35+
} else {
36+
"Dire"
37+
}
38+
}
39+
}
40+
return ""
41+
}
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
649\. Dota2 Senate
2+
3+
Medium
4+
5+
In the world of Dota2, there are two parties: the Radiant and the Dire.
6+
7+
The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise **one** of the two rights:
8+
9+
* **Ban one senator's right:** A senator can make another senator lose all his rights in this and all the following rounds.
10+
* **Announce the victory:** If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game.
11+
12+
Given a string `senate` representing each senator's party belonging. The character `'R'` and `'D'` represent the Radiant party and the Dire party. Then if there are `n` senators, the size of the given string will be `n`.
13+
14+
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
15+
16+
Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be `"Radiant"` or `"Dire"`.
17+
18+
**Example 1:**
19+
20+
**Input:** senate = "RD"
21+
22+
**Output:** "Radiant"
23+
24+
**Explanation:** The first senator comes from Radiant and he can just ban the next senator's right in round 1. And the second senator can't exercise any rights anymore since his right has been banned. And in round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
25+
26+
**Example 2:**
27+
28+
**Input:** senate = "RDD"
29+
30+
**Output:** "Dire"
31+
32+
**Explanation:** The first senator comes from Radiant and he can just ban the next senator's right in round 1. And the second senator can't exercise any rights anymore since his right has been banned. And the third senator comes from Dire and he can ban the first senator's right in round 1. And in round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
33+
34+
**Constraints:**
35+
36+
* `n == senate.length`
37+
* <code>1 <= n <= 10<sup>4</sup></code>
38+
* `senate[i]` is either `'R'` or `'D'`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0601_0700.s0650_2_keys_keyboard
2+
3+
// #Medium #Dynamic_Programming #Math #2023_02_12_Time_115_ms_(100.00%)_Space_32.7_MB_(66.67%)
4+
5+
class Solution {
6+
fun minSteps(n: Int): Int {
7+
var count = 1
8+
var cost = 0
9+
var addValue = 1
10+
while (count < n) {
11+
cost++
12+
count += addValue
13+
if (n % count == 0) {
14+
cost++
15+
addValue = count
16+
}
17+
}
18+
return cost
19+
}
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
650\. 2 Keys Keyboard
2+
3+
Medium
4+
5+
There is only one character `'A'` on the screen of a notepad. You can perform one of two operations on this notepad for each step:
6+
7+
* Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
8+
* Paste: You can paste the characters which are copied last time.
9+
10+
Given an integer `n`, return _the minimum number of operations to get the character_ `'A'` _exactly_ `n` _times on the screen_.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 3
15+
16+
**Output:** 3
17+
18+
**Explanation:** Initially, we have one character 'A'.
19+
20+
In step 1, we use Copy All operation.
21+
22+
In step 2, we use Paste operation to get 'AA'.
23+
24+
In step 3, we use Paste operation to get 'AAA'.
25+
26+
**Example 2:**
27+
28+
**Input:** n = 1
29+
30+
**Output:** 0
31+
32+
**Constraints:**
33+
34+
* `1 <= n <= 1000`
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0601_0700.s0652_find_duplicate_subtrees
2+
3+
// #Medium #Hash_Table #Depth_First_Search #Tree #Binary_Tree
4+
// #2023_02_12_Time_266_ms_(76.00%)_Space_59.8_MB_(72.00%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
fun findDuplicateSubtrees(root: TreeNode?): List<TreeNode> {
20+
val map: MutableMap<String, Int> = HashMap()
21+
val list: MutableList<TreeNode> = ArrayList<TreeNode>()
22+
helper(root, map, list)
23+
return list
24+
}
25+
26+
private fun helper(root: TreeNode?, map: MutableMap<String, Int>, list: MutableList<TreeNode>): String {
27+
if (root == null) {
28+
return "#"
29+
}
30+
val key = helper(root.left, map, list) + "#" + helper(root.right, map, list) + "#" + root.`val`
31+
map[key] = map.getOrDefault(key, 0) + 1
32+
if (map[key] == 2) {
33+
list.add(root)
34+
}
35+
return key
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
652\. Find Duplicate Subtrees
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return all **duplicate subtrees**.
6+
7+
For each kind of duplicate subtrees, you only need to return the root node of any **one** of them.
8+
9+
Two trees are **duplicate** if they have the **same structure** with the **same node values**.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/08/16/e1.jpg)
14+
15+
**Input:** root = [1,2,3,4,null,2,4,null,null,4]
16+
17+
**Output:** [[2,4],[4]]
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2020/08/16/e2.jpg)
22+
23+
**Input:** root = [2,1,1]
24+
25+
**Output:** [[1]]
26+
27+
**Example 3:**
28+
29+
![](https://assets.leetcode.com/uploads/2020/08/16/e33.jpg)
30+
31+
**Input:** root = [2,2,2,3,null,3,null]
32+
33+
**Output:** [[2,3],[3]]
34+
35+
**Constraints:**
36+
37+
* The number of the nodes in the tree will be in the range `[1, 5000]`
38+
* `-200 <= Node.val <= 200`
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0601_0700.s0653_two_sum_iv_input_is_a_bst
2+
3+
// #Easy #Hash_Table #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Two_Pointers
4+
// #Binary_Search_Tree #Data_Structure_I_Day_14_Tree
5+
// #2023_02_12_Time_231_ms_(96.08%)_Space_37.5_MB_(100.00%)
6+
7+
import com_github_leetcode.TreeNode
8+
9+
/*
10+
* Example:
11+
* var ti = TreeNode(5)
12+
* var v = ti.`val`
13+
* Definition for a binary tree node.
14+
* class TreeNode(var `val`: Int) {
15+
* var left: TreeNode? = null
16+
* var right: TreeNode? = null
17+
* }
18+
*/
19+
class Solution {
20+
fun findTarget(root: TreeNode?, k: Int): Boolean {
21+
if (root == null) {
22+
return false
23+
}
24+
val res: MutableList<Int> = ArrayList()
25+
inOrder(res, root)
26+
var i = 0
27+
var j = res.size - 1
28+
while (i < j) {
29+
val val1 = res[i]
30+
val val2 = res[j]
31+
if (val1 + val2 == k) {
32+
return true
33+
} else if (val1 + val2 < k) {
34+
i++
35+
} else {
36+
j--
37+
}
38+
}
39+
return false
40+
}
41+
42+
private fun inOrder(res: MutableList<Int>, root: TreeNode?) {
43+
if (root == null) {
44+
return
45+
}
46+
inOrder(res, root.left)
47+
res.add(root.`val`)
48+
inOrder(res, root.right)
49+
}
50+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
653\. Two Sum IV - Input is a BST
2+
3+
Easy
4+
5+
Given the `root` of a binary search tree and an integer `k`, return `true` _if there exist two elements in the BST such that their sum is equal to_ `k`, _or_ `false` _otherwise_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg)
10+
11+
**Input:** root = [5,3,6,2,4,null,7], k = 9
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg)
18+
19+
**Input:** root = [5,3,6,2,4,null,7], k = 28
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
26+
* <code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code>
27+
* `root` is guaranteed to be a **valid** binary search tree.
28+
* <code>-10<sup>5</sup> <= k <= 10<sup>5</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0601_0700.s0649_dota2_senate
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 predictPartyVictory() {
10+
assertThat(Solution().predictPartyVictory("RD"), equalTo("Radiant"))
11+
}
12+
13+
@Test
14+
fun predictPartyVictory2() {
15+
assertThat(Solution().predictPartyVictory("RDD"), equalTo("Dire"))
16+
}
17+
}

0 commit comments

Comments
 (0)