Skip to content

[PDKhan] WEEK 14 solutions #1642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 6, 2025
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
35 changes: 35 additions & 0 deletions binary-tree-level-order-traversal/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;

if(!root)
return result;

queue<TreeNode*> q;

q.push(root);

while(!q.empty()){
int len = q.size();
vector<int> level;

for(int i = 0; i < len; i++){
TreeNode* curr = q.front();
q.pop();

if(curr->left)
q.push(curr->left);

if(curr->right)
q.push(curr->right);

level.push_back(curr->val);
}

result.push_back(level);
}

return result;
}
};
12 changes: 12 additions & 0 deletions counting-bits/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
vector<int> countBits(int n) {
vector<int> result(n + 1, 0);

for(int i = 0; i <= n; i++){
result[i] = result[i >> 1] + (i & 1);
}

return result;
}
};
27 changes: 27 additions & 0 deletions house-robber-ii/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int robsearch(vector<int>& nums, int start, int end) {
int prev = 0;
int prevprev = 0;

for(int i = start; i <= end; i++){
int curr = max(prev, nums[i] + prevprev);

prevprev = prev;
prev = curr;
}

return prev;
}

int rob(vector<int>& nums) {
int len = nums.size();

if(len == 0)
return 0;
if(len == 1)
return nums[0];

return max(robsearch(nums, 0, len - 2), robsearch(nums, 1, len - 1));
}
};
34 changes: 34 additions & 0 deletions meeting-rooms-ii/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
/**
* @param intervals: an array of meeting time intervals
* @return: the minimum number of conference rooms required
*/
int minMeetingRooms(vector<Interval> &intervals) {
// Write your code here
if(intervals.empty())
return 0;

vector<int> starts, ends;

for(int i = 0; i < intervals.size(); i++){
starts.push_back(intervals[i].start);
ends.push_back(intervals[i].end);
}

sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());

int result = 0;
int endpos = 0;

for(int i = 0; i < starts.size(); i++){
if(starts[i] < ends[endpos])
result++;
else
endpos++;
}

return result;
}
};
65 changes: 65 additions & 0 deletions word-search-ii/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class TrieNode{
public:
TrieNode* children[26] = {};
string word = "";
};

class Solution {
public:
void insert(TrieNode* root, const string& word){
TrieNode* node = root;

for(int i = 0; i < word.length(); i++){
int idx = word[i] - 'a';

if(!node->children[idx])
node->children[idx] = new TrieNode();

node = node->children[idx];
}
node->word = word;
}

void dfs(int r, int c, vector<vector<char>>& board, TrieNode* root, vector<string>& result){
if(r < 0 || r >= board.size() || c < 0 || c >= board[r].size())
return;

char ch = board[r][c];

if(ch == '#' || !root->children[ch - 'a'])
return;

root = root->children[ch - 'a'];

if(!root->word.empty()){
result.push_back(root->word);
root->word.clear();
}

board[r][c] = '#';

dfs(r - 1, c, board, root, result);
dfs(r + 1, c, board, root, result);
dfs(r, c - 1, board, root, result);
dfs(r, c + 1, board, root, result);

board[r][c] = ch;
}

vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
TrieNode* root = new TrieNode();

for(int i = 0; i < words.size(); i++)
insert(root, words[i]);

vector<string> result;

for(int i = 0; i < board.size(); i++){
for(int j = 0; j < board[i].size(); j++){
dfs(i, j, board, root, result);
}
}

return result;
}
};