forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
622 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/g3001_3100/s3014_minimum_number_of_pushes_to_type_word_i/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package g3001_3100.s3014_minimum_number_of_pushes_to_type_word_i; | ||
|
||
// #Easy #String #Math #Greedy #2024_02_28_Time_0_ms_(100.00%)_Space_41.5_MB_(91.88%) | ||
|
||
public class Solution { | ||
public int minimumPushes(String word) { | ||
if (word.length() <= 8) { | ||
return word.length(); | ||
} else { | ||
int iteration = 1; | ||
int len = word.length(); | ||
int count = 0; | ||
while (len > 0) { | ||
if (len >= 8) { | ||
count = count + 8 * iteration; | ||
len = len - 8; | ||
} else { | ||
count = count + len * iteration; | ||
len = 0; | ||
} | ||
iteration++; | ||
} | ||
return count; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/g3001_3100/s3014_minimum_number_of_pushes_to_type_word_i/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
3014\. Minimum Number of Pushes to Type Word I | ||
|
||
Easy | ||
|
||
You are given a string `word` containing **distinct** lowercase English letters. | ||
|
||
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"` _._ | ||
|
||
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`. | ||
|
||
Return _the **minimum** number of pushes needed to type_ `word` _after remapping the keys_. | ||
|
||
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. | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png) | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png) | ||
|
||
**Input:** word = "abcde" | ||
|
||
**Output:** 5 | ||
|
||
**Explanation:** The remapped keypad given in the image provides the minimum cost. | ||
|
||
"a" -> one push on key 2 | ||
|
||
"b" -> one push on key 3 | ||
|
||
"c" -> one push on key 4 | ||
|
||
"d" -> one push on key 5 | ||
|
||
"e" -> one push on key 6 | ||
|
||
Total cost is 1 + 1 + 1 + 1 + 1 = 5. | ||
|
||
It can be shown that no other mapping can provide a lower cost. | ||
|
||
**Example 2:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv1e2.png) | ||
|
||
**Input:** word = "xycdefghij" | ||
|
||
**Output:** 12 | ||
|
||
**Explanation:** The remapped keypad given in the image provides the minimum cost. | ||
|
||
"x" -> one push on key 2 | ||
|
||
"y" -> two pushes on key 2 | ||
|
||
"c" -> one push on key 3 | ||
|
||
"d" -> two pushes on key 3 | ||
|
||
"e" -> one push on key 4 | ||
|
||
"f" -> one push on key 5 | ||
|
||
"g" -> one push on key 6 | ||
|
||
"h" -> one push on key 7 | ||
|
||
"i" -> one push on key 8 | ||
|
||
"j" -> one push on key 9 | ||
|
||
Total cost is 1 + 2 + 1 + 2 + 1 + 1 + 1 + 1 + 1 + 1 = 12. | ||
|
||
It can be shown that no other mapping can provide a lower cost. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= word.length <= 26` | ||
* `word` consists of lowercase English letters. | ||
* All letters in `word` are distinct. |
37 changes: 37 additions & 0 deletions
37
...in/java/g3001_3100/s3015_count_the_number_of_houses_at_a_certain_distance_i/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package g3001_3100.s3015_count_the_number_of_houses_at_a_certain_distance_i; | ||
|
||
// #Medium #Graph #Prefix_Sum #Breadth_First_Search | ||
// #2024_02_28_Time_2_ms_(98.98%)_Space_44.1_MB_(90.10%) | ||
|
||
public class Solution { | ||
public int[] countOfPairs(int n, int x, int y) { | ||
int[] answer = new int[n]; | ||
int distance = n - 1; | ||
int k = distance - 1; | ||
while (distance > 0) { | ||
answer[k] = (n - distance) * 2; | ||
distance--; | ||
k--; | ||
} | ||
if (x > y) { | ||
int tmp = x; | ||
x = y; | ||
y = tmp; | ||
} | ||
int skip = y - x; | ||
if (skip < 2) { | ||
return answer; | ||
} | ||
for (int i = 1; i < n; i++) { | ||
for (int j = i + 1; j <= n; j++) { | ||
int oldDistance = j - i; | ||
int newDistance = Math.abs(x - i) + Math.abs(y - j) + 1; | ||
if (newDistance < oldDistance) { | ||
answer[oldDistance - 1] -= 2; | ||
answer[newDistance - 1] += 2; | ||
} | ||
} | ||
} | ||
return answer; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...a/g3001_3100/s3015_count_the_number_of_houses_at_a_certain_distance_i/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
3015\. Count the Number of Houses at a Certain Distance I | ||
|
||
Medium | ||
|
||
You are given three **positive** integers `n`, `x`, and `y`. | ||
|
||
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`. | ||
|
||
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`. | ||
|
||
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`. | ||
|
||
**Note** that `x` and `y` can be **equal**. | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/20/example2.png) | ||
|
||
**Input:** n = 3, x = 1, y = 3 | ||
|
||
**Output:** [6,0,0] | ||
|
||
**Explanation:** Let's look at each pair of houses: | ||
- For the pair (1, 2), we can go from house 1 to house 2 directly. | ||
- For the pair (2, 1), we can go from house 2 to house 1 directly. | ||
- For the pair (1, 3), we can go from house 1 to house 3 directly. | ||
- For the pair (3, 1), we can go from house 3 to house 1 directly. | ||
- For the pair (2, 3), we can go from house 2 to house 3 directly. | ||
- For the pair (3, 2), we can go from house 3 to house 2 directly. | ||
|
||
**Example 2:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/20/example3.png) | ||
|
||
**Input:** n = 5, x = 2, y = 4 | ||
|
||
**Output:** [10,8,2,0,0] | ||
|
||
**Explanation:** For each distance k the pairs are: | ||
- 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). | ||
- For k == 2, the pairs are (1, 3), (3, 1), (1, 4), (4, 1), (2, 5), (5, 2), (3, 5), and (5, 3). | ||
- For k == 3, the pairs are (1, 5), and (5, 1). | ||
- For k == 4 and k == 5, there are no pairs. | ||
|
||
**Example 3:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/20/example5.png) | ||
|
||
**Input:** n = 4, x = 1, y = 1 | ||
|
||
**Output:** [6,4,2,0] | ||
|
||
**Explanation:** For each distance k the pairs are: | ||
- For k == 1, the pairs are (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), and (4, 3). | ||
- For k == 2, the pairs are (1, 3), (3, 1), (2, 4), and (4, 2). | ||
- For k == 3, the pairs are (1, 4), and (4, 1). | ||
- For k == 4, there are no pairs. | ||
|
||
**Constraints:** | ||
|
||
* `2 <= n <= 100` | ||
* `1 <= x, y <= n` |
31 changes: 31 additions & 0 deletions
31
src/main/java/g3001_3100/s3016_minimum_number_of_pushes_to_type_word_ii/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package g3001_3100.s3016_minimum_number_of_pushes_to_type_word_ii; | ||
|
||
// #Medium #String #Hash_Table #Sorting #Greedy #Graph #Prefix_Sum #Counting #Breadth_First_Search | ||
// #2024_02_28_Time_7_ms_(100.00%)_Space_45.5_MB_(91.68%) | ||
|
||
public class Solution { | ||
public int minimumPushes(String word) { | ||
var count = new int[26]; | ||
var l = word.length(); | ||
for (var i = 0; i < l; ++i) { | ||
++count[word.charAt(i) - 'a']; | ||
} | ||
int j = 8; | ||
int result = 0; | ||
while (true) { | ||
var mi = 0; | ||
for (var i = 0; i < 26; ++i) { | ||
if (count[mi] < count[i]) { | ||
mi = i; | ||
} | ||
} | ||
if (count[mi] == 0) { | ||
break; | ||
} | ||
result += (j / 8) * count[mi]; | ||
count[mi] = 0; | ||
++j; | ||
} | ||
return result; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/g3001_3100/s3016_minimum_number_of_pushes_to_type_word_ii/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
3016\. Minimum Number of Pushes to Type Word II | ||
|
||
Medium | ||
|
||
You are given a string `word` containing lowercase English letters. | ||
|
||
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"` _._ | ||
|
||
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`. | ||
|
||
Return _the **minimum** number of pushes needed to type_ `word` _after remapping the keys_. | ||
|
||
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. | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/26/keypaddesc.png) | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv1e1.png) | ||
|
||
**Input:** word = "abcde" | ||
|
||
**Output:** 5 | ||
|
||
**Explanation:** The remapped keypad given in the image provides the minimum cost. | ||
|
||
"a" -> one push on key 2 | ||
|
||
"b" -> one push on key 3 | ||
|
||
"c" -> one push on key 4 | ||
|
||
"d" -> one push on key 5 | ||
|
||
"e" -> one push on key 6 | ||
|
||
Total cost is 1 + 1 + 1 + 1 + 1 = 5. | ||
|
||
It can be shown that no other mapping can provide a lower cost. | ||
|
||
**Example 2:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/26/keypadv2e2.png) | ||
|
||
**Input:** word = "xyzxyzxyzxyz" | ||
|
||
**Output:** 12 | ||
|
||
**Explanation:** The remapped keypad given in the image provides the minimum cost. | ||
|
||
"x" -> one push on key 2 | ||
|
||
"y" -> one push on key 3 | ||
|
||
"z" -> one push on key 4 Total cost is 1 * 4 + 1 * 4 + 1 * 4 = 12 | ||
|
||
It can be shown that no other mapping can provide a lower cost. | ||
|
||
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. | ||
|
||
**Example 3:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/27/keypadv2.png) | ||
|
||
**Input:** word = "aabbccddeeffgghhiiiiii" | ||
|
||
**Output:** 24 | ||
|
||
**Explanation:** The remapped keypad given in the image provides the minimum cost. | ||
|
||
"a" -> one push on key 2 | ||
|
||
"b" -> one push on key 3 | ||
|
||
"c" -> one push on key 4 | ||
|
||
"d" -> one push on key 5 | ||
|
||
"e" -> one push on key 6 | ||
|
||
"f" -> one push on key 7 | ||
|
||
"g" -> one push on key 8 | ||
|
||
"h" -> two pushes on key 9 | ||
|
||
"i" -> one push on key 9 | ||
|
||
Total cost is 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 1 * 2 + 2 * 2 + 6 * 1 = 24. | ||
|
||
It can be shown that no other mapping can provide a lower cost. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= word.length <= 10<sup>5</sup></code> | ||
* `word` consists of lowercase English letters. |
Oops, something went wrong.