Skip to content

Commit eae9173

Browse files
authored
Added tasks 822-857
1 parent c08b01a commit eae9173

File tree

38 files changed

+3062
-4
lines changed

38 files changed

+3062
-4
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
6060
|-|-|-|-|-|-
6161
| 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
62+
| 0841 |[Keys and Rooms](src/main/kotlin/g0801_0900/s0841_keys_and_rooms)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 189 | 69.23
6263

6364
#### Day 8 Standard Traversal
6465

@@ -76,6 +77,7 @@
7677

7778
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
7879
|-|-|-|-|-|-
80+
| 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
7981

8082
#### Day 11 Breadth First Search
8183

@@ -262,6 +264,7 @@
262264

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

267270
#### Day 15 Heap
@@ -859,6 +862,7 @@
859862

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

863867
#### Day 20 Heap Priority Queue
864868

@@ -1002,6 +1006,7 @@
10021006

10031007
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10041008
|-|-|-|-|-|-
1009+
| 0844 |[Backspace String Compare](src/main/kotlin/g0801_0900/s0844_backspace_string_compare)| Easy | String, Two_Pointers, Stack, Simulation | 126 | 98.31
10051010
| 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
10061011

10071012
#### Day 5 Sliding Window
@@ -1139,6 +1144,7 @@
11391144
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11401145
|-|-|-|-|-|-
11411146
| 0035 |[Search Insert Position](src/main/kotlin/g0001_0100/s0035_search_insert_position)| Easy | Top_100_Liked_Questions, Array, Binary_Search | 267 | 50.32
1147+
| 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
11421148

11431149
#### Day 3
11441150

@@ -1267,6 +1273,7 @@
12671273

12681274
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12691275
|-|-|-|-|-|-
1276+
| 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
12701277
| 0436 |[Find Right Interval](src/main/kotlin/g0401_0500/s0436_find_right_interval)| Medium | Array, Sorting, Binary_Search | 333 | 100.00
12711278

12721279
#### Day 12
@@ -1680,6 +1687,42 @@
16801687
| 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
16811688
| 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
16821689
| 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
1690+
| 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
1691+
| 0856 |[Score of Parentheses](src/main/kotlin/g0801_0900/s0856_score_of_parentheses)| Medium | String, Stack | 129 | 84.62
1692+
| 0855 |[Exam Room](src/main/kotlin/g0801_0900/s0855_exam_room)| Medium | Design, Ordered_Set | 644 | 83.33
1693+
| 0854 |[K-Similar Strings](src/main/kotlin/g0801_0900/s0854_k_similar_strings)| Hard | String, Breadth_First_Search | 136 | 100.00
1694+
| 0853 |[Car Fleet](src/main/kotlin/g0801_0900/s0853_car_fleet)| Medium | Array, Sorting, Stack, Monotonic_Stack | 757 | 85.29
1695+
| 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
1696+
| 0851 |[Loud and Rich](src/main/kotlin/g0801_0900/s0851_loud_and_rich)| Medium | Array, Depth_First_Search, Graph, Topological_Sort | 347 | 100.00
1697+
| 0850 |[Rectangle Area II](src/main/kotlin/g0801_0900/s0850_rectangle_area_ii)| Hard | Array, Ordered_Set, Segment_Tree, Line_Sweep | 171 | 100.00
1698+
| 0849 |[Maximize Distance to Closest Person](src/main/kotlin/g0801_0900/s0849_maximize_distance_to_closest_person)| Medium | Array | 196 | 88.46
1699+
| 0848 |[Shifting Letters](src/main/kotlin/g0801_0900/s0848_shifting_letters)| Medium | Array, String | 537 | 93.75
1700+
| 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
1701+
| 0846 |[Hand of Straights](src/main/kotlin/g0801_0900/s0846_hand_of_straights)| Medium | Array, Hash_Table, Sorting, Greedy | 306 | 96.15
1702+
| 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
1703+
| 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
1704+
| 0843 |[Guess the Word](src/main/kotlin/g0801_0900/s0843_guess_the_word)| Hard | Array, String, Math, Game_Theory, Interactive | 75 | 100.00
1705+
| 0842 |[Split Array into Fibonacci Sequence](src/main/kotlin/g0801_0900/s0842_split_array_into_fibonacci_sequence)| Medium | String, Backtracking | 142 | 100.00
1706+
| 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
1707+
| 0840 |[Magic Squares In Grid](src/main/kotlin/g0801_0900/s0840_magic_squares_in_grid)| Medium | Array, Math, Matrix | 149 | 100.00
1708+
| 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
1709+
| 0838 |[Push Dominoes](src/main/kotlin/g0801_0900/s0838_push_dominoes)| Medium | String, Dynamic_Programming, Two_Pointers | 270 | 100.00
1710+
| 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
1711+
| 0836 |[Rectangle Overlap](src/main/kotlin/g0801_0900/s0836_rectangle_overlap)| Easy | Math, Geometry | 121 | 100.00
1712+
| 0835 |[Image Overlap](src/main/kotlin/g0801_0900/s0835_image_overlap)| Medium | Array, Matrix | 163 | 100.00
1713+
| 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
1714+
| 0833 |[Find And Replace in String](src/main/kotlin/g0801_0900/s0833_find_and_replace_in_string)| Medium | Array, String, Sorting | 158 | 100.00
1715+
| 0832 |[Flipping an Image](src/main/kotlin/g0801_0900/s0832_flipping_an_image)| Easy | Array, Matrix, Two_Pointers, Simulation | 190 | 94.44
1716+
| 0831 |[Masking Personal Information](src/main/kotlin/g0801_0900/s0831_masking_personal_information)| Medium | String | 149 | 100.00
1717+
| 0830 |[Positions of Large Groups](src/main/kotlin/g0801_0900/s0830_positions_of_large_groups)| Easy | String | 221 | 100.00
1718+
| 0829 |[Consecutive Numbers Sum](src/main/kotlin/g0801_0900/s0829_consecutive_numbers_sum)| Hard | Math, Enumeration | 151 | 100.00
1719+
| 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
1720+
| 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
1721+
| 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
1722+
| 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
1723+
| 0824 |[Goat Latin](src/main/kotlin/g0801_0900/s0824_goat_latin)| Easy | String | 146 | 100.00
1724+
| 0823 |[Binary Trees With Factors](src/main/kotlin/g0801_0900/s0823_binary_trees_with_factors)| Medium | Array, Hash_Table, Dynamic_Programming | 298 | 100.00
1725+
| 0822 |[Card Flipping Game](src/main/kotlin/g0801_0900/s0822_card_flipping_game)| Medium | Array, Hash_Table | 186 | 100.00
16831726
| 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
16841727
| 0820 |[Short Encoding of Words](src/main/kotlin/g0801_0900/s0820_short_encoding_of_words)| Medium | Array, String, Hash_Table, Trie | 231 | 100.00
16851728
| 0819 |[Most Common Word](src/main/kotlin/g0801_0900/s0819_most_common_word)| Easy | String, Hash_Table, Counting | 211 | 83.33

src/main/kotlin/g0801_0900/s0815_bus_routes/readme.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ Return _the least number of buses you must take to travel from_ `source` _to_ `t
3939
## Solution
4040

4141
```kotlin
42-
import java.util.Arrays
4342
import java.util.LinkedList
4443
import java.util.Queue
4544
import kotlin.collections.ArrayList
@@ -89,14 +88,14 @@ class Solution {
8988
val len = routes.size
9089
val graph: Array<ArrayList<Int>?> = arrayOfNulls(len)
9190
for (i in 0 until len) {
92-
Arrays.sort(routes[i])
91+
routes[i].sort()
9392
graph[i] = ArrayList()
94-
var id = Arrays.binarySearch(routes[i], source)
93+
var id = routes[i].binarySearch(source)
9594
if (id >= 0) {
9695
queue.offer(i)
9796
taken[i] = true
9897
}
99-
id = Arrays.binarySearch(routes[i], target)
98+
id = routes[i].binarySearch(target)
10099
if (id >= 0) {
101100
targetRoutes.add(i)
102101
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
## 822\. Card Flipping Game
5+
6+
Medium
7+
8+
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).
9+
10+
After flipping the cards, an integer is considered **good** if it is facing down on some card and **not** facing up on any card.
11+
12+
Return _the minimum possible good integer after flipping the cards_. If there are no good integers, return `0`.
13+
14+
**Example 1:**
15+
16+
**Input:** fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
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].
23+
24+
2 is the minimum good integer as it appears facing down but not facing up.
25+
26+
It can be shown that 2 is the minimum possible good integer obtainable after flipping some cards.
27+
28+
**Example 2:**
29+
30+
**Input:** fronts = [1], backs = [1]
31+
32+
**Output:** 0
33+
34+
**Explanation:** There are no good integers no matter how we flip the cards, so we return 0.
35+
36+
**Constraints:**
37+
38+
* `n == fronts.length == backs.length`
39+
* `1 <= n <= 1000`
40+
* `1 <= fronts[i], backs[i] <= 2000`
41+
42+
## Solution
43+
44+
```kotlin
45+
class Solution {
46+
fun flipgame(fronts: IntArray, backs: IntArray): Int {
47+
val max = findMax(fronts, backs)
48+
var value = 10000
49+
val twinCardHash = IntArray(max + 1)
50+
val existingNumbersHash = IntArray(max + 1)
51+
for (i in fronts.indices) {
52+
if (fronts[i] == backs[i]) {
53+
twinCardHash[fronts[i]]++
54+
}
55+
existingNumbersHash[fronts[i]]++
56+
existingNumbersHash[backs[i]]++
57+
}
58+
for (i in 1..max) {
59+
if (twinCardHash[i] == 0 && i < value && existingNumbersHash[i] != 0) {
60+
value = i
61+
break
62+
}
63+
}
64+
return if (value == 10000) {
65+
0
66+
} else {
67+
value
68+
}
69+
}
70+
71+
companion object {
72+
private fun findMax(fronts: IntArray, backs: IntArray): Int {
73+
var max = 0
74+
for (front in fronts) {
75+
if (max < front) {
76+
max = front
77+
}
78+
}
79+
for (back in backs) {
80+
if (max < back) {
81+
max = back
82+
}
83+
}
84+
return max
85+
}
86+
}
87+
}
88+
```
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
## 823\. Binary Trees With Factors
5+
6+
Medium
7+
8+
Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.
9+
10+
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.
11+
12+
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>.
13+
14+
**Example 1:**
15+
16+
**Input:** arr = [2,4]
17+
18+
**Output:** 3
19+
20+
**Explanation:** We can make these trees: `[2], [4], [4, 2, 2]`
21+
22+
**Example 2:**
23+
24+
**Input:** arr = [2,4,5,10]
25+
26+
**Output:** 7
27+
28+
**Explanation:** We can make these trees: `[2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2]`.
29+
30+
**Constraints:**
31+
32+
* `1 <= arr.length <= 1000`
33+
* <code>2 <= arr[i] <= 10<sup>9</sup></code>
34+
* All the values of `arr` are **unique**.
35+
36+
## Solution
37+
38+
```kotlin
39+
class Solution {
40+
private val dp: MutableMap<Int, Long> = HashMap()
41+
private val nums: MutableMap<Int, Int> = HashMap()
42+
fun numFactoredBinaryTrees(arr: IntArray): Int {
43+
arr.sort()
44+
for (i in arr.indices) {
45+
nums[arr[i]] = i
46+
}
47+
var ans: Long = 0
48+
for (i in arr.indices.reversed()) {
49+
ans = (ans % MOD + recursion(arr, arr[i], i) % MOD) % MOD
50+
}
51+
return ans.toInt()
52+
}
53+
54+
private fun recursion(arr: IntArray, v: Int, idx: Int): Long {
55+
if (dp.containsKey(v)) {
56+
return dp[v]!!
57+
}
58+
var ret: Long = 1
59+
for (i in 0 until idx) {
60+
val child = arr[i]
61+
if (v % child == 0 && nums.containsKey(v / child)) {
62+
ret += (
63+
(
64+
recursion(arr, child, nums[arr[i]]!!) %
65+
MOD
66+
* recursion(arr, v / child, nums[v / child]!!) %
67+
MOD
68+
) %
69+
MOD
70+
)
71+
}
72+
}
73+
dp[v] = ret
74+
return ret
75+
}
76+
77+
companion object {
78+
private const val MOD = 1e9.toInt() + 7
79+
}
80+
}
81+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
## 824\. Goat Latin
5+
6+
Easy
7+
8+
You are given a string `sentence` that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.
9+
10+
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:
11+
12+
* If a word begins with a vowel (`'a'`, `'e'`, `'i'`, `'o'`, or `'u'`), append `"ma"` to the end of the word.
13+
* For example, the word `"apple"` becomes `"applema"`.
14+
* 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"`.
15+
* For example, the word `"goat"` becomes `"oatgma"`.
16+
* Add one letter `'a'` to the end of each word per its word index in the sentence, starting with `1`.
17+
* For example, the first word gets `"a"` added to the end, the second word gets `"aa"` added to the end, and so on.
18+
19+
Return _the final sentence representing the conversion from sentence to Goat Latin_.
20+
21+
**Example 1:**
22+
23+
**Input:** sentence = "I speak Goat Latin"
24+
25+
**Output:** "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
26+
27+
**Example 2:**
28+
29+
**Input:** sentence = "The quick brown fox jumped over the lazy dog"
30+
31+
**Output:** "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
32+
33+
**Constraints:**
34+
35+
* `1 <= sentence.length <= 150`
36+
* `sentence` consists of English letters and spaces.
37+
* `sentence` has no leading or trailing spaces.
38+
* All the words in `sentence` are separated by a single space.
39+
40+
## Solution
41+
42+
```kotlin
43+
class Solution {
44+
fun toGoatLatin(sentence: String): String {
45+
val splits = sentence.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
46+
val sb = StringBuilder()
47+
val a = StringBuilder()
48+
for (word in splits) {
49+
if (isVowel(word[0])) {
50+
sb.append(word).append("ma")
51+
} else {
52+
val firstChar = word[0]
53+
sb.append(word.substring(1)).append(firstChar).append("ma")
54+
}
55+
a.append("a")
56+
sb.append(a)
57+
sb.append(" ")
58+
}
59+
return sb.toString().trim { it <= ' ' }
60+
}
61+
62+
private fun isVowel(c: Char): Boolean {
63+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
64+
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'
65+
}
66+
}
67+
```

0 commit comments

Comments
 (0)