From cca1460a90e58a136df691c030c46fc8d0ad92ea Mon Sep 17 00:00:00 2001 From: smori Date: Sun, 21 Jul 2024 00:20:23 +0900 Subject: [PATCH] Add day20 --- Jongeun/Day20/131_PalindromePartitioning.cpp | 52 +++++++++++++++++ .../17_LetterCombinationsofaPhoneNumber.cpp | 42 ++++++++++++++ Jongeun/Day20/355_DesignTwitter.cpp | 56 +++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 Jongeun/Day20/131_PalindromePartitioning.cpp create mode 100644 Jongeun/Day20/17_LetterCombinationsofaPhoneNumber.cpp create mode 100644 Jongeun/Day20/355_DesignTwitter.cpp diff --git a/Jongeun/Day20/131_PalindromePartitioning.cpp b/Jongeun/Day20/131_PalindromePartitioning.cpp new file mode 100644 index 0000000..363fc80 --- /dev/null +++ b/Jongeun/Day20/131_PalindromePartitioning.cpp @@ -0,0 +1,52 @@ +class Solution +{ +public: + vector> partition(string s) + { + vector> res; + vector sub; + _partition(res, sub, s); + return res; + } + + void _partition(vector> &res, vector &sub, string s) + { + if (isPalindrom(s)) + { + sub.push_back(s); + res.push_back(sub); + sub.pop_back(); + } + + for (int i = 0; i < s.size() - 1; i++) + { + string left = s.substr(0, i + 1); + if (isPalindrom(left)) + { + sub.push_back(left); + _partition(res, sub, s.substr(i + 1)); + sub.pop_back(); + } + } + } + + bool isPalindrom(string &s) + { + int l = 0; + int r = s.size() - 1; + while (l <= r) + { + if (s[l] == s[r]) + { + l++; + r--; + } + else + { + return false; + } + } + + return true; + } +}; diff --git a/Jongeun/Day20/17_LetterCombinationsofaPhoneNumber.cpp b/Jongeun/Day20/17_LetterCombinationsofaPhoneNumber.cpp new file mode 100644 index 0000000..49dd2e7 --- /dev/null +++ b/Jongeun/Day20/17_LetterCombinationsofaPhoneNumber.cpp @@ -0,0 +1,42 @@ +class Solution +{ +public: + vector letterCombinations(string digits) + { + + // construct the map + unordered_map m; + m['2'] = "abc"; + m['3'] = "def"; + m['4'] = "ghi"; + m['5'] = "jkl"; + m['6'] = "mno"; + m['7'] = "pqrs"; + m['8'] = "tuv"; + m['9'] = "wxyz"; + + vector res; + if (digits == "") + { + return res; + } + + _letterCombinations(res, digits, m, "", 0); + return res; + } + + void _letterCombinations(vector &res, string &digits, unordered_map &m, string comb, int start) + { + if (start == digits.size()) + { + res.push_back(comb); + return; + } + + for (auto c : m[digits[start]]) + { + + _letterCombinations(res, digits, m, comb + c, start + 1); + } + } +}; diff --git a/Jongeun/Day20/355_DesignTwitter.cpp b/Jongeun/Day20/355_DesignTwitter.cpp new file mode 100644 index 0000000..721e05d --- /dev/null +++ b/Jongeun/Day20/355_DesignTwitter.cpp @@ -0,0 +1,56 @@ +class Twitter +{ +public: + Twitter() + { + } + + void postTweet(int userId, int tweetId) + { + tweets.push_back({userId, tweetId}); + } + + vector getNewsFeed(int userId) + { + vector res; + int num = 10; + unordered_set temp = followMap[userId]; + for (int i = tweets.size() - 1; i >= 0; i--) + { + if (tweets[i].first == userId || temp.find(tweets[i].first) != temp.end()) + { + res.push_back(tweets[i].second); + num--; + } + if (num == 0) + { + break; + } + } + + return res; + } + + void follow(int followerId, int followeeId) + { + followMap[followerId].insert(followeeId); + } + + void unfollow(int followerId, int followeeId) + { + followMap[followerId].erase(followeeId); + } + +private: + unordered_map> followMap; + vector> tweets; +}; + +/** + * Your Twitter object will be instantiated and called as such: + * Twitter* obj = new Twitter(); + * obj->postTweet(userId,tweetId); + * vector param_2 = obj->getNewsFeed(userId); + * obj->follow(followerId,followeeId); + * obj->unfollow(followerId,followeeId); + */