Embercode
✕
Greedy AlgorithmsQuick quiz

Labels and digits

8 questions. Covers Remove k digits for the smallest number.

Questions 1 to 5 of 8

  1. 1. What does this program print? ``cpp #include <algorithm> #include <array> #include <iostream> #include <string_view> #include <vector> std::vector<int> partitionLabels(std::string_view s) { const int n = static_cast<int>(s.size()); std::array<int, 26> last{}; last.fill(-1); for (int i = 0; i < n; ++i) last[s[i] - 'a'] = i; std::vector<int> sizes; int start = 0; int reach = 0; for (int i = 0; i < n; ++i) { reach = std::max(reach, last[s[i] - 'a']); if (i == reach) { sizes.push_back(i - start + 1); start = i + 1; } } return sizes; } int main() { for (int size : partitionLabels("aabcbd")) std::cout << size << ' '; std::cout << '\n'; } `` Choose the exact output, including the spaces between numbers.
  2. 2. What does this program print? ``cpp #include <iostream> #include <string> std::string removeKdigits(std::string_view num, int k) { std::string kept; for (char digit : num) { while (k > 0 && !kept.empty() && kept.back() > digit) { kept.pop_back(); --k; } kept.push_back(digit); } kept.erase(kept.size() - static_cast<std::size_t>(k)); std::size_t first = kept.find_first_not_of('0'); if (first == std::string::npos) { return "0"; } return kept.substr(first); } int main() { std::cout << removeKdigits("3025", 1) << '\n'; } `` Choose the exact output, including the spaces between numbers.
  3. 3. In partitionLabels, a piece runs from index start to index i. Write its length as a formula in i and start.

    Type your answer.

  4. 4. Which std::string member function returns the index of the first character that is not the given character, or std::string::npos when every character matches?

    Type your answer.

  5. 5. Why does the first pass of partitionLabels record the LAST position of each letter?

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.