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
130 changes: 130 additions & 0 deletions Jongeun/Day19/212_WordSearchII.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class TrieNode
{
public:
TrieNode *children[26];
bool isEnd;

TrieNode()
{
for (int i = 0; i < 26; i++)
{
children[i] = nullptr;
}
isEnd = false;
}
};

class Trie
{
public:
TrieNode *root;

Trie()
{
root = new TrieNode();
}

bool Search(string word)
{
TrieNode *cur = root;
for (auto c : word)
{
if (cur->children[c - 'a'] == nullptr)
{
return false;
}

cur = cur->children[c - 'a'];
}

return cur->isEnd;
}
};

class Solution
{
public:
vector<string> findWords(vector<vector<char>> &board, vector<string> &words)
{
int m = board.size();
int n = board[0].size();
Trie *trie = new Trie();
vector<string> res;

for (int i = 0; i < 1; i++)
{
for (int j = 0; < 1; j++)
{
// construct the trie based on the board
constructTrie(board, trie->root, m, n, i, j);
}
}

// search
for (string &str : words)
{
if (trie->Search(str))
{
res.push_back(str);
}
}

return res;
}

void constructTrie(vector<vector<char>> &board, TrieNode *node, int m, int n, int i, int j)
{
if (i < 0 || i >= m || j < 0 || j >= n)
{
// previous node
node->isEnd = true;
return;
}

// already visited
if (board[i][j] == '#')
{
node->isEnd = true;
return;
}

char c = board[i][j];

if (node->children[c - 'a'] == nullptr)
{
TrieNode *temp = new TrieNode();
node->children[c - 'a'] = temp;
}

board[i][j] = '#';
constructTrie(board, node->children[c - 'a'], m, n, i - 1, j);
constructTrie(board, node->children[c - 'a'], m, n, i, j - 1);
constructTrie(board, node->children[c - 'a'], m, n, i, j + 1);
constructTrie(board, node->children[c - 'a'], m, n, i + 1, j);
board[i][j] = c;
}
};

int main()
{
Solution sol;
vector<vector<char>> board = {{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'}};
vector<string> words = {"oath", "pea", "eat", "rain"};
vector<string> res = sol.findWords(board, words);

for (string &str : res)
{
cout << str << " ";
}

return 0;
}
69 changes: 69 additions & 0 deletions Jongeun/Day19/621_TaskScheduler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class Solution
{
public:
// Structure of the condition
// for sorting the pair by its
// second elements
struct myComp
{
constexpr bool operator()(
pair<int, int> const &a,
pair<int, int> const &b)
const noexcept
{
return a.second > b.second;
}
};
int leastInterval(vector<char> &tasks, int n)
{
vector<bool> isInQueue(26);
vector<int> leftTasks(26);
int totalLeft{};
priority_queue<pair<int, int>, vector<pair<int, int>>, myComp> mq; // min heap

for (int i = 0; i < tasks.size(); i++)
{

leftTasks[tasks[i] - 'A']++;
totalLeft++;
}

int time = 1;
while (totalLeft > 0)
{
// Pop
if (!mq.empty() && (time - mq.top().second) > n)
{
int i = mq.top().first;
isInQueue[i] = false;
mq.pop();
}

// Push
// find the one with the most left and not in queue
// O(26)
int mostIndex = -1;
int most = 0;
for (int i = 0; i < leftTasks.size(); i++)
{
if (!isInQueue[i] && leftTasks[i] > most)
{
most = leftTasks[i];
mostIndex = i;
}
}

if (mostIndex != -1)
{
mq.push({mostIndex, time});
isInQueue[mostIndex] = true;
leftTasks[mostIndex]--;
totalLeft--;
}

time++;
}

return time - 1;
}
};
73 changes: 73 additions & 0 deletions Jongeun/Day19/79_WordSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class Solution
{
public:
bool exist(vector<vector<char>> &board, string word)
{
bool isFind{false};

vector<vector<bool>> isPassed;

for (int i = 0; i < board.size(); i++)
{
vector<bool> temp;
for (int j = 0; j < board[i].size(); j++)
{
temp.push_back(false);
}
isPassed.push_back(temp);
}

for (int i = 0; i < board.size(); i++)
{
for (int j = 0; j < board[i].size(); j++)
{
_exist(board, isPassed, word, i, j, 0, isFind);
}
}

return isFind;
}

void _exist(vector<vector<char>> &board, vector<vector<bool>> &isPassed, string &word, int i, int j, int step, bool &isFind)
{
// bound checking
if (i < 0 || i >= board.size())
{
return;
}

if (j < 0 || j >= board[i].size())
{
return;
}

// safe bound

// is Already Passed?
if (isPassed[i][j])
{
return;
}

// found
if (word[step] == board[i][j])
{

if (step == word.size() - 1)
{
// end index
isFind = true;
return;
}

isPassed[i][j] = true;
// Not End index -> keep search(E,W,S,N)
_exist(board, isPassed, word, i - 1, j, step + 1, isFind);
_exist(board, isPassed, word, i, j - 1, step + 1, isFind);
_exist(board, isPassed, word, i, j + 1, step + 1, isFind);
_exist(board, isPassed, word, i + 1, j, step + 1, isFind);

isPassed[i][j] = false;
}
}
};