|
| 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)$ |
0 commit comments