add flashcard generation tooling and binary search cards

- gen-flashcards.py: auto-generate recognition cards from all problem files
- toolkit/gen-problem-cards.org: 199 auto-generated problem cards
- 5 binary search tool cards (std::binary_search, std::lower_bound, comparison, two-sum pattern, sorting gotcha)
- two-sum.org: add binary search C++ attempt
- lc-org.el: add doom emacs localleader keybinding support
This commit is contained in:
2026-06-08 01:28:25 +08:00
parent c67841fe07
commit e10cc4257d
9 changed files with 2052 additions and 9 deletions
@@ -67,10 +67,27 @@ class Solution:
** TODO C++
#+begin_src cpp :lc-problem 1
#include <algorithm>
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> twoSum(vector<int>& nums, int target) {
std::sort(nums.begin(), nums.end());
for (int i=0; i< nums.size(); i++) {
int x = nums[i];
if (x > target) {
return {};
}
// TODO: c++ binary search, i forgot how to do this
int want = target - x
auto it = std::binary_learch(nums.begin() + i + 1, nums.end(), want);
if (it != nums.end() && *it == want) {
int wi = std::distance(nums.begin(), it)
return {i, wi}
}
}
reuturn {};
}
};
#+end_src
#+RESULTS:
@@ -0,0 +1,11 @@
#+ANKI_DECK: study_deck_02
* When to use lower_bound vs binary_search :cpp:binary-search:algorithm:retrieval::recognition:
:PROPERTIES:
:END:
** Front
When should you use ~std::lower_bound~ instead of ~std::binary_search~?
** Back
Use ~lower_bound~ when you need the *position/index* of the element.
Use ~binary_search~ when you only need to know *if it exists* (bool).
@@ -0,0 +1,10 @@
#+ANKI_DECK: study_deck_02
* Two Sum gotcha: sorting destroys original indices :cpp:two-sum:binary-search:retrieval::recognition:
:PROPERTIES:
:END:
** Front
If you sort ~nums~ in-place for Two Sum, what goes wrong?
** Back
Sorting changes the indices. If the problem requires returning *original* indices, store ~{value, original_index}~ pairs before sorting.
@@ -0,0 +1,14 @@
#+ANKI_DECK: study_deck_02
* std::binary_search: check element exists :cpp:binary-search:algorithm:retrieval::recognition:
:PROPERTIES:
:END:
** Front
What does ~std::binary_search~ return, and what must the range be?
** Back
Returns ~bool~ (true if element found). The range must be *sorted*.
#+begin_src cpp
bool found = std::binary_search(vec.begin(), vec.end(), value);
#+end_src
@@ -0,0 +1,14 @@
#+ANKI_DECK: study_deck_02
* std::lower_bound: find position of element :cpp:binary-search:algorithm:retrieval::recognition:
:PROPERTIES:
:END:
** Front
What does ~std::lower_bound~ return?
** Back
An iterator to the first element *>=* the given value. Returns ~end()~ if no such element exists.
#+begin_src cpp
auto it = std::lower_bound(vec.begin(), vec.end(), value);
#+end_src
@@ -0,0 +1,16 @@
#+ANKI_DECK: study_deck_02
* Task: Two Sum complement lookup with lower_bound :cpp:binary-search:two-sum:retrieval::production:
:PROPERTIES:
:END:
** Front
Given sorted ~nums~ and index ~i~, write C++ to check if ~target - nums[i]~ exists in the rest of the array using ~std::lower_bound~.
** Back
#+begin_src cpp
int complement = target - nums[i];
auto it = std::lower_bound(nums.begin() + i + 1, nums.end(), complement);
if (it != nums.end() && *it == complement) {
return {(int)i, (int)std::distance(nums.begin(), it)};
}
#+end_src