Skip to content

Commit 23b55a8

Browse files
authored
Added tasks 1935, 1936.
1 parent ac8cfc7 commit 23b55a8

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1901_2000.s1935_maximum_number_of_words_you_can_type;
2+
3+
// #Easy #String #Hash_Table #2022_05_15_Time_2_ms_(95.06%)_Space_42.6_MB_(61.20%)
4+
5+
public class Solution {
6+
public int canBeTypedWords(String text, String brokenLetters) {
7+
String[] words = text.split(" ");
8+
int count = 0;
9+
for (String word : words) {
10+
boolean broken = false;
11+
for (char c : word.toCharArray()) {
12+
if (brokenLetters.indexOf(c) != -1) {
13+
broken = true;
14+
break;
15+
}
16+
}
17+
if (!broken) {
18+
count++;
19+
}
20+
}
21+
return count;
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1935\. Maximum Number of Words You Can Type
2+
3+
Easy
4+
5+
There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly.
6+
7+
Given a string `text` of words separated by a single space (no leading or trailing spaces) and a string `brokenLetters` of all **distinct** letter keys that are broken, return _the **number of words** in_ `text` _you can fully type using this keyboard_.
8+
9+
**Example 1:**
10+
11+
**Input:** text = "hello world", brokenLetters = "ad"
12+
13+
**Output:** 1
14+
15+
**Explanation:** We cannot type "world" because the 'd' key is broken.
16+
17+
**Example 2:**
18+
19+
**Input:** text = "leet code", brokenLetters = "lt"
20+
21+
**Output:** 1
22+
23+
**Explanation:** We cannot type "leet" because the 'l' and 't' keys are broken.
24+
25+
**Example 3:**
26+
27+
**Input:** text = "leet code", brokenLetters = "e"
28+
29+
**Output:** 0
30+
31+
**Explanation:** We cannot type either word because the 'e' key is broken.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= text.length <= 10<sup>4</sup></code>
36+
* `0 <= brokenLetters.length <= 26`
37+
* `text` consists of words separated by a single space without any leading or trailing spaces.
38+
* Each word only consists of lowercase English letters.
39+
* `brokenLetters` consists of **distinct** lowercase English letters.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1901_2000.s1936_add_minimum_number_of_rungs;
2+
3+
// #Medium #Array #Greedy #2022_05_15_Time_1_ms_(100.00%)_Space_54_MB_(89.37%)
4+
5+
public class Solution {
6+
public int addRungs(int[] rungs, int dist) {
7+
int addons = 0;
8+
int currentHeight = 0;
9+
for (int i = 0; i < rungs.length; ) {
10+
int nextRung = rungs[i];
11+
if (nextRung - currentHeight <= dist) {
12+
currentHeight = nextRung;
13+
i++;
14+
} else {
15+
int adds = (nextRung - currentHeight - 1) / dist;
16+
addons += adds;
17+
currentHeight += dist * adds;
18+
}
19+
}
20+
return addons;
21+
}
22+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
1936\. Add Minimum Number of Rungs
2+
3+
Medium
4+
5+
You are given a **strictly increasing** integer array `rungs` that represents the **height** of rungs on a ladder. You are currently on the **floor** at height `0`, and you want to reach the last rung.
6+
7+
You are also given an integer `dist`. You can only climb to the next highest rung if the distance between where you are currently at (the floor or on a rung) and the next rung is **at most** `dist`. You are able to insert rungs at any positive **integer** height if a rung is not already there.
8+
9+
Return _the **minimum** number of rungs that must be added to the ladder in order for you to climb to the last rung._
10+
11+
**Example 1:**
12+
13+
**Input:** rungs = [1,3,5,10], dist = 2
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
You currently cannot reach the last rung.
20+
21+
Add rungs at heights 7 and 8 to climb this ladder.
22+
23+
The ladder will now have rungs at [1,3,5,7,8,10].
24+
25+
**Example 2:**
26+
27+
**Input:** rungs = [3,6,8,10], dist = 3
28+
29+
**Output:** 0
30+
31+
**Explanation:** This ladder can be climbed without adding additional rungs.
32+
33+
**Example 3:**
34+
35+
**Input:** rungs = [3,4,6,7], dist = 2
36+
37+
**Output:** 1
38+
39+
**Explanation:**
40+
41+
You currently cannot reach the first rung from the ground.
42+
43+
Add a rung at height 1 to climb this ladder.
44+
45+
The ladder will now have rungs at [1,3,4,6,7].
46+
47+
**Constraints:**
48+
49+
* <code>1 <= rungs.length <= 10<sup>5</sup></code>
50+
* <code>1 <= rungs[i] <= 10<sup>9</sup></code>
51+
* <code>1 <= dist <= 10<sup>9</sup></code>
52+
* `rungs` is **strictly increasing**.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1901_2000.s1935_maximum_number_of_words_you_can_type;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void canBeTypedWords() {
11+
assertThat(new Solution().canBeTypedWords("hello world", "ad"), equalTo(1));
12+
}
13+
14+
@Test
15+
void canBeTypedWords2() {
16+
assertThat(new Solution().canBeTypedWords("leet code", "lt"), equalTo(1));
17+
}
18+
19+
@Test
20+
void canBeTypedWords3() {
21+
assertThat(new Solution().canBeTypedWords("leet code", "e"), equalTo(0));
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1901_2000.s1936_add_minimum_number_of_rungs;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void addRungs() {
11+
assertThat(new Solution().addRungs(new int[] {3, 6, 8, 10}, 3), equalTo(0));
12+
}
13+
14+
@Test
15+
void addRungs2() {
16+
assertThat(new Solution().addRungs(new int[] {3, 4, 6, 7}, 2), equalTo(1));
17+
}
18+
}

0 commit comments

Comments
 (0)