Skip to content

Commit cde20f9

Browse files
authored
Added tasks 2389, 2390, 2391, 2392.
1 parent ed88246 commit cde20f9

File tree

13 files changed

+478
-0
lines changed

13 files changed

+478
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,10 @@ implementation 'com.github.javadev:leetcode-in-java:1.13'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2392 |[Build a Matrix With Conditions](src/main/java/g2301_2400/s2392_build_a_matrix_with_conditions/Solution.java)| Hard | Array, Matrix, Graph, Topological_Sort | 9 | 97.22
1852+
| 2391 |[Minimum Amount of Time to Collect Garbage](src/main/java/g2301_2400/s2391_minimum_amount_of_time_to_collect_garbage/Solution.java)| Medium | Array, String, Prefix_Sum | 7 | 98.86
1853+
| 2390 |[Removing Stars From a String](src/main/java/g2301_2400/s2390_removing_stars_from_a_string/Solution.java)| Medium | String, Stack, Simulation | 31 | 90.55
1854+
| 2389 |[Longest Subsequence With Limited Sum](src/main/java/g2301_2400/s2389_longest_subsequence_with_limited_sum/Solution.java)| Easy | Array, Sorting, Greedy, Binary_Search, Prefix_Sum | 4 | 99.97
18511855
| 2386 |[Find the K-Sum of an Array](src/main/java/g2301_2400/s2386_find_the_k_sum_of_an_array/Solution.java)| Hard | Array, Sorting, Heap_Priority_Queue | 75 | 100.00
18521856
| 2385 |[Amount of Time for Binary Tree to Be Infected](src/main/java/g2301_2400/s2385_amount_of_time_for_binary_tree_to_be_infected/Solution.java)| Medium | Tree, Binary_Tree, Depth_First_Search, Breadth_First_Search | 20 | 100.00
18531857
| 2384 |[Largest Palindromic Number](src/main/java/g2301_2400/s2384_largest_palindromic_number/Solution.java)| Medium | String, Hash_Table, Greedy | 26 | 100.00
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2301_2400.s2389_longest_subsequence_with_limited_sum;
2+
3+
// #Easy #Array #Sorting #Greedy #Binary_Search #Prefix_Sum
4+
// #2022_09_02_Time_4_ms_(99.97%)_Space_42.7_MB_(95.35%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int[] answerQueries(int[] nums, int[] queries) {
10+
// we can sort the nums because the order of the subsequence does not matter
11+
Arrays.sort(nums);
12+
for (int i = 1; i < nums.length; i++) {
13+
nums[i] = nums[i] + nums[i - 1];
14+
}
15+
for (int i = 0; i < queries.length; i++) {
16+
int j = Arrays.binarySearch(nums, queries[i]);
17+
if (j < 0) {
18+
j = -j - 2;
19+
}
20+
queries[i] = j + 1;
21+
}
22+
return queries;
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2389\. Longest Subsequence With Limited Sum
2+
3+
Easy
4+
5+
You are given an integer array `nums` of length `n`, and an integer array `queries` of length `m`.
6+
7+
Return _an array_ `answer` _of length_ `m` _where_ `answer[i]` _is the **maximum** size of a **subsequence** that you can take from_ `nums` _such that the **sum** of its elements is less than or equal to_ `queries[i]`.
8+
9+
A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [4,5,2,1], queries = [3,10,21]
14+
15+
**Output:** [2,3,4]
16+
17+
**Explanation:** We answer the queries as follows:
18+
19+
- The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2 is the maximum size of such a subsequence, so answer[0] = 2.
20+
21+
- The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer[1] = 3.
22+
23+
- The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer[2] = 4.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [2,3,4,5], queries = [1]
28+
29+
**Output:** [0]
30+
31+
**Explanation:** The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0] = 0.
32+
33+
**Constraints:**
34+
35+
* `n == nums.length`
36+
* `m == queries.length`
37+
* `1 <= n, m <= 1000`
38+
* <code>1 <= nums[i], queries[i] <= 10<sup>6</sup></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2301_2400.s2390_removing_stars_from_a_string;
2+
3+
// #Medium #String #Stack #Simulation #2022_09_02_Time_31_ms_(90.55%)_Space_62.6_MB_(76.40%)
4+
5+
public class Solution {
6+
public String removeStars(String s) {
7+
StringBuilder sb = new StringBuilder();
8+
int stars = 0;
9+
for (int i = s.length() - 1; i >= 0; --i) {
10+
if (s.charAt(i) == '*') {
11+
++stars;
12+
} else if (stars > 0) {
13+
--stars;
14+
} else {
15+
sb.append(s.charAt(i));
16+
}
17+
}
18+
return sb.reverse().toString();
19+
}
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2390\. Removing Stars From a String
2+
3+
Medium
4+
5+
You are given a string `s`, which contains stars `*`.
6+
7+
In one operation, you can:
8+
9+
* Choose a star in `s`.
10+
* Remove the closest **non-star** character to its **left**, as well as remove the star itself.
11+
12+
Return _the string after **all** stars have been removed_.
13+
14+
**Note:**
15+
16+
* The input will be generated such that the operation is always possible.
17+
* It can be shown that the resulting string will always be unique.
18+
19+
**Example 1:**
20+
21+
**Input:** s = "leet\*\*cod\*e"
22+
23+
**Output:** "lecoe"
24+
25+
**Explanation:** Performing the removals from left to right:
26+
27+
- The closest character to the 1<sup>st</sup> star is 't' in "lee**<ins>t</ins>**\*\*cod\*e". s becomes "lee\*cod\*e".
28+
29+
- The closest character to the 2<sup>nd</sup> star is 'e' in "le**<ins>e</ins>**\*cod\*e". s becomes "lecod\*e".
30+
31+
- The closest character to the 3<sup>rd</sup> star is 'd' in "leco**<ins>d</ins>**\*e". s becomes "lecoe".
32+
33+
There are no more stars, so we return "lecoe".
34+
35+
**Example 2:**
36+
37+
**Input:** s = "erase\*\*\*\*\*"
38+
39+
**Output:** ""
40+
41+
**Explanation:** The entire string is removed, so we return an empty string.
42+
43+
**Constraints:**
44+
45+
* <code>1 <= s.length <= 10<sup>5</sup></code>
46+
* `s` consists of lowercase English letters and stars `*`.
47+
* The operation above can be performed on `s`.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g2301_2400.s2391_minimum_amount_of_time_to_collect_garbage;
2+
3+
// #Medium #Array #String #Prefix_Sum #2022_09_02_Time_7_ms_(98.86%)_Space_92_MB_(72.81%)
4+
5+
public class Solution {
6+
public int garbageCollection(String[] garbage, int[] travel) {
7+
int cTime = 0;
8+
for (String str : garbage) {
9+
cTime += str.length();
10+
}
11+
int n = travel.length;
12+
for (int i = 1; i < n; i++) {
13+
travel[i] += travel[i - 1];
14+
}
15+
int mT = getMostTra(garbage, 'M');
16+
int pT = getMostTra(garbage, 'P');
17+
int gT = getMostTra(garbage, 'G');
18+
int m = mT <= 0 ? 0 : travel[mT - 1];
19+
int p = pT <= 0 ? 0 : travel[pT - 1];
20+
int g = gT <= 0 ? 0 : travel[gT - 1];
21+
int tTime = m + p + g;
22+
return cTime + tTime;
23+
}
24+
25+
private int getMostTra(String[] garbage, char c) {
26+
int n = garbage.length;
27+
for (int i = n - 1; i >= 0; i--) {
28+
if (garbage[i].indexOf(c) != -1) {
29+
return i;
30+
}
31+
}
32+
return -1;
33+
}
34+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2391\. Minimum Amount of Time to Collect Garbage
2+
3+
Medium
4+
5+
You are given a **0-indexed** array of strings `garbage` where `garbage[i]` represents the assortment of garbage at the <code>i<sup>th</sup></code> house. `garbage[i]` consists only of the characters `'M'`, `'P'` and `'G'` representing one unit of metal, paper and glass garbage respectively. Picking up **one** unit of any type of garbage takes `1` minute.
6+
7+
You are also given a **0-indexed** integer array `travel` where `travel[i]` is the number of minutes needed to go from house `i` to house `i + 1`.
8+
9+
There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house `0` and must visit each house **in order**; however, they do **not** need to visit every house.
10+
11+
Only **one** garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks **cannot** do anything.
12+
13+
Return _the **minimum** number of minutes needed to pick up all the garbage._
14+
15+
**Example 1:**
16+
17+
**Input:** garbage = ["G","P","GP","GG"], travel = [2,4,3]
18+
19+
**Output:** 21
20+
21+
**Explanation:**
22+
23+
The paper garbage truck:
24+
25+
1. Travels from house 0 to house 1
26+
27+
2. Collects the paper garbage at house 1
28+
29+
3. Travels from house 1 to house 2
30+
31+
4. Collects the paper garbage at house 2 Altogether, it takes 8 minutes to pick up all the paper garbage.
32+
33+
The glass garbage truck:
34+
35+
1. Collects the glass garbage at house 0
36+
37+
2. Travels from house 0 to house 1
38+
39+
3. Travels from house 1 to house 2
40+
41+
4. Collects the glass garbage at house 2
42+
43+
5. Travels from house 2 to house 3
44+
45+
6. Collects the glass garbage at house 3
46+
47+
Altogether, it takes 13 minutes to pick up all the glass garbage.
48+
49+
Since there is no metal garbage, we do not need to consider the metal garbage truck.
50+
51+
Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
52+
53+
**Example 2:**
54+
55+
**Input:** garbage = ["MMM","PGM","GP"], travel = [3,10]
56+
57+
**Output:** 37
58+
59+
**Explanation:**
60+
61+
The metal garbage truck takes 7 minutes to pick up all the metal garbage.
62+
63+
The paper garbage truck takes 15 minutes to pick up all the paper garbage.
64+
65+
The glass garbage truck takes 15 minutes to pick up all the glass garbage.
66+
67+
It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
68+
69+
**Constraints:**
70+
71+
* <code>2 <= garbage.length <= 10<sup>5</sup></code>
72+
* `garbage[i]` consists of only the letters `'M'`, `'P'`, and `'G'`.
73+
* `1 <= garbage[i].length <= 10`
74+
* `travel.length == garbage.length - 1`
75+
* `1 <= travel[i] <= 100`
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package g2301_2400.s2392_build_a_matrix_with_conditions;
2+
3+
// #Hard #Array #Matrix #Graph #Topological_Sort
4+
// #2022_09_02_Time_9_ms_(97.22%)_Space_50.2_MB_(99.69%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Queue;
12+
13+
public class Solution {
14+
// Using topological sort to solve this problem
15+
public int[][] buildMatrix(int k, int[][] rowC, int[][] colC) {
16+
// First, get the topo-sorted of row and col
17+
List<Integer> row = toposort(k, rowC);
18+
List<Integer> col = toposort(k, colC);
19+
// base case: when the length of row or col is less than k, return empty.
20+
// That is: there is a loop in established graph
21+
if (row.size() < k || col.size() < k) {
22+
return new int[0][0];
23+
}
24+
int[][] res = new int[k][k];
25+
Map<Integer, Integer> map = new HashMap<>();
26+
for (int i = 0; i < k; i++) {
27+
// we record the number corresbonding to each column:
28+
// [number, column index]
29+
map.put(col.get(i), i);
30+
}
31+
// col: 3 2 1
32+
// row: 1 3 2
33+
for (int i = 0; i < k; i++) {
34+
// For each row: we have number row.get(i). And we need to know
35+
// which column we need to assign, which is from map.get(row.get(i))
36+
// known by map.get()
37+
res[i][map.get(row.get(i))] = row.get(i);
38+
}
39+
return res;
40+
}
41+
42+
private List<Integer> toposort(int k, int[][] matrix) {
43+
// need a int[] to record the indegree of each number [1, k]
44+
int[] deg = new int[k + 1];
45+
// need a list to record the order of each number, then return this list
46+
List<Integer> res = new ArrayList<>();
47+
// need a 2-D list to be the graph, and fill the graph
48+
List<List<Integer>> graph = new ArrayList<>();
49+
for (int i = 0; i < k; i++) {
50+
graph.add(new ArrayList<>());
51+
}
52+
// need a queue to do the BFS
53+
Queue<Integer> queue = new LinkedList<>();
54+
// First, we need to establish the graph, following the given matrix
55+
for (int[] a : matrix) {
56+
int from = a[0];
57+
int to = a[1];
58+
graph.get(from - 1).add(to);
59+
deg[to]++;
60+
}
61+
// Second, after building a graph, we start the bfs,
62+
// that is, traverse the node with 0 degree
63+
for (int i = 1; i <= k; i++) {
64+
if (deg[i] == 0) {
65+
queue.offer(i);
66+
res.add(i);
67+
}
68+
}
69+
// Third, start the topo sort
70+
while (!queue.isEmpty()) {
71+
int node = queue.poll();
72+
List<Integer> list = graph.get(node - 1);
73+
for (int i : list) {
74+
if (--deg[i] == 0) {
75+
queue.offer(i);
76+
res.add(i);
77+
}
78+
}
79+
}
80+
return res;
81+
}
82+
}

0 commit comments

Comments
 (0)