Skip to content

Commit c55fd94

Browse files
committed
feat: add solutions to lc problems: No.2670~2673
* No.2670.Find the Distinct Difference Array * No.2671.Frequency Tracker * No.2672.Number of Adjacent Elements With the Same Color * No.2673.Make Costs of Paths Equal in a Binary Tree
1 parent db2e68b commit c55fd94

File tree

45 files changed

+2284
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2284
-21
lines changed

solution/1100-1199/1114.Print in Order/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Foo {
109109
private Semaphore c = new Semaphore(0);
110110

111111
public Foo() {
112-
112+
113113
}
114114

115115
public void first(Runnable printFirst) throws InterruptedException {

solution/1100-1199/1116.Print Zero Even Odd/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ZeroEvenOdd:
6969
self.z = Semaphore(1)
7070
self.e = Semaphore(0)
7171
self.o = Semaphore(0)
72-
72+
7373
# printNumber(x) outputs "x", where x is an integer.
7474
def zero(self, printNumber: 'Callable[[int], None]') -> None:
7575
for i in range(self.n):
@@ -79,13 +79,13 @@ class ZeroEvenOdd:
7979
self.o.release()
8080
else:
8181
self.e.release()
82-
82+
8383
def even(self, printNumber: 'Callable[[int], None]') -> None:
8484
for i in range(2, self.n + 1, 2):
8585
self.e.acquire()
8686
printNumber(i)
8787
self.z.release()
88-
88+
8989
def odd(self, printNumber: 'Callable[[int], None]') -> None:
9090
for i in range(1, self.n + 1, 2):
9191
self.o.acquire()
@@ -101,7 +101,7 @@ class ZeroEvenOdd {
101101
private Semaphore z = new Semaphore(1);
102102
private Semaphore e = new Semaphore(0);
103103
private Semaphore o = new Semaphore(0);
104-
104+
105105
public ZeroEvenOdd(int n) {
106106
this.n = n;
107107
}

solution/1100-1199/1117.Building H2O/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class H2O {
9898
private Semaphore o = new Semaphore(0);
9999

100100
public H2O() {
101-
101+
102102
}
103103

104104
public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {

solution/1100-1199/1117.Building H2O/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class H2O {
8484
private Semaphore o = new Semaphore(0);
8585

8686
public H2O() {
87-
87+
8888
}
8989

9090
public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {

solution/1200-1299/1216.Valid Palindrome III/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给出一个字符串&nbsp;<code>s</code>&nbsp;和一个整数&nbsp;<code>k</code>,若这个字符串是不是一个「k&nbsp;<strong>回文</strong>&nbsp;」,则返回 <code>true</code> 。</p>
9+
<p>给出一个字符串&nbsp;<code>s</code>&nbsp;和一个整数&nbsp;<code>k</code>,若这个字符串是一个「k&nbsp;<strong>回文</strong>&nbsp;」,则返回 <code>true</code> 。</p>
1010

1111
<p>如果可以通过从字符串中删去最多 <code>k</code> 个字符将其转换为回文,那么这个字符串就是一个「<strong>k</strong>&nbsp;<strong>回文</strong>&nbsp;」。</p>
1212

solution/2600-2699/2668.Find Latest Salaries/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ SELECT
8686
firstname,
8787
lastname,
8888
max( salary ) AS salary,
89-
department_id
89+
department_id
9090
FROM
91-
Salary
91+
Salary
9292
GROUP BY
93-
emp_id
93+
emp_id
9494
ORDER BY
9595
emp_id;
9696
```

solution/2600-2699/2668.Find Latest Salaries/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ SELECT
8080
firstname,
8181
lastname,
8282
max( salary ) AS salary,
83-
department_id
83+
department_id
8484
FROM
85-
Salary
85+
Salary
8686
GROUP BY
87-
emp_id
87+
emp_id
8888
ORDER BY
8989
emp_id;
9090
```

solution/2600-2699/2669.Count Artist Occurrences On Spotify Ranking List/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ Each row contains an id, track_name, and artist.
6767
# Write your MySQL query statement below
6868
SELECT
6969
artist,
70-
count( 1 ) AS occurrences
70+
count( 1 ) AS occurrences
7171
FROM
72-
Spotify
72+
Spotify
7373
GROUP BY
74-
artist
74+
artist
7575
ORDER BY
7676
occurrences DESC,
7777
artist;

solution/2600-2699/2669.Count Artist Occurrences On Spotify Ranking List/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ Each row contains an id, track_name, and artist.
6161
# Write your MySQL query statement below
6262
SELECT
6363
artist,
64-
count( 1 ) AS occurrences
64+
count( 1 ) AS occurrences
6565
FROM
66-
Spotify
66+
Spotify
6767
GROUP BY
68-
artist
68+
artist
6969
ORDER BY
7070
occurrences DESC,
7171
artist;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# [2670. 找出不同元素数目差数组](https://leetcode.cn/problems/find-the-distinct-difference-array)
2+
3+
[English Version](/solution/2600-2699/2670.Find%20the%20Distinct%20Difference%20Array/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>nums</code> ,数组长度为 <code>n</code> 。</p>
10+
11+
<p><code>nums</code> 的 <strong>不同元素数目差</strong> 数组可以用一个长度为 <code>n</code> 的数组 <code>diff</code> 表示,其中 <code>diff[i]</code> 等于前缀 <code>nums[0, ..., i]</code> 中不同元素的数目 <strong>减去</strong> 后缀 <code>nums[i + 1, ..., n - 1]</code> 中不同元素的数目。</p>
12+
13+
<p>返回<em> </em><code>nums</code> 的 <strong>不同元素数目差</strong> 数组。</p>
14+
15+
<p>注意 <code>nums[i, ..., j]</code> 表示 <code>nums</code> 的一个从下标 <code>i</code> 开始到下标 <code>j</code> 结束的子数组(包含下标 <code>i</code> 和 <code>j</code> 对应元素)。特别需要说明的是,如果 <code>i &gt; j</code> ,则 <code>nums[i, ..., j]</code> 表示一个空子数组。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>nums = [1,2,3,4,5]
23+
<strong>输出:</strong>[-3,-1,1,3,5]
24+
<strong>解释:
25+
</strong>对于 i = 0,前缀中有 1 个不同的元素,而在后缀中有 4 个不同的元素。因此,diff[0] = 1 - 4 = -3 。
26+
对于 i = 1,前缀中有 2 个不同的元素,而在后缀中有 3 个不同的元素。因此,diff[1] = 2 - 3 = -1 。
27+
对于 i = 2,前缀中有 3 个不同的元素,而在后缀中有 2 个不同的元素。因此,diff[2] = 3 - 2 = 1 。
28+
对于 i = 3,前缀中有 4 个不同的元素,而在后缀中有 1 个不同的元素。因此,diff[3] = 4 - 1 = 3 。
29+
对于 i = 4,前缀中有 5 个不同的元素,而在后缀中有 0 个不同的元素。因此,diff[4] = 5 - 0 = 5 。
30+
</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
34+
<pre>
35+
<strong>输入:</strong>nums = [3,2,3,4,2]
36+
<strong>输出:</strong>[-2,-1,0,2,3]
37+
<strong>解释:</strong>
38+
对于 i = 0,前缀中有 1 个不同的元素,而在后缀中有 3 个不同的元素。因此,diff[0] = 1 - 3 = -2 。
39+
对于 i = 1,前缀中有 2 个不同的元素,而在后缀中有 3 个不同的元素。因此,diff[1] = 2 - 3 = -1 。
40+
对于 i = 2,前缀中有 2 个不同的元素,而在后缀中有 2 个不同的元素。因此,diff[2] = 2 - 2 = 0 。
41+
对于 i = 3,前缀中有 3 个不同的元素,而在后缀中有 1 个不同的元素。因此,diff[3] = 3 - 1 = 2 。
42+
对于 i = 4,前缀中有 3 个不同的元素,而在后缀中有 0 个不同的元素。因此,diff[4] = 3 - 0 = 3 。
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= n == nums.length&nbsp;&lt;= 50</code></li>
51+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
52+
</ul>
53+
54+
## 解法
55+
56+
<!-- 这里可写通用的实现逻辑 -->
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```python
65+
class Solution:
66+
def distinctDifferenceArray(self, nums: List[int]) -> List[int]:
67+
n = len(nums)
68+
ans = [0] * n
69+
for i in range(n):
70+
a = len(set(nums[: i + 1]))
71+
b = len(set(nums[i + 1 :]))
72+
ans[i] = a - b
73+
return ans
74+
```
75+
76+
```python
77+
class Solution:
78+
def distinctDifferenceArray(self, nums: List[int]) -> List[int]:
79+
n = len(nums)
80+
suf = [0] * (n + 1)
81+
s = set()
82+
for i in range(n - 1, -1, -1):
83+
s.add(nums[i])
84+
suf[i] = len(s)
85+
86+
s.clear()
87+
ans = [0] * n
88+
for i, x in enumerate(nums):
89+
s.add(x)
90+
ans[i] = len(s) - suf[i + 1]
91+
return ans
92+
```
93+
94+
### **Java**
95+
96+
<!-- 这里可写当前语言的特殊实现逻辑 -->
97+
98+
```java
99+
class Solution {
100+
public int[] distinctDifferenceArray(int[] nums) {
101+
int n = nums.length;
102+
int[] suf = new int[n + 1];
103+
Set<Integer> s = new HashSet<>();
104+
for (int i = n - 1; i >= 0; --i) {
105+
s.add(nums[i]);
106+
suf[i] = s.size();
107+
}
108+
s.clear();
109+
int[] ans = new int[n];
110+
for (int i = 0; i < n; ++i) {
111+
s.add(nums[i]);
112+
ans[i] = s.size() - suf[i + 1];
113+
}
114+
return ans;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
vector<int> distinctDifferenceArray(vector<int>& nums) {
125+
int n = nums.size();
126+
vector<int> suf(n + 1);
127+
unordered_set<int> s;
128+
for (int i = n - 1; i >= 0; --i) {
129+
s.insert(nums[i]);
130+
suf[i] = s.size();
131+
}
132+
s.clear();
133+
vector<int> ans(n);
134+
for (int i = 0; i < n; ++i) {
135+
s.insert(nums[i]);
136+
ans[i] = s.size() - suf[i + 1];
137+
}
138+
return ans;
139+
}
140+
};
141+
```
142+
143+
### **Go**
144+
145+
```go
146+
func distinctDifferenceArray(nums []int) []int {
147+
n := len(nums)
148+
suf := make([]int, n+1)
149+
s := map[int]bool{}
150+
for i := n - 1; i >= 0; i-- {
151+
s[nums[i]] = true
152+
suf[i] = len(s)
153+
}
154+
ans := make([]int, n)
155+
s = map[int]bool{}
156+
for i, x := range nums {
157+
s[x] = true
158+
ans[i] = len(s) - suf[i+1]
159+
}
160+
return ans
161+
}
162+
```
163+
164+
### **TypeScript**
165+
166+
```ts
167+
function distinctDifferenceArray(nums: number[]): number[] {
168+
const n = nums.length;
169+
const suf: number[] = new Array(n + 1).fill(0);
170+
const s: Set<number> = new Set();
171+
for (let i = n - 1; i >= 0; --i) {
172+
s.add(nums[i]);
173+
suf[i] = s.size;
174+
}
175+
s.clear();
176+
const ans: number[] = new Array(n);
177+
for (let i = 0; i < n; ++i) {
178+
s.add(nums[i]);
179+
ans[i] = s.size - suf[i + 1];
180+
}
181+
return ans;
182+
}
183+
```
184+
185+
### **...**
186+
187+
```
188+
189+
```
190+
191+
<!-- tabs:end -->

0 commit comments

Comments
 (0)