Skip to content

Commit 0860149

Browse files
authored
Added tasks 806, 807, 808, 809
1 parent 509a5d1 commit 0860149

File tree

13 files changed

+458
-0
lines changed

13 files changed

+458
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17111711
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| 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
17121712
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| 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
17131713
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
1714+
| 0809 |[Expressive Words](src/main/kotlin/g0801_0900/s0809_expressive_words/Solution.kt)| Medium | Array, String, Two_Pointers | 158 | 100.00
1715+
| 0808 |[Soup Servings](src/main/kotlin/g0801_0900/s0808_soup_servings/Solution.kt)| Medium | Dynamic_Programming, Math, Probability_and_Statistics | 112 | 100.00
1716+
| 0807 |[Max Increase to Keep City Skyline](src/main/kotlin/g0801_0900/s0807_max_increase_to_keep_city_skyline/Solution.kt)| Medium | Array, Greedy, Matrix | 158 | 100.00
1717+
| 0806 |[Number of Lines To Write String](src/main/kotlin/g0801_0900/s0806_number_of_lines_to_write_string/Solution.kt)| Easy | Array, String | 134 | 100.00
17141718
| 0805 |[Split Array With Same Average](src/main/kotlin/g0801_0900/s0805_split_array_with_same_average/Solution.kt)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask | 142 | 100.00
17151719
| 0804 |[Unique Morse Code Words](src/main/kotlin/g0801_0900/s0804_unique_morse_code_words/Solution.kt)| Easy | Array, String, Hash_Table | 158 | 80.00
17161720
| 0803 |[Bricks Falling When Hit](src/main/kotlin/g0801_0900/s0803_bricks_falling_when_hit/Solution.kt)| Hard | Array, Matrix, Union_Find | 742 | 100.00
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0801_0900.s0806_number_of_lines_to_write_string
2+
3+
// #Easy #Array #String #2023_03_17_Time_134_ms_(100.00%)_Space_34.6_MB_(9.09%)
4+
5+
class Solution {
6+
fun numberOfLines(widths: IntArray, s: String): IntArray {
7+
var count = 0
8+
var line = 0
9+
var i = 0
10+
while (i < s.length) {
11+
count += widths[s[i].code - 'a'.code]
12+
if (count == 100) {
13+
line++
14+
count = 0
15+
}
16+
if (count > 100) {
17+
line++
18+
i--
19+
count = 0
20+
}
21+
i++
22+
}
23+
if (count in 1..99) {
24+
line++
25+
}
26+
if (count == 0) {
27+
count = 100
28+
}
29+
return intArrayOf(line, count)
30+
}
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
806\. Number of Lines To Write String
2+
3+
Easy
4+
5+
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.
6+
7+
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`.
8+
9+
Return _an array_ `result` _of length 2 where:_
10+
11+
* `result[0]` _is the total number of lines._
12+
* `result[1]` _is the width of the last line in pixels._
13+
14+
**Example 1:**
15+
16+
**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"
17+
18+
**Output:** [3,60]
19+
20+
**Explanation:** You can write s as follows:
21+
22+
abcdefghij // 100 pixels wide
23+
24+
klmnopqrst // 100 pixels wide
25+
26+
uvwxyz // 60 pixels wide
27+
28+
There are a total of 3 lines, and the last line is 60 pixels wide.
29+
30+
**Example 2:**
31+
32+
**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"
33+
34+
**Output:** [2,4]
35+
36+
**Explanation:** You can write s as follows:
37+
38+
bbbcccdddaa // 98 pixels wide
39+
40+
a // 4 pixels wide
41+
42+
There are a total of 2 lines, and the last line is 4 pixels wide.
43+
44+
**Constraints:**
45+
46+
* `widths.length == 26`
47+
* `2 <= widths[i] <= 10`
48+
* `1 <= s.length <= 1000`
49+
* `s` contains only lowercase English letters.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g0801_0900.s0807_max_increase_to_keep_city_skyline
2+
3+
// #Medium #Array #Greedy #Matrix #2023_03_17_Time_158_ms_(100.00%)_Space_35.7_MB_(100.00%)
4+
5+
class Solution {
6+
fun maxIncreaseKeepingSkyline(grid: Array<IntArray>): Int {
7+
val rows = grid.size
8+
val cols = grid[0].size
9+
val tallestR = IntArray(rows)
10+
val tallestC = IntArray(cols)
11+
var max: Int
12+
for (i in 0 until rows) {
13+
max = 0
14+
for (j in 0 until cols) {
15+
if (grid[i][j] > max) {
16+
max = grid[i][j]
17+
}
18+
}
19+
tallestR[i] = max
20+
}
21+
for (i in 0 until cols) {
22+
max = 0
23+
for (ints in grid) {
24+
if (ints[i] > max) {
25+
max = ints[i]
26+
}
27+
}
28+
tallestC[i] = max
29+
}
30+
var increase = 0
31+
for (i in 0 until rows) {
32+
for (j in 0 until cols) {
33+
if (tallestR[i] < tallestC[j]) {
34+
increase += tallestR[i] - grid[i][j]
35+
grid[i][j] += tallestR[i] - grid[i][j]
36+
} else {
37+
increase += tallestC[j] - grid[i][j]
38+
grid[i][j] += tallestC[j] - grid[i][j]
39+
}
40+
}
41+
}
42+
return increase
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
807\. Max Increase to Keep City Skyline
2+
3+
Medium
4+
5+
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`.
6+
7+
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.
8+
9+
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.
10+
11+
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_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/06/21/807-ex1.png)
16+
17+
**Input:** grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
18+
19+
**Output:** 35
20+
21+
**Explanation:** The building heights are shown in the center of the above image.
22+
23+
The skylines when viewed from each cardinal direction are drawn in red.
24+
25+
The grid after increasing the height of buildings without affecting skylines is:
26+
27+
gridNew = [ [8, 4, 8, 7],
28+
[7, 4, 7, 7],
29+
[9, 4, 8, 7],
30+
[3, 3, 3, 3] ]
31+
32+
**Example 2:**
33+
34+
**Input:** grid = [[0,0,0],[0,0,0],[0,0,0]]
35+
36+
**Output:** 0
37+
38+
**Explanation:** Increasing the height of any building will result in the skyline changing.
39+
40+
**Constraints:**
41+
42+
* `n == grid.length`
43+
* `n == grid[r].length`
44+
* `2 <= n <= 50`
45+
* `0 <= grid[r][c] <= 100`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0801_0900.s0808_soup_servings
2+
3+
// #Medium #Dynamic_Programming #Math #Probability_and_Statistics
4+
// #2023_03_17_Time_112_ms_(100.00%)_Space_34_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
fun soupServings(n: Int): Double {
9+
return solve(n)
10+
}
11+
12+
private fun solve(n: Int): Double {
13+
var n = n
14+
n = n / 25 + if (n % 25 > 0) 1 else 0
15+
return if (n >= 500) {
16+
1.0
17+
} else find(n, n, Array(n + 1) { arrayOfNulls(n + 1) })
18+
}
19+
20+
private fun find(a: Int, b: Int, mem: Array<Array<Double?>>): Double {
21+
if (a <= 0 && b <= 0) {
22+
return 0.5
23+
} else if (a <= 0) {
24+
return 1.0
25+
} else if (b <= 0) {
26+
return 0.0
27+
}
28+
if (mem[a][b] != null) {
29+
return mem[a][b]!!
30+
}
31+
var prob: Double = find(a - 4, b, mem)
32+
prob += find(a - 3, b - 1, mem)
33+
prob += find(a - 2, b - 2, mem)
34+
prob += find(a - 1, b - 3, mem)
35+
mem[a][b] = 0.25 * prob
36+
return mem[a][b]!!
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
808\. Soup Servings
2+
3+
Medium
4+
5+
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:
6+
7+
1. Serve `100` ml of **soup A** and `0` ml of **soup B**,
8+
2. Serve `75` ml of **soup A** and `25` ml of **soup B**,
9+
3. Serve `50` ml of **soup A** and `50` ml of **soup B**, and
10+
4. Serve `25` ml of **soup A** and `75` ml of **soup B**.
11+
12+
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.
13+
14+
**Note** that we do not have an operation where all `100` ml's of **soup B** are used first.
15+
16+
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.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 50
21+
22+
**Output:** 0.62500
23+
24+
**Explanation:** If we choose the first two operations, A will become empty first.
25+
26+
For the third operation, A and B will become empty at the same time.
27+
28+
For the fourth operation, B will become empty first.
29+
30+
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.
31+
32+
**Example 2:**
33+
34+
**Input:** n = 100
35+
36+
**Output:** 0.71875
37+
38+
**Constraints:**
39+
40+
* <code>0 <= n <= 10<sup>9</sup></code>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0801_0900.s0809_expressive_words
2+
3+
// #Medium #Array #String #Two_Pointers #2023_03_17_Time_158_ms_(100.00%)_Space_35.1_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun expressiveWords(s: String, words: Array<String>): Int {
8+
var ans = 0
9+
for (w in words) {
10+
if (check(s, w)) {
11+
ans++
12+
}
13+
}
14+
return ans
15+
}
16+
17+
private fun check(s: String, w: String): Boolean {
18+
var i = 0
19+
var j = 0
20+
/* Logic is to check whether character at same index of S and w are same
21+
if same,
22+
1. Find the consecutive number of occurrences of the char in S (say len1) and w ( say len2)
23+
2. If len1 == len 2 , move to the next char in S and w
24+
3. If len1 >= 3 and len2 < len1, means we can make the char in w stretchy to match len1
25+
4. else, return false, because it's not possible to stretch the char in w
26+
*/while (i < s.length && j < w.length) {
27+
val ch1 = s[i]
28+
val ch2 = w[j]
29+
val len1 = getLen(s, i)
30+
val len2 = getLen(w, j)
31+
if (ch1 == ch2) {
32+
if (len1 == len2 || len1 >= 3 && len2 < len1) {
33+
i += len1
34+
j += len2
35+
} else {
36+
return false
37+
}
38+
} else {
39+
return false
40+
}
41+
}
42+
return i == s.length && j == w.length
43+
}
44+
45+
private fun getLen(value: String, i: Int): Int {
46+
var i = i
47+
i += 1
48+
var count = 1
49+
for (j in i until value.length) {
50+
if (value[j] == value[i - 1]) {
51+
count++
52+
} else {
53+
break
54+
}
55+
}
56+
return count
57+
}
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
809\. Expressive Words
2+
3+
Medium
4+
5+
Sometimes people repeat letters to represent extra feeling. For example:
6+
7+
* `"hello" -> "heeellooo"`
8+
* `"hi" -> "hiiii"`
9+
10+
In these strings like `"heeellooo"`, we have groups of adjacent letters that are all the same: `"h"`, `"eee"`, `"ll"`, `"ooo"`.
11+
12+
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**.
13+
14+
* 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`.
15+
16+
Return _the number of query strings that are **stretchy**_.
17+
18+
**Example 1:**
19+
20+
**Input:** s = "heeellooo", words = ["hello", "hi", "helo"]
21+
22+
**Output:** 1
23+
24+
**Explanation:**
25+
26+
We can extend "e" and "o" in the word "hello" to get "heeellooo".
27+
28+
We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.
29+
30+
**Example 2:**
31+
32+
**Input:** s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"]
33+
34+
**Output:** 3
35+
36+
**Constraints:**
37+
38+
* `1 <= s.length, words.length <= 100`
39+
* `1 <= words[i].length <= 100`
40+
* `s` and `words[i]` consist of lowercase letters.

0 commit comments

Comments
 (0)