Skip to content

Commit 8416d29

Browse files
committed
Add: Add 2025/11/23
1 parent 7317472 commit 8416d29

File tree

3 files changed

+290
-0
lines changed

3 files changed

+290
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# 1262. Greatest Sum Divisible by Three
2+
3+
Given an integer array `nums`, return the maximum possible sum of elements of the array such that it is divisible by three.
4+
5+
**Constraints:**
6+
7+
- `1 <= nums.length <= 4 * 10^4`
8+
- `1 <= nums[i] <= 10^4`
9+
10+
## 基礎思路
11+
12+
本題要求從整個陣列中選取元素,使其總和 **可以被 3 整除**,並且總和**最大化**
13+
14+
在思考解法時,我們需要掌握以下關鍵觀察:
15+
16+
- **所有數字 mod 3 的特性**
17+
一個整數 mod 3 的結果只可能是 0、1 或 2。
18+
當所有元素加總後,若 `sum % 3 === 0`,代表已可直接回傳。
19+
20+
- **若總和不被 3 整除,只能移除少量元素修正餘數**
21+
`sum % 3 === 1`,則需移除:
22+
23+
- **一個餘數為 1 的最小數**,或
24+
- **兩個餘數為 2 的最小數的總和**
25+
擇其較小。
26+
27+
`sum % 3 === 2`,則需移除:
28+
29+
- **一個餘數為 2 的最小數**,或
30+
- **兩個餘數為 1 的最小數的總和**
31+
擇其較小。
32+
33+
- **只需追蹤每種餘數下最小的兩個值**
34+
因為最多只會移除一個或兩個數字,所以我們只需維護:
35+
36+
- 最小兩個 remainder = 1 的值
37+
- 最小兩個 remainder = 2 的值
38+
39+
- **單遍掃描即可完成所有統計**
40+
我們用一次迴圈計算總和並更新最小值候選。
41+
最後依照餘數判斷最佳移除量。
42+
43+
此策略能在單一線掃描內完成,效率極佳且不需要排序。
44+
45+
## 解題步驟
46+
47+
### Step 1:初始化各類變數
48+
49+
建立總和變數,並準備追蹤餘數為 1 與餘數為 2 的最小兩個值。
50+
51+
```typescript
52+
const length = nums.length;
53+
54+
// 所有元素總和
55+
let totalSum = 0;
56+
57+
// 追蹤餘數為 1 的最小與次小數
58+
let minRemainderOneFirst = Number.MAX_SAFE_INTEGER;
59+
let minRemainderOneSecond = Number.MAX_SAFE_INTEGER;
60+
61+
// 追蹤餘數為 2 的最小與次小數
62+
let minRemainderTwoFirst = Number.MAX_SAFE_INTEGER;
63+
let minRemainderTwoSecond = Number.MAX_SAFE_INTEGER;
64+
```
65+
66+
### Step 2:單次遍歷陣列並記錄必要資訊
67+
68+
在同一個迴圈中:
69+
70+
- 累加元素總和
71+
- 根據 `num % 3` 更新對應的最小候選值
72+
73+
```typescript
74+
for (let index = 0; index < length; index++) {
75+
const currentNumber = nums[index];
76+
77+
// 加總所有元素
78+
totalSum += currentNumber;
79+
80+
const currentRemainder = currentNumber % 3;
81+
82+
if (currentRemainder === 1) {
83+
// 維護餘數為 1 的最小與次小值
84+
if (currentNumber < minRemainderOneFirst) {
85+
minRemainderOneSecond = minRemainderOneFirst;
86+
minRemainderOneFirst = currentNumber;
87+
} else if (currentNumber < minRemainderOneSecond) {
88+
minRemainderOneSecond = currentNumber;
89+
}
90+
} else if (currentRemainder === 2) {
91+
// 維護餘數為 2 的最小與次小值
92+
if (currentNumber < minRemainderTwoFirst) {
93+
minRemainderTwoSecond = minRemainderTwoFirst;
94+
minRemainderTwoFirst = currentNumber;
95+
} else if (currentNumber < minRemainderTwoSecond) {
96+
minRemainderTwoSecond = currentNumber;
97+
}
98+
}
99+
}
100+
```
101+
102+
### Step 3:若總和可被 3 整除,直接回傳
103+
104+
```typescript
105+
// 若總和本身可被 3 整除,無須移除任何數
106+
const remainder = totalSum % 3;
107+
if (remainder === 0) {
108+
return totalSum;
109+
}
110+
```
111+
112+
### Step 4:根據 remainder 推導最小移除量
113+
114+
`sum % 3 === 1`
115+
116+
- 移除一個餘 1 的最小數
117+
- 或移除兩個餘 2 的最小數總合
118+
119+
`sum % 3 === 2`
120+
121+
- 移除一個餘 2 的最小數
122+
- 或移除兩個餘 1 的最小數總合
123+
124+
選最小的作為最佳移除量。
125+
126+
```typescript
127+
let minimumToSubtract = Number.MAX_SAFE_INTEGER;
128+
129+
if (remainder === 1) {
130+
// 選項 1:移除餘數為 1 的最小值
131+
if (minRemainderOneFirst !== Number.MAX_SAFE_INTEGER) {
132+
minimumToSubtract = minRemainderOneFirst;
133+
}
134+
135+
// 選項 2:移除餘數為 2 的最小兩個值
136+
if (minRemainderTwoSecond !== Number.MAX_SAFE_INTEGER) {
137+
const removeTwoRemainderTwo =
138+
minRemainderTwoFirst + minRemainderTwoSecond;
139+
140+
if (removeTwoRemainderTwo < minimumToSubtract) {
141+
minimumToSubtract = removeTwoRemainderTwo;
142+
}
143+
}
144+
} else {
145+
// remainder === 2
146+
147+
// 選項 1:移除餘數為 2 的最小值
148+
if (minRemainderTwoFirst !== Number.MAX_SAFE_INTEGER) {
149+
minimumToSubtract = minRemainderTwoFirst;
150+
}
151+
152+
// 選項 2:移除餘數為 1 的最小兩個值
153+
if (minRemainderOneSecond !== Number.MAX_SAFE_INTEGER) {
154+
const removeTwoRemainderOne =
155+
minRemainderOneFirst + minRemainderOneSecond;
156+
157+
if (removeTwoRemainderOne < minimumToSubtract) {
158+
minimumToSubtract = removeTwoRemainderOne;
159+
}
160+
}
161+
}
162+
```
163+
164+
### Step 5:若仍無合法移除方式,只能回傳 0
165+
166+
```typescript
167+
// 若無合法移除組合,則無法讓總和 divisible by 3
168+
if (minimumToSubtract === Number.MAX_SAFE_INTEGER) {
169+
return 0;
170+
}
171+
```
172+
173+
### Step 6:回傳最終最大可行總和
174+
175+
```typescript
176+
// 回傳最終最大、且可被 3 整除的總和
177+
return totalSum - minimumToSubtract;
178+
```
179+
180+
## 時間複雜度
181+
182+
- 單趟掃描陣列,更新總和與最小候選值皆為 $O(1)$
183+
- 不需要排序或額外資料結構
184+
- 總時間複雜度為 $O(n)$。
185+
186+
> $O(n)$
187+
188+
## 空間複雜度
189+
190+
- 只使用固定數量的變數統計最小值
191+
- 無需額外陣列或額外儲存空間
192+
- 總空間複雜度為 $O(1)$。
193+
194+
> $O(1)$
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
function maxSumDivThree(nums: number[]): number {
2+
const length = nums.length;
3+
4+
// Running total sum of all elements
5+
let totalSum = 0;
6+
7+
// Track two smallest numbers with remainder 1 when divided by 3
8+
let minRemainderOneFirst = Number.MAX_SAFE_INTEGER;
9+
let minRemainderOneSecond = Number.MAX_SAFE_INTEGER;
10+
11+
// Track two smallest numbers with remainder 2 when divided by 3
12+
let minRemainderTwoFirst = Number.MAX_SAFE_INTEGER;
13+
let minRemainderTwoSecond = Number.MAX_SAFE_INTEGER;
14+
15+
// Single pass: accumulate sum and update candidates for minimal removal
16+
for (let index = 0; index < length; index++) {
17+
const currentNumber = nums[index];
18+
19+
// Add current number to total sum
20+
totalSum += currentNumber;
21+
22+
const currentRemainder = currentNumber % 3;
23+
24+
if (currentRemainder === 1) {
25+
// Maintain first and second-smallest numbers with remainder 1
26+
if (currentNumber < minRemainderOneFirst) {
27+
minRemainderOneSecond = minRemainderOneFirst;
28+
minRemainderOneFirst = currentNumber;
29+
} else if (currentNumber < minRemainderOneSecond) {
30+
minRemainderOneSecond = currentNumber;
31+
}
32+
} else if (currentRemainder === 2) {
33+
// Maintain first and second-smallest numbers with remainder 2
34+
if (currentNumber < minRemainderTwoFirst) {
35+
minRemainderTwoSecond = minRemainderTwoFirst;
36+
minRemainderTwoFirst = currentNumber;
37+
} else if (currentNumber < minRemainderTwoSecond) {
38+
minRemainderTwoSecond = currentNumber;
39+
}
40+
}
41+
}
42+
43+
// If already divisible by 3, we keep the full sum
44+
const remainder = totalSum % 3;
45+
if (remainder === 0) {
46+
return totalSum;
47+
}
48+
49+
// Compute the minimal amount to subtract to make the sum divisible by 3
50+
let minimumToSubtract = Number.MAX_SAFE_INTEGER;
51+
52+
if (remainder === 1) {
53+
// Option 1: remove one smallest remainder-1 number
54+
if (minRemainderOneFirst !== Number.MAX_SAFE_INTEGER) {
55+
minimumToSubtract = minRemainderOneFirst;
56+
}
57+
58+
// Option 2: remove two smallest remainder-2 numbers
59+
if (minRemainderTwoSecond !== Number.MAX_SAFE_INTEGER) {
60+
const removeTwoRemainderTwo =
61+
minRemainderTwoFirst + minRemainderTwoSecond;
62+
63+
if (removeTwoRemainderTwo < minimumToSubtract) {
64+
minimumToSubtract = removeTwoRemainderTwo;
65+
}
66+
}
67+
} else {
68+
// remainder === 2
69+
70+
// Option 1: remove one smallest remainder-2 number
71+
if (minRemainderTwoFirst !== Number.MAX_SAFE_INTEGER) {
72+
minimumToSubtract = minRemainderTwoFirst;
73+
}
74+
75+
// Option 2: remove two smallest remainder-1 numbers
76+
if (minRemainderOneSecond !== Number.MAX_SAFE_INTEGER) {
77+
const removeTwoRemainderOne =
78+
minRemainderOneFirst + minRemainderOneSecond;
79+
80+
if (removeTwoRemainderOne < minimumToSubtract) {
81+
minimumToSubtract = removeTwoRemainderOne;
82+
}
83+
}
84+
}
85+
86+
// Fallback: if no valid removal strategy exists, result is zero
87+
if (minimumToSubtract === Number.MAX_SAFE_INTEGER) {
88+
return 0;
89+
}
90+
91+
// Return the adjusted maximum sum divisible by 3
92+
return totalSum - minimumToSubtract;
93+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function maxSumDivThree(nums: number[]): number {
2+
3+
}

0 commit comments

Comments
 (0)