Skip to content

Commit

Permalink
Added tasks 3014-3019
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhNIT authored Feb 28, 2024
1 parent 8810188 commit 6a114cb
Show file tree
Hide file tree
Showing 15 changed files with 622 additions and 0 deletions.
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;
}
}
}
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.
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;
}
}
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`
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;
}
}
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.
Loading

0 comments on commit 6a114cb

Please sign in to comment.