Skip to content

Commit d50542e

Browse files
committed
Added tasks 3392-3405
1 parent 3f4aa70 commit d50542e

File tree

15 files changed

+1367
-151
lines changed
  • src/main/java
    • g0801_0900/s0869_reordered_power_of_2
    • g3301_3400
      • s3387_maximize_amount_after_two_days_of_conversions
      • s3392_count_subarrays_of_length_three_with_a_condition
      • s3393_count_paths_with_the_given_xor_value
      • s3394_check_if_grid_can_be_cut_into_sections
      • s3395_subsequences_with_a_unique_middle_mode_i
      • s3396_minimum_number_of_operations_to_make_elements_in_array_distinct
      • s3397_maximum_number_of_distinct_elements_after_operations
      • s3398_smallest_substring_with_identical_characters_i
      • s3399_smallest_substring_with_identical_characters_ii
    • g3401_3500
      • s3402_minimum_operations_to_make_columns_strictly_increasing
      • s3403_find_the_lexicographically_largest_string_from_the_box_i
      • s3404_count_special_subsequences
      • s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements

15 files changed

+1367
-151
lines changed

README.md

Lines changed: 117 additions & 105 deletions
Large diffs are not rendered by default.

src/main/java/g0801_0900/s0869_reordered_power_of_2/readme.md

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,6 @@ public class Solution {
4343
return false;
4444
}
4545

46-
private int noOfDigits(int n) {
47-
if (n == 0) {
48-
return 1;
49-
}
50-
return (int) Math.log10(Math.abs(n)) + 1;
51-
}
52-
53-
private boolean check(int num1, int num2) {
54-
int[] num = new int[10];
55-
while (num1 > 0) {
56-
num[num1 % 10]++;
57-
num1 /= 10;
58-
}
59-
while (num2 > 0) {
60-
num[num2 % 10]--;
61-
num2 /= 10;
62-
}
63-
for (int i = 0; i < 10; i++) {
64-
if (num[i] != 0) {
65-
return false;
66-
}
67-
}
68-
return true;
69-
}
70-
7146
private int[] countDigits(int num) {
7247
int[] digitCount = new int[10];
7348
while (num > 0) {

src/main/java/g3301_3400/s3387_maximize_amount_after_two_days_of_conversions/readme.md

Lines changed: 85 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,93 @@ In this example, there is no need to make any conversions on either day.
7575
## Solution
7676

7777
```java
78-
import java.util.ArrayDeque;
79-
80-
public class Example {
81-
public static void main(String[] args) {
82-
ArrayDeque<String> animals = new ArrayDeque<>();
83-
84-
// "unshift" - Add an element at the beginning of the list
85-
animals.addFirst("Cat"); // Adds "Cat" at the front
86-
System.out.println("After unshift: " + animals);
87-
88-
// "push" - Add an element to the end of the list
89-
animals.addLast("Dog"); // Adds "Dog" at the end
90-
System.out.println("After push: " + animals);
78+
import java.util.ArrayList;
79+
import java.util.HashMap;
80+
import java.util.HashSet;
81+
import java.util.List;
82+
import java.util.Map;
83+
import java.util.Set;
84+
85+
@SuppressWarnings("java:S3824")
86+
public class Solution {
87+
private double res;
88+
private Map<String, List<Pair>> map1;
89+
private Map<String, List<Pair>> map2;
90+
91+
private static class Pair {
92+
String tarcurr;
93+
double rate;
94+
95+
Pair(String t, double r) {
96+
tarcurr = t;
97+
rate = r;
98+
}
99+
}
91100

92-
// "shift" - Remove the first element from the list
93-
String shiftedAnimal = animals.removeFirst(); // Removes "Cat"
94-
System.out.println("After shift (removed): " + shiftedAnimal);
95-
System.out.println("List after shift: " + animals);
101+
private void solve(
102+
String currCurrency, double value, String targetCurrency, int day, Set<String> used) {
103+
if (currCurrency.equals(targetCurrency)) {
104+
res = Math.max(res, value);
105+
if (day == 2) {
106+
return;
107+
}
108+
}
109+
List<Pair> list;
110+
if (day == 1) {
111+
list = map1.getOrDefault(currCurrency, new ArrayList<>());
112+
} else {
113+
list = map2.getOrDefault(currCurrency, new ArrayList<>());
114+
}
115+
for (Pair p : list) {
116+
if (used.add(p.tarcurr)) {
117+
solve(p.tarcurr, value * p.rate, targetCurrency, day, used);
118+
used.remove(p.tarcurr);
119+
}
120+
}
121+
if (day == 1) {
122+
solve(currCurrency, value, targetCurrency, day + 1, new HashSet<>());
123+
}
124+
}
96125

97-
// "pop" - Remove the last element from the list
98-
String poppedAnimal = animals.removeLast(); // Removes "Dog"
99-
System.out.println("After pop (removed): " + poppedAnimal);
100-
System.out.println("List after pop: " + animals);
126+
public double maxAmount(
127+
String initialCurrency,
128+
List<List<String>> pairs1,
129+
double[] rates1,
130+
List<List<String>> pairs2,
131+
double[] rates2) {
132+
map1 = new HashMap<>();
133+
map2 = new HashMap<>();
134+
int size = pairs1.size();
135+
for (int i = 0; i < size; i++) {
136+
List<String> curr = pairs1.get(i);
137+
String c1 = curr.get(0);
138+
String c2 = curr.get(1);
139+
if (!map1.containsKey(c1)) {
140+
map1.put(c1, new ArrayList<>());
141+
}
142+
map1.get(c1).add(new Pair(c2, rates1[i]));
143+
if (!map1.containsKey(c2)) {
144+
map1.put(c2, new ArrayList<>());
145+
}
146+
map1.get(c2).add(new Pair(c1, 1d / rates1[i]));
147+
}
148+
size = pairs2.size();
149+
for (int i = 0; i < size; i++) {
150+
List<String> curr = pairs2.get(i);
151+
String c1 = curr.get(0);
152+
String c2 = curr.get(1);
153+
if (!map2.containsKey(c1)) {
154+
map2.put(c1, new ArrayList<>());
155+
}
156+
map2.get(c1).add(new Pair(c2, rates2[i]));
157+
if (!map2.containsKey(c2)) {
158+
map2.put(c2, new ArrayList<>());
159+
}
160+
map2.get(c2).add(new Pair(c1, 1d / rates2[i]));
161+
}
162+
res = 1d;
163+
solve(initialCurrency, 1d, initialCurrency, 1, new HashSet<>());
164+
return res;
101165
}
102166
}
103167
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3392\. Count Subarrays of Length Three With a Condition
5+
6+
Easy
7+
8+
Given an integer array `nums`, return the number of subarrays of length 3 such that the sum of the first and third numbers equals _exactly_ half of the second number.
9+
10+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,2,1,4,1]
15+
16+
**Output:** 1
17+
18+
**Explanation:**
19+
20+
Only the subarray `[1,4,1]` contains exactly 3 elements where the sum of the first and third numbers equals half the middle number.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [1,1,1]
25+
26+
**Output:** 0
27+
28+
**Explanation:**
29+
30+
`[1,1,1]` is the only subarray of length 3. However, its first and third numbers do not add to half the middle number.
31+
32+
**Constraints:**
33+
34+
* `3 <= nums.length <= 100`
35+
* `-100 <= nums[i] <= 100`
36+
37+
## Solution
38+
39+
```java
40+
public class Solution {
41+
public int countSubarrays(int[] nums) {
42+
int window = 3;
43+
int cnt = 0;
44+
for (int i = 0; i <= nums.length - window; i++) {
45+
float first = nums[i];
46+
float second = nums[i + 1];
47+
float third = nums[i + 2];
48+
if (second / 2 == first + third) {
49+
cnt++;
50+
}
51+
}
52+
return cnt;
53+
}
54+
}
55+
```
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3393\. Count Paths With the Given XOR Value
5+
6+
Medium
7+
8+
You are given a 2D integer array `grid` with size `m x n`. You are also given an integer `k`.
9+
10+
Your task is to calculate the number of paths you can take from the top-left cell `(0, 0)` to the bottom-right cell `(m - 1, n - 1)` satisfying the following **constraints**:
11+
12+
* You can either move to the right or down. Formally, from the cell `(i, j)` you may move to the cell `(i, j + 1)` or to the cell `(i + 1, j)` if the target cell _exists_.
13+
* The `XOR` of all the numbers on the path must be **equal** to `k`.
14+
15+
Return the total number of such paths.
16+
17+
Since the answer can be very large, return the result **modulo** <code>10<sup>9</sup> + 7</code>.
18+
19+
**Example 1:**
20+
21+
**Input:** grid = \[\[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
The 3 paths are:
28+
29+
* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)`
30+
* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)`
31+
* `(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)`
32+
33+
**Example 2:**
34+
35+
**Input:** grid = \[\[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2
36+
37+
**Output:** 5
38+
39+
**Explanation:**
40+
41+
The 5 paths are:
42+
43+
* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)`
44+
* `(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)`
45+
* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)`
46+
* `(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)`
47+
* `(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)`
48+
49+
**Example 3:**
50+
51+
**Input:** grid = \[\[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10
52+
53+
**Output:** 0
54+
55+
**Constraints:**
56+
57+
* `1 <= m == grid.length <= 300`
58+
* `1 <= n == grid[r].length <= 300`
59+
* `0 <= grid[r][c] < 16`
60+
* `0 <= k < 16`
61+
62+
## Solution
63+
64+
```java
65+
import java.util.Arrays;
66+
67+
public class Solution {
68+
private static final int MOD = (int) (1e9 + 7);
69+
private int m = -1;
70+
private int n = -1;
71+
private int[][][] dp;
72+
73+
public int countPathsWithXorValue(int[][] grid, int k) {
74+
m = grid.length;
75+
n = grid[0].length;
76+
dp = new int[m][n][16];
77+
for (int[][] a : dp) {
78+
for (int[] b : a) {
79+
Arrays.fill(b, -1);
80+
}
81+
}
82+
return dfs(grid, 0, k, 0, 0);
83+
}
84+
85+
private int dfs(int[][] grid, int xorVal, int k, int i, int j) {
86+
if (i < 0 || j < 0 || j >= n || i >= m) {
87+
return 0;
88+
}
89+
xorVal ^= grid[i][j];
90+
if (dp[i][j][xorVal] != -1) {
91+
return dp[i][j][xorVal];
92+
}
93+
if (i == m - 1 && j == n - 1 && xorVal == k) {
94+
return 1;
95+
}
96+
int down = dfs(grid, xorVal, k, i + 1, j);
97+
int right = dfs(grid, xorVal, k, i, j + 1);
98+
dp[i][j][xorVal] = (down + right) % MOD;
99+
return dp[i][j][xorVal];
100+
}
101+
}
102+
```

0 commit comments

Comments
 (0)