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:
@@ -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
|
||||
Reference in New Issue
Block a user