This commit is contained in:
2026-06-01 18:12:40 +08:00
parent c9fe2fab76
commit 0f9312eaee
206 changed files with 409 additions and 215 deletions
+27
View File
@@ -0,0 +1,27 @@
#+ANKI_DECK: study_deck_02
#+title: Notes
* DONE 0242. Valid Anagram :easy:arrays:hashing:counting:
:PROPERTIES:
:ROADMAP: [[file:../roadmap.org::*0242. Valid Anagram][0242. Valid Anagram]]
:END:
** Approach
Frequency counter with fixed-size array (Alpha Frequency Array trick).
Early exit on size mismatch. Single pass over both strings.
** C++
#+begin_src cpp
class Solution {
public:
bool isAnagram(std::string s, std::string t) {
if (s.size() != t.size()) return false;
std::array<int, 26> freq{};
for (int i = 0; i < s.size(); i++) {
freq[s[i] - 'a']++;
freq[t[i] - 'a']--;
}
return std::all_of(freq.begin(), freq.end(), [](int x){ return x == 0; });
}
};
#+end_src
+107
View File
@@ -0,0 +1,107 @@
#+ANKI_DECK: study_deck_02
#+TITLE: DSA Tricks & Patterns
* Common Patterns
** Sliding Window
Use when: contiguous subarray/substring, "longest/shortest/most k"
Template:
#+begin_src cpp
int left = 0;
for (int right = 0; right < n; right++) {
// expand window
while (/* window invalid */) {
// shrink window
left++;
}
// update answer
}
#+end_src
** Two Pointers
Use when: sorted array, palindrome, pair sum
Template:
#+begin_src cpp
int l = 0, r = n - 1;
while (l < r) {
if (/* condition */) l++;
else r--;
}
#+end_src
** Binary Search
Use when: monotonic function, "minimum maximum", "first/last"
Template:
#+begin_src cpp
int lo = 0, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (/* check(mid) */) hi = mid;
else lo = mid + 1;
}
#+end_src
** BFS vs DFS
- BFS: shortest path (unweighted), level-order, queue
- DFS: path existence, backtracking, cycle detection, stack/recursion
** Union-Find
Use when: connected components, cycle detection, grouping
- Path compression + union by rank = O(α(n))
** Monotonic Stack
Use when: next greater/smaller element, stock span
Template:
#+begin_src cpp
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && st.top() < arr[i]) {
// process st.top()
st.pop();
}
st.push(i);
}
#+end_src
** Topological Sort
Use when: dependency ordering, DAG, course schedule
- Kahn's (BFS + in-degree) or DFS post-order reversal
** Dijkstra's
Use when: shortest path, non-negative weights, weighted graph
- Priority queue + relaxation
** Bit Manipulation Tricks
- ~x = -x - 1
- x & (x - 1) clears lowest set bit
- x & -x isolates lowest set bit
- XOR swap: a ^= b; b ^= a; a ^= b;
** KMP / Z-Algorithm
Use when: pattern matching, repeated substrings
- KMP: O(n + m), failure function
- Z: O(n + m), Z-array
** DP State Design
- Ask: what do I need to track? (index, remaining, previous choice)
- State = f(index, constraint1, constraint2, ...)
- Space optimize with rolling array when only previous row needed
** Graph Coloring (2-coloring / Bipartite)
Use when: odd cycle check, two groups
- BFS/DFS alternating colors
** Prefix Sum
Use when: subarray sum, range queries
- Build once O(n), query O(1)
** Trie
Use when: prefix search, autocomplete, word dictionary
- Each node = character, path = word
** Heap / Priority Queue
Use when: k-th element, merge k sorted, median maintenance
- C++: `priority_queue` (max-heap by default)
** Fast & Slow Pointers
Use when: cycle detection, linked list midpoint
- Floyd's tortoise and hare
+28
View File
@@ -0,0 +1,28 @@
#+ANKI_DECK: study_deck_02
#+TITLE: DSA Tricks & Patterns
* Task: Count character frequencies faster than map :dsa:counting:array:string:retrieval::production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ROADMAP: [[file:../roadmap.org::*0242. Valid Anagram][0242. Valid Anagram]]
:ANKI_NOTE_ID: 1780308820578
:END:
** Front
:PROPERTIES:
:ANKI_NOTE_ID: 1780308820574
:END:
Write C++ to count character frequencies in a string using a
fixed-size array instead of ~std::map~ (assume lowercase a-z only).
** Back
:PROPERTIES:
:ANKI_NOTE_ID: 1780308820567
:END:
#+begin_src cpp
std::array<int, 26> freq{};
for (char c : s) freq[c - 'a']++;
#+end_src
*Why:* O(1) per access vs O(log n) for ~std::map~. Use when alphabet
is bounded and small.