Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6a114cb

Browse files
Added tasks 3014-3019
1 parent 8810188 commit 6a114cb

File tree

15 files changed

+622
-0
lines changed

15 files changed

+622
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3001_3100.s3014_minimum_number_of_pushes_to_type_word_i;
2+
3+
// #Easy #String #Math #Greedy #2024_02_28_Time_0_ms_(100.00%)_Space_41.5_MB_(91.88%)
4+
5+
public class Solution {
6+
public int minimumPushes(String word) {
7+
if (word.length() <= 8) {
8+
return word.length();
9+
} else {
10+
int iteration = 1;
11+
int len = word.length();
12+
int count = 0;
13+
while (len > 0) {
14+
if (len >= 8) {
15+
count = count + 8 * iteration;
16+
len = len - 8;
17+
} else {
18+
count = count + len * iteration;
19+
len = 0;
20+
}
21+
iteration++;
22+
}
23+
return count;
24+
}
25+
}
26+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
3014\. Minimum Number of Pushes to Type Word I
2+
3+
Easy
4+
5+
You are given a string `word` containing **distinct** lowercase English letters.
6+
7+
Telephone keypads have keys mapped with **distinct** collections of lowercase English letters, which can be used to form words by pushing them. For example, the key `2` is mapped with `["a","b","c"]`, we need to push the key one time to type `"a"`, two times to type `"b"`, and three times to type `"c"` _._
8+
9+
It is allowed to remap the keys numbered `2` to `9` to **distinct** collections of letters. The keys can be remapped to **any** amount of letters, but each letter **must** be mapped to **exactly** one key. You need to find the **minimum** number of times the keys will be pushed to type the string `word`.
10+
11+
Return _the **minimum** number of pushes needed to type_ `word` _after remapping the keys_.
12+
13+
An example mapping of letters to keys on a telephone keypad is given below. Note that `1`, `*`, `#`, and `0` do **not** map to any letters.
14+
15+
![](https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png)
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png)
20+
21+
**Input:** word = "abcde"
22+
23+
**Output:** 5
24+
25+
**Explanation:** The remapped keypad given in the image provides the minimum cost.
26+
27+
"a" -> one push on key 2
28+
29+
"b" -> one push on key 3
30+
31+
"c" -> one push on key 4
32+
33+
"d" -> one push on key 5
34+
35+
"e" -> one push on key 6
36+
37+
Total cost is 1 +たす 1 +たす 1 +たす 1 +たす 1 = 5.
38+
39+
It can be shown that no other mapping can provide a lower cost.
40+
41+
**Example 2:**
42+
43+
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png)
44+
45+
**Input:** word = "xycdefghij"
46+
47+
**Output:** 12
48+
49+
**Explanation:** The remapped keypad given in the image provides the minimum cost.
50+
51+
"x" -> one push on key 2
52+
53+
"y" -> two pushes on key 2
54+
55+
"c" -> one push on key 3
56+
57+
"d" -> two pushes on key 3
58+
59+
"e" -> one push on key 4
60+
61+
"f" -> one push on key 5
62+
63+
"g" -> one push on key 6
64+
65+
"h" -> one push on key 7
66+
67+
"i" -> one push on key 8
68+
69+
"j" -> one push on key 9
70+
71+
Total cost is 1 +たす 2 +たす 1 +たす 2 +たす 1 +たす 1 +たす 1 +たす 1 +たす 1 +たす 1 = 12.
72+
73+
It can be shown that no other mapping can provide a lower cost.
74+
75+
**Constraints:**
76+
77+
* `1 <= word.length <= 26`
78+
* `word` consists of lowercase English letters.
79+
* All letters in `word` are distinct.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3001_3100.s3015_count_the_number_of_houses_at_a_certain_distance_i;
2+
3+
// #Medium #Graph #Prefix_Sum #Breadth_First_Search
4+
// #2024_02_28_Time_2_ms_(98.98%)_Space_44.1_MB_(90.10%)
5+
6+
public class Solution {
7+
public int[] countOfPairs(int n, int x, int y) {
8+
int[] answer = new int[n];
9+
int distance = n - 1;
10+
int k = distance - 1;
11+
while (distance > 0) {
12+
answer[k] = (n - distance) * 2;
13+
distance--;
14+
k--;
15+
}
16+
if (x > y) {
17+
int tmp = x;
18+
x = y;
19+
y = tmp;
20+
}
21+
int skip = y - x;
22+
if (skip < 2) {
23+
return answer;
24+
}
25+
for (int i = 1; i < n; i++) {
26+
for (int j = i + 1; j <= n; j++) {
27+
int oldDistance = j - i;
28+
int newDistance = Math.abs(x - i) + Math.abs(y - j) + 1;
29+
if (newDistance < oldDistance) {
30+
answer[oldDistance - 1] -= 2;
31+
answer[newDistance - 1] += 2;
32+
}
33+
}
34+
}
35+
return answer;
36+
}
37+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
3015\. Count the Number of Houses at a Certain Distance I
2+
3+
Medium
4+
5+
You are given three **positive** integers `n`, `x`, and `y`.
6+
7+
In a city, there exist houses numbered `1` to `n` connected by `n` streets. There is a street connecting the house numbered `i` with the house numbered `i + 1` for all `1 <= i <= n - 1` . An additional street connects the house numbered `x` with the house numbered `y`.
8+
9+
For each `k`, such that `1 <= k <= n`, you need to find the number of **pairs of houses** <code>(house<sub>1</sub>, house<sub>2</sub>)</code> such that the **minimum** number of streets that need to be traveled to reach <code>house<sub>2</sub></code> from <code>house<sub>1</sub></code> is `k`.
10+
11+
Return _a **1-indexed** array_ `result` _of length_ `n` _where_ `result[k]` _represents the **total** number of pairs of houses such that the **minimum** streets required to reach one house from the other is_ `k`.
12+
13+
**Note** that `x` and `y` can be **equal**.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2023/12/20/example2.png)
18+
19+
**Input:** n = 3, x = 1, y = 3
20+
21+
**Output:** [6,0,0]
22+
23+
**Explanation:** Let's look at each pair of houses:
24+
- For the pair (1, 2), we can go from house 1 to house 2 directly.
25+
- For the pair (2, 1), we can go from house 2 to house 1 directly.
26+
- For the pair (1, 3), we can go from house 1 to house 3 directly.
27+
- For the pair (3, 1), we can go from house 3 to house 1 directly.
28+
- For the pair (2, 3), we can go from house 2 to house 3 directly.
29+
- For the pair (3, 2), we can go from house 3 to house 2 directly.
30+
31+
**Example 2:**
32+
33+
![](https://assets.leetcode.com/uploads/2023/12/20/example3.png)
34+
35+
**Input:** n = 5, x = 2, y = 4
36+
37+
**Output:** [10,8,2,0,0]
38+
39+
**Explanation:** For each distance k the pairs are:
40+
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3), (4, 5), and (5, 4).
41+
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3).
42+
- For k == 3, the pairs are (1, 5), and (5, 1).
43+
- For k == 4 and k == 5, there are no pairs.
44+
45+
**Example 3:**
46+
47+
![](https://assets.leetcode.com/uploads/2023/12/20/example5.png)
48+
49+
**Input:** n = 4, x = 1, y = 1
50+
51+
**Output:** [6,4,2,0]
52+
53+
**Explanation:** For each distance k the pairs are:
54+
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3).
55+
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2).
56+
- For k == 3, the pairs are (1, 4), and (4, 1).
57+
- For k == 4, there are no pairs.
58+
59+
**Constraints:**
60+
61+
* `2 <= n <= 100`
62+
* `1 <= x, y <= n`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3001_3100.s3016_minimum_number_of_pushes_to_type_word_ii;
2+
3+
// #Medium #String #Hash_Table #Sorting #Greedy #Graph #Prefix_Sum #Counting #Breadth_First_Search
4+
// #2024_02_28_Time_7_ms_(100.00%)_Space_45.5_MB_(91.68%)
5+
6+
public class Solution {
7+
public int minimumPushes(String word) {
8+
var count = new int[26];
9+
var l = word.length();
10+
for (var i = 0; i < l; ++i) {
11+
++count[word.charAt(i) - 'a'];
12+
}
13+
int j = 8;
14+
int result = 0;
15+
while (true) {
16+
var mi = 0;
17+
for (var i = 0; i < 26; ++i) {
18+
if (count[mi] < count[i]) {
19+
mi = i;
20+
}
21+
}
22+
if (count[mi] == 0) {
23+
break;
24+
}
25+
result += (j / 8) * count[mi];
26+
count[mi] = 0;
27+
++j;
28+
}
29+
return result;
30+
}
31+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
3016\. Minimum Number of Pushes to Type Word II
2+
3+
Medium
4+
5+
You are given a string `word` containing lowercase English letters.
6+
7+
Telephone keypads have keys mapped with **distinct** collections of lowercase English letters, which can be used to form words by pushing them. For example, the key `2` is mapped with `["a","b","c"]`, we need to push the key one time to type `"a"`, two times to type `"b"`, and three times to type `"c"` _._
8+
9+
It is allowed to remap the keys numbered `2` to `9` to **distinct** collections of letters. The keys can be remapped to **any** amount of letters, but each letter **must** be mapped to **exactly** one key. You need to find the **minimum** number of times the keys will be pushed to type the string `word`.
10+
11+
Return _the **minimum** number of pushes needed to type_ `word` _after remapping the keys_.
12+
13+
An example mapping of letters to keys on a telephone keypad is given below. Note that `1`, `*`, `#`, and `0` do **not** map to any letters.
14+
15+
![](https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png)
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png)
20+
21+
**Input:** word = "abcde"
22+
23+
**Output:** 5
24+
25+
**Explanation:** The remapped keypad given in the image provides the minimum cost.
26+
27+
"a" -> one push on key 2
28+
29+
"b" -> one push on key 3
30+
31+
"c" -> one push on key 4
32+
33+
"d" -> one push on key 5
34+
35+
"e" -> one push on key 6
36+
37+
Total cost is 1 +たす 1 +たす 1 +たす 1 +たす 1 = 5.
38+
39+
It can be shown that no other mapping can provide a lower cost.
40+
41+
**Example 2:**
42+
43+
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png)
44+
45+
**Input:** word = "xyzxyzxyzxyz"
46+
47+
**Output:** 12
48+
49+
**Explanation:** The remapped keypad given in the image provides the minimum cost.
50+
51+
"x" -> one push on key 2
52+
53+
"y" -> one push on key 3
54+
55+
"z" -> one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12
56+
57+
It can be shown that no other mapping can provide a lower cost.
58+
59+
Note that the key 9 is not mapped to any letter: it is not necessary to map letters to every key, but to map all the letters.
60+
61+
**Example 3:**
62+
63+
![](https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png)
64+
65+
**Input:** word = "aabbccddeeffgghhiiiiii"
66+
67+
**Output:** 24
68+
69+
**Explanation:** The remapped keypad given in the image provides the minimum cost.
70+
71+
"a" -> one push on key 2
72+
73+
"b" -> one push on key 3
74+
75+
"c" -> one push on key 4
76+
77+
"d" -> one push on key 5
78+
79+
"e" -> one push on key 6
80+
81+
"f" -> one push on key 7
82+
83+
"g" -> one push on key 8
84+
85+
"h" -> two pushes on key 9
86+
87+
"i" -> one push on key 9
88+
89+
Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24.
90+
91+
It can be shown that no other mapping can provide a lower cost.
92+
93+
**Constraints:**
94+
95+
* <code>1 <= word.length <= 10<sup>5</sup></code>
96+
* `word` consists of lowercase English letters.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /