Skip to content

Commit df6cca6

Browse files
authored
Added tasks 2470, 2471, 2472, 2475, 2476
1 parent 4569fce commit df6cca6

File tree

16 files changed

+551
-0
lines changed

16 files changed

+551
-0
lines changed

README.md

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

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2476 |[Closest Nodes Queries in a Binary Search Tree](src/main/java/g2401_2500/s2476_closest_nodes_queries_in_a_binary_search_tree/Solution.java)| Medium | Array, Tree, Binary_Search, Binary_Tree, Depth_First_Search | 185 | 51.92
1852+
| 2475 |[Number of Unequal Triplets in Array](src/main/java/g2401_2500/s2475_number_of_unequal_triplets_in_array/Solution.java)| Easy | Array, Hash_Table | 1 | 97.49
1853+
| 2472 |[Maximum Number of Non-overlapping Palindrome Substrings](src/main/java/g2401_2500/s2472_maximum_number_of_non_overlapping_palindrome_substrings/Solution.java)| Hard | String, Dynamic_Programming | 2 | 91.04
1854+
| 2471 |[Minimum Number of Operations to Sort a Binary Tree by Level](src/main/java/g2401_2500/s2471_minimum_number_of_operations_to_sort_a_binary_tree_by_level/Solution.java)| Medium | Tree, Binary_Tree, Breadth_First_Search | 76 | 76.46
1855+
| 2470 |[Number of Subarrays With LCM Equal to K](src/main/java/g2401_2500/s2470_number_of_subarrays_with_lcm_equal_to_k/Solution.java)| Medium | Array, Math, Number_Theory | 23 | 62.35
18511856
| 2469 |[Convert the Temperature](src/main/java/g2401_2500/s2469_convert_the_temperature/Solution.java)| Easy | Math | 0 | 100.00
18521857
| 2468 |[Split Message Based on Limit](src/main/java/g2401_2500/s2468_split_message_based_on_limit/Solution.java)| Hard | String, Binary_Search | 27 | 99.08
18531858
| 2467 |[Most Profitable Path in a Tree](src/main/java/g2401_2500/s2467_most_profitable_path_in_a_tree/Solution.java)| Medium | Array, Tree, Graph, Depth_First_Search, Breadth_First_Search | 20 | 100.00
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2401_2500.s2470_number_of_subarrays_with_lcm_equal_to_k;
2+
3+
// #Medium #Array #Math #Number_Theory #2023_01_24_Time_23_ms_(62.35%)_Space_41.9_MB_(63.16%)
4+
5+
public class Solution {
6+
public int subarrayLCM(int[] nums, int k) {
7+
int ans = 0;
8+
for (int i = 0; i < nums.length; i++) {
9+
int lcm = nums[i];
10+
for (int j = i; j < nums.length; j++) {
11+
lcm = (lcm * nums[j]) / (gcd(lcm, nums[j]));
12+
if (lcm == k) {
13+
ans++;
14+
}
15+
if (k % lcm != 0) {
16+
break;
17+
}
18+
}
19+
}
20+
return ans;
21+
}
22+
23+
private int gcd(int a, int b) {
24+
return b == 0 ? a : gcd(b, a % b);
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2470\. Number of Subarrays With LCM Equal to K
2+
3+
Medium
4+
5+
Given an integer array `nums` and an integer `k`, return _the number of **subarrays** of_ `nums` _where the least common multiple of the subarray's elements is_ `k`.
6+
7+
A **subarray** is a contiguous non-empty sequence of elements within an array.
8+
9+
The **least common multiple of an array** is the smallest positive integer that is divisible by all the array elements.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,6,2,7,1], k = 6
14+
15+
**Output:** 4
16+
17+
**Explanation:** The subarrays of nums where 6 is the least common multiple of all the subarray's elements are:
18+
- [<ins>**3**</ins>,<ins>**6**</ins>,2,7,1]
19+
- [<ins>**3**</ins>,<ins>**6**</ins>,<ins>**2**</ins>,7,1]
20+
- [3,<ins>**6**</ins>,2,7,1]
21+
- [3,<ins>**6**</ins>,<ins>**2**</ins>,7,1]
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [3], k = 2
26+
27+
**Output:** 0
28+
29+
**Explanation:** There are no subarrays of nums where 2 is the least common multiple of all the subarray's elements.
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 1000`
34+
* `1 <= nums[i], k <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package g2401_2500.s2471_minimum_number_of_operations_to_sort_a_binary_tree_by_level;
2+
3+
// #Medium #Tree #Binary_Tree #Breadth_First_Search
4+
// #2023_01_24_Time_76_ms_(76.46%)_Space_66_MB_(63.11%)
5+
6+
import com_github_leetcode.TreeNode;
7+
import java.util.ArrayDeque;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
public class Solution {
15+
public int minimumOperations(TreeNode root) {
16+
ArrayDeque<TreeNode> q = new ArrayDeque<>();
17+
int count = 0;
18+
if ((root.left != null && root.right != null) && (root.left.val > root.right.val)) {
19+
count++;
20+
}
21+
if (root.left != null) {
22+
q.add(root.left);
23+
}
24+
if (root.right != null) {
25+
q.add(root.right);
26+
}
27+
while (!q.isEmpty()) {
28+
int size = q.size();
29+
List<Integer> al = new ArrayList<>();
30+
while (size-- > 0) {
31+
TreeNode node = q.poll();
32+
assert node != null;
33+
if (node.left != null) {
34+
al.add(node.left.val);
35+
q.add(node.left);
36+
}
37+
if (node.right != null) {
38+
al.add(node.right.val);
39+
q.add(node.right);
40+
}
41+
}
42+
count += helper(al);
43+
}
44+
return count;
45+
}
46+
47+
private int helper(List<Integer> list) {
48+
int swaps = 0;
49+
int[] sorted = new int[list.size()];
50+
for (int i = 0; i < sorted.length; i++) {
51+
sorted[i] = list.get(i);
52+
}
53+
Arrays.sort(sorted);
54+
Map<Integer, Integer> ind = new HashMap<>();
55+
for (int i = 0; i < list.size(); i++) {
56+
ind.put(list.get(i), i);
57+
}
58+
59+
for (int i = 0; i < list.size(); i++) {
60+
if (list.get(i) != sorted[i]) {
61+
swaps++;
62+
ind.put(list.get(i), ind.get(sorted[i]));
63+
list.set(ind.get(sorted[i]), list.get(i));
64+
}
65+
}
66+
return swaps;
67+
}
68+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2471\. Minimum Number of Operations to Sort a Binary Tree by Level
2+
3+
Medium
4+
5+
You are given the `root` of a binary tree with **unique values**.
6+
7+
In one operation, you can choose any two nodes **at the same level** and swap their values.
8+
9+
Return _the minimum number of operations needed to make the values at each level sorted in a **strictly increasing order**_.
10+
11+
The **level** of a node is the number of edges along the path between it and the root node_._
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png)
16+
17+
**Input:** root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
18+
19+
**Output:** 3
20+
21+
**Explanation:**
22+
- Swap 4 and 3. The 2<sup>nd</sup> level becomes [3,4].
23+
- Swap 7 and 5. The 3<sup>rd</sup> level becomes [5,6,8,7].
24+
- Swap 8 and 7. The 3<sup>rd</sup> level becomes [5,6,7,8].
25+
26+
We used 3 operations so return 3. It can be proven that 3 is the minimum number of operations needed.
27+
28+
**Example 2:**
29+
30+
![](https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png)
31+
32+
**Input:** root = [1,3,2,7,6,5,4]
33+
34+
**Output:** 3
35+
36+
**Explanation:**
37+
- Swap 3 and 2. The 2<sup>nd</sup> level becomes [2,3].
38+
- Swap 7 and 4. The 3<sup>rd</sup> level becomes [4,6,5,7].
39+
- Swap 6 and 5. The 3<sup>rd</sup> level becomes [4,5,6,7].
40+
41+
We used 3 operations so return 3. It can be proven that 3 is the minimum number of operations needed.
42+
43+
**Example 3:**
44+
45+
![](https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png)
46+
47+
**Input:** root = [1,2,3,4,5,6]
48+
49+
**Output:** 0
50+
51+
**Explanation:** Each level is already sorted in increasing order so return 0.
52+
53+
**Constraints:**
54+
55+
* The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.
56+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
57+
* All the values of the tree are **unique**.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g2401_2500.s2472_maximum_number_of_non_overlapping_palindrome_substrings;
2+
3+
// #Hard #String #Dynamic_Programming #2023_01_24_Time_2_ms_(91.04%)_Space_42.2_MB_(64.93%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maxPalindromes(String s, int k) {
9+
int[] dp = new int[s.length()];
10+
Arrays.fill(dp, -1);
11+
return dfs(s, dp, k, 0);
12+
}
13+
14+
private int dfs(String s, int[] dp, int k, int start) {
15+
if (start >= s.length()) {
16+
return 0;
17+
}
18+
if (dp[start] != -1) {
19+
return dp[start];
20+
}
21+
int ans = 0;
22+
for (int n = 0; n <= 1; n++) {
23+
for (int i = start; i <= s.length() - k - n; i++) {
24+
int left = i;
25+
int right = i + k + n - 1;
26+
while (left < right) {
27+
if (s.charAt(left) != s.charAt(right)) {
28+
break;
29+
}
30+
left++;
31+
right--;
32+
}
33+
if (left >= right) {
34+
ans = Math.max(ans, 1 + dfs(s, dp, k, i + k + n));
35+
break;
36+
}
37+
}
38+
}
39+
dp[start] = ans;
40+
return ans;
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2472\. Maximum Number of Non-overlapping Palindrome Substrings
2+
3+
Hard
4+
5+
You are given a string `s` and a **positive** integer `k`.
6+
7+
Select a set of **non-overlapping** substrings from the string `s` that satisfy the following conditions:
8+
9+
* The **length** of each substring is **at least** `k`.
10+
* Each substring is a **palindrome**.
11+
12+
Return _the **maximum** number of substrings in an optimal selection_.
13+
14+
A **substring** is a contiguous sequence of characters within a string.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "abaccdbbd", k = 3
19+
20+
**Output:** 2
21+
22+
**Explanation:** We can select the substrings underlined in s = "<ins>**aba**</ins>cc<ins>**dbbd**</ins>". Both "aba" and "dbbd" are palindromes and have a length of at least k = 3. It can be shown that we cannot find a selection with more than two valid substrings.
23+
24+
**Example 2:**
25+
26+
**Input:** s = "adbcda", k = 2
27+
28+
**Output:** 0
29+
30+
**Explanation:** There is no palindrome substring of length at least 2 in the string.
31+
32+
**Constraints:**
33+
34+
* `1 <= k <= s.length <= 2000`
35+
* `s` consists of lowercase English letters.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2401_2500.s2475_number_of_unequal_triplets_in_array;
2+
3+
// #Easy #Array #Hash_Table #2023_01_24_Time_1_ms_(97.49%)_Space_40.3_MB_(57.72%)
4+
5+
public class Solution {
6+
public int unequalTriplets(int[] nums) {
7+
int trips = 0;
8+
int pairs = 0;
9+
int[] count = new int[1001];
10+
for (int i = 0; i < nums.length; ++i) {
11+
trips += pairs - count[nums[i]] * (i - count[nums[i]]);
12+
pairs += i - count[nums[i]];
13+
count[nums[i]] += 1;
14+
}
15+
return trips;
16+
}
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2475\. Number of Unequal Triplets in Array
2+
3+
Easy
4+
5+
You are given a **0-indexed** array of positive integers `nums`. Find the number of triplets `(i, j, k)` that meet the following conditions:
6+
7+
* `0 <= i < j < k < nums.length`
8+
* `nums[i]`, `nums[j]`, and `nums[k]` are **pairwise distinct**.
9+
* In other words, `nums[i] != nums[j]`, `nums[i] != nums[k]`, and `nums[j] != nums[k]`.
10+
11+
Return _the number of triplets that meet the conditions._
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [4,4,2,4,3]
16+
17+
**Output:** 3
18+
19+
**Explanation:** The following triplets meet the conditions:
20+
21+
- (0, 2, 4) because 4 != 2 != 3
22+
- (1, 2, 4) because 4 != 2 != 3
23+
- (2, 3, 4) because 2 != 4 != 3
24+
Since there are 3 triplets, we return 3.
25+
Note that (2, 0, 4) is not a valid triplet because 2 > 0.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,1,1,1,1]
30+
31+
**Output:** 0
32+
33+
**Explanation:** No triplets meet the conditions so we return 0.
34+
35+
**Constraints:**
36+
37+
* `3 <= nums.length <= 100`
38+
* `1 <= nums[i] <= 1000`

0 commit comments

Comments
 (0)