Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Jongeun/Day15/1046_LastStoneWeight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

class Solution
{
public:
int lastStoneWeight(vector<int> &stones)
{
priority_queue<int> 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<int> stones = {2, 7, 4, 1, 8, 1};
cout << sol.lastStoneWeight(stones) << endl;
return 0;
}
116 changes: 116 additions & 0 deletions Jongeun/Day15/208_ImplementTriePrefixTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <iostream>
#include <vector>
#include <string>

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);
*/
71 changes: 71 additions & 0 deletions Jongeun/Day15/39_CombinationSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

class Solution
{
public:
vector<vector<int>> combinationSum(vector<int> &candidates, int target)
{

vector<vector<int>> result;
// sort(candidates.begin(),candidates.end());

unordered_map<int, int> comb;
for (auto i : candidates)
{
comb[i] = 0;
}
_combinationSum(result, candidates, comb, target, 0);
return result;
}

void _combinationSum(vector<vector<int>> &result, vector<int> &candidates, unordered_map<int, int> &comb, int left, int step)
{
if (left == 0)
{
vector<int> 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<int> candidates = {2, 3, 6, 7};
int target = 7;
vector<vector<int>> result = obj.combinationSum(candidates, target);
for (auto i : result)
{
for (auto j : i)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}