簡単 0件解決済み 正答率0%

To-Do List

Keep a list of tasks: add them, mark them done, remove them and report what is still pending.

このチャレンジで学べること

  • Keep tasks with a title and a done flag
  • Find an item by title before changing it
  • Filter the items that are still pending

問題

翻訳を表示

A to-do app keeps tasks in a typed collection such as List<T>. Each task has a title and a done flag.

Start with an empty list and process the operations, each a command and a title (the title is everything after the first space):

  • add <title>: add a pending task at the end, unless a task with that title already exists.
  • done <title>: mark that task as done (no effect if it does not exist).
  • remove <title>: delete that task (no effect if it does not exist).

Implement todo_list(operations).

Input

operations: The operations. Titles may contain spaces and are compared exactly.

Output

Return the titles of the tasks that are not done, in the order they were added.

制約

0 ≤ number of operations ≤ 10,000

Titles have 1 to 50 characters

例

例 1
入力:
operations = ["add Buy milk", "add Write report", "done Buy milk", "add Call Ada"]

期待される出力:
expected = ["Write report", "Call Ada"]
入力:
std::vector<std::string> operations = {"add Buy milk", "add Write report", "done Buy milk", "add Call Ada"};

期待される出力:
std::vector<std::string> expected = {"Write report", "Call Ada"};
入力:
string[] operations = new string[] {"add Buy milk", "add Write report", "done Buy milk", "add Call Ada"};

期待される出力:
string[] expected = new string[] {"Write report", "Call Ada"};

解説

Buy milk is done, so only the other two remain, in the order they were added: ["Write report", "Call Ada"].

例 2
入力:
operations = ["add Gym", "add Gym", "remove Gym", "done Gym"]

期待される出力:
expected = []
入力:
std::vector<std::string> operations = {"add Gym", "add Gym", "remove Gym", "done Gym"};

期待される出力:
std::vector<std::string> expected = {};
入力:
string[] operations = new string[] {"add Gym", "add Gym", "remove Gym", "done Gym"};

期待される出力:
string[] expected = new string[] {};

解説

The second add is ignored because Gym already exists; removing it leaves nothing, and done on a missing task does nothing: [].

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Store each task as a small record of title and done.

ヒント 2

Look a task up by its exact title before adding, completing or removing it.

ヒント 3

At the end keep only the tasks that are not done, in insertion order.

目安の計算量

O(n²) time, O(n) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。