Skip to content

Commit 5965f64

Browse files
authored
Added tasks 806, 807, 808, 809, 810.
1 parent 93058d4 commit 5965f64

File tree

15 files changed

+555
-0
lines changed

15 files changed

+555
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0801_0900.s0806_number_of_lines_to_write_string;
2+
3+
// #Easy #Array #String
4+
5+
public class Solution {
6+
public int[] numberOfLines(int[] widths, String s) {
7+
int count = 0;
8+
int line = 0;
9+
for (int i = 0; i < s.length(); i++) {
10+
count += widths[s.charAt(i) - 'a'];
11+
if (count == 100) {
12+
line++;
13+
count = 0;
14+
}
15+
if (count > 100) {
16+
line++;
17+
i--;
18+
count = 0;
19+
}
20+
}
21+
if (count > 0 && count < 100) {
22+
line++;
23+
}
24+
if (count == 0) {
25+
count = 100;
26+
}
27+
return new int[] {line, count};
28+
}
29+
}
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0801_0900.s0807_max_increase_to_keep_city_skyline;
2+
3+
// #Medium #Array #Greedy #Matrix
4+
5+
public class Solution {
6+
public int maxIncreaseKeepingSkyline(int[][] grid) {
7+
8+
int rows = grid.length;
9+
int cols = grid[0].length;
10+
int[] tallestR = new int[rows];
11+
int[] tallestC = new int[cols];
12+
int max;
13+
14+
for (int i = 0; i < rows; i++) {
15+
max = 0;
16+
for (int j = 0; j < cols; j++) {
17+
if (grid[i][j] > max) {
18+
max = grid[i][j];
19+
}
20+
}
21+
tallestR[i] = max;
22+
}
23+
24+
for (int i = 0; i < cols; i++) {
25+
max = 0;
26+
for (int[] ints : grid) {
27+
if (ints[i] > max) {
28+
max = ints[i];
29+
}
30+
}
31+
tallestC[i] = max;
32+
}
33+
34+
int increase = 0;
35+
for (int i = 0; i < rows; i++) {
36+
for (int j = 0; j < cols; j++) {
37+
if (tallestR[i] < tallestC[j]) {
38+
increase += tallestR[i] - grid[i][j];
39+
grid[i][j] += tallestR[i] - grid[i][j];
40+
} else {
41+
increase += tallestC[j] - grid[i][j];
42+
grid[i][j] += tallestC[j] - grid[i][j];
43+
}
44+
}
45+
}
46+
47+
return increase;
48+
}
49+
}
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`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0801_0900.s0808_soup_servings;
2+
3+
// #Medium #Dynamic_Programming #Math #Probability_and_Statistics
4+
5+
public class Solution {
6+
public double soupServings(int n) {
7+
return solve(n);
8+
}
9+
10+
private double solve(int n) {
11+
n = n / 25 + (n % 25 > 0 ? 1 : 0);
12+
if (n >= 500) {
13+
return 1.0;
14+
}
15+
return find(n, n, new Double[n + 1][n + 1]);
16+
}
17+
18+
private double find(int a, int b, Double[][] mem) {
19+
if (a <= 0 && b <= 0) {
20+
return 0.5;
21+
} else if (a <= 0) {
22+
return 1;
23+
} else if (b <= 0) {
24+
return 0;
25+
}
26+
27+
double prob = 0;
28+
29+
if (mem[a][b] != null) {
30+
return mem[a][b];
31+
}
32+
33+
prob = find(a - 4, b, mem);
34+
prob += find(a - 3, b - 1, mem);
35+
prob += find(a - 2, b - 2, mem);
36+
prob += find(a - 1, b - 3, mem);
37+
mem[a][b] = 0.25 * prob;
38+
return mem[a][b];
39+
}
40+
41+
private double find(int a, int b) {
42+
if (a <= 0 && b <= 0) {
43+
return 0.5;
44+
} else if (a <= 0) {
45+
return 1;
46+
} else if (b <= 0) {
47+
return 0;
48+
}
49+
50+
double prob = 0;
51+
52+
prob = find(a - 100, b);
53+
prob += find(a - 75, b - 25);
54+
prob += find(a - 50, b - 50);
55+
prob += find(a - 25, b - 75);
56+
return 0.25 * prob;
57+
}
58+
}
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>
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
4+
5+
public class Solution {
6+
public int expressiveWords(String s, String[] words) {
7+
int ans = 0;
8+
for (String w : words) {
9+
if (check(s, w)) {
10+
ans++;
11+
}
12+
}
13+
return ans;
14+
}
15+
16+
private boolean check(String s, String w) {
17+
int i = 0;
18+
int j = 0;
19+
/* Logic is to check whether character at same index of S and w are same
20+
if same,
21+
1. Find the consecutive number of occurrences of the char in S (say len1) and w ( say len2)
22+
2. If len1 == len 2 , move to the next char in S and w
23+
3. If len1 >= 3 and len2 < len1, means we can make the char in w stretchy to match len1
24+
4. else, return false, because it's not possible to stretch the char in w
25+
*/
26+
while (i < s.length() && j < w.length()) {
27+
char ch1 = s.charAt(i);
28+
char ch2 = w.charAt(j);
29+
30+
int len1 = getLen(s, i);
31+
int len2 = getLen(w, j);
32+
if (ch1 == ch2) {
33+
if (len1 == len2 || (len1 >= 3 && len2 < len1)) {
34+
i = i + len1;
35+
j = j + len2;
36+
} else {
37+
return false;
38+
}
39+
} else {
40+
return false;
41+
}
42+
}
43+
return i == s.length() && j == w.length();
44+
}
45+
46+
private int getLen(String value, int i) {
47+
i = i + 1;
48+
int count = 1;
49+
for (int j = i; j < value.length(); j++) {
50+
if (value.charAt(j) == value.charAt(i - 1)) {
51+
count++;
52+
} else {
53+
break;
54+
}
55+
}
56+
return count;
57+
}
58+
}
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g0801_0900.s0810_chalkboard_xor_game;
2+
3+
// #Hard #Array #Math #Bit_Manipulation #Game_Theory #Brainteaser
4+
5+
public class Solution {
6+
public boolean xorGame(int[] nums) {
7+
int xor = 0;
8+
for (int i : nums) {
9+
xor = xor ^ i;
10+
}
11+
return xor == 0 || (nums.length & 1) == 0;
12+
}
13+
}

0 commit comments

Comments
 (0)