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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 0815 |[Bus Routes](src/main/kotlin/g0801_0900/s0815_bus_routes)| Hard | Array, Hash_Table, Breadth_First_Search | 429 | 100.00

#### Day 12 Dynamic Programming

Expand Down Expand Up @@ -1679,6 +1680,22 @@
| 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
| 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
| 0818 |[Race Car](src/main/kotlin/g0801_0900/s0818_race_car)| Hard | Dynamic_Programming | 123 | 100.00
| 0817 |[Linked List Components](src/main/kotlin/g0801_0900/s0817_linked_list_components)| Medium | Hash_Table, Linked_List | 239 | 100.00
| 0816 |[Ambiguous Coordinates](src/main/kotlin/g0801_0900/s0816_ambiguous_coordinates)| Medium | String, Backtracking | 231 | 100.00
| 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
| 0814 |[Binary Tree Pruning](src/main/kotlin/g0801_0900/s0814_binary_tree_pruning)| Medium | Depth_First_Search, Tree, Binary_Tree | 127 | 100.00
| 0813 |[Largest Sum of Averages](src/main/kotlin/g0801_0900/s0813_largest_sum_of_averages)| Medium | Array, Dynamic_Programming | 160 | 100.00
| 0812 |[Largest Triangle Area](src/main/kotlin/g0801_0900/s0812_largest_triangle_area)| Easy | Array, Math, Geometry | 156 | 71.43
| 0811 |[Subdomain Visit Count](src/main/kotlin/g0801_0900/s0811_subdomain_visit_count)| Medium | Array, String, Hash_Table, Counting | 220 | 100.00
| 0810 |[Chalkboard XOR Game](src/main/kotlin/g0801_0900/s0810_chalkboard_xor_game)| Hard | Array, Math, Bit_Manipulation, Game_Theory, Brainteaser | 172 | 100.00
| 0809 |[Expressive Words](src/main/kotlin/g0801_0900/s0809_expressive_words)| Medium | Array, String, Two_Pointers | 158 | 100.00
| 0808 |[Soup Servings](src/main/kotlin/g0801_0900/s0808_soup_servings)| Medium | Dynamic_Programming, Math, Probability_and_Statistics | 112 | 100.00
| 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
| 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
| 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
| 0804 |[Unique Morse Code Words](src/main/kotlin/g0801_0900/s0804_unique_morse_code_words)| Easy | Array, String, Hash_Table | 158 | 80.00
| 0803 |[Bricks Falling When Hit](src/main/kotlin/g0801_0900/s0803_bricks_falling_when_hit)| Hard | Array, Matrix, Union_Find | 742 | 100.00
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[![](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)

## 806\. Number of Lines To Write String

Easy

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.

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`.

Return _an array_ `result` _of length 2 where:_

* `result[0]` _is the total number of lines._
* `result[1]` _is the width of the last line in pixels._

**Example 1:**

**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"

**Output:** [3,60]

**Explanation:** You can write s as follows:

abcdefghij // 100 pixels wide

klmnopqrst // 100 pixels wide

uvwxyz // 60 pixels wide

There are a total of 3 lines, and the last line is 60 pixels wide.

**Example 2:**

**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"

**Output:** [2,4]

**Explanation:** You can write s as follows:

bbbcccdddaa // 98 pixels wide

a // 4 pixels wide

There are a total of 2 lines, and the last line is 4 pixels wide.

**Constraints:**

* `widths.length == 26`
* `2 <= widths[i] <= 10`
* `1 <= s.length <= 1000`
* `s` contains only lowercase English letters.

## Solution

```kotlin
class Solution {
fun numberOfLines(widths: IntArray, s: String): IntArray {
var count = 0
var line = 0
var i = 0
while (i < s.length) {
count += widths[s[i].code - 'a'.code]
if (count == 100) {
line++
count = 0
}
if (count > 100) {
line++
i--
count = 0
}
i++
}
if (count in 1..99) {
line++
}
if (count == 0) {
count = 100
}
return intArrayOf(line, count)
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
[![](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)

## 807\. Max Increase to Keep City Skyline

Medium

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`.

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.

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.

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_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/06/21/807-ex1.png)

**Input:** grid = \[\[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]

**Output:** 35

**Explanation:** The building heights are shown in the center of the above image.

The skylines when viewed from each cardinal direction are drawn in red.

The grid after increasing the height of buildings without affecting skylines is:

gridNew = [ [8, 4, 8, 7],
[7, 4, 7, 7],
[9, 4, 8, 7],
[3, 3, 3, 3] ]

**Example 2:**

**Input:** grid = \[\[0,0,0],[0,0,0],[0,0,0]]

**Output:** 0

**Explanation:** Increasing the height of any building will result in the skyline changing.

**Constraints:**

* `n == grid.length`
* `n == grid[r].length`
* `2 <= n <= 50`
* `0 <= grid[r][c] <= 100`

## Solution

```kotlin
class Solution {
fun maxIncreaseKeepingSkyline(grid: Array<IntArray>): Int {
val rows = grid.size
val cols = grid[0].size
val tallestR = IntArray(rows)
val tallestC = IntArray(cols)
var max: Int
for (i in 0 until rows) {
max = 0
for (j in 0 until cols) {
if (grid[i][j] > max) {
max = grid[i][j]
}
}
tallestR[i] = max
}
for (i in 0 until cols) {
max = 0
for (ints in grid) {
if (ints[i] > max) {
max = ints[i]
}
}
tallestC[i] = max
}
var increase = 0
for (i in 0 until rows) {
for (j in 0 until cols) {
if (tallestR[i] < tallestC[j]) {
increase += tallestR[i] - grid[i][j]
grid[i][j] += tallestR[i] - grid[i][j]
} else {
increase += tallestC[j] - grid[i][j]
grid[i][j] += tallestC[j] - grid[i][j]
}
}
}
return increase
}
}
```
81 changes: 81 additions & 0 deletions src/main/kotlin/g0801_0900/s0808_soup_servings/readme.md
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)

## 808\. Soup Servings

Medium

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:

1. Serve `100` ml of **soup A** and `0` ml of **soup B**,
2. Serve `75` ml of **soup A** and `25` ml of **soup B**,
3. Serve `50` ml of **soup A** and `50` ml of **soup B**, and
4. Serve `25` ml of **soup A** and `75` ml of **soup B**.

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.

**Note** that we do not have an operation where all `100` ml's of **soup B** are used first.

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.

**Example 1:**

**Input:** n = 50

**Output:** 0.62500

**Explanation:** If we choose the first two operations, A will become empty first.

For the third operation, A and B will become empty at the same time.

For the fourth operation, B will become empty first.

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.

**Example 2:**

**Input:** n = 100

**Output:** 0.71875

**Constraints:**

* <code>0 <= n <= 10<sup>9</sup></code>

## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun soupServings(n: Int): Double {
return solve(n)
}

private fun solve(n: Int): Double {
var n = n
n = n / 25 + if (n % 25 > 0) 1 else 0
return if (n >= 500) {
1.0
} else find(n, n, Array(n + 1) { arrayOfNulls(n + 1) })
}

private fun find(a: Int, b: Int, mem: Array<Array<Double?>>): Double {
if (a <= 0 && b <= 0) {
return 0.5
} else if (a <= 0) {
return 1.0
} else if (b <= 0) {
return 0.0
}
if (mem[a][b] != null) {
return mem[a][b]!!
}
var prob: Double = find(a - 4, b, mem)
prob += find(a - 3, b - 1, mem)
prob += find(a - 2, b - 2, mem)
prob += find(a - 1, b - 3, mem)
mem[a][b] = 0.25 * prob
return mem[a][b]!!
}
}
```
96 changes: 96 additions & 0 deletions src/main/kotlin/g0801_0900/s0809_expressive_words/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
[![](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)

## 809\. Expressive Words

Medium

Sometimes people repeat letters to represent extra feeling. For example:

* `"hello" -> "heeellooo"`
* `"hi" -> "hiiii"`

In these strings like `"heeellooo"`, we have groups of adjacent letters that are all the same: `"h"`, `"eee"`, `"ll"`, `"ooo"`.

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**.

* 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`.

Return _the number of query strings that are **stretchy**_.

**Example 1:**

**Input:** s = "heeellooo", words = ["hello", "hi", "helo"]

**Output:** 1

**Explanation:**

We can extend "e" and "o" in the word "hello" to get "heeellooo".

We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.

**Example 2:**

**Input:** s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"]

**Output:** 3

**Constraints:**

* `1 <= s.length, words.length <= 100`
* `1 <= words[i].length <= 100`
* `s` and `words[i]` consist of lowercase letters.

## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun expressiveWords(s: String, words: Array<String>): Int {
var ans = 0
for (w in words) {
if (check(s, w)) {
ans++
}
}
return ans
}

private fun check(s: String, w: String): Boolean {
var i = 0
var j = 0
while (i < s.length && j < w.length) {
val ch1 = s[i]
val ch2 = w[j]
val len1 = getLen(s, i)
val len2 = getLen(w, j)
if (ch1 == ch2) {
if (len1 == len2 || len1 >= 3 && len2 < len1) {
i += len1
j += len2
} else {
return false
}
} else {
return false
}
}
return i == s.length && j == w.length
}

private fun getLen(value: String, i: Int): Int {
var i = i
i += 1
var count = 1
for (j in i until value.length) {
if (value[j] == value[i - 1]) {
count++
} else {
break
}
}
return count
}
}
```
Loading