Skip to content

Commit dcf22d9

Browse files
authored
Added tasks 649-682
1 parent e4f91c7 commit dcf22d9

File tree

29 files changed

+2223
-0
lines changed

29 files changed

+2223
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@
827827
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
828828
|-|-|-|-|-|-
829829
| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38
830+
| 0653 |[Two Sum IV - Input is a BST](src/main/kotlin/g0601_0700/s0653_two_sum_iv_input_is_a_bst)| Easy | Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Two_Pointers, Binary_Search_Tree | 231 | 96.08
830831
| 0235 |[Lowest Common Ancestor of a Binary Search Tree](src/main/kotlin/g0201_0300/s0235_lowest_common_ancestor_of_a_binary_search_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 404 | 75.59
831832

832833
### Data Structure II
@@ -1188,6 +1189,7 @@
11881189
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11891190
|-|-|-|-|-|-
11901191
| 0300 |[Longest Increasing Subsequence](src/main/kotlin/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 318 | 82.28
1192+
| 0673 |[Number of Longest Increasing Subsequence](src/main/kotlin/g0601_0700/s0673_number_of_longest_increasing_subsequence)| Medium | Array, Dynamic_Programming, Segment_Tree, Binary_Indexed_Tree | 226 | 91.67
11911193

11921194
#### Day 17 Dynamic Programming
11931195

@@ -1311,6 +1313,7 @@
13111313

13121314
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13131315
|-|-|-|-|-|-
1316+
| 0658 |[Find K Closest Elements](src/main/kotlin/g0601_0700/s0658_find_k_closest_elements)| Medium | Array, Sorting, Binary_Search, Two_Pointers, Heap_Priority_Queue | 375 | 95.16
13141317

13151318
#### Day 3
13161319

@@ -1641,6 +1644,34 @@
16411644
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
16421645
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16431646
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
1647+
| 0682 |[Baseball Game](src/main/kotlin/g0601_0700/s0682_baseball_game)| Easy | Array, Stack, Simulation | 159 | 100.00
1648+
| 0680 |[Valid Palindrome II](src/main/kotlin/g0601_0700/s0680_valid_palindrome_ii)| Easy | String, Greedy, Two_Pointers | 296 | 79.17
1649+
| 0679 |[24 Game](src/main/kotlin/g0601_0700/s0679_24_game)| Hard | Array, Math, Backtracking | 175 | 100.00
1650+
| 0678 |[Valid Parenthesis String](src/main/kotlin/g0601_0700/s0678_valid_parenthesis_string)| Medium | String, Dynamic_Programming, Greedy, Stack | 133 | 100.00
1651+
| 0677 |[Map Sum Pairs](src/main/kotlin/g0601_0700/s0677_map_sum_pairs)| Medium | String, Hash_Table, Design, Trie | 197 | 80.00
1652+
| 0676 |[Implement Magic Dictionary](src/main/kotlin/g0601_0700/s0676_implement_magic_dictionary)| Medium | String, Hash_Table, Design, Trie | 256 | 100.00
1653+
| 0675 |[Cut Off Trees for Golf Event](src/main/kotlin/g0601_0700/s0675_cut_off_trees_for_golf_event)| Hard | Array, Breadth_First_Search, Matrix, Heap_Priority_Queue | 777 | 100.00
1654+
| 0674 |[Longest Continuous Increasing Subsequence](src/main/kotlin/g0601_0700/s0674_longest_continuous_increasing_subsequence)| Easy | Array | 201 | 84.21
1655+
| 0673 |[Number of Longest Increasing Subsequence](src/main/kotlin/g0601_0700/s0673_number_of_longest_increasing_subsequence)| Medium | Array, Dynamic_Programming, Segment_Tree, Binary_Indexed_Tree, Algorithm_II_Day_16_Dynamic_Programming | 226 | 91.67
1656+
| 0672 |[Bulb Switcher II](src/main/kotlin/g0601_0700/s0672_bulb_switcher_ii)| Medium | Math, Depth_First_Search, Breadth_First_Search, Bit_Manipulation | 131 | 100.00
1657+
| 0671 |[Second Minimum Node In a Binary Tree](src/main/kotlin/g0601_0700/s0671_second_minimum_node_in_a_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree | 128 | 100.00
1658+
| 0670 |[Maximum Swap](src/main/kotlin/g0601_0700/s0670_maximum_swap)| Medium | Math, Greedy | 143 | 100.00
1659+
| 0669 |[Trim a Binary Search Tree](src/main/kotlin/g0601_0700/s0669_trim_a_binary_search_tree)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 195 | 100.00
1660+
| 0668 |[Kth Smallest Number in Multiplication Table](src/main/kotlin/g0601_0700/s0668_kth_smallest_number_in_multiplication_table)| Hard | Math, Binary_Search | 151 | 100.00
1661+
| 0667 |[Beautiful Arrangement II](src/main/kotlin/g0601_0700/s0667_beautiful_arrangement_ii)| Medium | Array, Math | 175 | 100.00
1662+
| 0665 |[Non-decreasing Array](src/main/kotlin/g0601_0700/s0665_non_decreasing_array)| Medium | Array | 256 | 85.71
1663+
| 0664 |[Strange Printer](src/main/kotlin/g0601_0700/s0664_strange_printer)| Hard | String, Dynamic_Programming | 196 | 100.00
1664+
| 0662 |[Maximum Width of Binary Tree](src/main/kotlin/g0601_0700/s0662_maximum_width_of_binary_tree)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 189 | 75.00
1665+
| 0661 |[Image Smoother](src/main/kotlin/g0601_0700/s0661_image_smoother)| Easy | Array, Matrix | 352 | 100.00
1666+
| 0659 |[Split Array into Consecutive Subsequences](src/main/kotlin/g0601_0700/s0659_split_array_into_consecutive_subsequences)| Medium | Array, Hash_Table, Greedy, Heap_Priority_Queue | 352 | 100.00
1667+
| 0658 |[Find K Closest Elements](src/main/kotlin/g0601_0700/s0658_find_k_closest_elements)| Medium | Array, Sorting, Binary_Search, Two_Pointers, Heap_Priority_Queue, Binary_Search_II_Day_2 | 375 | 95.16
1668+
| 0657 |[Robot Return to Origin](src/main/kotlin/g0601_0700/s0657_robot_return_to_origin)| Easy | String, Simulation | 186 | 100.00
1669+
| 0655 |[Print Binary Tree](src/main/kotlin/g0601_0700/s0655_print_binary_tree)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 176 | 100.00
1670+
| 0654 |[Maximum Binary Tree](src/main/kotlin/g0601_0700/s0654_maximum_binary_tree)| Medium | Array, Tree, Binary_Tree, Stack, Monotonic_Stack, Divide_and_Conquer | 271 | 90.00
1671+
| 0653 |[Two Sum IV - Input is a BST](src/main/kotlin/g0601_0700/s0653_two_sum_iv_input_is_a_bst)| 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
1672+
| 0652 |[Find Duplicate Subtrees](src/main/kotlin/g0601_0700/s0652_find_duplicate_subtrees)| Medium | Hash_Table, Depth_First_Search, Tree, Binary_Tree | 266 | 76.00
1673+
| 0650 |[2 Keys Keyboard](src/main/kotlin/g0601_0700/s0650_2_keys_keyboard)| Medium | Dynamic_Programming, Math | 115 | 100.00
1674+
| 0649 |[Dota2 Senate](src/main/kotlin/g0601_0700/s0649_dota2_senate)| Medium | String, Greedy, Queue | 217 | 100.00
16441675
| 0648 |[Replace Words](src/main/kotlin/g0601_0700/s0648_replace_words)| Medium | Array, String, Hash_Table, Trie | 392 | 100.00
16451676
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16461677
| 0646 |[Maximum Length of Pair Chain](src/main/kotlin/g0601_0700/s0646_maximum_length_of_pair_chain)| Medium | Array, Dynamic_Programming, Sorting, Greedy | 249 | 100.00
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 649\. Dota2 Senate
5+
6+
Medium
7+
8+
In the world of Dota2, there are two parties: the Radiant and the Dire.
9+
10+
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:
11+
12+
* **Ban one senator's right:** A senator can make another senator lose all his rights in this and all the following rounds.
13+
* **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.
14+
15+
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`.
16+
17+
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.
18+
19+
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"`.
20+
21+
**Example 1:**
22+
23+
**Input:** senate = "RD"
24+
25+
**Output:** "Radiant"
26+
27+
**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.
28+
29+
**Example 2:**
30+
31+
**Input:** senate = "RDD"
32+
33+
**Output:** "Dire"
34+
35+
**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.
36+
37+
**Constraints:**
38+
39+
* `n == senate.length`
40+
* <code>1 <= n <= 10<sup>4</sup></code>
41+
* `senate[i]` is either `'R'` or `'D'`.
42+
43+
## Solution
44+
45+
```kotlin
46+
class Solution {
47+
fun predictPartyVictory(senate: String): String {
48+
val blocks = IntArray(2)
49+
val status = BooleanArray(senate.length)
50+
var changes = true
51+
while (changes) {
52+
changes = false
53+
for (i in senate.indices) {
54+
if (status[i]) {
55+
continue
56+
}
57+
val curr = senate[i]
58+
val block: Int = if (curr == 'R') {
59+
0
60+
} else {
61+
1
62+
}
63+
if (blocks[1 - block] > 0) {
64+
status[i] = true
65+
blocks[1 - block]--
66+
changes = true
67+
} else {
68+
blocks[block]++
69+
}
70+
}
71+
}
72+
for (i in senate.indices) {
73+
if (!status[i]) {
74+
return if (senate[i] == 'R') {
75+
"Radiant"
76+
} else {
77+
"Dire"
78+
}
79+
}
80+
}
81+
return ""
82+
}
83+
}
84+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 650\. 2 Keys Keyboard
5+
6+
Medium
7+
8+
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:
9+
10+
* Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
11+
* Paste: You can paste the characters which are copied last time.
12+
13+
Given an integer `n`, return _the minimum number of operations to get the character_ `'A'` _exactly_ `n` _times on the screen_.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 3
18+
19+
**Output:** 3
20+
21+
**Explanation:** Initially, we have one character 'A'.
22+
23+
In step 1, we use Copy All operation.
24+
25+
In step 2, we use Paste operation to get 'AA'.
26+
27+
In step 3, we use Paste operation to get 'AAA'.
28+
29+
**Example 2:**
30+
31+
**Input:** n = 1
32+
33+
**Output:** 0
34+
35+
**Constraints:**
36+
37+
* `1 <= n <= 1000`
38+
39+
## Solution
40+
41+
```kotlin
42+
class Solution {
43+
fun minSteps(n: Int): Int {
44+
var count = 1
45+
var cost = 0
46+
var addValue = 1
47+
while (count < n) {
48+
cost++
49+
count += addValue
50+
if (n % count == 0) {
51+
cost++
52+
addValue = count
53+
}
54+
}
55+
return cost
56+
}
57+
}
58+
```
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 652\. Find Duplicate Subtrees
5+
6+
Medium
7+
8+
Given the `root` of a binary tree, return all **duplicate subtrees**.
9+
10+
For each kind of duplicate subtrees, you only need to return the root node of any **one** of them.
11+
12+
Two trees are **duplicate** if they have the **same structure** with the **same node values**.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2020/08/16/e1.jpg)
17+
18+
**Input:** root = [1,2,3,4,null,2,4,null,null,4]
19+
20+
**Output:** [[2,4],[4]]
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2020/08/16/e2.jpg)
25+
26+
**Input:** root = [2,1,1]
27+
28+
**Output:** [[1]]
29+
30+
**Example 3:**
31+
32+
![](https://assets.leetcode.com/uploads/2020/08/16/e33.jpg)
33+
34+
**Input:** root = [2,2,2,3,null,3,null]
35+
36+
**Output:** [[2,3],[3]]
37+
38+
**Constraints:**
39+
40+
* The number of the nodes in the tree will be in the range `[1, 5000]`
41+
* `-200 <= Node.val <= 200`
42+
43+
## Solution
44+
45+
```kotlin
46+
import com_github_leetcode.TreeNode
47+
48+
/*
49+
* Example:
50+
* var ti = TreeNode(5)
51+
* var v = ti.`val`
52+
* Definition for a binary tree node.
53+
* class TreeNode(var `val`: Int) {
54+
* var left: TreeNode? = null
55+
* var right: TreeNode? = null
56+
* }
57+
*/
58+
class Solution {
59+
fun findDuplicateSubtrees(root: TreeNode?): List<TreeNode> {
60+
val map: MutableMap<String, Int> = HashMap()
61+
val list: MutableList<TreeNode> = ArrayList<TreeNode>()
62+
helper(root, map, list)
63+
return list
64+
}
65+
66+
private fun helper(root: TreeNode?, map: MutableMap<String, Int>, list: MutableList<TreeNode>): String {
67+
if (root == null) {
68+
return "#"
69+
}
70+
val key = helper(root.left, map, list) + "#" + helper(root.right, map, list) + "#" + root.`val`
71+
map[key] = map.getOrDefault(key, 0) + 1
72+
if (map[key] == 2) {
73+
list.add(root)
74+
}
75+
return key
76+
}
77+
}
78+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 653\. Two Sum IV - Input is a BST
5+
6+
Easy
7+
8+
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_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg)
13+
14+
**Input:** root = [5,3,6,2,4,null,7], k = 9
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg)
21+
22+
**Input:** root = [5,3,6,2,4,null,7], k = 28
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
29+
* <code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code>
30+
* `root` is guaranteed to be a **valid** binary search tree.
31+
* <code>-10<sup>5</sup> <= k <= 10<sup>5</sup></code>
32+
33+
## Solution
34+
35+
```kotlin
36+
import com_github_leetcode.TreeNode
37+
38+
/*
39+
* Example:
40+
* var ti = TreeNode(5)
41+
* var v = ti.`val`
42+
* Definition for a binary tree node.
43+
* class TreeNode(var `val`: Int) {
44+
* var left: TreeNode? = null
45+
* var right: TreeNode? = null
46+
* }
47+
*/
48+
class Solution {
49+
fun findTarget(root: TreeNode?, k: Int): Boolean {
50+
if (root == null) {
51+
return false
52+
}
53+
val res: MutableList<Int> = ArrayList()
54+
inOrder(res, root)
55+
var i = 0
56+
var j = res.size - 1
57+
while (i < j) {
58+
val val1 = res[i]
59+
val val2 = res[j]
60+
if (val1 + val2 == k) {
61+
return true
62+
} else if (val1 + val2 < k) {
63+
i++
64+
} else {
65+
j--
66+
}
67+
}
68+
return false
69+
}
70+
71+
private fun inOrder(res: MutableList<Int>, root: TreeNode?) {
72+
if (root == null) {
73+
return
74+
}
75+
inOrder(res, root.left)
76+
res.add(root.`val`)
77+
inOrder(res, root.right)
78+
}
79+
}
80+
```

0 commit comments

Comments
 (0)