Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added tasks 649-682
  • Loading branch information
javadev committed Feb 17, 2023
commit 62586d583f21e49150bb14a9ad3eecc690e2ee33
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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
| 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

### Data Structure II
Expand Down Expand Up @@ -1188,6 +1189,7 @@
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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

#### Day 17 Dynamic Programming

Expand Down Expand Up @@ -1311,6 +1313,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 3

Expand Down Expand Up @@ -1641,6 +1644,34 @@
| 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
| 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
| 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
| 0682 |[Baseball Game](src/main/kotlin/g0601_0700/s0682_baseball_game)| Easy | Array, Stack, Simulation | 159 | 100.00
| 0680 |[Valid Palindrome II](src/main/kotlin/g0601_0700/s0680_valid_palindrome_ii)| Easy | String, Greedy, Two_Pointers | 296 | 79.17
| 0679 |[24 Game](src/main/kotlin/g0601_0700/s0679_24_game)| Hard | Array, Math, Backtracking | 175 | 100.00
| 0678 |[Valid Parenthesis String](src/main/kotlin/g0601_0700/s0678_valid_parenthesis_string)| Medium | String, Dynamic_Programming, Greedy, Stack | 133 | 100.00
| 0677 |[Map Sum Pairs](src/main/kotlin/g0601_0700/s0677_map_sum_pairs)| Medium | String, Hash_Table, Design, Trie | 197 | 80.00
| 0676 |[Implement Magic Dictionary](src/main/kotlin/g0601_0700/s0676_implement_magic_dictionary)| Medium | String, Hash_Table, Design, Trie | 256 | 100.00
| 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
| 0674 |[Longest Continuous Increasing Subsequence](src/main/kotlin/g0601_0700/s0674_longest_continuous_increasing_subsequence)| Easy | Array | 201 | 84.21
| 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
| 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
| 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
| 0670 |[Maximum Swap](src/main/kotlin/g0601_0700/s0670_maximum_swap)| Medium | Math, Greedy | 143 | 100.00
| 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
| 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
| 0667 |[Beautiful Arrangement II](src/main/kotlin/g0601_0700/s0667_beautiful_arrangement_ii)| Medium | Array, Math | 175 | 100.00
| 0665 |[Non-decreasing Array](src/main/kotlin/g0601_0700/s0665_non_decreasing_array)| Medium | Array | 256 | 85.71
| 0664 |[Strange Printer](src/main/kotlin/g0601_0700/s0664_strange_printer)| Hard | String, Dynamic_Programming | 196 | 100.00
| 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
| 0661 |[Image Smoother](src/main/kotlin/g0601_0700/s0661_image_smoother)| Easy | Array, Matrix | 352 | 100.00
| 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
| 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
| 0657 |[Robot Return to Origin](src/main/kotlin/g0601_0700/s0657_robot_return_to_origin)| Easy | String, Simulation | 186 | 100.00
| 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
| 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
| 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
| 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
| 0650 |[2 Keys Keyboard](src/main/kotlin/g0601_0700/s0650_2_keys_keyboard)| Medium | Dynamic_Programming, Math | 115 | 100.00
| 0649 |[Dota2 Senate](src/main/kotlin/g0601_0700/s0649_dota2_senate)| Medium | String, Greedy, Queue | 217 | 100.00
| 0648 |[Replace Words](src/main/kotlin/g0601_0700/s0648_replace_words)| Medium | Array, String, Hash_Table, Trie | 392 | 100.00
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
| 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
Expand Down
84 changes: 84 additions & 0 deletions src/main/kotlin/g0601_0700/s0649_dota2_senate/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 649\. Dota2 Senate

Medium

In the world of Dota2, there are two parties: the Radiant and the Dire.

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:

* **Ban one senator's right:** A senator can make another senator lose all his rights in this and all the following rounds.
* **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.

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`.

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.

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"`.

**Example 1:**

**Input:** senate = "RD"

**Output:** "Radiant"

**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.

**Example 2:**

**Input:** senate = "RDD"

**Output:** "Dire"

**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.

**Constraints:**

* `n == senate.length`
* <code>1 <= n <= 10<sup>4</sup></code>
* `senate[i]` is either `'R'` or `'D'`.

## Solution

```kotlin
class Solution {
fun predictPartyVictory(senate: String): String {
val blocks = IntArray(2)
val status = BooleanArray(senate.length)
var changes = true
while (changes) {
changes = false
for (i in senate.indices) {
if (status[i]) {
continue
}
val curr = senate[i]
val block: Int = if (curr == 'R') {
0
} else {
1
}
if (blocks[1 - block] > 0) {
status[i] = true
blocks[1 - block]--
changes = true
} else {
blocks[block]++
}
}
}
for (i in senate.indices) {
if (!status[i]) {
return if (senate[i] == 'R') {
"Radiant"
} else {
"Dire"
}
}
}
return ""
}
}
```
58 changes: 58 additions & 0 deletions src/main/kotlin/g0601_0700/s0650_2_keys_keyboard/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 650\. 2 Keys Keyboard

Medium

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:

* Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
* Paste: You can paste the characters which are copied last time.

Given an integer `n`, return _the minimum number of operations to get the character_ `'A'` _exactly_ `n` _times on the screen_.

**Example 1:**

**Input:** n = 3

**Output:** 3

**Explanation:** Initially, we have one character 'A'.

In step 1, we use Copy All operation.

In step 2, we use Paste operation to get 'AA'.

In step 3, we use Paste operation to get 'AAA'.

**Example 2:**

**Input:** n = 1

**Output:** 0

**Constraints:**

* `1 <= n <= 1000`

## Solution

```kotlin
class Solution {
fun minSteps(n: Int): Int {
var count = 1
var cost = 0
var addValue = 1
while (count < n) {
cost++
count += addValue
if (n % count == 0) {
cost++
addValue = count
}
}
return cost
}
}
```
78 changes: 78 additions & 0 deletions src/main/kotlin/g0601_0700/s0652_find_duplicate_subtrees/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 652\. Find Duplicate Subtrees

Medium

Given the `root` of a binary tree, return all **duplicate subtrees**.

For each kind of duplicate subtrees, you only need to return the root node of any **one** of them.

Two trees are **duplicate** if they have the **same structure** with the **same node values**.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/08/16/e1.jpg)

**Input:** root = [1,2,3,4,null,2,4,null,null,4]

**Output:** [[2,4],[4]]

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/08/16/e2.jpg)

**Input:** root = [2,1,1]

**Output:** [[1]]

**Example 3:**

![](https://assets.leetcode.com/uploads/2020/08/16/e33.jpg)

**Input:** root = [2,2,2,3,null,3,null]

**Output:** [[2,3],[3]]

**Constraints:**

* The number of the nodes in the tree will be in the range `[1, 5000]`
* `-200 <= Node.val <= 200`

## Solution

```kotlin
import com_github_leetcode.TreeNode

/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun findDuplicateSubtrees(root: TreeNode?): List<TreeNode> {
val map: MutableMap<String, Int> = HashMap()
val list: MutableList<TreeNode> = ArrayList<TreeNode>()
helper(root, map, list)
return list
}

private fun helper(root: TreeNode?, map: MutableMap<String, Int>, list: MutableList<TreeNode>): String {
if (root == null) {
return "#"
}
val key = helper(root.left, map, list) + "#" + helper(root.right, map, list) + "#" + root.`val`
map[key] = map.getOrDefault(key, 0) + 1
if (map[key] == 2) {
list.add(root)
}
return key
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 653\. Two Sum IV - Input is a BST

Easy

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_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg)

**Input:** root = [5,3,6,2,4,null,7], k = 9

**Output:** true

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg)

**Input:** root = [5,3,6,2,4,null,7], k = 28

**Output:** false

**Constraints:**

* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
* <code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code>
* `root` is guaranteed to be a **valid** binary search tree.
* <code>-10<sup>5</sup> <= k <= 10<sup>5</sup></code>

## Solution

```kotlin
import com_github_leetcode.TreeNode

/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun findTarget(root: TreeNode?, k: Int): Boolean {
if (root == null) {
return false
}
val res: MutableList<Int> = ArrayList()
inOrder(res, root)
var i = 0
var j = res.size - 1
while (i < j) {
val val1 = res[i]
val val2 = res[j]
if (val1 + val2 == k) {
return true
} else if (val1 + val2 < k) {
i++
} else {
j--
}
}
return false
}

private fun inOrder(res: MutableList<Int>, root: TreeNode?) {
if (root == null) {
return
}
inOrder(res, root.left)
res.add(root.`val`)
inOrder(res, root.right)
}
}
```
Loading