Skip to content

Commit f2c8bf1

Browse files
authored
Added tasks 2456, 2457, 2458, 2460, 2461.
1 parent b0355d8 commit f2c8bf1

File tree

15 files changed

+548
-0
lines changed

15 files changed

+548
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g2401_2500.s2456_most_popular_video_creator;
2+
3+
// #Medium #Array #String #Hash_Table #Sorting #Heap_Priority_Queue
4+
// #2022_12_16_Time_57_ms_(97.10%)_Space_85.2_MB_(99.42%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class Solution {
13+
public List<List<String>> mostPopularCreator(String[] creators, String[] ids, int[] views) {
14+
HashMap<String, Long> totalViews = new HashMap<>();
15+
HashMap<String, Integer> maxView = new HashMap<>();
16+
long globalMaxView = 0;
17+
for (int i = 0; i < creators.length; i++) {
18+
long currentView = totalViews.getOrDefault(creators[i], 0L) + views[i];
19+
globalMaxView = Math.max(currentView, globalMaxView);
20+
totalViews.put(creators[i], currentView);
21+
int lastIndex = maxView.getOrDefault(creators[i], -1);
22+
if (!maxView.containsKey(creators[i])
23+
|| views[lastIndex] < views[i]
24+
|| (views[lastIndex] == views[i] && ids[lastIndex].compareTo(ids[i]) > 0)) {
25+
maxView.put(creators[i], i);
26+
}
27+
}
28+
List<List<String>> res = new ArrayList<>();
29+
for (Map.Entry<String, Long> entry : totalViews.entrySet()) {
30+
if (entry.getValue() == globalMaxView) {
31+
res.add(Arrays.asList(entry.getKey(), ids[maxView.get(entry.getKey())]));
32+
}
33+
}
34+
return res;
35+
}
36+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2456\. Most Popular Video Creator
2+
3+
Medium
4+
5+
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.
6+
7+
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.
8+
9+
* If multiple creators have the highest popularity, find all of them.
10+
* If multiple videos have the highest view count for a creator, find the lexicographically **smallest** id.
11+
12+
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.
13+
14+
**Example 1:**
15+
16+
**Input:** creators = ["alice","bob","alice","chris"], ids = ["one","two","three","four"], views = [5,10,5,4]
17+
18+
**Output:** [["alice","one"],["bob","two"]]
19+
20+
**Explanation:**
21+
22+
The popularity of alice is 5 + 5 = 10.
23+
24+
The popularity of bob is 10.
25+
26+
The popularity of chris is 4.
27+
28+
alice and bob are the most popular creators.
29+
30+
For bob, the video with the highest view count is "two".
31+
32+
For alice, the videos with the highest view count are "one" and "three".
33+
34+
Since "one" is lexicographically smaller than "three", it is included in the answer.
35+
36+
**Example 2:**
37+
38+
**Input:** creators = ["alice","alice","alice"], ids = ["a","b","c"], views = [1,2,2]
39+
40+
**Output:** [["alice","b"]]
41+
42+
**Explanation:**
43+
44+
The videos with id "b" and "c" have the highest view count.
45+
46+
Since "b" is lexicographically smaller than "c", it is included in the answer.
47+
48+
**Constraints:**
49+
50+
* `n == creators.length == ids.length == views.length`
51+
* <code>1 <= n <= 10<sup>5</sup></code>
52+
* `1 <= creators[i].length, ids[i].length <= 5`
53+
* `creators[i]` and `ids[i]` consist only of lowercase English letters.
54+
* <code>0 <= views[i] <= 10<sup>5</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2401_2500.s2457_minimum_addition_to_make_integer_beautiful;
2+
3+
// #Medium #Math #Greedy #2022_12_16_Time_0_ms_(100.00%)_Space_39.2_MB_(86.59%)
4+
5+
public class Solution {
6+
public long makeIntegerBeautiful(long n, int target) {
7+
if (sumOfDigits(n) <= target) {
8+
return 0;
9+
}
10+
long old = n;
11+
long newNumber = 1;
12+
while (sumOfDigits(n) > target) {
13+
newNumber = newNumber * 10;
14+
n = n / 10 + 1;
15+
}
16+
newNumber = (n) * newNumber;
17+
return newNumber - old;
18+
}
19+
20+
public long sumOfDigits(long n) {
21+
long sum = 0;
22+
while (n > 0) {
23+
sum = sum + n % 10;
24+
n = n / 10;
25+
}
26+
return sum;
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2457\. Minimum Addition to Make Integer Beautiful
2+
3+
Medium
4+
5+
You are given two positive integers `n` and `target`.
6+
7+
An integer is considered **beautiful** if the sum of its digits is less than or equal to `target`.
8+
9+
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.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 16, target = 6
14+
15+
**Output:** 4
16+
17+
**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.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 467, target = 6
22+
23+
**Output:** 33
24+
25+
**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.
26+
27+
**Example 3:**
28+
29+
**Input:** n = 1, target = 1
30+
31+
**Output:** 0
32+
33+
**Explanation:** Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= n <= 10<sup>12</sup></code>
38+
* `1 <= target <= 150`
39+
* The input will be generated such that it is always possible to make `n` beautiful.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g2401_2500.s2458_height_of_binary_tree_after_subtree_removal_queries;
2+
3+
// #Hard #Array #Tree #Binary_Tree #Depth_First_Search #Breadth_First_Search
4+
// #2022_12_20_Time_52_ms_(87.45%)_Space_70.6_MB_(98.17%)
5+
6+
import com_github_leetcode.TreeNode;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class Solution {
11+
public int[] treeQueries(TreeNode root, int[] queries) {
12+
Map<Integer, int[]> levels = new HashMap<>();
13+
Map<Integer, int[]> map = new HashMap<>();
14+
int max = dfs(root, 0, map, levels) - 1;
15+
int n = queries.length;
16+
for (int i = 0; i < n; i++) {
17+
int q = queries[i];
18+
int[] node = map.get(q);
19+
int height = node[0];
20+
int level = node[1];
21+
int[] lev = levels.get(level);
22+
if (lev[0] == height) {
23+
if (lev[1] != -1) {
24+
queries[i] = max - Math.abs(lev[0] - lev[1]);
25+
} else {
26+
queries[i] = max - height - 1;
27+
}
28+
} else {
29+
queries[i] = max;
30+
}
31+
}
32+
return queries;
33+
}
34+
35+
private int dfs(TreeNode root, int level, Map<Integer, int[]> map, Map<Integer, int[]> levels) {
36+
if (root == null) {
37+
return 0;
38+
}
39+
int left = dfs(root.left, level + 1, map, levels);
40+
int right = dfs(root.right, level + 1, map, levels);
41+
int height = Math.max(left, right);
42+
int[] lev = levels.getOrDefault(level, new int[] {-1, -1});
43+
if (height >= lev[0]) {
44+
lev[1] = lev[0];
45+
lev[0] = height;
46+
} else {
47+
lev[1] = Math.max(lev[1], height);
48+
}
49+
levels.put(level, lev);
50+
map.put(root.val, new int[] {height, level});
51+
return Math.max(left, right) + 1;
52+
}
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2458\. Height of Binary Tree After Subtree Removal Queries
2+
3+
Hard
4+
5+
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`.
6+
7+
You have to perform `m` **independent** queries on the tree where in the <code>i<sup>th</sup></code> query you do the following:
8+
9+
* **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.
10+
11+
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_.
12+
13+
**Note**:
14+
15+
* The queries are independent, so the tree returns to its **initial** state after each query.
16+
* The height of a tree is the **number of edges in the longest simple path** from the root to some node in the tree.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-1.png)
21+
22+
**Input:** root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4]
23+
24+
**Output:** [2]
25+
26+
**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).
27+
28+
**Example 2:**
29+
30+
![](https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-2.png)
31+
32+
**Input:** root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8]
33+
34+
**Output:** [3,2,3,2]
35+
36+
**Explanation:** We have the following queries:
37+
38+
- Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4).
39+
40+
- Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1).
41+
42+
- Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6).
43+
44+
- Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3).
45+
46+
**Constraints:**
47+
48+
* The number of nodes in the tree is `n`.
49+
* <code>2 <= n <= 10<sup>5</sup></code>
50+
* `1 <= Node.val <= n`
51+
* All the values in the tree are **unique**.
52+
* `m == queries.length`
53+
* <code>1 <= m <= min(n, 10<sup>4</sup>)</code>
54+
* `1 <= queries[i] <= n`
55+
* `queries[i] != root.val`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2401_2500.s2460_apply_operations_to_an_array;
2+
3+
// #Easy #Array #Simulation #2022_12_20_Time_1_ms_(87.93%)_Space_42.4_MB_(89.13%)
4+
5+
public class Solution {
6+
public int[] applyOperations(int[] nums) {
7+
for (int i = 0; i < nums.length - 1; i++) {
8+
if (nums[i] == nums[i + 1]) {
9+
nums[i] *= 2;
10+
nums[i + 1] = 0;
11+
}
12+
}
13+
int index = 0;
14+
for (int i = 0; i < nums.length; i++) {
15+
if (nums[i] != 0) {
16+
nums[index] = nums[i];
17+
index++;
18+
}
19+
}
20+
for (int i = index; i < nums.length; i++) {
21+
nums[i] = 0;
22+
}
23+
return nums;
24+
}
25+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2460\. Apply Operations to an Array
2+
3+
Easy
4+
5+
You are given a **0-indexed** array `nums` of size `n` consisting of **non-negative** integers.
6+
7+
You need to apply `n - 1` operations to this array where, in the <code>i<sup>th</sup></code> operation (**0-indexed**), you will apply the following on the <code>i<sup>th</sup></code> element of `nums`:
8+
9+
* If `nums[i] == nums[i + 1]`, then multiply `nums[i]` by `2` and set `nums[i + 1]` to `0`. Otherwise, you skip this operation.
10+
11+
After performing **all** the operations, **shift** all the `0`'s to the **end** of the array.
12+
13+
* For example, the array `[1,0,2,0,0,1]` after shifting all its `0`'s to the end, is `[1,2,1,0,0,0]`.
14+
15+
Return _the resulting array_.
16+
17+
**Note** that the operations are applied **sequentially**, not all at once.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [1,2,2,1,1,0]
22+
23+
**Output:** [1,4,2,0,0,0]
24+
25+
**Explanation:** We do the following operations:
26+
27+
- i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
28+
29+
- i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,**<ins>4</ins>**,**<ins>0</ins>**,1,1,0].
30+
31+
- i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
32+
33+
- i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,**<ins>2</ins>**,**<ins>0</ins>**,0].
34+
35+
- i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,**<ins>0</ins>**,**<ins>0</ins>**].
36+
37+
After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
38+
39+
**Example 2:**
40+
41+
**Input:** nums = [0,1]
42+
43+
**Output:** [1,0]
44+
45+
**Explanation:** No operation can be applied, we just shift the 0 to the end.
46+
47+
**Constraints:**
48+
49+
* `2 <= nums.length <= 2000`
50+
* `0 <= nums[i] <= 1000`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g2401_2500.s2461_maximum_sum_of_distinct_subarrays_with_length_k;
2+
3+
// #Medium #Array #Hash_Table #Sliding_Window #2022_12_20_Time_40_ms_(93.40%)_Space_59.8_MB_(89.83%)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public long maximumSubarraySum(int[] nums, int k) {
10+
Set<Integer> seen = new HashSet<>();
11+
long sum = 0;
12+
long current = 0;
13+
int i = 0;
14+
int j = 0;
15+
while (j < nums.length) {
16+
while (seen.contains(nums[j])) {
17+
int val = nums[i++];
18+
seen.remove(val);
19+
current -= val;
20+
}
21+
seen.add(nums[j]);
22+
current += nums[j];
23+
j++;
24+
if (seen.size() == k) {
25+
sum = Math.max(sum, current);
26+
current -= nums[i];
27+
seen.remove(nums[i]);
28+
i++;
29+
}
30+
}
31+
return sum;
32+
}
33+
}

0 commit comments

Comments
 (0)