Skip to content

Commit 501002a

Browse files
authored
Added tasks 1734, 1735, 1736, 1737, 1738.
1 parent 05c55be commit 501002a

File tree

15 files changed

+505
-0
lines changed

15 files changed

+505
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g1701_1800.s1734_decode_xored_permutation;
2+
3+
// #Medium #Array #Bit_Manipulation #2022_04_29_Time_6_ms_(34.52%)_Space_140.9_MB_(66.67%)
4+
5+
public class Solution {
6+
public int[] decode(int[] encoded) {
7+
int[] decoded = new int[encoded.length + 1];
8+
for (int i = 1; i < encoded.length; i = i + 2) {
9+
decoded[0] = decoded[0] ^ encoded[i];
10+
decoded[0] = decoded[0] ^ i;
11+
decoded[0] = decoded[0] ^ i + 1;
12+
}
13+
decoded[0] = decoded[0] ^ decoded.length;
14+
for (int i = 1; i < decoded.length; i++) {
15+
decoded[i] = decoded[i - 1] ^ encoded[i - 1];
16+
}
17+
return decoded;
18+
}
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
1734\. Decode XORed Permutation
2+
3+
Medium
4+
5+
There is an integer array `perm` that is a permutation of the first `n` positive integers, where `n` is always **odd**.
6+
7+
It was encoded into another integer array `encoded` of length `n - 1`, such that `encoded[i] = perm[i] XOR perm[i + 1]`. For example, if `perm = [1,3,2]`, then `encoded = [2,1]`.
8+
9+
Given the `encoded` array, return _the original array_ `perm`. It is guaranteed that the answer exists and is unique.
10+
11+
**Example 1:**
12+
13+
**Input:** encoded = [3,1]
14+
15+
**Output:** [1,2,3]
16+
17+
**Explanation:** If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]
18+
19+
**Example 2:**
20+
21+
**Input:** encoded = [6,5,4,6]
22+
23+
**Output:** [2,4,1,5,3]
24+
25+
**Constraints:**
26+
27+
* <code>3 <= n < 10<sup>5</sup></code>
28+
* `n` is odd.
29+
* `encoded.length == n - 1`
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g1701_1800.s1735_count_ways_to_make_array_with_product;
2+
3+
// #Hard #Array #Dynamic_Programming #Math #2022_04_29_Time_189_ms_(43.75%)_Space_50.3_MB_(56.25%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
private static final int MOD = 1_000_000_007;
10+
private long[][] tri;
11+
private List<Integer> primes;
12+
13+
public int[] waysToFillArray(int[][] queries) {
14+
int len = queries.length;
15+
int[] res = new int[len];
16+
primes = getPrimes(100);
17+
tri = getTri(10015, 15);
18+
for (int i = 0; i < len; i++) {
19+
res[i] = calculate(queries[i][0], queries[i][1]);
20+
}
21+
return res;
22+
}
23+
24+
private List<Integer> getPrimes(int limit) {
25+
boolean[] notPrime = new boolean[limit + 1];
26+
List<Integer> res = new ArrayList<>();
27+
for (int i = 2; i <= limit; i++) {
28+
if (!notPrime[i]) {
29+
res.add(i);
30+
for (int j = i * i; j <= limit; j += i) {
31+
notPrime[j] = true;
32+
}
33+
}
34+
}
35+
return res;
36+
}
37+
38+
private long[][] getTri(int m, int n) {
39+
long[][] res = new long[m + 1][n + 1];
40+
for (int i = 0; i <= m; i++) {
41+
res[i][0] = 1;
42+
for (int j = 1; j <= Math.min(n, i); j++) {
43+
res[i][j] = (res[i - 1][j - 1] + res[i - 1][j]) % MOD;
44+
}
45+
}
46+
return res;
47+
}
48+
49+
private int calculate(int n, int target) {
50+
long res = 1;
51+
for (int prime : primes) {
52+
if (prime > target) {
53+
break;
54+
}
55+
int cnt = 0;
56+
while (target % prime == 0) {
57+
cnt++;
58+
target /= prime;
59+
}
60+
res = (res * tri[cnt + n - 1][cnt]) % MOD;
61+
}
62+
return target > 1 ? (int) (res * n % MOD) : (int) res;
63+
}
64+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
1735\. Count Ways to Make Array With Product
2+
3+
Hard
4+
5+
You are given a 2D integer array, `queries`. For each `queries[i]`, where <code>queries[i] = [n<sub>i</sub>, k<sub>i</sub>]</code>, find the number of different ways you can place positive integers into an array of size <code>n<sub>i</sub></code> such that the product of the integers is <code>k<sub>i</sub></code>. As the number of ways may be too large, the answer to the <code>i<sup>th</sup></code> query is the number of ways **modulo** <code>10<sup>9</sup> + 7</code>.
6+
7+
Return _an integer array_ `answer` _where_ `answer.length == queries.length`_, and_ `answer[i]` _is the answer to the_ <code>i<sup>th</sup></code> _query._
8+
9+
**Example 1:**
10+
11+
**Input:** queries = [[2,6],[5,1],[73,660]]
12+
13+
**Output:** [4,1,50734910]
14+
15+
**Explanation:** Each query is independent. [2,6]: There are 4 ways to fill an array of size 2 that multiply to 6: [1,6], [2,3], [3,2], [6,1]. [5,1]: There is 1 way to fill an array of size 5 that multiply to 1: [1,1,1,1,1]. [73,660]: There are 1050734917 ways to fill an array of size 73 that multiply to 660. 1050734917 modulo 10<sup>9</sup> + 7 = 50734910.
16+
17+
**Example 2:**
18+
19+
**Input:** queries = [[1,1],[2,2],[3,3],[4,4],[5,5]]
20+
21+
**Output:** [1,2,3,10,5]
22+
23+
**Constraints:**
24+
25+
* <code>1 <= queries.length <= 10<sup>4</sup></code>
26+
* <code>1 <= n<sub>i</sub>, k<sub>i</sub> <= 10<sup>4</sup></code>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g1701_1800.s1736_latest_time_by_replacing_hidden_digits;
2+
3+
// #Easy #String #Greedy #2022_04_29_Time_3_ms_(35.96%)_Space_42.5_MB_(22.81%)
4+
5+
public class Solution {
6+
public String maximumTime(String time) {
7+
StringBuilder sb = new StringBuilder();
8+
String[] strs = time.split(":");
9+
String hour = strs[0];
10+
String min = strs[1];
11+
if (hour.charAt(0) == '?') {
12+
if (hour.charAt(1) == '?') {
13+
sb.append("23");
14+
} else if (hour.charAt(1) > '3') {
15+
sb.append("1");
16+
sb.append(hour.charAt(1));
17+
} else {
18+
sb.append("2");
19+
sb.append(hour.charAt(1));
20+
}
21+
} else if (hour.charAt(0) == '0' || hour.charAt(0) == '1') {
22+
if (hour.charAt(1) == '?') {
23+
sb.append(hour.charAt(0));
24+
sb.append("9");
25+
} else {
26+
sb.append(hour);
27+
}
28+
} else if (hour.charAt(0) == '2') {
29+
if (hour.charAt(1) == '?') {
30+
sb.append("23");
31+
} else {
32+
sb.append(hour);
33+
}
34+
}
35+
sb.append(":");
36+
if (min.charAt(0) == '?') {
37+
if (min.charAt(1) == '?') {
38+
sb.append("59");
39+
} else {
40+
sb.append("5");
41+
sb.append(min.charAt(1));
42+
}
43+
return sb.toString();
44+
}
45+
sb.append(min.charAt(0));
46+
if (min.charAt(1) == '?') {
47+
sb.append("9");
48+
} else {
49+
sb.append(min.charAt(1));
50+
}
51+
return sb.toString();
52+
}
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1736\. Latest Time by Replacing Hidden Digits
2+
3+
Easy
4+
5+
You are given a string `time` in the form of `hh:mm`, where some of the digits in the string are hidden (represented by `?`).
6+
7+
The valid times are those inclusively between `00:00` and `23:59`.
8+
9+
Return _the latest valid time you can get from_ `time` _by replacing the hidden_ _digits_.
10+
11+
**Example 1:**
12+
13+
**Input:** time = "2?:?0"
14+
15+
**Output:** "23:50"
16+
17+
**Explanation:** The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.
18+
19+
**Example 2:**
20+
21+
**Input:** time = "0?:3?"
22+
23+
**Output:** "09:39"
24+
25+
**Example 3:**
26+
27+
**Input:** time = "1?:22"
28+
29+
**Output:** "19:22"
30+
31+
**Constraints:**
32+
33+
* `time` is in the format `hh:mm`.
34+
* It is guaranteed that you can produce a valid time from the given string.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1701_1800.s1737_change_minimum_characters_to_satisfy_one_of_three_conditions;
2+
3+
// #Medium #String #Hash_Table #Prefix_Sum #Counting
4+
// #2022_04_29_Time_8_ms_(87.70%)_Space_54.4_MB_(51.42%)
5+
6+
public class Solution {
7+
public int minCharacters(String s1, String s2) {
8+
int[] a = new int[26];
9+
int[] b = new int[26];
10+
int l1 = s1.length();
11+
int l2 = s2.length();
12+
for (char i : s1.toCharArray()) {
13+
a[i - 'a']++;
14+
}
15+
for (char i : s2.toCharArray()) {
16+
b[i - 'a']++;
17+
}
18+
int min = Integer.MAX_VALUE;
19+
int t1 = 0;
20+
int t2 = 0;
21+
int max = -1;
22+
for (int i = 0; i < 25; i++) {
23+
t1 += a[i];
24+
t2 += b[i];
25+
min = Math.min(min, Math.min(t1 + l2 - t2, t2 + l1 - t1));
26+
max = Math.max(max, a[i] + b[i]);
27+
}
28+
max = Math.max(max, a[25] + b[25]);
29+
return Math.min(min, l1 + l2 - max);
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1737\. Change Minimum Characters to Satisfy One of Three Conditions
2+
3+
Medium
4+
5+
You are given two strings `a` and `b` that consist of lowercase letters. In one operation, you can change any character in `a` or `b` to **any lowercase letter**.
6+
7+
Your goal is to satisfy **one** of the following three conditions:
8+
9+
* **Every** letter in `a` is **strictly less** than **every** letter in `b` in the alphabet.
10+
* **Every** letter in `b` is **strictly less** than **every** letter in `a` in the alphabet.
11+
* **Both** `a` and `b` consist of **only one** distinct letter.
12+
13+
Return _the **minimum** number of operations needed to achieve your goal._
14+
15+
**Example 1:**
16+
17+
**Input:** a = "aba", b = "caa"
18+
19+
**Output:** 2
20+
21+
**Explanation:** Consider the best way to make each condition true:
22+
23+
1) Change b to "ccc" in 2 operations, then every letter in a is less than every letter in b.
24+
25+
2) Change a to "bbb" and b to "aaa" in 3 operations, then every letter in b is less than every letter in a.
26+
27+
3) Change a to "aaa" and b to "aaa" in 2 operations, then a and b consist of one distinct letter. The best way was done in 2 operations (either condition 1 or condition 3).
28+
29+
**Example 2:**
30+
31+
**Input:** a = "dabadd", b = "cda"
32+
33+
**Output:** 3
34+
35+
**Explanation:** The best way is to make condition 1 true by changing b to "eee".
36+
37+
**Constraints:**
38+
39+
* <code>1 <= a.length, b.length <= 10<sup>5</sup></code>
40+
* `a` and `b` consist only of lowercase letters.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package g1701_1800.s1738_find_kth_largest_xor_coordinate_value;
2+
3+
// #Medium #Array #Matrix #Bit_Manipulation #Heap_Priority_Queue #Prefix_Sum #Divide_and_Conquer
4+
// #Quickselect #2022_04_29_Time_40_ms_(97.08%)_Space_208.6_MB_(58.39%)
5+
6+
public class Solution {
7+
public int kthLargestValue(int[][] matrix, int k) {
8+
int t = 0;
9+
int rows = matrix.length;
10+
int cols = matrix[0].length;
11+
int[][] prefixXor = new int[rows + 1][cols + 1];
12+
int[] array = new int[rows * cols];
13+
14+
for (int r = 1; r <= rows; r++) {
15+
for (int c = 1; c <= cols; c++) {
16+
prefixXor[r][c] =
17+
matrix[r - 1][c - 1]
18+
^ prefixXor[r - 1][c]
19+
^ prefixXor[r][c - 1]
20+
^ prefixXor[r - 1][c - 1];
21+
array[t++] = prefixXor[r][c];
22+
}
23+
}
24+
25+
int target = array.length - k;
26+
quickSelect(array, 0, array.length - 1, target);
27+
return array[target];
28+
}
29+
30+
private int quickSelect(int[] array, int left, int right, int target) {
31+
if (left == right) {
32+
return left;
33+
}
34+
35+
int pivot = array[right];
36+
int j = left;
37+
int k = right - 1;
38+
while (j <= k) {
39+
if (array[j] < pivot) {
40+
j++;
41+
} else if (array[k] > pivot) {
42+
k--;
43+
} else {
44+
swap(array, j++, k--);
45+
}
46+
}
47+
swap(array, j, right);
48+
if (j == target) {
49+
return j;
50+
} else if (j > target) {
51+
return quickSelect(array, left, j - 1, target);
52+
} else {
53+
return quickSelect(array, j + 1, right, target);
54+
}
55+
}
56+
57+
private void swap(int[] array, int i, int j) {
58+
int tmp = array[i];
59+
array[i] = array[j];
60+
array[j] = tmp;
61+
}
62+
}

0 commit comments

Comments
 (0)