diff --git a/Jongeun/Day15/1046_LastStoneWeight.cpp b/Jongeun/Day15/1046_LastStoneWeight.cpp new file mode 100644 index 0000000..aafd33a --- /dev/null +++ b/Jongeun/Day15/1046_LastStoneWeight.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +using namespace std; + +class Solution +{ +public: + int lastStoneWeight(vector &stones) + { + priority_queue mpq; + for (auto i : stones) + { + mpq.push(i); + } + + while (mpq.size() > 1) + { + int m1 = mpq.top(); + mpq.pop(); + int m2 = mpq.top(); + mpq.pop(); + + if (abs(m1 - m2) != 0) + { + int m3 = abs(m1 - m2); + mpq.push(m2); + } + } + + if (mpq.empty()) + { + return 0; + } + else + { + return mpq.top(); + } + } +}; + +int main() +{ + Solution sol; + vector stones = {2, 7, 4, 1, 8, 1}; + cout << sol.lastStoneWeight(stones) << endl; + return 0; +} diff --git a/Jongeun/Day15/208_ImplementTriePrefixTree.cpp b/Jongeun/Day15/208_ImplementTriePrefixTree.cpp new file mode 100644 index 0000000..29e138c --- /dev/null +++ b/Jongeun/Day15/208_ImplementTriePrefixTree.cpp @@ -0,0 +1,116 @@ +#include +#include +#include + +using namespace std; + +/* + Implement trie (store/retrieve keys in dataset of strings) + + Each node contains pointer to next letter & is word flag + + Time: O(n) insert, O(n) search, O(n) startsWith + Space: O(n) insert, O(1) search, O(1) startsWith +*/ + +class TrieNode +{ +public: + TrieNode *children[26]; + bool isWord; + + TrieNode() + { + for (int i = 0; i < 26; i++) + { + children[i] = NULL; + } + isWord = false; + } +}; + +class Trie +{ +public: + Trie() + { + root = new TrieNode(); + } + + void insert(string word) + { + TrieNode *node = root; + int curr = 0; + + for (int i = 0; i < word.size(); i++) + { + curr = word[i] - 'a'; + if (node->children[curr] == NULL) + { + node->children[curr] = new TrieNode(); + } + node = node->children[curr]; + } + + node->isWord = true; + } + + bool search(string word) + { + TrieNode *node = root; + int curr = 0; + + for (int i = 0; i < word.size(); i++) + { + curr = word[i] - 'a'; + if (node->children[curr] == NULL) + { + return false; + } + node = node->children[curr]; + } + + return node->isWord; + } + + bool startsWith(string prefix) + { + TrieNode *node = root; + int curr = 0; + + for (int i = 0; i < prefix.size(); i++) + { + curr = prefix[i] - 'a'; + if (node->children[curr] == NULL) + { + return false; + } + node = node->children[curr]; + } + + return true; + } + +private: + TrieNode *root; +}; + +int main() +{ + Trie *obj = new Trie(); + obj->insert("apple"); + cout << obj->search("apple") << endl; // 1 + cout << obj->search("app") << endl; // 0 + cout << obj->startsWith("app") << endl; // 1 + obj->insert("app"); + cout << obj->search("app") << endl; // 1 + return 0; +} + +/** + * Your Trie object will be instantiated and called as such: + * Trie* obj = new Trie(); + * obj->insert(word); + * bool param_2 = obj->search(word); + * bool param_3 = obj->startsWith(prefix); + */ diff --git a/Jongeun/Day15/39_CombinationSum.cpp b/Jongeun/Day15/39_CombinationSum.cpp new file mode 100644 index 0000000..f3fb3b4 --- /dev/null +++ b/Jongeun/Day15/39_CombinationSum.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +using namespace std; + +class Solution +{ +public: + vector> combinationSum(vector &candidates, int target) + { + + vector> result; + // sort(candidates.begin(),candidates.end()); + + unordered_map comb; + for (auto i : candidates) + { + comb[i] = 0; + } + _combinationSum(result, candidates, comb, target, 0); + return result; + } + + void _combinationSum(vector> &result, vector &candidates, unordered_map &comb, int left, int step) + { + if (left == 0) + { + vector temp; + for (auto num : comb) + { + for (int i = 0; i < num.second; i++) + { + temp.push_back(num.first); + } + } + result.push_back(temp); + return; + } + else if (left < 0 || step >= candidates.size()) + { + return; + } + + int freq = 0; + + while (left >= 0) + { + comb[candidates[step]] = freq; + left -= candidates[step] * freq; + _combinationSum(result, candidates, comb, left, step + 1); + freq++; + } + } +}; + +int main() +{ + Solution obj; + vector candidates = {2, 3, 6, 7}; + int target = 7; + vector> result = obj.combinationSum(candidates, target); + for (auto i : result) + { + for (auto j : i) + { + cout << j << " "; + } + cout << endl; + } + return 0; +}