Skip to content

Commit 75d8f06

Browse files
authored
Merge pull request #67 from AlgorithmIsMyLife/17일차_Jongeun
Add day19
2 parents c04e60a + 8ca3b9a commit 75d8f06

File tree

3 files changed

+272
-0
lines changed

3 files changed

+272
-0
lines changed

Jongeun/Day19/212_WordSearchII.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
5+
using namespace std;
6+
7+
class TrieNode
8+
{
9+
public:
10+
TrieNode *children[26];
11+
bool isEnd;
12+
13+
TrieNode()
14+
{
15+
for (int i = 0; i < 26; i++)
16+
{
17+
children[i] = nullptr;
18+
}
19+
isEnd = false;
20+
}
21+
};
22+
23+
class Trie
24+
{
25+
public:
26+
TrieNode *root;
27+
28+
Trie()
29+
{
30+
root = new TrieNode();
31+
}
32+
33+
bool Search(string word)
34+
{
35+
TrieNode *cur = root;
36+
for (auto c : word)
37+
{
38+
if (cur->children[c - 'a'] == nullptr)
39+
{
40+
return false;
41+
}
42+
43+
cur = cur->children[c - 'a'];
44+
}
45+
46+
return cur->isEnd;
47+
}
48+
};
49+
50+
class Solution
51+
{
52+
public:
53+
vector<string> findWords(vector<vector<char>> &board, vector<string> &words)
54+
{
55+
int m = board.size();
56+
int n = board[0].size();
57+
Trie *trie = new Trie();
58+
vector<string> res;
59+
60+
for (int i = 0; i < 1; i++)
61+
{
62+
for (int j = 0; < 1; j++)
63+
{
64+
// construct the trie based on the board
65+
constructTrie(board, trie->root, m, n, i, j);
66+
}
67+
}
68+
69+
// search
70+
for (string &str : words)
71+
{
72+
if (trie->Search(str))
73+
{
74+
res.push_back(str);
75+
}
76+
}
77+
78+
return res;
79+
}
80+
81+
void constructTrie(vector<vector<char>> &board, TrieNode *node, int m, int n, int i, int j)
82+
{
83+
if (i < 0 || i >= m || j < 0 || j >= n)
84+
{
85+
// previous node
86+
node->isEnd = true;
87+
return;
88+
}
89+
90+
// already visited
91+
if (board[i][j] == '#')
92+
{
93+
node->isEnd = true;
94+
return;
95+
}
96+
97+
char c = board[i][j];
98+
99+
if (node->children[c - 'a'] == nullptr)
100+
{
101+
TrieNode *temp = new TrieNode();
102+
node->children[c - 'a'] = temp;
103+
}
104+
105+
board[i][j] = '#';
106+
constructTrie(board, node->children[c - 'a'], m, n, i - 1, j);
107+
constructTrie(board, node->children[c - 'a'], m, n, i, j - 1);
108+
constructTrie(board, node->children[c - 'a'], m, n, i, j + 1);
109+
constructTrie(board, node->children[c - 'a'], m, n, i + 1, j);
110+
board[i][j] = c;
111+
}
112+
};
113+
114+
int main()
115+
{
116+
Solution sol;
117+
vector<vector<char>> board = {{'o', 'a', 'a', 'n'},
118+
{'e', 't', 'a', 'e'},
119+
{'i', 'h', 'k', 'r'},
120+
{'i', 'f', 'l', 'v'}};
121+
vector<string> words = {"oath", "pea", "eat", "rain"};
122+
vector<string> res = sol.findWords(board, words);
123+
124+
for (string &str : res)
125+
{
126+
cout << str << " ";
127+
}
128+
129+
return 0;
130+
}

Jongeun/Day19/621_TaskScheduler.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class Solution
2+
{
3+
public:
4+
// Structure of the condition
5+
// for sorting the pair by its
6+
// second elements
7+
struct myComp
8+
{
9+
constexpr bool operator()(
10+
pair<int, int> const &a,
11+
pair<int, int> const &b)
12+
const noexcept
13+
{
14+
return a.second > b.second;
15+
}
16+
};
17+
int leastInterval(vector<char> &tasks, int n)
18+
{
19+
vector<bool> isInQueue(26);
20+
vector<int> leftTasks(26);
21+
int totalLeft{};
22+
priority_queue<pair<int, int>, vector<pair<int, int>>, myComp> mq; // min heap
23+
24+
for (int i = 0; i < tasks.size(); i++)
25+
{
26+
27+
leftTasks[tasks[i] - 'A']++;
28+
totalLeft++;
29+
}
30+
31+
int time = 1;
32+
while (totalLeft > 0)
33+
{
34+
// Pop
35+
if (!mq.empty() && (time - mq.top().second) > n)
36+
{
37+
int i = mq.top().first;
38+
isInQueue[i] = false;
39+
mq.pop();
40+
}
41+
42+
// Push
43+
// find the one with the most left and not in queue
44+
// O(26)
45+
int mostIndex = -1;
46+
int most = 0;
47+
for (int i = 0; i < leftTasks.size(); i++)
48+
{
49+
if (!isInQueue[i] && leftTasks[i] > most)
50+
{
51+
most = leftTasks[i];
52+
mostIndex = i;
53+
}
54+
}
55+
56+
if (mostIndex != -1)
57+
{
58+
mq.push({mostIndex, time});
59+
isInQueue[mostIndex] = true;
60+
leftTasks[mostIndex]--;
61+
totalLeft--;
62+
}
63+
64+
time++;
65+
}
66+
67+
return time - 1;
68+
}
69+
};

Jongeun/Day19/79_WordSearch.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Solution
2+
{
3+
public:
4+
bool exist(vector<vector<char>> &board, string word)
5+
{
6+
bool isFind{false};
7+
8+
vector<vector<bool>> isPassed;
9+
10+
for (int i = 0; i < board.size(); i++)
11+
{
12+
vector<bool> temp;
13+
for (int j = 0; j < board[i].size(); j++)
14+
{
15+
temp.push_back(false);
16+
}
17+
isPassed.push_back(temp);
18+
}
19+
20+
for (int i = 0; i < board.size(); i++)
21+
{
22+
for (int j = 0; j < board[i].size(); j++)
23+
{
24+
_exist(board, isPassed, word, i, j, 0, isFind);
25+
}
26+
}
27+
28+
return isFind;
29+
}
30+
31+
void _exist(vector<vector<char>> &board, vector<vector<bool>> &isPassed, string &word, int i, int j, int step, bool &isFind)
32+
{
33+
// bound checking
34+
if (i < 0 || i >= board.size())
35+
{
36+
return;
37+
}
38+
39+
if (j < 0 || j >= board[i].size())
40+
{
41+
return;
42+
}
43+
44+
// safe bound
45+
46+
// is Already Passed?
47+
if (isPassed[i][j])
48+
{
49+
return;
50+
}
51+
52+
// found
53+
if (word[step] == board[i][j])
54+
{
55+
56+
if (step == word.size() - 1)
57+
{
58+
// end index
59+
isFind = true;
60+
return;
61+
}
62+
63+
isPassed[i][j] = true;
64+
// Not End index -> keep search(E,W,S,N)
65+
_exist(board, isPassed, word, i - 1, j, step + 1, isFind);
66+
_exist(board, isPassed, word, i, j - 1, step + 1, isFind);
67+
_exist(board, isPassed, word, i, j + 1, step + 1, isFind);
68+
_exist(board, isPassed, word, i + 1, j, step + 1, isFind);
69+
70+
isPassed[i][j] = false;
71+
}
72+
}
73+
};

0 commit comments

Comments
 (0)