Skip to content

Commit 3134403

Browse files
authored
Added tasks 2825-2842
1 parent f789900 commit 3134403

File tree

17 files changed

+1244
-1
lines changed
  • src/main/java
    • g0301_0400/s0350_intersection_of_two_arrays_ii
    • g2801_2900
      • s2825_make_string_a_subsequence_using_cyclic_increments
      • s2826_sorting_three_groups
      • s2827_number_of_beautiful_integers_in_the_range
      • s2828_check_if_a_string_is_an_acronym_of_words
      • s2829_determine_the_minimum_sum_of_a_k_avoiding_array
      • s2830_maximize_the_profit_as_the_salesman
      • s2831_find_the_longest_equal_subarray
      • s2833_furthest_point_from_origin
      • s2834_find_the_minimum_possible_sum_of_a_beautiful_array
      • s2835_minimum_operations_to_form_subsequence_with_target_sum
      • s2836_maximize_value_of_function_in_a_ball_passing_game
      • s2839_check_if_strings_can_be_made_equal_with_operations_i
      • s2840_check_if_strings_can_be_made_equal_with_operations_ii
      • s2841_maximum_sum_of_almost_unique_subarray
      • s2842_count_k_subsequences_of_a_string_with_maximum_beauty

17 files changed

+1244
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,21 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 2842 |[Count K-Subsequences of a String With Maximum Beauty](src/main/java/g2801_2900/s2842_count_k_subsequences_of_a_string_with_maximum_beauty)| Hard | String, Hash_Table, Math, Greedy, Combinatorics | 5 | 80.91
1820+
| 2841 |[Maximum Sum of Almost Unique Subarray](src/main/java/g2801_2900/s2841_maximum_sum_of_almost_unique_subarray)| Medium | Array, Hash_Table, Sliding_Window | 18 | 97.37
1821+
| 2840 |[Check if Strings Can be Made Equal With Operations II](src/main/java/g2801_2900/s2840_check_if_strings_can_be_made_equal_with_operations_ii)| Medium | String, Hash_Table, Sorting | 4 | 100.00
1822+
| 2839 |[Check if Strings Can be Made Equal With Operations I](src/main/java/g2801_2900/s2839_check_if_strings_can_be_made_equal_with_operations_i)| Easy | String | 1 | 99.09
1823+
| 2836 |[Maximize Value of Function in a Ball Passing Game](src/main/java/g2801_2900/s2836_maximize_value_of_function_in_a_ball_passing_game)| Hard | Array, Dynamic_Programming, Bit_Manipulation | 235 | 45.00
1824+
| 2835 |[Minimum Operations to Form Subsequence With Target Sum](src/main/java/g2801_2900/s2835_minimum_operations_to_form_subsequence_with_target_sum)| Hard | Array, Greedy, Bit_Manipulation | 2 | 94.66
1825+
| 2834 |[Find the Minimum Possible Sum of a Beautiful Array](src/main/java/g2801_2900/s2834_find_the_minimum_possible_sum_of_a_beautiful_array)| Medium | Math, Greedy | 0 | 100.00
1826+
| 2833 |[Furthest Point From Origin](src/main/java/g2801_2900/s2833_furthest_point_from_origin)| Easy | Array, Counting | 1 | 100.00
1827+
| 2831 |[Find the Longest Equal Subarray](src/main/java/g2801_2900/s2831_find_the_longest_equal_subarray)| Medium | Array, Hash_Table, Binary_Search, Sliding_Window | 15 | 96.81
1828+
| 2830 |[Maximize the Profit as the Salesman](src/main/java/g2801_2900/s2830_maximize_the_profit_as_the_salesman)| Medium | Array, Dynamic_Programming, Sorting, Binary_Search | 36 | 80.00
1829+
| 2829 |[Determine the Minimum Sum of a k-avoiding Array](src/main/java/g2801_2900/s2829_determine_the_minimum_sum_of_a_k_avoiding_array)| Medium | Math, Greedy | 1 | 100.00
1830+
| 2828 |[Check if a String Is an Acronym of Words](src/main/java/g2801_2900/s2828_check_if_a_string_is_an_acronym_of_words)| Easy | Array, String | 1 | 100.00
1831+
| 2827 |[Number of Beautiful Integers in the Range](src/main/java/g2801_2900/s2827_number_of_beautiful_integers_in_the_range)| Hard | Dynamic_Programming, Math | 7 | 96.77
1832+
| 2826 |[Sorting Three Groups](src/main/java/g2801_2900/s2826_sorting_three_groups)| Medium | Array, Dynamic_Programming | 4 | 100.00
1833+
| 2825 |[Make String a Subsequence Using Cyclic Increments](src/main/java/g2801_2900/s2825_make_string_a_subsequence_using_cyclic_increments)| Medium | String, Two_Pointers | 5 | 99.69
18191834
| 2824 |[Count Pairs Whose Sum is Less than Target](src/main/java/g2801_2900/s2824_count_pairs_whose_sum_is_less_than_target)| Easy | Array, Sorting, Two_Pointers | 2 | 98.16
18201835
| 2818 |[Apply Operations to Maximize Score](src/main/java/g2801_2900/s2818_apply_operations_to_maximize_score)| Hard | Array, Math, Greedy, Stack, Monotonic_Stack, Number_Theory | 94 | 100.00
18211836
| 2817 |[Minimum Absolute Difference Between Elements With Constraint](src/main/java/g2801_2900/s2817_minimum_absolute_difference_between_elements_with_constraint)| Medium | Array, Binary_Search, Ordered_Set | 104 | 96.69

src/main/java/g0301_0400/s0350_intersection_of_two_arrays_ii/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public class Solution {
5959
// Check if nums2 value is less then nums1 value;
6060
// Increment "j"
6161
j++;
62-
6362
} else {
6463
// Check if nums1 value is equals to nums2 value;
6564
// Dump into nums1 and increment k, increment i & increment j as well;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
## 2825\. Make String a Subsequence Using Cyclic Increments
5+
6+
Medium
7+
8+
You are given two **0-indexed** strings `str1` and `str2`.
9+
10+
In an operation, you select a **set** of indices in `str1`, and for each index `i` in the set, increment `str1[i]` to the next character **cyclically**. That is `'a'` becomes `'b'`, `'b'` becomes `'c'`, and so on, and `'z'` becomes `'a'`.
11+
12+
Return `true` _if it is possible to make_ `str2` _a subsequence of_ `str1` _by performing the operation **at most once**_, _and_ `false` _otherwise_.
13+
14+
**Note:** A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.
15+
16+
**Example 1:**
17+
18+
**Input:** str1 = "abc", str2 = "ad"
19+
20+
**Output:** true
21+
22+
**Explanation:** Select index 2 in str1. Increment str1[2] to become 'd'. Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.
23+
24+
**Example 2:**
25+
26+
**Input:** str1 = "zc", str2 = "ad"
27+
28+
**Output:** true
29+
30+
**Explanation:** Select indices 0 and 1 in str1. Increment str1[0] to become 'a'. Increment str1[1] to become 'd'. Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.
31+
32+
**Example 3:**
33+
34+
**Input:** str1 = "ab", str2 = "d"
35+
36+
**Output:** false
37+
38+
**Explanation:** In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. Therefore, false is returned.
39+
40+
**Constraints:**
41+
42+
* <code>1 <= str1.length <= 10<sup>5</sup></code>
43+
* <code>1 <= str2.length <= 10<sup>5</sup></code>
44+
* `str1` and `str2` consist of only lowercase English letters.
45+
46+
## Solution
47+
48+
```java
49+
public class Solution {
50+
public boolean canMakeSubsequence(String str1, String str2) {
51+
int str1ptr = 0;
52+
for (int i = 0; i < str2.length(); i++) {
53+
char c2 = str2.charAt(i);
54+
boolean found = false;
55+
while (str1ptr < str1.length()) {
56+
char c1 = str1.charAt(str1ptr++);
57+
if (c1 == c2 || (c1 - 'a' + 1) % 26 == c2 - 'a') {
58+
found = true;
59+
break;
60+
}
61+
}
62+
if (!found) {
63+
return false;
64+
}
65+
}
66+
return true;
67+
}
68+
}
69+
```
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
## 2826\. Sorting Three Groups
5+
6+
Medium
7+
8+
You are given a **0-indexed** integer array `nums` of length `n`.
9+
10+
The numbers from `0` to `n - 1` are divided into three groups numbered from `1` to `3`, where number `i` belongs to group `nums[i]`. Notice that some groups may be **empty**.
11+
12+
You are allowed to perform this operation any number of times:
13+
14+
* Pick number `x` and change its group. More formally, change `nums[x]` to any number from `1` to `3`.
15+
16+
A new array `res` is constructed using the following procedure:
17+
18+
1. Sort the numbers in each group independently.
19+
2. Append the elements of groups `1`, `2`, and `3` to `res` **in this order**.
20+
21+
Array `nums` is called a **beautiful array** if the constructed array `res` is sorted in **non-decreasing** order.
22+
23+
Return _the **minimum** number of operations to make_ `nums` _a **beautiful array**_.
24+
25+
**Example 1:**
26+
27+
**Input:** nums = [2,1,3,2,1]
28+
29+
**Output:** 3
30+
31+
**Explanation:** It's optimal to perform three operations:
32+
1. change nums[0] to 1.
33+
2. change nums[2] to 1.
34+
3. change nums[3] to 1.
35+
36+
After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3,4] and group 2 and group 3 become empty. Hence, res is equal to [0,1,2,3,4] which is sorted in non-decreasing order.
37+
38+
It can be proven that there is no valid sequence of less than three operations.
39+
40+
**Example 2:**
41+
42+
**Input:** nums = [1,3,2,1,3,3]
43+
44+
**Output:** 2
45+
46+
**Explanation:** It's optimal to perform two operations:
47+
1. change nums[1] to 1.
48+
2. change nums[2] to 1.
49+
50+
After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3], group 2 becomes empty, and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order.
51+
52+
It can be proven that there is no valid sequence of less than two operations.
53+
54+
**Example 3:**
55+
56+
**Input:** nums = [2,2,2,2,3,3]
57+
58+
**Output:** 0
59+
60+
**Explanation:** It's optimal to not perform operations.
61+
62+
After sorting the numbers in each group, group 1 becomes empty, group 2 becomes equal to [0,1,2,3] and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order.
63+
64+
**Constraints:**
65+
66+
* `1 <= nums.length <= 100`
67+
* `1 <= nums[i] <= 3`
68+
69+
## Solution
70+
71+
```java
72+
import java.util.List;
73+
74+
public class Solution {
75+
public int minimumOperations(List<Integer> nums) {
76+
int n = nums.size();
77+
int[] arr = new int[3];
78+
int max = 0;
79+
for (Integer num : nums) {
80+
int locMax = 0;
81+
int value = num;
82+
for (int j = 0; j < value; j++) {
83+
locMax = Math.max(locMax, arr[j]);
84+
}
85+
86+
locMax++;
87+
arr[value - 1] = locMax;
88+
if (locMax > max) {
89+
max = locMax;
90+
}
91+
}
92+
return n - max;
93+
}
94+
}
95+
```
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
## 2827\. Number of Beautiful Integers in the Range
5+
6+
Hard
7+
8+
You are given positive integers `low`, `high`, and `k`.
9+
10+
A number is **beautiful** if it meets both of the following conditions:
11+
12+
* The count of even digits in the number is equal to the count of odd digits.
13+
* The number is divisible by `k`.
14+
15+
Return _the number of beautiful integers in the range_ `[low, high]`.
16+
17+
**Example 1:**
18+
19+
**Input:** low = 10, high = 20, k = 3
20+
21+
**Output:** 2
22+
23+
**Explanation:** There are 2 beautiful integers in the given range: [12,18].
24+
- 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
25+
- 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that:
26+
- 16 is not beautiful because it is not divisible by k = 3.
27+
- 15 is not beautiful because it does not contain equal counts even and odd digits. It can be shown that there are only 2 beautiful integers in the given range.
28+
29+
**Example 2:**
30+
31+
**Input:** low = 1, high = 10, k = 1
32+
33+
**Output:** 1
34+
35+
**Explanation:** There is 1 beautiful integer in the given range: [10].
36+
- 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1. It can be shown that there is only 1 beautiful integer in the given range.
37+
38+
**Example 3:**
39+
40+
**Input:** low = 5, high = 5, k = 2
41+
42+
**Output:** 0
43+
44+
**Explanation:** There are 0 beautiful integers in the given range.
45+
- 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits.
46+
47+
**Constraints:**
48+
49+
* <code>0 < low <= high <= 10<sup>9</sup></code>
50+
* `0 < k <= 20`
51+
52+
## Solution
53+
54+
```java
55+
import java.util.Arrays;
56+
57+
@SuppressWarnings("java:S107")
58+
public class Solution {
59+
private int[][][][][] dp;
60+
private int maxLength;
61+
62+
public int numberOfBeautifulIntegers(int low, int high, int k) {
63+
String num1 = String.valueOf(low);
64+
String num2 = String.valueOf(high);
65+
maxLength = Math.max(num1.length(), num2.length());
66+
dp = new int[4][maxLength][maxLength][maxLength][k];
67+
for (int[][][][] a : dp) {
68+
for (int[][][] b : a) {
69+
for (int[][] c : b) {
70+
for (int[] d : c) {
71+
Arrays.fill(d, -1);
72+
}
73+
}
74+
}
75+
}
76+
return dp(num1, num2, 0, 3, 0, 0, 0, 0, k);
77+
}
78+
79+
private int dp(
80+
String low, String high, int i, int mode, int odd, int even, int num, int rem, int k) {
81+
if (i == maxLength) {
82+
return num % k == 0 && odd == even ? 1 : 0;
83+
}
84+
if (dp[mode][i][odd][even][rem] != -1) {
85+
return dp[mode][i][odd][even][rem];
86+
}
87+
int res = 0;
88+
boolean lowLimit = mode % 2 == 1;
89+
boolean highLimit = mode / 2 == 1;
90+
int start = 0;
91+
int end = 9;
92+
if (lowLimit) {
93+
start = digitAt(low, i);
94+
}
95+
if (highLimit) {
96+
end = digitAt(high, i);
97+
}
98+
for (int j = start; j <= end; j++) {
99+
int newMode = 0;
100+
if (j == start && lowLimit) {
101+
newMode += 1;
102+
}
103+
if (j == end && highLimit) {
104+
newMode += 2;
105+
}
106+
int newEven = even;
107+
if (num != 0 || j != 0) {
108+
newEven += j % 2 == 0 ? 1 : 0;
109+
}
110+
int newOdd = odd + (j % 2 == 1 ? 1 : 0);
111+
res +=
112+
dp(
113+
low,
114+
high,
115+
i + 1,
116+
newMode,
117+
newOdd,
118+
newEven,
119+
num * 10 + j,
120+
(num * 10 + j) % k,
121+
k);
122+
}
123+
dp[mode][i][odd][even][rem] = res;
124+
return res;
125+
}
126+
127+
private int digitAt(String num, int i) {
128+
int index = num.length() - maxLength + i;
129+
return index < 0 ? 0 : num.charAt(index) - '0';
130+
}
131+
}
132+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
## 2828\. Check if a String Is an Acronym of Words
5+
6+
Easy
7+
8+
Given an array of strings `words` and a string `s`, determine if `s` is an **acronym** of words.
9+
10+
The string `s` is considered an acronym of `words` if it can be formed by concatenating the **first** character of each string in `words` **in order**. For example, `"ab"` can be formed from `["apple", "banana"]`, but it can't be formed from `["bear", "aardvark"]`.
11+
12+
Return `true` _if_ `s` _is an acronym of_ `words`_, and_ `false` _otherwise._
13+
14+
**Example 1:**
15+
16+
**Input:** words = ["alice","bob","charlie"], s = "abc"
17+
18+
**Output:** true
19+
20+
**Explanation:** The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym.
21+
22+
**Example 2:**
23+
24+
**Input:** words = ["an","apple"], s = "a"
25+
26+
**Output:** false
27+
28+
**Explanation:** The first character in the words "an" and "apple" are 'a' and 'a', respectively. The acronym formed by concatenating these characters is "aa". Hence, s = "a" is not the acronym.
29+
30+
**Example 3:**
31+
32+
**Input:** words = ["never","gonna","give","up","on","you"], s = "ngguoy"
33+
34+
**Output:** true
35+
36+
**Explanation:** By concatenating the first character of the words in the array, we get the string "ngguoy". Hence, s = "ngguoy" is the acronym.
37+
38+
**Constraints:**
39+
40+
* `1 <= words.length <= 100`
41+
* `1 <= words[i].length <= 10`
42+
* `1 <= s.length <= 100`
43+
* `words[i]` and `s` consist of lowercase English letters.
44+
45+
## Solution
46+
47+
```java
48+
import java.util.List;
49+
50+
public class Solution {
51+
public boolean isAcronym(List<String> words, String s) {
52+
if (s.length() != words.size()) {
53+
return false;
54+
}
55+
for (int i = 0; i < words.size(); i++) {
56+
if (words.get(i).charAt(0) != s.charAt(i)) {
57+
return false;
58+
}
59+
}
60+
return true;
61+
}
62+
}
63+
```

0 commit comments

Comments
 (0)