Skip to content

Commit ca9adae

Browse files
authored
Added tasks 2343, 2344.
1 parent 6083f5e commit ca9adae

File tree

7 files changed

+248
-0
lines changed

7 files changed

+248
-0
lines changed

README.md

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

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2344 |[Minimum Deletions to Make Array Divisible](src/main/java/g2301_2400/s2344_minimum_deletions_to_make_array_divisible/Solution.java)| Hard || 13 | 88.89
1852+
| 2343 |[Query Kth Smallest Trimmed Number](src/main/java/g2301_2400/s2343_query_kth_smallest_trimmed_number/Solution.java)| Medium || 61 | 75.00
18511853
| 2342 |[Max Sum of a Pair With Equal Sum of Digits](src/main/java/g2301_2400/s2342_max_sum_of_a_pair_with_equal_sum_of_digits/Solution.java)| Medium || 99 | 100.00
18521854
| 2341 |[Maximum Number of Pairs in Array](src/main/java/g2301_2400/s2341_maximum_number_of_pairs_in_array/Solution.java)| Easy || 2 | 80.00
18531855
| 2338 |[Count the Number of Ideal Arrays](src/main/java/g2301_2400/s2338_count_the_number_of_ideal_arrays/Solution.java)| Hard | Math, Dynamic_Programming, Combinatorics, Number_Theory | 21 | 99.04
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g2301_2400.s2343_query_kth_smallest_trimmed_number;
2+
3+
// #Medium #2022_07_19_Time_52_ms_(75.00%)_Space_58.8_MB_(37.50%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public int[] smallestTrimmedNumbers(String[] nums, int[][] queries) {
11+
int numberOfDigits = nums[0].length();
12+
int placeValue = numberOfDigits;
13+
List<int[]> list = new ArrayList<>(numberOfDigits);
14+
while (--placeValue >= 0) {
15+
countSort(nums, placeValue, numberOfDigits, list);
16+
}
17+
int[] op = new int[queries.length];
18+
int i = 0;
19+
for (int[] query : queries) {
20+
int listIndex = query[1] - 1;
21+
int place = query[0] - 1;
22+
op[i++] = list.get(listIndex)[place];
23+
}
24+
return op;
25+
}
26+
27+
private void countSort(String[] arr, int exp, int numberOfDigits, List<int[]> list) {
28+
int n = arr.length;
29+
String[] output = new String[n];
30+
int i;
31+
int[] count = new int[10];
32+
Arrays.fill(count, 0);
33+
// Store count of occurrences in count[]
34+
for (i = 0; i < n; i++) {
35+
int digit = arr[i].charAt(exp) - '0';
36+
count[digit]++;
37+
}
38+
for (i = 1; i < 10; i++) {
39+
count[i] += count[i - 1];
40+
}
41+
// Build the output array
42+
int[] op = new int[n];
43+
for (i = n - 1; i >= 0; i--) {
44+
int digit = arr[i].charAt(exp) - '0';
45+
int place = count[digit] - 1;
46+
output[place] = arr[i];
47+
if (exp == numberOfDigits - 1) {
48+
op[place] = i;
49+
} else {
50+
op[place] = list.get(list.size() - 1)[i];
51+
}
52+
count[digit]--;
53+
}
54+
list.add(op);
55+
System.arraycopy(output, 0, arr, 0, n);
56+
}
57+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2343\. Query Kth Smallest Trimmed Number
2+
3+
Medium
4+
5+
You are given a **0-indexed** array of strings `nums`, where each string is of **equal length** and consists of only digits.
6+
7+
You are also given a **0-indexed** 2D integer array `queries` where <code>queries[i] = [k<sub>i</sub>, trim<sub>i</sub>]</code>. For each `queries[i]`, you need to:
8+
9+
* **Trim** each number in `nums` to its **rightmost** <code>trim<sub>i</sub></code> digits.
10+
* Determine the **index** of the <code>k<sub>i</sub><sup>th</sup></code> smallest trimmed number in `nums`. If two trimmed numbers are equal, the number with the **lower** index is considered to be smaller.
11+
* Reset each number in `nums` to its original length.
12+
13+
Return _an array_ `answer` _of the same length as_ `queries`, _where_ `answer[i]` _is the answer to the_ <code>i<sup>th</sup></code> _query._
14+
15+
**Note**:
16+
17+
* To trim to the rightmost `x` digits means to keep removing the leftmost digit, until only `x` digits remain.
18+
* Strings in `nums` may contain leading zeros.
19+
20+
**Example 1:**
21+
22+
**Input:** nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]]
23+
24+
**Output:** [2,2,1,0]
25+
26+
**Explanation:**
27+
28+
1. After trimming to the last digit, nums = ["2","3","1","4"]. The smallest number is 1 at index 2.
29+
30+
2. Trimmed to the last 3 digits, nums is unchanged. The 2<sup>nd</sup> smallest number is 251 at index 2.
31+
32+
3. Trimmed to the last 2 digits, nums = ["02","73","51","14"]. The 4<sup>th</sup> smallest number is 73.
33+
34+
4. Trimmed to the last 2 digits, the smallest number is 2 at index 0.
35+
36+
Note that the trimmed number "02" is evaluated as 2.
37+
38+
**Example 2:**
39+
40+
**Input:** nums = ["24","37","96","04"], queries = [[2,1],[2,2]]
41+
42+
**Output:** [3,0]
43+
44+
**Explanation:**
45+
46+
1. Trimmed to the last digit, nums = ["4","7","6","4"]. The 2<sup>nd</sup> smallest number is 4 at index 3.
47+
48+
There are two occurrences of 4, but the one at index 0 is considered smaller than the one at index 3.
49+
50+
2. Trimmed to the last 2 digits, nums is unchanged. The 2<sup>nd</sup> smallest number is 24.
51+
52+
**Constraints:**
53+
54+
* `1 <= nums.length <= 100`
55+
* `1 <= nums[i].length <= 100`
56+
* `nums[i]` consists of only digits.
57+
* All `nums[i].length` are **equal**.
58+
* `1 <= queries.length <= 100`
59+
* `queries[i].length == 2`
60+
* <code>1 <= k<sub>i</sub> <= nums.length</code>
61+
* <code>1 <= trim<sub>i</sub> <= nums[i].length</code>
62+
63+
**Follow up:** Could you use the **Radix Sort Algorithm** to solve this problem? What will be the complexity of that solution?
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g2301_2400.s2344_minimum_deletions_to_make_array_divisible;
2+
3+
// #Hard #2022_07_19_Time_13_ms_(88.89%)_Space_77.2_MB_(77.78%)
4+
5+
public class Solution {
6+
public int minOperations(int[] nums, int[] numsDivide) {
7+
int g = numsDivide[0];
8+
for (int i : numsDivide) {
9+
g = gcd(g, i);
10+
}
11+
int minOp = 0;
12+
int smallest = Integer.MAX_VALUE;
13+
for (int num : nums) {
14+
if (g % num == 0) {
15+
smallest = Math.min(smallest, num);
16+
}
17+
}
18+
for (int num : nums) {
19+
if (num < smallest) {
20+
++minOp;
21+
}
22+
}
23+
return smallest == Integer.MAX_VALUE ? -1 : minOp;
24+
}
25+
26+
private int gcd(int a, int b) {
27+
while (b > 0) {
28+
int tmp = a;
29+
a = b;
30+
b = tmp % b;
31+
}
32+
return a;
33+
}
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2344\. Minimum Deletions to Make Array Divisible
2+
3+
Hard
4+
5+
You are given two positive integer arrays `nums` and `numsDivide`. You can delete any number of elements from `nums`.
6+
7+
Return _the **minimum** number of deletions such that the **smallest** element in_ `nums` _**divides** all the elements of_ `numsDivide`. If this is not possible, return `-1`.
8+
9+
Note that an integer `x` divides `y` if `y % x == 0`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
The smallest element in [2,3,2,4,3] is 2, which does not divide all the elements of numsDivide.
20+
21+
We use 2 deletions to delete the elements in nums that are equal to 2 which makes nums = [3,4,3].
22+
23+
The smallest element in [3,4,3] is 3, which divides all the elements of numsDivide.
24+
25+
It can be shown that 2 is the minimum number of deletions needed.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [4,3,6], numsDivide = [8,2,6,10]
30+
31+
**Output:** -1
32+
33+
**Explanation:**
34+
35+
We want the smallest element in nums to divide all the elements of numsDivide.
36+
37+
There is no way to delete elements from nums to allow this.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length, numsDivide.length <= 10<sup>5</sup></code>
42+
* <code>1 <= nums[i], numsDivide[i] <= 10<sup>9</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2301_2400.s2343_query_kth_smallest_trimmed_number;
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 smallestTrimmedNumbers() {
11+
assertThat(
12+
new Solution()
13+
.smallestTrimmedNumbers(
14+
new String[] {"102", "473", "251", "814"},
15+
new int[][] {{1, 1}, {2, 3}, {4, 2}, {1, 2}}),
16+
equalTo(new int[] {2, 2, 1, 0}));
17+
}
18+
19+
@Test
20+
void smallestTrimmedNumbers2() {
21+
assertThat(
22+
new Solution()
23+
.smallestTrimmedNumbers(
24+
new String[] {"24", "37", "96", "04"},
25+
new int[][] {{2, 1}, {2, 2}}),
26+
equalTo(new int[] {3, 0}));
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2301_2400.s2344_minimum_deletions_to_make_array_divisible;
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 minOperations() {
11+
assertThat(
12+
new Solution().minOperations(new int[] {2, 3, 2, 4, 3}, new int[] {9, 6, 9, 3, 15}),
13+
equalTo(2));
14+
}
15+
16+
@Test
17+
void minOperations2() {
18+
assertThat(
19+
new Solution().minOperations(new int[] {4, 3, 6}, new int[] {8, 2, 6, 10}),
20+
equalTo(-1));
21+
}
22+
}

0 commit comments

Comments
 (0)