Skip to content

feat: greedy, sliding window, DFS, LRU cache#9

Open
paveg wants to merge 1 commit intomainfrom
feat/greedy-sliding-window-dfs-lru
Open

feat: greedy, sliding window, DFS, LRU cache#9
paveg wants to merge 1 commit intomainfrom
feat/greedy-sliding-window-dfs-lru

Conversation

@paveg
Copy link
Copy Markdown
Owner

@paveg paveg commented Mar 30, 2026

Summary

Greedy・Sliding Window・DFS・データ構造系の問題を追加。

  • 452 Minimum Arrows to Burst Balloons — 区間スケジューリング (Greedy)
  • 2134 Minimum Swaps to Group All 1s Together II — 円形スライディングウィンドウ
  • 438 Find All Anagrams in a String — 固定幅スライディングウィンドウ + 文字カウント
  • 1529 Minimum Suffix Flips — 貪欲法
  • 2554 Maximum Number of Integers to Choose From a Range I — 貪欲法 + Set
  • 1151 Minimum Swaps to Group All 1s Together — スライディングウィンドウ
  • 146 LRU Cache — HashMap + 双方向連結リスト
  • 200 Number of Islands — DFS flood fill
  • 23 Merge K Sorted Lists — ヒープ (Hard)
  • 295 Find Median from Data Stream — 双ヒープ (Hard)

アルゴリズム処理機序

1. Minimum Arrows to Burst Balloons (452) — 区間スケジューリング

flowchart LR
    A[区間をendでソート] --> B[arrow = 最初のend]
    B --> C{次の区間のstart > arrow?}
    C -->|Yes| D[arrows++ / arrow更新]
    C -->|No| E[同じ矢で貫通]
    D --> C
    E --> C
Loading
sort.Slice(points, func(i, j int) bool {
    return points[i][1] < points[j][1]
})
result, xEnd := 1, points[0][1]
for i := 1; i < len(points); i++ {
    if points[i][0] > xEnd {
        result++
        xEnd = points[i][1]
    }
}

2. Minimum Swaps to Group All 1s Together II (2134) — 円形スライディングウィンドウ

flowchart LR
    A[1の総数 = windowサイズ] --> B[初期window内の0を数える]
    B --> C[windowを1ずつスライド]
    C --> D[出る要素が0なら zeros--]
    D --> E[入る要素が0なら zeros++]
    E --> F[minZeros更新]
    F --> C
Loading

円形配列は %n でインデックスをラップ

ones := 0
for _, v := range nums {
    ones += v
}
zeros := 0
for i := 0; i < ones; i++ {
    if nums[i] == 0 {
        zeros++
    }
}
minZeros := zeros
for i := 0; i < len(nums); i++ {
    if nums[(i+ones)%n] == 0 {
        zeros++
    }
    if nums[i] == 0 {
        zeros--
    }
    if zeros < minZeros {
        minZeros = zeros
    }
}

3. Find All Anagrams in a String (438) — 固定幅ウィンドウ

flowchart LR
    A["pCount = p の文字頻度"] --> B["sCount = s[0..len(p)] の文字頻度"]
    B --> C{sCount == pCount?}
    C -->|Yes| D[index追加]
    C -->|No| E[ウィンドウを右へ1スライド]
    D --> E
    E --> F["右端追加 / 左端削除"]
    F --> C
Loading
var sCount, pCount [26]int
for i := 0; i < len(p); i++ {
    pCount[p[i]-'a']++
    sCount[s[i]-'a']++
}
for i := len(p); i < len(s); i++ {
    if sCount == pCount {
        result = append(result, i-len(p))
    }
    sCount[s[i]-'a']++
    sCount[s[i-len(p)]-'a']--
}

4. Minimum Suffix Flips (1529) — 貪欲法

flowchart LR
    A["current = '0'"] --> B{"target[i] != current?"}
    B -->|Yes| C[flips++ / current反転]
    B -->|No| D[次へ]
    C --> D
    D --> B
Loading
flips, current := 0, byte('0')
for i := 0; i < len(target); i++ {
    if target[i] != current {
        flips++
        current = target[i]
    }
}

5. Maximum Number of Integers to Choose (2554) — 貪欲法 + Set

ban := map[int]bool{}
for _, b := range banned {
    ban[b] = true
}
count, sum := 0, 0
for i := 1; i <= n; i++ {
    if ban[i] {
        continue
    }
    if sum+i > maxSum {
        break
    }
    sum += i
    count++
}

6. LRU Cache (146) — HashMap + 双方向連結リスト

flowchart TB
    subgraph Structure
        HM[HashMap key→Node]
        DLL["Head ⇄ Node ⇄ ... ⇄ Tail"]
    end
    subgraph Get
        G1{key存在?} -->|Yes| G2[remove → insertHead → return val]
        G1 -->|No| G3[return -1]
    end
    subgraph Put
        P1{key存在?} -->|Yes| P2[val更新 → remove → insertHead]
        P1 -->|No| P3[新Node作成 → insertHead]
        P3 --> P4{cap超過?}
        P4 -->|Yes| P5[tail.prev を remove + delete]
    end
Loading
func (l *LRUCache) Get(key int) int {
    if node, ok := l.cache[key]; ok {
        l.remove(node)
        l.insertHead(node)
        return node.val
    }
    return -1
}

func (l *LRUCache) Put(key int, value int) {
    if node, ok := l.cache[key]; ok {
        node.val = value
        l.remove(node)
        l.insertHead(node)
        return
    }
    node := &Node{key: key, val: value}
    l.cache[key] = node
    l.insertHead(node)
    if len(l.cache) > l.cap {
        lru := l.tail.prev
        l.remove(lru)
        delete(l.cache, lru.key)
    }
}

7. Number of Islands (200) — DFS Flood Fill

flowchart TB
    A[グリッド全走査] --> B{"grid[i][j] == '1'?"}
    B -->|Yes| C[count++ / DFS開始]
    B -->|No| A
    C --> D["現セルを'0'に塗り替え"]
    D --> E[上下左右に再帰]
    E --> A
Loading
var dfs func(i, j int)
dfs = func(i, j int) {
    if i < 0 || i >= y || j < 0 || j >= x || grid[i][j] == '0' {
        return
    }
    grid[i][j] = '0'
    dfs(i-1, j)
    dfs(i+1, j)
    dfs(i, j-1)
    dfs(i, j+1)
}
for i := 0; i < y; i++ {
    for j := 0; j < x; j++ {
        if grid[i][j] == '1' {
            count++
            dfs(i, j)
        }
    }
}

テクニック分類

テクニック 問題
区間スケジューリング (Greedy) 452
スライディングウィンドウ 438, 1151, 2134
貪欲法 1529, 2554
DFS / Flood Fill 200
HashMap + 連結リスト 146
ヒープ 23, 295
サイクル検出 1151 (HackerRank variant)

Test plan

  • go build ./... で全ファイルコンパイル確認
  • 各問題のLeetCode上でAccepted確認

🤖 Generated with Claude Code

Add solutions for interval scheduling, sliding window,
character counting, greedy, cycle detection, and data structure problems.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant