Skip to content

Commit 5eb824d

Browse files
committed
Add: Add 2025/2/7
1 parent 72526f7 commit 5eb824d

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

22
/.idea
3+
/.vscode
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 3160. Find the Number of Distinct Colors Among the Balls
2+
3+
You are given an integer `limit` and a 2D array `queries` of size `n x 2`.
4+
5+
There are `limit + 1` balls with distinct labels in the range `[0, limit]`.
6+
Initially, all balls are uncolored. For every query in `queries` that is of the form `[x, y]`,
7+
you mark ball `x` with the color `y`. After each query, you need to find the number of distinct colors among the balls.
8+
9+
Return an array result of length `n`, where `result[i]` denotes the number of distinct colors after $i^{th}$ query.
10+
11+
Note that when answering a query, lack of a color will not be considered as a color.
12+
13+
## 基礎思路
14+
因為我們需要每個 `query` 操作後的結果,故我們需要模擬這個過程,並且記錄每次操作後的結果。
15+
我們可以利用兩個對應表,一個是紀錄有被染色的球的顏色,另一個是紀錄每個顏色的數量。
16+
當我們進行染色操作時,我們可以先檢查這個球是否已經被染色,如果已經被染色,我們就需要將原本的顏色數量減一,並且將新的顏色數量加一。
17+
每次模擬操作後,當下的"顏色數量"就是我們的答案。
18+
19+
## 解題步驟
20+
21+
### Step 1: 初始化對應表
22+
23+
```typescript
24+
const ballColor = new Map<number, number>(); // 紀錄有被染色的球的顏色
25+
const colorCount = new Map<number, number>(); // 紀錄每個顏色的數量
26+
```
27+
28+
### Step 2: 模擬操作
29+
30+
```typescript
31+
// 紀錄每次操作後的結果
32+
const result: number[] = [];
33+
34+
for (const [index, color] of queries) {
35+
if (ballColor.has(index)) {
36+
// 如果該球已經被染色,我們需要將原本的顏色數量減一
37+
const prevColor = ballColor.get(index)!;
38+
const count = colorCount.get(prevColor)!;
39+
if (count === 1) {
40+
// 當數量洽為 1 時,我們需要將這個顏色從對應表中移除
41+
colorCount.delete(prevColor);
42+
} else {
43+
// 減少前一個顏色的數量
44+
colorCount.set(prevColor, count - 1);
45+
}
46+
}
47+
48+
// 將球染上新的顏色
49+
ballColor.set(index, color);
50+
// 增加新的顏色數量
51+
colorCount.set(color, (colorCount.get(color) || 0) + 1);
52+
53+
// 紀錄當前的顏色數量
54+
result.push(colorCount.size);
55+
}
56+
```
57+
58+
## 時間複雜度
59+
- 我們需要進行 `n` 次操作,故時間複雜度為 $O(n)$
60+
- 總時間複雜度為 $O(n)$
61+
62+
> $O(n)$
63+
64+
## 空間複雜度
65+
- 我們需要儲存每個被更新球的對應表,最壞下會全部都被更新,故空間複雜度為 $O(n)$
66+
- 對於顏色數量的對應表,如果每次查詢的顏色都不同,也會是 $O(n)$ 的空間複雜度
67+
- 總空間複雜度為 $O(n)$
68+
69+
> $O(n)$
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function queryResults(_limit: number, queries: number[][]): number[] {
2+
const ballColor = new Map<number, number>(); // Record the updated color of each ball
3+
const colorCount = new Map<number, number>(); // Record the count of each color
4+
5+
const result: number[] = [];
6+
7+
for (const [index, color] of queries) {
8+
if (ballColor.has(index)) {
9+
// If the ball has been colored before, decrease the count of the previous color
10+
const prevColor = ballColor.get(index)!;
11+
const count = colorCount.get(prevColor)!;
12+
if (count === 1) {
13+
// Remove the previous color if the count is 1
14+
colorCount.delete(prevColor);
15+
} else {
16+
// Decrease the count of the previous color
17+
colorCount.set(prevColor, count - 1);
18+
}
19+
}
20+
21+
// Update the color of the ball
22+
ballColor.set(index, color);
23+
// Increase the count of the current color
24+
colorCount.set(color, (colorCount.get(color) || 0) + 1);
25+
26+
// Record the number of distinct colors
27+
result.push(colorCount.size);
28+
}
29+
30+
return result;
31+
}

index.html

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Daily LeetCode</title>
7+
<style>
8+
body {
9+
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
10+
margin: 0;
11+
background-color: #f9f9f9;
12+
color: #333;
13+
line-height: 1.6;
14+
}
15+
16+
.container {
17+
max-width: 900px;
18+
margin: 20px auto;
19+
padding: 30px;
20+
background-color: #fff;
21+
border-radius: 5px;
22+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
23+
}
24+
25+
header {
26+
background-color: #f0f0f0;
27+
padding: 20px;
28+
border-radius: 5px;
29+
margin-bottom: 20px;
30+
text-align: center;
31+
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
32+
}
33+
34+
header p {
35+
font-size: 1.2em;
36+
color: #555;
37+
}
38+
39+
h1 {
40+
font-size: 3.0rem;
41+
font-weight: bold;
42+
margin-bottom: 0.5rem;
43+
color: #007bff;
44+
}
45+
46+
h2 {
47+
font-size: 1.8rem;
48+
font-weight: bold;
49+
margin-top: 4rem;
50+
margin-bottom: 1rem;
51+
color: #007bff;
52+
}
53+
54+
section {
55+
background-color: #fff;
56+
padding: 1rem;
57+
border-radius: 5px;
58+
}
59+
60+
p {
61+
font-size: 1.1rem;
62+
margin-bottom: 1.5rem;
63+
font-weight: 500;
64+
}
65+
66+
ul {
67+
list-style: none;
68+
padding: 0;
69+
}
70+
71+
li {
72+
margin-bottom: 0.5rem;
73+
}
74+
75+
a {
76+
color: #007bff;
77+
text-decoration: none;
78+
font-weight: bold;
79+
}
80+
81+
a:hover {
82+
color: #0056b3;
83+
text-decoration: underline;
84+
}
85+
86+
footer {
87+
text-align: center;
88+
margin-top: 2rem;
89+
padding: 1rem;
90+
font-size: 0.9rem;
91+
color: #555;
92+
background-color: #f0f0f0;
93+
}
94+
95+
.tech-list {
96+
padding: 0;
97+
list-style: none;
98+
}
99+
100+
.tech-list li {
101+
display: inline-block;
102+
margin-right: 10px;
103+
padding: 7px 12px;
104+
background-color: #333;
105+
color: #fff;
106+
border-radius: 7px;
107+
margin-top: 5px; /* added top margin */
108+
}
109+
110+
hr {
111+
border: 0;
112+
height: 1px;
113+
background: #ccc;
114+
margin: 2rem 0;
115+
}
116+
</style>
117+
</head>
118+
<body>
119+
<div class="container">
120+
<header>
121+
<h1>Daily LeetCode</h1>
122+
<p>My solutions to LeetCode problems</p>
123+
</header>
124+
<hr>
125+
<section>
126+
<p>This repository contains my <a href="https://leetcode.com/">LeetCode</a> solutions. I am using TypeScript and provide explanations in Traditional Chinese.</p>
127+
</section>
128+
129+
<section>
130+
<h2>Technologies Used</h2>
131+
<ul class="tech-list">
132+
<li>TypeScript</li>
133+
</ul>
134+
</section>
135+
136+
<footer>
137+
&copy; Daily LeetCode
138+
</footer>
139+
</div>
140+
</body>
141+
</html>

0 commit comments

Comments
 (0)