This commit is contained in:
2026-05-03 23:48:22 +08:00
parent ffdbb39158
commit eaed0664f3
2 changed files with 243 additions and 0 deletions
+24
View File
@@ -57,3 +57,27 @@ what is using?
we need to write binary search, lower bound search we need to write binary search, lower bound search
learn about binary search functions the natural one by god learn about binary search functions the natural one by god
keep
#+begin_src c++
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
bool state = false;
int prev = 0;
int best = 0;
nums.push_back(0);
for (int i=0; i<nums.size(); i++) {
if (!state && nums[i]==1) {
prev = i;
state = true;
} else if (state && nums[i]==0) {
best = std::max(best, i-prev);
state = false;
}
}
return best;
}
};
#+end_src
+219
View File
@@ -0,0 +1,219 @@
* Task: Create a std::map with initial values :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734300
:END:
** Front
Write C++ code to create a ~std::map<std::string, int>~ initialized with ~{{"apple", 1}, {"banana", 2}}~
** Back
#+begin_src c++
std::map<std::string, int> m = {{"apple", 1}, {"banana", 2}};
#+end_src
* Task: Access a map element safely with at() :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734296
:END:
** Front
Write code to safely access ~m.at("apple")~ from a ~std::map<std::string, int>~
** Back
#+begin_src c++
std::map<std::string, int> m = {{"apple", 1}};
int val = m.at("apple"); // returns 1, throws if not found
#+end_src
* Task: Insert into map with insert() :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734292
:END:
** Front
Write code to insert ~{"cherry", 3}~ into a ~std::map<std::string, int>~ using ~insert()~
** Back
#+begin_src c++
auto [it, success] = m.insert({"cherry", 3});
// Returns pair<iterator, bool> — success is true if inserted
#+end_src
* Task: Insert or assign with insert_or_assign :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734288
:END:
** Front
Write code to insert ~{"apple", 99}~ or update it if it already exists
** Back
#+begin_src c++
auto [it, inserted] = m.insert_or_assign("apple", 99);
// If "apple" exists, updates value to 99
// inserted is false if key existed, true if newly inserted
#+end_src
* Task: Construct element in-place with emplace :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734284
:END:
** Front
Write code to construct a ~std::map<std::string, std::vector<int>>~ element in-place without copying
** Back
#+begin_src c++
std::map<std::string, std::vector<int>> m;
m.emplace(std::piecewise_construct,
std::forward_as_tuple("nums"),
std::forward_as_tuple(3, 1)); // vector with {1,1,1}
#+end_src
* Task: Insert only if key missing with try_emplace :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734280
:END:
** Front
Write code to only insert "banana" if it's NOT already in the map
** Back
#+begin_src c++
m.try_emplace("banana", 99);
// Does nothing if "banana" exists, inserts if missing
// Unlike insert_or_assign, never overwrites existing value
#+end_src
* Task: Iterate over all key-value pairs :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734276
:END:
** Front
Write code to iterate over a ~std::map<std::string, int>~ and print all key-value pairs
** Back
#+begin_src c++
for (const auto& [key, val] : m) {
std::cout << key << ": " << val << "\n";
}
#+end_src
* Task: Check if key exists with contains (C++20) :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734272
:END:
** Front
Write code to check if "apple" exists in a ~std::map<std::string, int>~ using C++20
** Back
#+begin_src c++
if (m.contains("apple")) {
// key exists
}
#+end_src
* Task: Find element and check existence :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734268
:END:
** Front
Write code to find "apple" in a map and only process if found
** Back
#+begin_src c++
auto it = m.find("apple");
if (it != m.end()) {
// it->first = key, it->second = value
int val = it->second;
}
#+end_src
* Task: Erase element by key :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734264
:END:
** Front
Write code to remove the element with key "apple" from a ~std::map~
** Back
#+begin_src c++
size_t erased = m.erase("apple");
// Returns number of elements erased (0 or 1)
#+end_src
* Task: Range query with lower_bound and upper_bound :cpp:map:retrieval:production:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734260
:END:
** Front
Write code to find all elements with keys between "apple" and "cherry" (inclusive)
** Back
#+begin_src c++
auto [start, end] = m.equal_range("apple");
// lower_bound = first >= "apple"
// upper_bound = first > "apple"
for (auto it = start; it != end; ++it) {
// process it->first, it->second
}
#+end_src
* std::map vs std::unordered_map :cpp:map:retrieval:comparison:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734256
:END:
** Front
What are the key differences between ~std::map~ and ~std::unordered_map~?
** Back
- ~std::map~: sorted by key, O(log n) operations, uses red-black tree
- ~std::unordered_map~: unsorted, O(1) avg operations, uses hash table
- Use ~std::map~ when you need sorted iteration or range queries
- Use ~std::unordered_map~ when you need fastest lookups and order doesn't matter
* What is value_type for std::map? :cpp:map:retrieval:recognition:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734252
:END:
** Front
What is the ~value_type~ of ~std::map<Key, T>~?
** Back
~std::pair<const Key, T>~
* insert() return type :cpp:map:retrieval:recognition:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734247
:END:
** Front
What does ~insert()~ return on a ~std::map~?
** Back
~std::pair<iterator, bool>~
- ~iterator~ points to the inserted (or existing) element
- ~bool~ is ~true~ if insertion happened, ~false~ if key already existed
* Why avoid operator[] for lookup? :cpp:map:retrieval:recognition:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734242
:END:
** Front
Why is it risky to use ~map["missing"]~ to check if a key exists?
** Back
~operator[]~ inserts a default-constructed value if the key doesn't exist, modifying the map as a side effect. Use ~find()~ or ~contains()~ (C++20) for read-only lookup.
* What does extract() do? (C++17) :cpp:map:retrieval:recognition:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734238
:END:
** Front
What does ~m.extract(key)~ do on a ~std::map~?
** Back
Removes the element with that key and returns a ~node_type~ (a node handle). The node can be inserted into another container without copying. O(log n) but no allocator overhead.
* What does merge() do? (C++17) :cpp:map:retrieval:recognition:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:ANKI_NOTE_ID: 1777821734232
:END:
** Front
What does ~m1.merge(m2)~ do?
** Back
Transfers elements from ~m2~ to ~m1~. For each element transferred, if a key already exists in ~m1~, that element stays in ~m2~. No element copies are made.