Skip to content

Commit b535c0b

Browse files
authored
Added tasks 2667-2673
1 parent f3f3551 commit b535c0b

File tree

15 files changed

+468
-0
lines changed

15 files changed

+468
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2667\. Create Hello World Function
2+
3+
Easy
4+
5+
Write a function `createHelloWorld`. It should return a new function that always returns `"Hello World"`.
6+
7+
**Example 1:**
8+
9+
**Input:** args = []
10+
11+
**Output:** "Hello World"
12+
13+
**Explanation:**
14+
15+
const f = createHelloWorld();
16+
f(); // "Hello World"
17+
18+
The function returned by createHelloWorld should always return "Hello World".
19+
20+
**Example 2:**
21+
22+
**Input:** args = [{},null,42]
23+
24+
**Output:** "Hello World"
25+
26+
**Explanation:**
27+
28+
const f = createHelloWorld();
29+
f({}, null, 42); // "Hello World"
30+
31+
Any arguments could be passed to the function but it should still always return "Hello World".
32+
33+
**Constraints:**
34+
35+
* `0 <= args.length <= 10`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// #Easy #2023_07_26_Time_52_ms_(92.72%)_Space_43.1_MB_(21.30%)
2+
3+
function createHelloWorld() {
4+
return function (...args): string {
5+
return 'Hello World'
6+
}
7+
}
8+
9+
/*
10+
* const f = createHelloWorld();
11+
* f(); // "Hello World"
12+
*/
13+
14+
export { createHelloWorld }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2601_2700.s2670_find_the_distinct_difference_array
2+
3+
// #Easy #Array #Hash_Table #2023_07_26_Time_320_ms_(94.74%)_Space_47.3_MB_(15.79%)
4+
5+
class Solution {
6+
fun distinctDifferenceArray(nums: IntArray): IntArray {
7+
val n = nums.size
8+
val prefixSet = HashSet<Int>()
9+
val suffixSet = HashSet<Int>()
10+
val preList = IntArray(n)
11+
val sufList = IntArray(n)
12+
val ans = IntArray(n)
13+
for (i in 0..nums.lastIndex) {
14+
prefixSet.add(nums[i])
15+
suffixSet.add(nums[n - 1 - i])
16+
preList[i] = prefixSet.size
17+
sufList[n - 1 - i] = suffixSet.size
18+
}
19+
for (i in 0..nums.lastIndex) {
20+
if (i == nums.lastIndex) {
21+
ans[i] = preList[i]
22+
} else {
23+
ans[i] = preList[i] - sufList[i + 1]
24+
}
25+
}
26+
return ans
27+
}
28+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2670\. Find the Distinct Difference Array
2+
3+
Easy
4+
5+
You are given a **0-indexed** array `nums` of length `n`.
6+
7+
The **distinct difference** array of `nums` is an array `diff` of length `n` such that `diff[i]` is equal to the number of distinct elements in the suffix `nums[i + 1, ..., n - 1]` **subtracted from** the number of distinct elements in the prefix `nums[0, ..., i]`.
8+
9+
Return _the **distinct difference** array of_ `nums`.
10+
11+
Note that `nums[i, ..., j]` denotes the subarray of `nums` starting at index `i` and ending at index `j` inclusive. Particularly, if `i > j` then `nums[i, ..., j]` denotes an empty subarray.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3,4,5]
16+
17+
**Output:** [-3,-1,1,3,5]
18+
19+
**Explanation:**
20+
21+
For index i = 0, there is 1 element in the prefix and 4 distinct elements in the suffix. Thus, diff[0] = 1 - 4 = -3.
22+
23+
For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
24+
25+
For index i = 2, there are 3 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 3 - 2 = 1.
26+
27+
For index i = 3, there are 4 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 4 - 1 = 3.
28+
29+
For index i = 4, there are 5 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 5 - 0 = 5.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [3,2,3,4,2]
34+
35+
**Output:** [-2,-1,0,2,3]
36+
37+
**Explanation:**
38+
39+
For index i = 0, there is 1 element in the prefix and 3 distinct elements in the suffix. Thus, diff[0] = 1 - 3 = -2.
40+
41+
For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.
42+
43+
For index i = 2, there are 2 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 2 - 2 = 0.
44+
45+
For index i = 3, there are 3 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 3 - 1 = 2.
46+
47+
For index i = 4, there are 3 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 3 - 0 = 3.
48+
49+
**Constraints:**
50+
51+
* `1 <= n == nums.length <= 50`
52+
* `1 <= nums[i] <= 50`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g2601_2700.s2671_frequency_tracker
2+
3+
// #Medium #Hash_Table #Design #2023_07_26_Time_1109_ms_(80.00%)_Space_134.7_MB_(60.00%)
4+
5+
class FrequencyTracker() {
6+
private val count = IntArray(100001)
7+
private val freq = IntArray(100001)
8+
9+
fun add(number: Int) {
10+
val curFreq = ++count[number]
11+
freq[curFreq - 1]--
12+
freq[curFreq]++
13+
}
14+
15+
fun deleteOne(number: Int) {
16+
if (count[number] > 0) {
17+
val curFreq = --count[number]
18+
freq[curFreq + 1]--
19+
freq[curFreq]++
20+
}
21+
}
22+
23+
fun hasFrequency(frequency: Int) = freq[frequency] > 0
24+
}
25+
26+
/*
27+
* Your FrequencyTracker object will be instantiated and called as such:
28+
* var obj = FrequencyTracker()
29+
* obj.add(number)
30+
* obj.deleteOne(number)
31+
* var param_3 = obj.hasFrequency(frequency)
32+
*/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2671\. Frequency Tracker
2+
3+
Medium
4+
5+
Design a data structure that keeps track of the values in it and answers some queries regarding their frequencies.
6+
7+
Implement the `FrequencyTracker` class.
8+
9+
* `FrequencyTracker()`: Initializes the `FrequencyTracker` object with an empty array initially.
10+
* `void add(int number)`: Adds `number` to the data structure.
11+
* `void deleteOne(int number)`: Deletes **one** occurrence of `number` from the data structure. The data structure **may not contain** `number`, and in this case nothing is deleted.
12+
* `bool hasFrequency(int frequency)`: Returns `true` if there is a number in the data structure that occurs `frequency` number of times, otherwise, it returns `false`.
13+
14+
**Example 1:**
15+
16+
**Input** ["FrequencyTracker", "add", "add", "hasFrequency"] [[], [3], [3], [2]]
17+
18+
**Output:** [null, null, null, true]
19+
20+
**Explanation:**
21+
22+
FrequencyTracker frequencyTracker = new FrequencyTracker();
23+
frequencyTracker.add(3); // The data structure now contains [3]
24+
frequencyTracker.add(3); // The data structure now contains [3, 3]
25+
frequencyTracker.hasFrequency(2); // Returns true, because 3 occurs twice
26+
27+
**Example 2:**
28+
29+
**Input** ["FrequencyTracker", "add", "deleteOne", "hasFrequency"] [[], [1], [1], [1]]
30+
31+
**Output:** [null, null, null, false]
32+
33+
**Explanation:**
34+
35+
FrequencyTracker frequencyTracker = new FrequencyTracker();
36+
frequencyTracker.add(1); // The data structure now contains [1]
37+
frequencyTracker.deleteOne(1); // The data structure becomes empty []
38+
frequencyTracker.hasFrequency(1); // Returns false, because the data structure is empty
39+
40+
**Example 3:**
41+
42+
**Input** ["FrequencyTracker", "hasFrequency", "add", "hasFrequency"] [[], [2], [3], [1]]
43+
44+
**Output:** [null, false, null, true]
45+
46+
**Explanation:**
47+
48+
FrequencyTracker frequencyTracker = new FrequencyTracker();
49+
frequencyTracker.hasFrequency(2); // Returns false, because the data structure is empty
50+
frequencyTracker.add(3); // The data structure now contains [3]
51+
frequencyTracker.hasFrequency(1); // Returns true, because 3 occurs once
52+
53+
**Constraints:**
54+
55+
* <code>1 <= number <= 10<sup>5</sup></code>
56+
* <code>1 <= frequency <= 10<sup>5</sup></code>
57+
* At most, <code>2 * 10<sup>5</sup></code> calls will be made to `add`, `deleteOne`, and `hasFrequency` in **total**.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2601_2700.s2672_number_of_adjacent_elements_with_the_same_color
2+
3+
// #Medium #Array #2023_07_26_Time_1208_ms_(100.00%)_Space_116_MB_(25.00%)
4+
5+
class Solution {
6+
fun colorTheArray(n: Int, queries: Array<IntArray>): IntArray {
7+
val nums = IntArray(n)
8+
val res = IntArray(queries.size)
9+
var count = 0
10+
for ((i, q) in queries.withIndex()) {
11+
val (e, c) = q
12+
if (e > 0 && nums[e] != 0 && nums[e - 1] == nums[e]) count--
13+
if (e < n - 1 && nums[e] != 0 && nums[e + 1] == nums[e]) count--
14+
nums[e] = c
15+
if (e > 0 && nums[e] != 0 && nums[e - 1] == nums[e]) count++
16+
if (e < n - 1 && nums[e] != 0 && nums[e + 1] == nums[e]) count++
17+
res[i] = count
18+
}
19+
return res
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2672\. Number of Adjacent Elements With the Same Color
2+
3+
Medium
4+
5+
There is a **0-indexed** array `nums` of length `n`. Initially, all elements are **uncolored** (has a value of `0`).
6+
7+
You are given a 2D integer array `queries` where <code>queries[i] = [index<sub>i</sub>, color<sub>i</sub>]</code>.
8+
9+
For each query, you color the index <code>index<sub>i</sub></code> with the color <code>color<sub>i</sub></code> in the array `nums`.
10+
11+
Return _an array_ `answer` _of the same length as_ `queries` _where_ `answer[i]` _is the number of adjacent elements with the same color **after** the_ <code>i<sup>th</sup></code> _query_.
12+
13+
More formally, `answer[i]` is the number of indices `j`, such that `0 <= j < n - 1` and `nums[j] == nums[j + 1]` and `nums[j] != 0` after the <code>i<sup>th</sup></code> query.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
18+
19+
**Output:** [0,1,1,0,2]
20+
21+
**Explanation:** Initially array nums = [0,0,0,0], where 0 denotes uncolored elements of the array.
22+
- After the 1<sup>st</sup> query nums = [2,0,0,0]. The count of adjacent elements with the same color is 0.
23+
- After the 2<sup>nd</sup> query nums = [2,2,0,0]. The count of adjacent elements with the same color is 1.
24+
- After the 3<sup>rd</sup> query nums = [2,2,0,1]. The count of adjacent elements with the same color is 1.
25+
- After the 4<sup>th</sup> query nums = [2,1,0,1]. The count of adjacent elements with the same color is 0.
26+
- After the 5<sup>th</sup> query nums = [2,1,1,1]. The count of adjacent elements with the same color is 2.
27+
28+
**Example 2:**
29+
30+
**Input:** n = 1, queries = [[0,100000]]
31+
32+
**Output:** [0]
33+
34+
**Explanation:** Initially array nums = [0], where 0 denotes uncolored elements of the array.
35+
- After the 1<sup>st</sup> query nums = [100000]. The count of adjacent elements with the same color is 0.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= n <= 10<sup>5</sup></code>
40+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
41+
* `queries[i].length == 2`
42+
* <code>0 <= index<sub>i</sub> <= n - 1</code>
43+
* <code>1 <= color<sub>i</sub> <= 10<sup>5</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2601_2700.s2673_make_costs_of_paths_equal_in_a_binary_tree
2+
3+
// #Medium #Array #Dynamic_Programming #Greedy #Tree #Binary_Tree
4+
// #2023_07_26_Time_645_ms_(75.00%)_Space_57.8_MB_(75.00%)
5+
6+
class Solution {
7+
fun minIncrements(n: Int, cost: IntArray): Int {
8+
val last = n / 2 - 1
9+
var res = 0
10+
for (i in last downTo 0) {
11+
var abs = cost[2 * i + 1] - cost[2 * i + 2]
12+
if (abs < 0) abs *= -1
13+
cost[i] += maxOf(cost[2 * i + 1], cost[2 * i + 2])
14+
res += abs
15+
}
16+
return res
17+
}
18+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2673\. Make Costs of Paths Equal in a Binary Tree
2+
3+
Medium
4+
5+
You are given an integer `n` representing the number of nodes in a **perfect binary tree** consisting of nodes numbered from `1` to `n`. The root of the tree is node `1` and each node `i` in the tree has two children where the left child is the node `2 * i` and the right child is `2 * i + 1`.
6+
7+
Each node in the tree also has a **cost** represented by a given **0-indexed** integer array `cost` of size `n` where `cost[i]` is the cost of node `i + 1`. You are allowed to **increment** the cost of **any** node by `1` **any** number of times.
8+
9+
Return _the **minimum** number of increments you need to make the cost of paths from the root to each **leaf** node equal_.
10+
11+
**Note**:
12+
13+
* A **perfect binary tree** is a tree where each node, except the leaf nodes, has exactly 2 children.
14+
* The **cost of a path** is the sum of costs of nodes in the path.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2023/04/04/binaryytreeedrawio-4.png)
19+
20+
**Input:** n = 7, cost = [1,5,2,2,3,3,1]
21+
22+
**Output:** 6
23+
24+
**Explanation:** We can do the following increments:
25+
- Increase the cost of node 4 one time.
26+
- Increase the cost of node 3 three times.
27+
- Increase the cost of node 7 two times.
28+
29+
Each path from the root to a leaf will have a total cost of 9.
30+
31+
The total increments we did is 1 + 3 + 2 = 6.
32+
It can be shown that this is the minimum answer we can achieve.
33+
34+
**Example 2:**
35+
36+
![](https://assets.leetcode.com/uploads/2023/04/04/binaryytreee2drawio.png)
37+
38+
**Input:** n = 3, cost = [5,3,3]
39+
40+
**Output:** 0
41+
42+
**Explanation:** The two paths already have equal total costs, so no increments are needed.
43+
44+
**Constraints:**
45+
46+
* <code>3 <= n <= 10<sup>5</sup></code>
47+
* `n + 1` is a power of `2`
48+
* `cost.length == n`
49+
* <code>1 <= cost[i] <= 10<sup>4</sup></code>

0 commit comments

Comments
 (0)