↓快轉到主要內容
  1. LeetCode/

LeetCode 3960: Frequency Balance Subarray

·1 分鐘· ·
LeetCode Medium Hash-Table Counting Array
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: medium 第一次嘗試:2026-06-18

  • 提交紀錄:Wrong Answer → Accepted

解題思路
#

n <= 1000,O(n²) 可以。固定左端點往右擴,每加一個數只改動 O(1) 個計數(Hash Table 模板四):

  • cnt:值 -> 次數。
  • freq:次數 -> 有幾個值是這個次數。值的次數從 c 變成 c + 1 時,freq[c] 減 1(減到 0 要刪掉)、freq[c + 1] 加 1。

成立的條件:只有一種值,或 freq 剛好有兩種次數而且大的是小的兩倍。

我的嘗試
#

第一版多了一個條件 min(freq_map) in apperance_map,把「次數」拿去查「值 -> 次數」的表,兩者意義不同。例如 [2, 3, 2]:2 出現 2 次、3 出現 1 次,整段成立,答案 3;最小的次數 1 不是陣列裡的值,這版回傳 1。拿掉這個條件就 AC 了。

正確解法
#

class Solution:
    def getLength(self, nums: List[int]) -> int:
        n, best = len(nums), 0
        for i in range(n):
            cnt, freq = {}, Counter()
            for j in range(i, n):
                c = cnt.get(nums[j], 0)
                if c:
                    freq[c] -= 1
                    if freq[c] == 0:
                        del freq[c]
                cnt[nums[j]] = c + 1
                freq[c + 1] += 1
                if len(cnt) == 1 or (len(freq) == 2 and max(freq) == 2 * min(freq)):
                    best = max(best, j - i + 1)
        return best

複雜度
#

時間 O(n²),空間 O(n)。max(freq) 和 min(freq) 只在 len(freq) == 2 時才算,所以是 O(1)。

要特別避免的錯誤
#

  • 次數減到 0 要刪掉,不然 len(freq) 不準。
  • 「次數」和「值」是兩種不同的 key,不要混用。

面試口說整理
#

n 只有 1000,我固定左端點往右擴,同時維護每個值的次數,以及每種次數有幾個值。每加一個數只更新兩個次數的計數。只有一種值,或剛好兩種次數而且大的是小的兩倍時就更新答案。時間 O(n²)。

相關文章

LeetCode 923: 3Sum With Multiplicity
·3 分鐘
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 解題筆記:排序後固定第一個數、對撞指針找另外兩個;兩端值相同時用組合數一次算完,不同時把兩端各自的重複數相乘
LeetCode 3679: Minimum Discards to Balance Inventory
·1 分鐘
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 解題筆記:規則已經決定哪些要丟,直接模擬;長度 w 的視窗只計算「留下來」的到貨,被丟掉的離開視窗時不能再扣
LeetCode 1814: Count Nice Pairs in an Array
·1 分鐘
LeetCode Medium Array Hash-Table Math Counting
LeetCode 1814 解題筆記:把條件移項成 nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]),按這個值分組數 pair,記得取模
LeetCode 3071: Minimum Operations to Write the Letter Y on a Grid
·1 分鐘
LeetCode Medium Array Hash-Table Matrix Counting
LeetCode 3071 解題筆記:分別數 Y 上和 Y 外每個值的次數,枚舉 Y 的值 a 和背景的值 b(a != b),取最少要改的格子數
LeetCode 229: Majority Element II
·1 分鐘
LeetCode Medium Array Hash-Table Sorting Counting
LeetCode 229 解題筆記:超過 n / 3 的數最多兩個;Boyer-Moore 維護兩個候選人,最後再掃一次確認次數
LeetCode 3843: First Element with Unique Frequency
·1 分鐘
LeetCode Medium Hash-Table Counting Array
LeetCode 3843 解題筆記:數每個值的次數,再數「每種次數有幾個值」;由左到右第一個次數只屬於它自己的就是答案