From bbc9da3b80e3da85bfa651e224dd18e44788620e Mon Sep 17 00:00:00 2001 From: PDKhan Date: Wed, 2 Jul 2025 09:30:41 -0500 Subject: [PATCH 1/5] Counting Bits solution --- counting-bits/PDKhan.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 counting-bits/PDKhan.cpp diff --git a/counting-bits/PDKhan.cpp b/counting-bits/PDKhan.cpp new file mode 100644 index 000000000..ca14dfa8a --- /dev/null +++ b/counting-bits/PDKhan.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + vector countBits(int n) { + vector result(n + 1, 0); + + for(int i = 0; i <= n; i++){ + result[i] = result[i >> 1] + (i & 1); + } + + return result; + } +}; From eac1612b2d207d2e2c3349818f4aa95abdd9c149 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Wed, 2 Jul 2025 09:31:14 -0500 Subject: [PATCH 2/5] Binary Tree Level Oder Traversal solution --- binary-tree-level-order-traversal/PDKhan.cpp | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 binary-tree-level-order-traversal/PDKhan.cpp diff --git a/binary-tree-level-order-traversal/PDKhan.cpp b/binary-tree-level-order-traversal/PDKhan.cpp new file mode 100644 index 000000000..3bc25d7ca --- /dev/null +++ b/binary-tree-level-order-traversal/PDKhan.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + vector> levelOrder(TreeNode* root) { + vector> result; + + if(!root) + return result; + + queue q; + + q.push(root); + + while(!q.empty()){ + int len = q.size(); + vector 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; + } +}; From e97ed528d7ba9502004edff7c22f7cbd10f6cc70 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Wed, 2 Jul 2025 09:31:38 -0500 Subject: [PATCH 3/5] House Robber II solution --- house-robber-ii/PDKhan.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 house-robber-ii/PDKhan.cpp diff --git a/house-robber-ii/PDKhan.cpp b/house-robber-ii/PDKhan.cpp new file mode 100644 index 000000000..e6f240e02 --- /dev/null +++ b/house-robber-ii/PDKhan.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int robsearch(vector& 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& 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)); + } +}; From 6813e7bbfabaf18ae25e7ae679ef187b7381286d Mon Sep 17 00:00:00 2001 From: PDKhan Date: Wed, 2 Jul 2025 09:31:55 -0500 Subject: [PATCH 4/5] Meeting Rooms II solution --- meeting-rooms-ii/PDKhan.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 meeting-rooms-ii/PDKhan.cpp diff --git a/meeting-rooms-ii/PDKhan.cpp b/meeting-rooms-ii/PDKhan.cpp new file mode 100644 index 000000000..f7a39ad87 --- /dev/null +++ b/meeting-rooms-ii/PDKhan.cpp @@ -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 &intervals) { + // Write your code here + if(intervals.empty()) + return 0; + + vector 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; + } +}; From 688bc99923ba45eaf9cf8302b211f7231c6bd078 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Wed, 2 Jul 2025 09:32:11 -0500 Subject: [PATCH 5/5] Word Search II solution --- word-search-ii/PDKhan.cpp | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 word-search-ii/PDKhan.cpp diff --git a/word-search-ii/PDKhan.cpp b/word-search-ii/PDKhan.cpp new file mode 100644 index 000000000..541d4cce0 --- /dev/null +++ b/word-search-ii/PDKhan.cpp @@ -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>& board, TrieNode* root, vector& 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 findWords(vector>& board, vector& words) { + TrieNode* root = new TrieNode(); + + for(int i = 0; i < words.size(); i++) + insert(root, words[i]); + + vector 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; + } +};