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; + } +}; 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; + } +}; 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)); + } +}; 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; + } +}; 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; + } +};