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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 324 | 24.06

#### Day 3 Matrix Related Problems

Expand Down Expand Up @@ -387,6 +388,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0692 |[Top K Frequent Words](src/main/kotlin/g0601_0700/s0692_top_k_frequent_words)| Medium | String, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Trie, Bucket_Sort | 239 | 81.10

### Level 2

Expand Down Expand Up @@ -1025,6 +1027,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 324 | 24.06

#### Day 8 Breadth First Search Depth First Search

Expand Down Expand Up @@ -1644,6 +1647,18 @@
| 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
| 0696 |[Count Binary Substrings](src/main/kotlin/g0601_0700/s0696_count_binary_substrings)| Easy | String, Two_Pointers | 222 | 100.00
| 0695 |[Max Area of Island](src/main/kotlin/g0601_0700/s0695_max_area_of_island)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_I_Day_7_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_2_Matrix_Related_Problems | 324 | 24.06
| 0693 |[Binary Number with Alternating Bits](src/main/kotlin/g0601_0700/s0693_binary_number_with_alternating_bits)| Easy | Bit_Manipulation | 129 | 100.00
| 0692 |[Top K Frequent Words](src/main/kotlin/g0601_0700/s0692_top_k_frequent_words)| Medium | String, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Trie, Bucket_Sort, Level_1_Day_15_Heap | 239 | 81.10
| 0691 |[Stickers to Spell Word](src/main/kotlin/g0601_0700/s0691_stickers_to_spell_word)| Hard | Array, String, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 249 | 100.00
| 0690 |[Employee Importance](src/main/kotlin/g0601_0700/s0690_employee_importance)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search | 255 | 100.00
| 0689 |[Maximum Sum of 3 Non-Overlapping Subarrays](src/main/kotlin/g0601_0700/s0689_maximum_sum_of_3_non_overlapping_subarrays)| Hard | Array, Dynamic_Programming | 248 | 100.00
| 0688 |[Knight Probability in Chessboard](src/main/kotlin/g0601_0700/s0688_knight_probability_in_chessboard)| Medium | Dynamic_Programming | 144 | 100.00
| 0687 |[Longest Univalue Path](src/main/kotlin/g0601_0700/s0687_longest_univalue_path)| Medium | Depth_First_Search, Tree, Binary_Tree | 303 | 100.00
| 0686 |[Repeated String Match](src/main/kotlin/g0601_0700/s0686_repeated_string_match)| Medium | String, String_Matching | 164 | 100.00
| 0685 |[Redundant Connection II](src/main/kotlin/g0601_0700/s0685_redundant_connection_ii)| Hard | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 176 | 100.00
| 0684 |[Redundant Connection](src/main/kotlin/g0601_0700/s0684_redundant_connection)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 181 | 88.24
| 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
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0601_0700/s0679_24_game/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Solution {
return false
}

fun judgePoint24(nums: IntArray?): Boolean {
fun judgePoint24(nums: IntArray): Boolean {
val a = Arrays.stream(nums).asDoubleStream().toArray()
return backtrack(a, a.size)
}
Expand Down
71 changes: 71 additions & 0 deletions src/main/kotlin/g0601_0700/s0684_redundant_connection/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[![](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)

## 684\. Redundant Connection

Medium

In this problem, a tree is an **undirected graph** that is connected and has no cycles.

You are given a graph that started as a tree with `n` nodes labeled from `1` to `n`, with one additional edge added. The added edge has two **different** vertices chosen from `1` to `n`, and was not an edge that already existed. The graph is represented as an array `edges` of length `n` where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the graph.

Return _an edge that can be removed so that the resulting graph is a tree of_ `n` _nodes_. If there are multiple answers, return the answer that occurs last in the input.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/05/02/reduntant1-1-graph.jpg)

**Input:** edges = \[\[1,2],[1,3],[2,3]]

**Output:** [2,3]

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/05/02/reduntant1-2-graph.jpg)

**Input:** edges = \[\[1,2],[2,3],[3,4],[1,4],[1,5]]

**Output:** [1,4]

**Constraints:**

* `n == edges.length`
* `3 <= n <= 1000`
* `edges[i].length == 2`
* <code>1 <= a<sub>i</sub> < b<sub>i</sub> <= edges.length</code>
* <code>a<sub>i</sub> != b<sub>i</sub></code>
* There are no repeated edges.
* The given graph is connected.

## Solution

```kotlin
class Solution {
private lateinit var par: IntArray
fun findRedundantConnection(edges: Array<IntArray>): IntArray {
val ans = IntArray(2)
val n = edges.size
par = IntArray(n + 1)
for (i in 0 until n) {
par[i] = i
}
for (edge in edges) {
val lx = find(edge[0])
val ly = find(edge[1])
if (lx != ly) {
par[lx] = ly
} else {
ans[0] = edge[0]
ans[1] = edge[1]
}
}
return ans
}

private fun find(x: Int): Int {
return if (par[x] == x) {
x
} else find(par[x])
}
}
```
91 changes: 91 additions & 0 deletions src/main/kotlin/g0601_0700/s0685_redundant_connection_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
[![](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)

## 685\. Redundant Connection II

Hard

In this problem, a rooted tree is a **directed** graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.

The given input is a directed graph that started as a rooted tree with `n` nodes (with distinct values from `1` to `n`), with one additional directed edge added. The added edge has two different vertices chosen from `1` to `n`, and was not an edge that already existed.

The resulting graph is given as a 2D-array of `edges`. Each element of `edges` is a pair <code>[u<sub>i</sub>, v<sub>i</sub>]</code> that represents a **directed** edge connecting nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>, where <code>u<sub>i</sub></code> is a parent of child <code>v<sub>i</sub></code>.

Return _an edge that can be removed so that the resulting graph is a rooted tree of_ `n` _nodes_. If there are multiple answers, return the answer that occurs last in the given 2D-array.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/12/20/graph1.jpg)

**Input:** edges = \[\[1,2],[1,3],[2,3]]

**Output:** [2,3]

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/12/20/graph2.jpg)

**Input:** edges = \[\[1,2],[2,3],[3,4],[4,1],[1,5]]

**Output:** [4,1]

**Constraints:**

* `n == edges.length`
* `3 <= n <= 1000`
* `edges[i].length == 2`
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
* <code>u<sub>i</sub> != v<sub>i</sub></code>

## Solution

```kotlin
class Solution {
private lateinit var par: IntArray
fun findRedundantDirectedConnection(edges: Array<IntArray>): IntArray {
val n = edges.size
val hasPar = IntArray(n + 1)
for (edge in edges) {
val v = edge[1]
hasPar[v]++
}
par = IntArray(n + 1)
for (i in par.indices) {
par[i] = i
}
for (edge in edges) {
val u = edge[0]
val v = edge[1]
if (hasPar[v] == 1) {
val lu = find(u)
val lv = find(v)
if (lu != lv) {
par[lu] = lv
} else {
return edge
}
}
}
for (edge in edges) {
val u = edge[0]
val v = edge[1]
if (hasPar[v] > 1) {
val lu = find(u)
val lv = find(v)
if (lu != lv) {
par[lu] = lv
} else {
return edge
}
}
}
return IntArray(2)
}

private fun find(x: Int): Int {
return if (par[x] == x) {
x
} else find(par[x])
}
}
```
81 changes: 81 additions & 0 deletions src/main/kotlin/g0601_0700/s0686_repeated_string_match/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
[![](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)

## 686\. Repeated String Match

Medium

Given two strings `a` and `b`, return _the minimum number of times you should repeat string_ `a` _so that string_ `b` _is a substring of it_. If it is impossible for `b` to be a substring of `a` after repeating it, return `-1`.

**Notice:** string `"abc"` repeated 0 times is `""`, repeated 1 time is `"abc"` and repeated 2 times is `"abcabc"`.

**Example 1:**

**Input:** a = "abcd", b = "cdabcdab"

**Output:** 3

**Explanation:** We return 3 because by repeating a three times "ab**cdabcdab**cd", b is a substring of it.

**Example 2:**

**Input:** a = "a", b = "aa"

**Output:** 2

**Constraints:**

* <code>1 <= a.length, b.length <= 10<sup>4</sup></code>
* `a` and `b` consist of lowercase English letters.

## Solution

```kotlin
class Solution {
fun repeatedStringMatch(a: String, b: String): Int {
val existsChar = CharArray(127)
for (chA in a.toCharArray()) {
existsChar[chA.code] = 1.toChar()
}
for (chB in b.toCharArray()) {
if (existsChar[chB.code].code < 1) {
return -1
}
}
val lenB = b.length - 1
val sb = StringBuilder(a)
var lenSbA = sb.length - 1
var repeatCount = 1
while (lenSbA < lenB) {
sb.append(a)
repeatCount++
lenSbA = sb.length - 1
}
if (!isFound(sb, b)) {
sb.append(a)
repeatCount++
return if (!isFound(sb, b)) -1 else repeatCount
}
return repeatCount
}

private fun isFound(a: StringBuilder, b: String): Boolean {
for (i in a.indices) {
var k = i
var m = 0
while (k < a.length && m < b.length) {
if (a[k] != b[m]) {
break
} else {
k++
m++
}
}
if (m == b.length) {
return true
}
}
return false
}
}
```
84 changes: 84 additions & 0 deletions src/main/kotlin/g0601_0700/s0687_longest_univalue_path/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)

## 687\. Longest Univalue Path

Medium

Given the `root` of a binary tree, return _the length of the longest path, where each node in the path has the same value_. This path may or may not pass through the root.

**The length of the path** between two nodes is represented by the number of edges between them.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/10/13/ex1.jpg)

**Input:** root = [5,4,5,1,1,null,5]

**Output:** 2

**Explanation:** The shown image shows that the longest path of the same value (i.e. 5).

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/10/13/ex2.jpg)

**Input:** root = [1,4,5,4,4,null,5]

**Output:** 2

**Explanation:** The shown image shows that the longest path of the same value (i.e. 4).

**Constraints:**

* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
* `-1000 <= Node.val <= 1000`
* The depth of the tree will not exceed `1000`.

## 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 longestUnivaluePath(root: TreeNode?): Int {
if (root == null) {
return 0
}
val res = IntArray(1)
preorderLongestSinglePathLen(root, res)
return res[0]
}

private fun preorderLongestSinglePathLen(root: TreeNode?, res: IntArray): Int {
if (root == null) {
return -1
}
var left = preorderLongestSinglePathLen(root.left, res)
var right = preorderLongestSinglePathLen(root.right, res)
left = if (root.left == null || root.`val` == root.left!!.`val`) {
left + 1
} else {
0
}
right = if (root.right == null || root.`val` == root.right!!.`val`) {
right + 1
} else {
0
}
val longestPathLenPassingThroughRoot = left + right
res[0] = res[0].coerceAtLeast(longestPathLenPassingThroughRoot)
return left.coerceAtLeast(right)
}
}
```
Loading