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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0785 |[Is Graph Bipartite?](src/main/kotlin/g0701_0800/s0785_is_graph_bipartite)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 215 | 82.35

### SQL I

Expand Down Expand Up @@ -946,6 +947,7 @@
|-|-|-|-|-|-
| 0077 |[Combinations](src/main/kotlin/g0001_0100/s0077_combinations)| Medium | Backtracking | 244 | 100.00
| 0046 |[Permutations](src/main/kotlin/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking | 186 | 100.00
| 0784 |[Letter Case Permutation](src/main/kotlin/g0701_0800/s0784_letter_case_permutation)| Medium | String, Bit_Manipulation, Backtracking | 219 | 84.62

#### Day 12 Dynamic Programming

Expand Down Expand Up @@ -1674,6 +1676,34 @@
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence)| 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
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges)| 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
| 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
| 0796 |[Rotate String](src/main/kotlin/g0701_0800/s0796_rotate_string)| Easy | String, String_Matching | 134 | 100.00
| 0795 |[Number of Subarrays with Bounded Maximum](src/main/kotlin/g0701_0800/s0795_number_of_subarrays_with_bounded_maximum)| Medium | Array, Two_Pointers | 361 | 66.67
| 0794 |[Valid Tic-Tac-Toe State](src/main/kotlin/g0701_0800/s0794_valid_tic_tac_toe_state)| Medium | Array, String | 138 | 100.00
| 0793 |[Preimage Size of Factorial Zeroes Function](src/main/kotlin/g0701_0800/s0793_preimage_size_of_factorial_zeroes_function)| Hard | Math, Binary_Search | 114 | 100.00
| 0792 |[Number of Matching Subsequences](src/main/kotlin/g0701_0800/s0792_number_of_matching_subsequences)| Medium | String, Hash_Table, Sorting, Trie | 346 | 100.00
| 0791 |[Custom Sort String](src/main/kotlin/g0701_0800/s0791_custom_sort_string)| Medium | String, Hash_Table, Sorting | 133 | 90.00
| 0790 |[Domino and Tromino Tiling](src/main/kotlin/g0701_0800/s0790_domino_and_tromino_tiling)| Medium | Dynamic_Programming | 116 | 100.00
| 0789 |[Escape The Ghosts](src/main/kotlin/g0701_0800/s0789_escape_the_ghosts)| Medium | Array, Math | 187 | 100.00
| 0788 |[Rotated Digits](src/main/kotlin/g0701_0800/s0788_rotated_digits)| Medium | Dynamic_Programming, Math | 137 | 80.00
| 0787 |[Cheapest Flights Within K Stops](src/main/kotlin/g0701_0800/s0787_cheapest_flights_within_k_stops)| Medium | Dynamic_Programming, Depth_First_Search, Breadth_First_Search, Heap_Priority_Queue, Graph, Shortest_Path | 185 | 99.20
| 0786 |[K-th Smallest Prime Fraction](src/main/kotlin/g0701_0800/s0786_k_th_smallest_prime_fraction)| Medium | Array, Binary_Search, Heap_Priority_Queue | 165 | 100.00
| 0785 |[Is Graph Bipartite?](src/main/kotlin/g0701_0800/s0785_is_graph_bipartite)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Graph_Theory_I_Day_14_Graph_Theory | 215 | 82.35
| 0784 |[Letter Case Permutation](src/main/kotlin/g0701_0800/s0784_letter_case_permutation)| Medium | String, Bit_Manipulation, Backtracking, Algorithm_I_Day_11_Recursion_Backtracking | 219 | 84.62
| 0783 |[Minimum Distance Between BST Nodes](src/main/kotlin/g0701_0800/s0783_minimum_distance_between_bst_nodes)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 144 | 96.35
| 0782 |[Transform to Chessboard](src/main/kotlin/g0701_0800/s0782_transform_to_chessboard)| Hard | Array, Math, Matrix, Bit_Manipulation | 188 | 100.00
| 0781 |[Rabbits in Forest](src/main/kotlin/g0701_0800/s0781_rabbits_in_forest)| Medium | Array, Hash_Table, Math, Greedy | 132 | 100.00
| 0780 |[Reaching Points](src/main/kotlin/g0701_0800/s0780_reaching_points)| Hard | Math | 139 | 100.00
| 0779 |[K-th Symbol in Grammar](src/main/kotlin/g0701_0800/s0779_k_th_symbol_in_grammar)| Medium | Math, Bit_Manipulation, Recursion | 114 | 100.00
| 0778 |[Swim in Rising Water](src/main/kotlin/g0701_0800/s0778_swim_in_rising_water)| Hard | Array, Depth_First_Search, Breadth_First_Search, Binary_Search, Matrix, Heap_Priority_Queue, Union_Find | 190 | 100.00
| 0777 |[Swap Adjacent in LR String](src/main/kotlin/g0701_0800/s0777_swap_adjacent_in_lr_string)| Medium | String, Two_Pointers | 188 | 100.00
| 0775 |[Global and Local Inversions](src/main/kotlin/g0701_0800/s0775_global_and_local_inversions)| Medium | Array, Math | 495 | 100.00
| 0773 |[Sliding Puzzle](src/main/kotlin/g0701_0800/s0773_sliding_puzzle)| Hard | Array, Breadth_First_Search, Matrix | 166 | 100.00
| 0771 |[Jewels and Stones](src/main/kotlin/g0701_0800/s0771_jewels_and_stones)| Easy | String, Hash_Table | 143 | 99.08
| 0770 |[Basic Calculator IV](src/main/kotlin/g0701_0800/s0770_basic_calculator_iv)| Hard | String, Hash_Table, Math, Stack, Recursion | 222 | 100.00
| 0769 |[Max Chunks To Make Sorted](src/main/kotlin/g0701_0800/s0769_max_chunks_to_make_sorted)| Medium | Array, Sorting, Greedy, Stack, Monotonic_Stack | 136 | 90.00
| 0768 |[Max Chunks To Make Sorted II](src/main/kotlin/g0701_0800/s0768_max_chunks_to_make_sorted_ii)| Hard | Array, Sorting, Greedy, Stack, Monotonic_Stack | 202 | 100.00
| 0767 |[Reorganize String](src/main/kotlin/g0701_0800/s0767_reorganize_string)| Medium | String, Hash_Table, Sorting, Greedy, Heap_Priority_Queue, Counting | 148 | 100.00
| 0766 |[Toeplitz Matrix](src/main/kotlin/g0701_0800/s0766_toeplitz_matrix)| Easy | Array, Matrix | 174 | 100.00
| 0765 |[Couples Holding Hands](src/main/kotlin/g0701_0800/s0765_couples_holding_hands)| Hard | Depth_First_Search, Greedy, Breadth_First_Search, Graph, Union_Find | 131 | 100.00
| 0764 |[Largest Plus Sign](src/main/kotlin/g0701_0800/s0764_largest_plus_sign)| Medium | Array, Dynamic_Programming | 415 | 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Given two integer arrays `inorder` and `postorder` where `inorder` is the inorde
```kotlin
import com_github_leetcode.TreeNode

/**
/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import com_github_leetcode.TreeNode
import java.util.Collections
import kotlin.collections.ArrayList

/**
/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ A **height-balanced** binary tree is a binary tree in which the depth of the two
```kotlin
import com_github_leetcode.TreeNode

/**
/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class SummaryRanges {
}
}

/**
/*
* Your SummaryRanges object will be instantiated and called as such:
* var obj = SummaryRanges()
* obj.addNum(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class Solution {
}
}
processInOrder()

return ans.toIntArray()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Given `n`, calculate `F(n)`.
```kotlin
class Solution {
private val memo = IntArray(31)

fun fib(n: Int): Int {
if (n == 0) {
return 0
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g0501_0600/s0520_detect_capital/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Given a string `word`, return `true` if the usage of capitals in it is right.

```kotlin
class Solution {
fun detectCapitalUse(word: String?): Boolean {
if (word == null || word.length == 0) {
fun detectCapitalUse(word: String): Boolean {
if (word.isEmpty()) {
return false
}
var upper = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com_github_leetcode.TreeNode
class Solution {
private var ans = Int.MAX_VALUE
private var prev = Int.MAX_VALUE

fun getMinimumDifference(root: TreeNode?): Int {
if (root == null) {
return ans
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/g0501_0600/s0554_brick_wall/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class Solution {
}
}
var value = 0

if (gapMap.size > 0) {
value = gapMap.values.max()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ If the value of `isLeaf` or `val` is True we represent it as **1** in the list `
* var bottomRight: Node? = null
* }
*/

class Solution {
fun intersect(quadTree1: Node?, quadTree2: Node?): Node? {
if (quadTree1!!.isLeaf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import com_github_leetcode.Node
* var neighbors: List<Node?> = listOf()
* }
*/

class Solution {
private var max = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import com_github_leetcode.TreeNode
*/
class Solution {
private var sum = 0

private fun sumTilt(root: TreeNode?): Int {
if (root == null) {
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Given the integer array `candyType` of length `n`, return _the **maximum** numbe
## Solution

```kotlin
internal class Solution {
class Solution {
fun distributeCandies(candyType: IntArray): Int {
val s: MutableSet<Int> = HashSet()
for (i in candyType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Return _an edge that can be removed so that the resulting graph is a tree of_ `n
```kotlin
class Solution {
private lateinit var par: IntArray

fun findRedundantConnection(edges: Array<IntArray>): IntArray {
val ans = IntArray(2)
val n = edges.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import com_github_leetcode.Employee
* var subordinates:List<Int> = listOf()
* }
*/

class Solution {
fun getImportance(employees: List<Employee?>, id: Int): Int {
val map: MutableMap<Int, Employee> = HashMap()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g0701_0800/s0724_find_pivot_index/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ Return _the **leftmost pivot index**_. If no such index exists, return `-1`.

```kotlin
class Solution {
fun pivotIndex(nums: IntArray?): Int {
if (nums == null || nums.isEmpty()) {
fun pivotIndex(nums: IntArray): Int {
if (nums.isEmpty()) {
return -1
}
val sums = IntArray(nums.size)
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/g0701_0800/s0749_contain_virus/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Solution {
private var m = 0
private var n = 0
private val dirs = arrayOf(intArrayOf(-1, 0), intArrayOf(1, 0), intArrayOf(0, 1), intArrayOf(0, -1))

fun containVirus(grid: Array<IntArray>): Int {
m = grid.size
n = grid[0].size
Expand Down
88 changes: 88 additions & 0 deletions src/main/kotlin/g0701_0800/s0766_toeplitz_matrix/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[![](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)

## 766\. Toeplitz Matrix

Easy

Given an `m x n` `matrix`, return _`true` if the matrix is Toeplitz. Otherwise, return `false`._

A matrix is **Toeplitz** if every diagonal from top-left to bottom-right has the same elements.

**Example 1:**

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

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

**Output:** true

**Explanation:** In the above grid, the diagonals are: "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". In each diagonal all elements are the same, so the answer is True.

**Example 2:**

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

**Input:** matrix = \[\[1,2],[2,2]]

**Output:** false

**Explanation:** The diagonal "[1, 2]" has different elements.

**Constraints:**

* `m == matrix.length`
* `n == matrix[i].length`
* `1 <= m, n <= 20`
* `0 <= matrix[i][j] <= 99`

**Follow up:**

* What if the `matrix` is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
* What if the `matrix` is so large that you can only load up a partial row into the memory at once?

## Solution

```kotlin
class Solution {
fun isToeplitzMatrix(matrix: Array<IntArray>): Boolean {
val m = matrix.size
val n = matrix[0].size
var i = 0
var j = 0
var sameVal = matrix[i][j]
while (++i < m && ++j < n) {
if (matrix[i][j] != sameVal) {
return false
}
}
i = 1
j = 0
while (i < m) {
var tmpI = i
var tmpJ = j
sameVal = matrix[i][j]
while (++tmpI < m && ++tmpJ < n) {
if (matrix[tmpI][tmpJ] != sameVal) {
return false
}
}
i++
}
i = 0
j = 1
while (j < n) {
var tmpJ = j
var tmpI = i
sameVal = matrix[tmpI][tmpJ]
while (++tmpI < m && ++tmpJ < n) {
if (matrix[tmpI][tmpJ] != sameVal) {
return false
}
}
j++
}
return true
}
}
```
69 changes: 69 additions & 0 deletions src/main/kotlin/g0701_0800/s0767_reorganize_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[![](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)

## 767\. Reorganize String

Medium

Given a string `s`, rearrange the characters of `s` so that any two adjacent characters are not the same.

Return _any possible rearrangement of_ `s` _or return_ `""` _if not possible_.

**Example 1:**

**Input:** s = "aab"

**Output:** "aba"

**Example 2:**

**Input:** s = "aaab"

**Output:** ""

**Constraints:**

* `1 <= s.length <= 500`
* `s` consists of lowercase English letters.

## Solution

```kotlin
class Solution {
fun reorganizeString(s: String): String {
val hash = IntArray(26)
for (element in s) {
hash[element.code - 'a'.code]++
}
var max = 0
var letter = 0
for (i in hash.indices) {
if (hash[i] > max) {
max = hash[i]
letter = i
}
}
if (max > (s.length + 1) / 2) {
return ""
}
val res = CharArray(s.length)
var idx = 0
while (hash[letter] > 0) {
res[idx] = (letter + 'a'.code).toChar()
idx += 2
hash[letter]--
}
for (i in hash.indices) {
while (hash[i] > 0) {
if (idx >= res.size) {
idx = 1
}
res[idx] = (i + 'a'.code).toChar()
idx += 2
hash[i]--
}
}
return String(res)
}
}
```
Loading