Skip to content

Commit f90d929

Browse files
authored
Added tasks 2456-2461.
1 parent d6f0a07 commit f90d929

File tree

9 files changed

+622
-197
lines changed
  • src/main/java
    • g0401_0500/s0416_partition_equal_subset_sum
    • g0901_1000/s0948_bag_of_tokens
    • g2401_2500
      • s2444_count_subarrays_with_fixed_bounds
      • s2457_minimum_addition_to_make_integer_beautiful
      • s2458_height_of_binary_tree_after_subtree_removal_queries
      • s2460_apply_operations_to_an_array
      • s2461_maximum_sum_of_distinct_subarrays_with_length_k

9 files changed

+622
-197
lines changed

README.md

Lines changed: 173 additions & 168 deletions
Large diffs are not rendered by default.

src/main/java/g0401_0500/s0416_partition_equal_subset_sum/readme.md

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,22 @@ Given a **non-empty** array `nums` containing **only positive integers**, find i
3333
```java
3434
public class Solution {
3535
public boolean canPartition(int[] nums) {
36-
int sum = 0;
36+
int sums = 0;
3737
for (int num : nums) {
38-
sum = sum + num;
38+
sums += num;
3939
}
40-
if (sum % 2 != 0) {
40+
if (sums % 2 == 1) {
4141
return false;
4242
}
43-
sum = sum / 2;
44-
// if use primitive boolean array will make default value to false
45-
// we need the default value "null" to help us to do the memo
46-
Boolean[] dp = new Boolean[sum + 1];
47-
return sumTo(nums, sum, 0, dp);
48-
}
49-
50-
private boolean sumTo(int[] nums, int sum, int index, Boolean[] dp) {
51-
if (sum == 0) {
52-
return true;
53-
}
54-
if (sum < 0) {
55-
return false;
56-
}
57-
if (index == nums.length) {
58-
return false;
59-
}
60-
if (dp[sum] != null) {
61-
return dp[sum];
43+
sums /= 2;
44+
boolean[] dp = new boolean[sums + 1];
45+
dp[0] = true;
46+
for (int num : nums) {
47+
for (int sum = sums; sum >= num; sum--) {
48+
dp[sum] = dp[sum] || dp[sum - num];
49+
}
6250
}
63-
// use the number or not use the number
64-
dp[sum] = sumTo(nums, sum - nums[index], index + 1, dp) || sumTo(nums, sum, index + 1, dp);
65-
return dp[sum];
51+
return dp[sums];
6652
}
6753
}
6854
```

src/main/java/g0901_1000/s0948_bag_of_tokens/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public class Solution {
6262
int score = 0;
6363
int l = 0;
6464
int r = tokens.length - 1;
65-
6665
while (l <= r) {
6766
if (tokens[l] <= power) {
6867
power -= tokens[l];

src/main/java/g2401_2500/s2444_count_subarrays_with_fixed_bounds/readme.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ public class Solution {
6262
maxi++;
6363
}
6464
}
65-
6665
if (mini == 0 || maxi == 0) {
6766
break;
6867
}
69-
7068
for (; mini != 0 && maxi != 0; ans += 1 + (i - b), a++) {
7169
if (nums[a] == minK) {
7270
mini--;
@@ -79,7 +77,6 @@ public class Solution {
7977
}
8078
i++;
8179
}
82-
8380
return ans;
8481
}
8582
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
## 2456\. Most Popular Video Creator
5+
6+
Medium
7+
8+
You are given two string arrays `creators` and `ids`, and an integer array `views`, all of length `n`. The <code>i<sup>th</sup></code> video on a platform was created by `creator[i]`, has an id of `ids[i]`, and has `views[i]` views.
9+
10+
The **popularity** of a creator is the **sum** of the number of views on **all** of the creator's videos. Find the creator with the **highest** popularity and the id of their **most** viewed video.
11+
12+
* If multiple creators have the highest popularity, find all of them.
13+
* If multiple videos have the highest view count for a creator, find the lexicographically **smallest** id.
14+
15+
Return _a 2D array of strings_ `answer` _where_ <code>answer[i] = [creator<sub>i</sub>, id<sub>i</sub>]</code> _means that_ <code>creator<sub>i</sub></code> _has the **highest** popularity and_ <code>id<sub>i</sub></code> _is the id of their most popular video._ The answer can be returned in any order.
16+
17+
**Example 1:**
18+
19+
**Input:** creators = ["alice","bob","alice","chris"], ids = ["one","two","three","four"], views = [5,10,5,4]
20+
21+
**Output:** [["alice","one"],["bob","two"]]
22+
23+
**Explanation:**
24+
25+
The popularity of alice is 5 + 5 = 10.
26+
27+
The popularity of bob is 10.
28+
29+
The popularity of chris is 4.
30+
31+
alice and bob are the most popular creators.
32+
33+
For bob, the video with the highest view count is "two".
34+
35+
For alice, the videos with the highest view count are "one" and "three".
36+
37+
Since "one" is lexicographically smaller than "three", it is included in the answer.
38+
39+
**Example 2:**
40+
41+
**Input:** creators = ["alice","alice","alice"], ids = ["a","b","c"], views = [1,2,2]
42+
43+
**Output:** [["alice","b"]]
44+
45+
**Explanation:**
46+
47+
The videos with id "b" and "c" have the highest view count.
48+
49+
Since "b" is lexicographically smaller than "c", it is included in the answer.
50+
51+
**Constraints:**
52+
53+
* `n == creators.length == ids.length == views.length`
54+
* <code>1 <= n <= 10<sup>5</sup></code>
55+
* `1 <= creators[i].length, ids[i].length <= 5`
56+
* `creators[i]` and `ids[i]` consist only of lowercase English letters.
57+
* <code>0 <= views[i] <= 10<sup>5</sup></code>
58+
59+
## Solution
60+
61+
```java
62+
import java.util.ArrayList;
63+
import java.util.Arrays;
64+
import java.util.HashMap;
65+
import java.util.List;
66+
import java.util.Map;
67+
68+
public class Solution {
69+
public List<List<String>> mostPopularCreator(String[] creators, String[] ids, int[] views) {
70+
HashMap<String, Long> totalViews = new HashMap<>();
71+
HashMap<String, Integer> maxView = new HashMap<>();
72+
long globalMaxView = 0;
73+
for (int i = 0; i < creators.length; i++) {
74+
long currentView = totalViews.getOrDefault(creators[i], 0L) + views[i];
75+
globalMaxView = Math.max(currentView, globalMaxView);
76+
totalViews.put(creators[i], currentView);
77+
int lastIndex = maxView.getOrDefault(creators[i], -1);
78+
if (!maxView.containsKey(creators[i])
79+
|| views[lastIndex] < views[i]
80+
|| (views[lastIndex] == views[i] && ids[lastIndex].compareTo(ids[i]) > 0)) {
81+
maxView.put(creators[i], i);
82+
}
83+
}
84+
List<List<String>> res = new ArrayList<>();
85+
for (Map.Entry<String, Long> entry : totalViews.entrySet()) {
86+
if (entry.getValue() == globalMaxView) {
87+
res.add(Arrays.asList(entry.getKey(), ids[maxView.get(entry.getKey())]));
88+
}
89+
}
90+
return res;
91+
}
92+
}
93+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
## 2457\. Minimum Addition to Make Integer Beautiful
5+
6+
Medium
7+
8+
You are given two positive integers `n` and `target`.
9+
10+
An integer is considered **beautiful** if the sum of its digits is less than or equal to `target`.
11+
12+
Return the _minimum **non-negative** integer_ `x` _such that_ `n + x` _is beautiful_. The input will be generated such that it is always possible to make `n` beautiful.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 16, target = 6
17+
18+
**Output:** 4
19+
20+
**Explanation:** Initially n is 16 and its digit sum is 1 + 6 = 7. After adding 4, n becomes 20 and digit sum becomes 2 + 0 = 2. It can be shown that we can not make n beautiful with adding non-negative integer less than 4.
21+
22+
**Example 2:**
23+
24+
**Input:** n = 467, target = 6
25+
26+
**Output:** 33
27+
28+
**Explanation:** Initially n is 467 and its digit sum is 4 + 6 + 7 = 17. After adding 33, n becomes 500 and digit sum becomes 5 + 0 + 0 = 5. It can be shown that we can not make n beautiful with adding non-negative integer less than 33.
29+
30+
**Example 3:**
31+
32+
**Input:** n = 1, target = 1
33+
34+
**Output:** 0
35+
36+
**Explanation:** Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= n <= 10<sup>12</sup></code>
41+
* `1 <= target <= 150`
42+
* The input will be generated such that it is always possible to make `n` beautiful.
43+
44+
## Solution
45+
46+
```java
47+
public class Solution {
48+
public long makeIntegerBeautiful(long n, int target) {
49+
if (sumOfDigits(n) <= target) {
50+
return 0;
51+
}
52+
long old = n;
53+
long newNumber = 1;
54+
while (sumOfDigits(n) > target) {
55+
newNumber = newNumber * 10;
56+
n = n / 10 + 1;
57+
}
58+
newNumber = (n) * newNumber;
59+
return newNumber - old;
60+
}
61+
62+
public long sumOfDigits(long n) {
63+
long sum = 0;
64+
while (n > 0) {
65+
sum = sum + n % 10;
66+
n = n / 10;
67+
}
68+
return sum;
69+
}
70+
}
71+
```
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
## 2458\. Height of Binary Tree After Subtree Removal Queries
5+
6+
Hard
7+
8+
You are given the `root` of a **binary tree** with `n` nodes. Each node is assigned a unique value from `1` to `n`. You are also given an array `queries` of size `m`.
9+
10+
You have to perform `m` **independent** queries on the tree where in the <code>i<sup>th</sup></code> query you do the following:
11+
12+
* **Remove** the subtree rooted at the node with the value `queries[i]` from the tree. It is **guaranteed** that `queries[i]` will **not** be equal to the value of the root.
13+
14+
Return _an array_ `answer` _of size_ `m` _where_ `answer[i]` _is the height of the tree after performing the_ <code>i<sup>th</sup></code> _query_.
15+
16+
**Note**:
17+
18+
* The queries are independent, so the tree returns to its **initial** state after each query.
19+
* The height of a tree is the **number of edges in the longest simple path** from the root to some node in the tree.
20+
21+
**Example 1:**
22+
23+
![](https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-1.png)
24+
25+
**Input:** root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4]
26+
27+
**Output:** [2]
28+
29+
**Explanation:** The diagram above shows the tree after removing the subtree rooted at node with value 4. The height of the tree is 2 (The path 1 -> 3 -> 2).
30+
31+
**Example 2:**
32+
33+
![](https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-2.png)
34+
35+
**Input:** root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8]
36+
37+
**Output:** [3,2,3,2]
38+
39+
**Explanation:** We have the following queries:
40+
41+
- Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4).
42+
43+
- Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1).
44+
45+
- Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6).
46+
47+
- Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3).
48+
49+
**Constraints:**
50+
51+
* The number of nodes in the tree is `n`.
52+
* <code>2 <= n <= 10<sup>5</sup></code>
53+
* `1 <= Node.val <= n`
54+
* All the values in the tree are **unique**.
55+
* `m == queries.length`
56+
* <code>1 <= m <= min(n, 10<sup>4</sup>)</code>
57+
* `1 <= queries[i] <= n`
58+
* `queries[i] != root.val`
59+
60+
## Solution
61+
62+
```java
63+
import com_github_leetcode.TreeNode;
64+
import java.util.HashMap;
65+
import java.util.Map;
66+
67+
public class Solution {
68+
public int[] treeQueries(TreeNode root, int[] queries) {
69+
Map<Integer, int[]> levels = new HashMap<>();
70+
Map<Integer, int[]> map = new HashMap<>();
71+
int max = dfs(root, 0, map, levels) - 1;
72+
int n = queries.length;
73+
for (int i = 0; i < n; i++) {
74+
int q = queries[i];
75+
int[] node = map.get(q);
76+
int height = node[0];
77+
int level = node[1];
78+
int[] lev = levels.get(level);
79+
if (lev[0] == height) {
80+
if (lev[1] != -1) {
81+
queries[i] = max - Math.abs(lev[0] - lev[1]);
82+
} else {
83+
queries[i] = max - height - 1;
84+
}
85+
} else {
86+
queries[i] = max;
87+
}
88+
}
89+
return queries;
90+
}
91+
92+
private int dfs(TreeNode root, int level, Map<Integer, int[]> map, Map<Integer, int[]> levels) {
93+
if (root == null) {
94+
return 0;
95+
}
96+
int left = dfs(root.left, level + 1, map, levels);
97+
int right = dfs(root.right, level + 1, map, levels);
98+
int height = Math.max(left, right);
99+
int[] lev = levels.getOrDefault(level, new int[] {-1, -1});
100+
if (height >= lev[0]) {
101+
lev[1] = lev[0];
102+
lev[0] = height;
103+
} else {
104+
lev[1] = Math.max(lev[1], height);
105+
}
106+
levels.put(level, lev);
107+
map.put(root.val, new int[] {height, level});
108+
return Math.max(left, right) + 1;
109+
}
110+
}
111+
```

0 commit comments

Comments
 (0)