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
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0797 |[All Paths From Source to Target](src/main/kotlin/g0701_0800/s0797_all_paths_from_source_to_target)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Backtracking | 232 | 100.00
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 189 | 69.23

#### Day 8 Standard Traversal

Expand All @@ -76,6 +77,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0847 |[Shortest Path Visiting All Nodes](src/main/kotlin/g0801_0900/s0847_shortest_path_visiting_all_nodes)| Hard | Dynamic_Programming, Breadth_First_Search, Bit_Manipulation, Graph, Bitmask | 164 | 100.00

#### Day 11 Breadth First Search

Expand Down Expand Up @@ -262,6 +264,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0844 |[Backspace String Compare](src/main/kotlin/g0801_0900/s0844_backspace_string_compare)| Easy | String, Two_Pointers, Stack, Simulation | 126 | 98.31
| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion | 224 | 64.86

#### Day 15 Heap
Expand Down Expand Up @@ -859,6 +862,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 189 | 69.23

#### Day 20 Heap Priority Queue

Expand Down Expand Up @@ -1002,6 +1006,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0844 |[Backspace String Compare](src/main/kotlin/g0801_0900/s0844_backspace_string_compare)| Easy | String, Two_Pointers, Stack, Simulation | 126 | 98.31
| 0011 |[Container With Most Water](src/main/kotlin/g0001_0100/s0011_container_with_most_water)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers | 474 | 89.18

#### Day 5 Sliding Window
Expand Down Expand Up @@ -1139,6 +1144,7 @@
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0035 |[Search Insert Position](src/main/kotlin/g0001_0100/s0035_search_insert_position)| Easy | Top_100_Liked_Questions, Array, Binary_Search | 267 | 50.32
| 0852 |[Peak Index in a Mountain Array](src/main/kotlin/g0801_0900/s0852_peak_index_in_a_mountain_array)| Easy | Array, Binary_Search | 433 | 94.29

#### Day 3

Expand Down Expand Up @@ -1267,6 +1273,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0826 |[Most Profit Assigning Work](src/main/kotlin/g0801_0900/s0826_most_profit_assigning_work)| Medium | Array, Sorting, Greedy, Binary_Search, Two_Pointers | 366 | 100.00
| 0436 |[Find Right Interval](src/main/kotlin/g0401_0500/s0436_find_right_interval)| Medium | Array, Sorting, Binary_Search | 333 | 100.00

#### Day 12
Expand Down Expand Up @@ -1680,6 +1687,42 @@
| 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
| 0857 |[Minimum Cost to Hire K Workers](src/main/kotlin/g0801_0900/s0857_minimum_cost_to_hire_k_workers)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue | 302 | 100.00
| 0856 |[Score of Parentheses](src/main/kotlin/g0801_0900/s0856_score_of_parentheses)| Medium | String, Stack | 129 | 84.62
| 0855 |[Exam Room](src/main/kotlin/g0801_0900/s0855_exam_room)| Medium | Design, Ordered_Set | 644 | 83.33
| 0854 |[K-Similar Strings](src/main/kotlin/g0801_0900/s0854_k_similar_strings)| Hard | String, Breadth_First_Search | 136 | 100.00
| 0853 |[Car Fleet](src/main/kotlin/g0801_0900/s0853_car_fleet)| Medium | Array, Sorting, Stack, Monotonic_Stack | 757 | 85.29
| 0852 |[Peak Index in a Mountain Array](src/main/kotlin/g0801_0900/s0852_peak_index_in_a_mountain_array)| Easy | Array, Binary_Search, Binary_Search_I_Day_2 | 433 | 94.29
| 0851 |[Loud and Rich](src/main/kotlin/g0801_0900/s0851_loud_and_rich)| Medium | Array, Depth_First_Search, Graph, Topological_Sort | 347 | 100.00
| 0850 |[Rectangle Area II](src/main/kotlin/g0801_0900/s0850_rectangle_area_ii)| Hard | Array, Ordered_Set, Segment_Tree, Line_Sweep | 171 | 100.00
| 0849 |[Maximize Distance to Closest Person](src/main/kotlin/g0801_0900/s0849_maximize_distance_to_closest_person)| Medium | Array | 196 | 88.46
| 0848 |[Shifting Letters](src/main/kotlin/g0801_0900/s0848_shifting_letters)| Medium | Array, String | 537 | 93.75
| 0847 |[Shortest Path Visiting All Nodes](src/main/kotlin/g0801_0900/s0847_shortest_path_visiting_all_nodes)| Hard | Dynamic_Programming, Breadth_First_Search, Bit_Manipulation, Graph, Bitmask, Graph_Theory_I_Day_10_Standard_Traversal | 164 | 100.00
| 0846 |[Hand of Straights](src/main/kotlin/g0801_0900/s0846_hand_of_straights)| Medium | Array, Hash_Table, Sorting, Greedy | 306 | 96.15
| 0845 |[Longest Mountain in Array](src/main/kotlin/g0801_0900/s0845_longest_mountain_in_array)| Medium | Array, Dynamic_Programming, Two_Pointers, Enumeration | 222 | 100.00
| 0844 |[Backspace String Compare](src/main/kotlin/g0801_0900/s0844_backspace_string_compare)| Easy | String, Two_Pointers, Stack, Simulation, Algorithm_II_Day_4_Two_Pointers, Level_1_Day_14_Stack | 126 | 98.31
| 0843 |[Guess the Word](src/main/kotlin/g0801_0900/s0843_guess_the_word)| Hard | Array, String, Math, Game_Theory, Interactive | 75 | 100.00
| 0842 |[Split Array into Fibonacci Sequence](src/main/kotlin/g0801_0900/s0842_split_array_into_fibonacci_sequence)| Medium | String, Backtracking | 142 | 100.00
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Data_Structure_II_Day_19_Graph, Graph_Theory_I_Day_7_Standard_Traversal | 189 | 69.23
| 0840 |[Magic Squares In Grid](src/main/kotlin/g0801_0900/s0840_magic_squares_in_grid)| Medium | Array, Math, Matrix | 149 | 100.00
| 0839 |[Similar String Groups](src/main/kotlin/g0801_0900/s0839_similar_string_groups)| Hard | Array, String, Depth_First_Search, Breadth_First_Search, Union_Find | 205 | 100.00
| 0838 |[Push Dominoes](src/main/kotlin/g0801_0900/s0838_push_dominoes)| Medium | String, Dynamic_Programming, Two_Pointers | 270 | 100.00
| 0837 |[New 21 Game](src/main/kotlin/g0801_0900/s0837_new_21_game)| Medium | Dynamic_Programming, Math, Sliding_Window, Probability_and_Statistics | 144 | 75.00
| 0836 |[Rectangle Overlap](src/main/kotlin/g0801_0900/s0836_rectangle_overlap)| Easy | Math, Geometry | 121 | 100.00
| 0835 |[Image Overlap](src/main/kotlin/g0801_0900/s0835_image_overlap)| Medium | Array, Matrix | 163 | 100.00
| 0834 |[Sum of Distances in Tree](src/main/kotlin/g0801_0900/s0834_sum_of_distances_in_tree)| Hard | Dynamic_Programming, Depth_First_Search, Tree, Graph | 746 | 100.00
| 0833 |[Find And Replace in String](src/main/kotlin/g0801_0900/s0833_find_and_replace_in_string)| Medium | Array, String, Sorting | 158 | 100.00
| 0832 |[Flipping an Image](src/main/kotlin/g0801_0900/s0832_flipping_an_image)| Easy | Array, Matrix, Two_Pointers, Simulation | 190 | 94.44
| 0831 |[Masking Personal Information](src/main/kotlin/g0801_0900/s0831_masking_personal_information)| Medium | String | 149 | 100.00
| 0830 |[Positions of Large Groups](src/main/kotlin/g0801_0900/s0830_positions_of_large_groups)| Easy | String | 221 | 100.00
| 0829 |[Consecutive Numbers Sum](src/main/kotlin/g0801_0900/s0829_consecutive_numbers_sum)| Hard | Math, Enumeration | 151 | 100.00
| 0828 |[Count Unique Characters of All Substrings of a Given String](src/main/kotlin/g0801_0900/s0828_count_unique_characters_of_all_substrings_of_a_given_string)| Hard | String, Hash_Table, Dynamic_Programming | 216 | 100.00
| 0827 |[Making A Large Island](src/main/kotlin/g0801_0900/s0827_making_a_large_island)| Hard | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 985 | 100.00
| 0826 |[Most Profit Assigning Work](src/main/kotlin/g0801_0900/s0826_most_profit_assigning_work)| Medium | Array, Sorting, Greedy, Binary_Search, Two_Pointers, Binary_Search_II_Day_11 | 366 | 100.00
| 0825 |[Friends Of Appropriate Ages](src/main/kotlin/g0801_0900/s0825_friends_of_appropriate_ages)| Medium | Array, Sorting, Binary_Search, Two_Pointers | 278 | 100.00
| 0824 |[Goat Latin](src/main/kotlin/g0801_0900/s0824_goat_latin)| Easy | String | 146 | 100.00
| 0823 |[Binary Trees With Factors](src/main/kotlin/g0801_0900/s0823_binary_trees_with_factors)| Medium | Array, Hash_Table, Dynamic_Programming | 298 | 100.00
| 0822 |[Card Flipping Game](src/main/kotlin/g0801_0900/s0822_card_flipping_game)| Medium | Array, Hash_Table | 186 | 100.00
| 0821 |[Shortest Distance to a Character](src/main/kotlin/g0801_0900/s0821_shortest_distance_to_a_character)| Easy | Array, String, Two_Pointers | 168 | 88.00
| 0820 |[Short Encoding of Words](src/main/kotlin/g0801_0900/s0820_short_encoding_of_words)| Medium | Array, String, Hash_Table, Trie | 231 | 100.00
| 0819 |[Most Common Word](src/main/kotlin/g0801_0900/s0819_most_common_word)| Easy | String, Hash_Table, Counting | 211 | 83.33
Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/g0801_0900/s0815_bus_routes/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Return _the least number of buses you must take to travel from_ `source` _to_ `t
## Solution

```kotlin
import java.util.Arrays
import java.util.LinkedList
import java.util.Queue
import kotlin.collections.ArrayList
Expand Down Expand Up @@ -89,14 +88,14 @@ class Solution {
val len = routes.size
val graph: Array<ArrayList<Int>?> = arrayOfNulls(len)
for (i in 0 until len) {
Arrays.sort(routes[i])
routes[i].sort()
graph[i] = ArrayList()
var id = Arrays.binarySearch(routes[i], source)
var id = routes[i].binarySearch(source)
if (id >= 0) {
queue.offer(i)
taken[i] = true
}
id = Arrays.binarySearch(routes[i], target)
id = routes[i].binarySearch(target)
if (id >= 0) {
targetRoutes.add(i)
}
Expand Down
88 changes: 88 additions & 0 deletions src/main/kotlin/g0801_0900/s0822_card_flipping_game/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)

## 822\. Card Flipping Game

Medium

You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the <code>i<sup>th</sup></code> card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may flip over any number of cards (possibly zero).

After flipping the cards, an integer is considered **good** if it is facing down on some card and **not** facing up on any card.

Return _the minimum possible good integer after flipping the cards_. If there are no good integers, return `0`.

**Example 1:**

**Input:** fronts = [1,2,4,4,7], backs = [1,3,4,1,3]

**Output:** 2

**Explanation:**

If we flip the second card, the face up numbers are [1,3,4,4,7] and the face down are [1,2,4,1,3].

2 is the minimum good integer as it appears facing down but not facing up.

It can be shown that 2 is the minimum possible good integer obtainable after flipping some cards.

**Example 2:**

**Input:** fronts = [1], backs = [1]

**Output:** 0

**Explanation:** There are no good integers no matter how we flip the cards, so we return 0.

**Constraints:**

* `n == fronts.length == backs.length`
* `1 <= n <= 1000`
* `1 <= fronts[i], backs[i] <= 2000`

## Solution

```kotlin
class Solution {
fun flipgame(fronts: IntArray, backs: IntArray): Int {
val max = findMax(fronts, backs)
var value = 10000
val twinCardHash = IntArray(max + 1)
val existingNumbersHash = IntArray(max + 1)
for (i in fronts.indices) {
if (fronts[i] == backs[i]) {
twinCardHash[fronts[i]]++
}
existingNumbersHash[fronts[i]]++
existingNumbersHash[backs[i]]++
}
for (i in 1..max) {
if (twinCardHash[i] == 0 && i < value && existingNumbersHash[i] != 0) {
value = i
break
}
}
return if (value == 10000) {
0
} else {
value
}
}

companion object {
private fun findMax(fronts: IntArray, backs: IntArray): Int {
var max = 0
for (front in fronts) {
if (max < front) {
max = front
}
}
for (back in backs) {
if (max < back) {
max = back
}
}
return max
}
}
}
```
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)

## 823\. Binary Trees With Factors

Medium

Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

Return _the number of binary trees we can make_. The answer may be too large so return the answer **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** arr = [2,4]

**Output:** 3

**Explanation:** We can make these trees: `[2], [4], [4, 2, 2]`

**Example 2:**

**Input:** arr = [2,4,5,10]

**Output:** 7

**Explanation:** We can make these trees: `[2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2]`.

**Constraints:**

* `1 <= arr.length <= 1000`
* <code>2 <= arr[i] <= 10<sup>9</sup></code>
* All the values of `arr` are **unique**.

## Solution

```kotlin
class Solution {
private val dp: MutableMap<Int, Long> = HashMap()
private val nums: MutableMap<Int, Int> = HashMap()
fun numFactoredBinaryTrees(arr: IntArray): Int {
arr.sort()
for (i in arr.indices) {
nums[arr[i]] = i
}
var ans: Long = 0
for (i in arr.indices.reversed()) {
ans = (ans % MOD + recursion(arr, arr[i], i) % MOD) % MOD
}
return ans.toInt()
}

private fun recursion(arr: IntArray, v: Int, idx: Int): Long {
if (dp.containsKey(v)) {
return dp[v]!!
}
var ret: Long = 1
for (i in 0 until idx) {
val child = arr[i]
if (v % child == 0 && nums.containsKey(v / child)) {
ret += (
(
recursion(arr, child, nums[arr[i]]!!) %
MOD
* recursion(arr, v / child, nums[v / child]!!) %
MOD
) %
MOD
)
}
}
dp[v] = ret
return ret
}

companion object {
private const val MOD = 1e9.toInt() + 7
}
}
```
67 changes: 67 additions & 0 deletions src/main/kotlin/g0801_0900/s0824_goat_latin/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[![](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)

## 824\. Goat Latin

Easy

You are given a string `sentence` that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.

We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:

* If a word begins with a vowel (`'a'`, `'e'`, `'i'`, `'o'`, or `'u'`), append `"ma"` to the end of the word.
* For example, the word `"apple"` becomes `"applema"`.
* If a word begins with a consonant (i.e., not a vowel), remove the first letter and append it to the end, then add `"ma"`.
* For example, the word `"goat"` becomes `"oatgma"`.
* Add one letter `'a'` to the end of each word per its word index in the sentence, starting with `1`.
* For example, the first word gets `"a"` added to the end, the second word gets `"aa"` added to the end, and so on.

Return _the final sentence representing the conversion from sentence to Goat Latin_.

**Example 1:**

**Input:** sentence = "I speak Goat Latin"

**Output:** "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

**Example 2:**

**Input:** sentence = "The quick brown fox jumped over the lazy dog"

**Output:** "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

**Constraints:**

* `1 <= sentence.length <= 150`
* `sentence` consists of English letters and spaces.
* `sentence` has no leading or trailing spaces.
* All the words in `sentence` are separated by a single space.

## Solution

```kotlin
class Solution {
fun toGoatLatin(sentence: String): String {
val splits = sentence.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val sb = StringBuilder()
val a = StringBuilder()
for (word in splits) {
if (isVowel(word[0])) {
sb.append(word).append("ma")
} else {
val firstChar = word[0]
sb.append(word.substring(1)).append(firstChar).append("ma")
}
a.append("a")
sb.append(a)
sb.append(" ")
}
return sb.toString().trim { it <= ' ' }
}

private fun isVowel(c: Char): Boolean {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'
}
}
```
Loading