Skip to content

Commit c08b01a

Browse files
authored
Added tasks 805-821
1 parent c160295 commit c08b01a

File tree

17 files changed

+1347
-0
lines changed

17 files changed

+1347
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@
347347
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
348348
|-|-|-|-|-|-
349349
| 0210 |[Course Schedule II](src/main/kotlin/g0201_0300/s0210_course_schedule_ii)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 266 | 96.32
350+
| 0815 |[Bus Routes](src/main/kotlin/g0801_0900/s0815_bus_routes)| Hard | Array, Hash_Table, Breadth_First_Search | 429 | 100.00
350351

351352
#### Day 12 Dynamic Programming
352353

@@ -1679,6 +1680,22 @@
16791680
| 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
16801681
| 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
16811682
| 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
1683+
| 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
1684+
| 0820 |[Short Encoding of Words](src/main/kotlin/g0801_0900/s0820_short_encoding_of_words)| Medium | Array, String, Hash_Table, Trie | 231 | 100.00
1685+
| 0819 |[Most Common Word](src/main/kotlin/g0801_0900/s0819_most_common_word)| Easy | String, Hash_Table, Counting | 211 | 83.33
1686+
| 0818 |[Race Car](src/main/kotlin/g0801_0900/s0818_race_car)| Hard | Dynamic_Programming | 123 | 100.00
1687+
| 0817 |[Linked List Components](src/main/kotlin/g0801_0900/s0817_linked_list_components)| Medium | Hash_Table, Linked_List | 239 | 100.00
1688+
| 0816 |[Ambiguous Coordinates](src/main/kotlin/g0801_0900/s0816_ambiguous_coordinates)| Medium | String, Backtracking | 231 | 100.00
1689+
| 0815 |[Bus Routes](src/main/kotlin/g0801_0900/s0815_bus_routes)| Hard | Array, Hash_Table, Breadth_First_Search, Level_2_Day_11_Graph/BFS/DFS | 429 | 100.00
1690+
| 0814 |[Binary Tree Pruning](src/main/kotlin/g0801_0900/s0814_binary_tree_pruning)| Medium | Depth_First_Search, Tree, Binary_Tree | 127 | 100.00
1691+
| 0813 |[Largest Sum of Averages](src/main/kotlin/g0801_0900/s0813_largest_sum_of_averages)| Medium | Array, Dynamic_Programming | 160 | 100.00
1692+
| 0812 |[Largest Triangle Area](src/main/kotlin/g0801_0900/s0812_largest_triangle_area)| Easy | Array, Math, Geometry | 156 | 71.43
1693+
| 0811 |[Subdomain Visit Count](src/main/kotlin/g0801_0900/s0811_subdomain_visit_count)| Medium | Array, String, Hash_Table, Counting | 220 | 100.00
1694+
| 0810 |[Chalkboard XOR Game](src/main/kotlin/g0801_0900/s0810_chalkboard_xor_game)| Hard | Array, Math, Bit_Manipulation, Game_Theory, Brainteaser | 172 | 100.00
1695+
| 0809 |[Expressive Words](src/main/kotlin/g0801_0900/s0809_expressive_words)| Medium | Array, String, Two_Pointers | 158 | 100.00
1696+
| 0808 |[Soup Servings](src/main/kotlin/g0801_0900/s0808_soup_servings)| Medium | Dynamic_Programming, Math, Probability_and_Statistics | 112 | 100.00
1697+
| 0807 |[Max Increase to Keep City Skyline](src/main/kotlin/g0801_0900/s0807_max_increase_to_keep_city_skyline)| Medium | Array, Greedy, Matrix | 158 | 100.00
1698+
| 0806 |[Number of Lines To Write String](src/main/kotlin/g0801_0900/s0806_number_of_lines_to_write_string)| Easy | Array, String | 134 | 100.00
16821699
| 0805 |[Split Array With Same Average](src/main/kotlin/g0801_0900/s0805_split_array_with_same_average)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask | 142 | 100.00
16831700
| 0804 |[Unique Morse Code Words](src/main/kotlin/g0801_0900/s0804_unique_morse_code_words)| Easy | Array, String, Hash_Table | 158 | 80.00
16841701
| 0803 |[Bricks Falling When Hit](src/main/kotlin/g0801_0900/s0803_bricks_falling_when_hit)| Hard | Array, Matrix, Union_Find | 742 | 100.00
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
## 806\. Number of Lines To Write String
5+
6+
Easy
7+
8+
You are given a string `s` of lowercase English letters and an array `widths` denoting **how many pixels wide** each lowercase English letter is. Specifically, `widths[0]` is the width of `'a'`, `widths[1]` is the width of `'b'`, and so on.
9+
10+
You are trying to write `s` across several lines, where **each line is no longer than** `100` **pixels**. Starting at the beginning of `s`, write as many letters on the first line such that the total width does not exceed `100` pixels. Then, from where you stopped in `s`, continue writing as many letters as you can on the second line. Continue this process until you have written all of `s`.
11+
12+
Return _an array_ `result` _of length 2 where:_
13+
14+
* `result[0]` _is the total number of lines._
15+
* `result[1]` _is the width of the last line in pixels._
16+
17+
**Example 1:**
18+
19+
**Input:** widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], s = "abcdefghijklmnopqrstuvwxyz"
20+
21+
**Output:** [3,60]
22+
23+
**Explanation:** You can write s as follows:
24+
25+
abcdefghij // 100 pixels wide
26+
27+
klmnopqrst // 100 pixels wide
28+
29+
uvwxyz // 60 pixels wide
30+
31+
There are a total of 3 lines, and the last line is 60 pixels wide.
32+
33+
**Example 2:**
34+
35+
**Input:** widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], s = "bbbcccdddaaa"
36+
37+
**Output:** [2,4]
38+
39+
**Explanation:** You can write s as follows:
40+
41+
bbbcccdddaa // 98 pixels wide
42+
43+
a // 4 pixels wide
44+
45+
There are a total of 2 lines, and the last line is 4 pixels wide.
46+
47+
**Constraints:**
48+
49+
* `widths.length == 26`
50+
* `2 <= widths[i] <= 10`
51+
* `1 <= s.length <= 1000`
52+
* `s` contains only lowercase English letters.
53+
54+
## Solution
55+
56+
```kotlin
57+
class Solution {
58+
fun numberOfLines(widths: IntArray, s: String): IntArray {
59+
var count = 0
60+
var line = 0
61+
var i = 0
62+
while (i < s.length) {
63+
count += widths[s[i].code - 'a'.code]
64+
if (count == 100) {
65+
line++
66+
count = 0
67+
}
68+
if (count > 100) {
69+
line++
70+
i--
71+
count = 0
72+
}
73+
i++
74+
}
75+
if (count in 1..99) {
76+
line++
77+
}
78+
if (count == 0) {
79+
count = 100
80+
}
81+
return intArrayOf(line, count)
82+
}
83+
}
84+
```
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
## 807\. Max Increase to Keep City Skyline
5+
6+
Medium
7+
8+
There is a city composed of `n x n` blocks, where each block contains a single building shaped like a vertical square prism. You are given a **0-indexed** `n x n` integer matrix `grid` where `grid[r][c]` represents the **height** of the building located in the block at row `r` and column `c`.
9+
10+
A city's **skyline** is the the outer contour formed by all the building when viewing the side of the city from a distance. The **skyline** from each cardinal direction north, east, south, and west may be different.
11+
12+
We are allowed to increase the height of **any number of buildings by any amount** (the amount can be different per building). The height of a `0`\-height building can also be increased. However, increasing the height of a building should **not** affect the city's **skyline** from any cardinal direction.
13+
14+
Return _the **maximum total sum** that the height of the buildings can be increased by **without** changing the city's **skyline** from any cardinal direction_.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2021/06/21/807-ex1.png)
19+
20+
**Input:** grid = \[\[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
21+
22+
**Output:** 35
23+
24+
**Explanation:** The building heights are shown in the center of the above image.
25+
26+
The skylines when viewed from each cardinal direction are drawn in red.
27+
28+
The grid after increasing the height of buildings without affecting skylines is:
29+
30+
gridNew = [ [8, 4, 8, 7],
31+
[7, 4, 7, 7],
32+
[9, 4, 8, 7],
33+
[3, 3, 3, 3] ]
34+
35+
**Example 2:**
36+
37+
**Input:** grid = \[\[0,0,0],[0,0,0],[0,0,0]]
38+
39+
**Output:** 0
40+
41+
**Explanation:** Increasing the height of any building will result in the skyline changing.
42+
43+
**Constraints:**
44+
45+
* `n == grid.length`
46+
* `n == grid[r].length`
47+
* `2 <= n <= 50`
48+
* `0 <= grid[r][c] <= 100`
49+
50+
## Solution
51+
52+
```kotlin
53+
class Solution {
54+
fun maxIncreaseKeepingSkyline(grid: Array<IntArray>): Int {
55+
val rows = grid.size
56+
val cols = grid[0].size
57+
val tallestR = IntArray(rows)
58+
val tallestC = IntArray(cols)
59+
var max: Int
60+
for (i in 0 until rows) {
61+
max = 0
62+
for (j in 0 until cols) {
63+
if (grid[i][j] > max) {
64+
max = grid[i][j]
65+
}
66+
}
67+
tallestR[i] = max
68+
}
69+
for (i in 0 until cols) {
70+
max = 0
71+
for (ints in grid) {
72+
if (ints[i] > max) {
73+
max = ints[i]
74+
}
75+
}
76+
tallestC[i] = max
77+
}
78+
var increase = 0
79+
for (i in 0 until rows) {
80+
for (j in 0 until cols) {
81+
if (tallestR[i] < tallestC[j]) {
82+
increase += tallestR[i] - grid[i][j]
83+
grid[i][j] += tallestR[i] - grid[i][j]
84+
} else {
85+
increase += tallestC[j] - grid[i][j]
86+
grid[i][j] += tallestC[j] - grid[i][j]
87+
}
88+
}
89+
}
90+
return increase
91+
}
92+
}
93+
```
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+
## 808\. Soup Servings
5+
6+
Medium
7+
8+
There are two types of soup: **type A** and **type B**. Initially, we have `n` ml of each type of soup. There are four kinds of operations:
9+
10+
1. Serve `100` ml of **soup A** and `0` ml of **soup B**,
11+
2. Serve `75` ml of **soup A** and `25` ml of **soup B**,
12+
3. Serve `50` ml of **soup A** and `50` ml of **soup B**, and
13+
4. Serve `25` ml of **soup A** and `75` ml of **soup B**.
14+
15+
When we serve some soup, we give it to someone, and we no longer have it. Each turn, we will choose from the four operations with an equal probability `0.25`. If the remaining volume of soup is not enough to complete the operation, we will serve as much as possible. We stop once we no longer have some quantity of both types of soup.
16+
17+
**Note** that we do not have an operation where all `100` ml's of **soup B** are used first.
18+
19+
Return _the probability that **soup A** will be empty first, plus half the probability that **A** and **B** become empty at the same time_. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
20+
21+
**Example 1:**
22+
23+
**Input:** n = 50
24+
25+
**Output:** 0.62500
26+
27+
**Explanation:** If we choose the first two operations, A will become empty first.
28+
29+
For the third operation, A and B will become empty at the same time.
30+
31+
For the fourth operation, B will become empty first.
32+
33+
So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 \* (1 + 1 + 0.5 + 0) = 0.625.
34+
35+
**Example 2:**
36+
37+
**Input:** n = 100
38+
39+
**Output:** 0.71875
40+
41+
**Constraints:**
42+
43+
* <code>0 <= n <= 10<sup>9</sup></code>
44+
45+
## Solution
46+
47+
```kotlin
48+
@Suppress("NAME_SHADOWING")
49+
class Solution {
50+
fun soupServings(n: Int): Double {
51+
return solve(n)
52+
}
53+
54+
private fun solve(n: Int): Double {
55+
var n = n
56+
n = n / 25 + if (n % 25 > 0) 1 else 0
57+
return if (n >= 500) {
58+
1.0
59+
} else find(n, n, Array(n + 1) { arrayOfNulls(n + 1) })
60+
}
61+
62+
private fun find(a: Int, b: Int, mem: Array<Array<Double?>>): Double {
63+
if (a <= 0 && b <= 0) {
64+
return 0.5
65+
} else if (a <= 0) {
66+
return 1.0
67+
} else if (b <= 0) {
68+
return 0.0
69+
}
70+
if (mem[a][b] != null) {
71+
return mem[a][b]!!
72+
}
73+
var prob: Double = find(a - 4, b, mem)
74+
prob += find(a - 3, b - 1, mem)
75+
prob += find(a - 2, b - 2, mem)
76+
prob += find(a - 1, b - 3, mem)
77+
mem[a][b] = 0.25 * prob
78+
return mem[a][b]!!
79+
}
80+
}
81+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
## 809\. Expressive Words
5+
6+
Medium
7+
8+
Sometimes people repeat letters to represent extra feeling. For example:
9+
10+
* `"hello" -> "heeellooo"`
11+
* `"hi" -> "hiiii"`
12+
13+
In these strings like `"heeellooo"`, we have groups of adjacent letters that are all the same: `"h"`, `"eee"`, `"ll"`, `"ooo"`.
14+
15+
You are given a string `s` and an array of query strings `words`. A query word is **stretchy** if it can be made to be equal to `s` by any number of applications of the following extension operation: choose a group consisting of characters `c`, and add some number of characters `c` to the group so that the size of the group is **three or more**.
16+
17+
* For example, starting with `"hello"`, we could do an extension on the group `"o"` to get `"hellooo"`, but we cannot get `"helloo"` since the group `"oo"` has a size less than three. Also, we could do another extension like `"ll" -> "lllll"` to get `"helllllooo"`. If `s = "helllllooo"`, then the query word `"hello"` would be **stretchy** because of these two extension operations: `query = "hello" -> "hellooo" -> "helllllooo" = s`.
18+
19+
Return _the number of query strings that are **stretchy**_.
20+
21+
**Example 1:**
22+
23+
**Input:** s = "heeellooo", words = ["hello", "hi", "helo"]
24+
25+
**Output:** 1
26+
27+
**Explanation:**
28+
29+
We can extend "e" and "o" in the word "hello" to get "heeellooo".
30+
31+
We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.
32+
33+
**Example 2:**
34+
35+
**Input:** s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"]
36+
37+
**Output:** 3
38+
39+
**Constraints:**
40+
41+
* `1 <= s.length, words.length <= 100`
42+
* `1 <= words[i].length <= 100`
43+
* `s` and `words[i]` consist of lowercase letters.
44+
45+
## Solution
46+
47+
```kotlin
48+
@Suppress("NAME_SHADOWING")
49+
class Solution {
50+
fun expressiveWords(s: String, words: Array<String>): Int {
51+
var ans = 0
52+
for (w in words) {
53+
if (check(s, w)) {
54+
ans++
55+
}
56+
}
57+
return ans
58+
}
59+
60+
private fun check(s: String, w: String): Boolean {
61+
var i = 0
62+
var j = 0
63+
while (i < s.length && j < w.length) {
64+
val ch1 = s[i]
65+
val ch2 = w[j]
66+
val len1 = getLen(s, i)
67+
val len2 = getLen(w, j)
68+
if (ch1 == ch2) {
69+
if (len1 == len2 || len1 >= 3 && len2 < len1) {
70+
i += len1
71+
j += len2
72+
} else {
73+
return false
74+
}
75+
} else {
76+
return false
77+
}
78+
}
79+
return i == s.length && j == w.length
80+
}
81+
82+
private fun getLen(value: String, i: Int): Int {
83+
var i = i
84+
i += 1
85+
var count = 1
86+
for (j in i until value.length) {
87+
if (value[j] == value[i - 1]) {
88+
count++
89+
} else {
90+
break
91+
}
92+
}
93+
return count
94+
}
95+
}
96+
```

0 commit comments

Comments
 (0)