From 451650d4690a6111acae1de9e78db8b307e80692 Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Fri, 21 Jul 2023 23:53:05 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.2590 --- .../2590.Design a Todo List/README.md | 99 +++++++++++++++++++ .../2590.Design a Todo List/README_EN.md | 99 +++++++++++++++++++ .../2590.Design a Todo List/Solution.rs | 94 ++++++++++++++++++ 3 files changed, 292 insertions(+) create mode 100644 solution/2500-2599/2590.Design a Todo List/Solution.rs diff --git a/solution/2500-2599/2590.Design a Todo List/README.md b/solution/2500-2599/2590.Design a Todo List/README.md index 29e40e39c0c93..be1cc93adf3fc 100644 --- a/solution/2500-2599/2590.Design a Todo List/README.md +++ b/solution/2500-2599/2590.Design a Todo List/README.md @@ -201,6 +201,105 @@ class TodoList { */ ``` +### **Rust** + +```rust +use std::collections::{HashMap, HashSet}; + +#[derive(Clone)] +struct Task { + task_id: i32, + description: String, + tags: HashSet, + due_date: i32, +} + +struct TodoList { + /// The global task id + id: i32, + /// The mapping from `user_id` to `task` + user_map: HashMap>, +} + +impl TodoList { + fn new() -> Self { + Self { + id: 1, + user_map: HashMap::new(), + } + } + + fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec) -> i32 { + if self.user_map.contains_key(&user_id) { + // Just add the task + self.user_map.get_mut(&user_id).unwrap().push(Task { + task_id: self.id, + description: task_description, + tags: tags.into_iter().collect::>(), + due_date, + }); + // Increase the global id + self.id += 1; + return self.id - 1; + } + // Otherwise, create a new user + self.user_map.insert( + user_id, + vec![Task { + task_id: self.id, + description: task_description, + tags: tags.into_iter().collect::>(), + due_date, + }], + ); + self.id += 1; + self.id - 1 + } + + fn get_all_tasks(&self, user_id: i32) -> Vec { + if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + return vec![]; + } + // Get the task vector + let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); + // Sort by due date + ret_vec.sort_by(|lhs, rhs| { + lhs.due_date.cmp(&rhs.due_date) + }); + // Return the description vector + ret_vec + .into_iter() + .map(|x| x.description) + .collect() + } + + fn get_tasks_for_tag(&self, user_id: i32, tag: String) -> Vec { + if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + return vec![]; + } + // Get the task vector + let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); + // Sort by due date + ret_vec.sort_by(|lhs, rhs| { + lhs.due_date.cmp(&rhs.due_date) + }); + // Return the description vector + ret_vec + .into_iter() + .filter(|x| x.tags.contains(&tag)) + .map(|x| x.description) + .collect() + } + + fn complete_task(&mut self, user_id: i32, task_id: i32) { + if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + return; + } + self.user_map.get_mut(&user_id).unwrap().retain(|x| (*x).task_id != task_id); + } +} +``` + ### **C++** ```cpp diff --git a/solution/2500-2599/2590.Design a Todo List/README_EN.md b/solution/2500-2599/2590.Design a Todo List/README_EN.md index 3ab0b2a46b7ae..5f20a592a146b 100644 --- a/solution/2500-2599/2590.Design a Todo List/README_EN.md +++ b/solution/2500-2599/2590.Design a Todo List/README_EN.md @@ -191,6 +191,105 @@ class TodoList { */ ``` +### **Rust** + +```rust +use std::collections::{HashMap, HashSet}; + +#[derive(Clone)] +struct Task { + task_id: i32, + description: String, + tags: HashSet, + due_date: i32, +} + +struct TodoList { + /// The global task id + id: i32, + /// The mapping from `user_id` to `task` + user_map: HashMap>, +} + +impl TodoList { + fn new() -> Self { + Self { + id: 1, + user_map: HashMap::new(), + } + } + + fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec) -> i32 { + if self.user_map.contains_key(&user_id) { + // Just add the task + self.user_map.get_mut(&user_id).unwrap().push(Task { + task_id: self.id, + description: task_description, + tags: tags.into_iter().collect::>(), + due_date, + }); + // Increase the global id + self.id += 1; + return self.id - 1; + } + // Otherwise, create a new user + self.user_map.insert( + user_id, + vec![Task { + task_id: self.id, + description: task_description, + tags: tags.into_iter().collect::>(), + due_date, + }], + ); + self.id += 1; + self.id - 1 + } + + fn get_all_tasks(&self, user_id: i32) -> Vec { + if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + return vec![]; + } + // Get the task vector + let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); + // Sort by due date + ret_vec.sort_by(|lhs, rhs| { + lhs.due_date.cmp(&rhs.due_date) + }); + // Return the description vector + ret_vec + .into_iter() + .map(|x| x.description) + .collect() + } + + fn get_tasks_for_tag(&self, user_id: i32, tag: String) -> Vec { + if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + return vec![]; + } + // Get the task vector + let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); + // Sort by due date + ret_vec.sort_by(|lhs, rhs| { + lhs.due_date.cmp(&rhs.due_date) + }); + // Return the description vector + ret_vec + .into_iter() + .filter(|x| x.tags.contains(&tag)) + .map(|x| x.description) + .collect() + } + + fn complete_task(&mut self, user_id: i32, task_id: i32) { + if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + return; + } + self.user_map.get_mut(&user_id).unwrap().retain(|x| (*x).task_id != task_id); + } +} +``` + ### **C++** ```cpp diff --git a/solution/2500-2599/2590.Design a Todo List/Solution.rs b/solution/2500-2599/2590.Design a Todo List/Solution.rs new file mode 100644 index 0000000000000..08ded6ed325bf --- /dev/null +++ b/solution/2500-2599/2590.Design a Todo List/Solution.rs @@ -0,0 +1,94 @@ +use std::collections::{HashMap, HashSet}; + +#[derive(Clone)] +struct Task { + task_id: i32, + description: String, + tags: HashSet, + due_date: i32, +} + +struct TodoList { + /// The global task id + id: i32, + /// The mapping from `user_id` to `task` + user_map: HashMap>, +} + +impl TodoList { + fn new() -> Self { + Self { + id: 1, + user_map: HashMap::new(), + } + } + + fn add_task(&mut self, user_id: i32, task_description: String, due_date: i32, tags: Vec) -> i32 { + if self.user_map.contains_key(&user_id) { + // Just add the task + self.user_map.get_mut(&user_id).unwrap().push(Task { + task_id: self.id, + description: task_description, + tags: tags.into_iter().collect::>(), + due_date, + }); + // Increase the global id + self.id += 1; + return self.id - 1; + } + // Otherwise, create a new user + self.user_map.insert( + user_id, + vec![Task { + task_id: self.id, + description: task_description, + tags: tags.into_iter().collect::>(), + due_date, + }], + ); + self.id += 1; + self.id - 1 + } + + fn get_all_tasks(&self, user_id: i32) -> Vec { + if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + return vec![]; + } + // Get the task vector + let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); + // Sort by due date + ret_vec.sort_by(|lhs, rhs| { + lhs.due_date.cmp(&rhs.due_date) + }); + // Return the description vector + ret_vec + .into_iter() + .map(|x| x.description) + .collect() + } + + fn get_tasks_for_tag(&self, user_id: i32, tag: String) -> Vec { + if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + return vec![]; + } + // Get the task vector + let mut ret_vec = (*self.user_map.get(&user_id).unwrap()).clone(); + // Sort by due date + ret_vec.sort_by(|lhs, rhs| { + lhs.due_date.cmp(&rhs.due_date) + }); + // Return the description vector + ret_vec + .into_iter() + .filter(|x| x.tags.contains(&tag)) + .map(|x| x.description) + .collect() + } + + fn complete_task(&mut self, user_id: i32, task_id: i32) { + if !self.user_map.contains_key(&user_id) || self.user_map.get(&user_id).unwrap().is_empty() { + return; + } + self.user_map.get_mut(&user_id).unwrap().retain(|x| (*x).task_id != task_id); + } +} \ No newline at end of file