leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 6a5c6dd5bb9cc5c1a7bc58afec6cf583f43d942a
parent 100c1faec99c7b1210f744dae99361da2e64e41e
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 17 Jan 2024 23:09:48 +0000

3 Random Problems

Diffstat:
AProblems/0676.cpp | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AProblems/0817.cpp | 19+++++++++++++++++++
AProblems/2109.cpp | 14++++++++++++++
MREADME.md | 3+++
4 files changed, 92 insertions(+), 0 deletions(-)

diff --git a/Problems/0676.cpp b/Problems/0676.cpp @@ -0,0 +1,56 @@ +class MagicDictionary { + struct Node { + Node(){}; + Node *children[27] = {nullptr}; + bool &terminate = reinterpret_cast<bool &>(children[0]); + }; + + Node *trie = new Node(); + + public: + void buildDict(const vector<string> &dictionary) { + // create trie and fill it with words + for (const auto &word : dictionary) { + Node *crnt = trie; + for (const char c : word) { + const int idx = c & 0x1F; + if (!crnt->children[idx]) crnt->children[idx] = new Node(); + crnt = crnt->children[idx]; + } + crnt->terminate = true; + } + } + + bool search(const string &searchWord) { + typedef pair<const Node *, int> entry; + stack<entry> st; + + const int n = size(searchWord); + + // generate all posible char changes that make sense + const Node *crnt = trie; + for (int i = 0; i < n; i++) { + const int idx = searchWord[i] & 0x1F; + for (int j = 1; j <= 26; j++) { + if (j == idx) continue; + if (crnt->children[j]) st.emplace(crnt->children[j], i + 1); + } + if (!crnt->children[idx]) break; + crnt = crnt->children[idx]; + } + + // check are any of them valid + while (!st.empty()) { + auto [crnt, start] = st.top(); + st.pop(); + for (int i = start; i < n; i++) { + const int idx = searchWord[i] & 0x1F; + if (!crnt->children[idx]) goto next; + crnt = crnt->children[idx]; + } + if (crnt->terminate) return true; + next:; + } + return false; + } +}; diff --git a/Problems/0817.cpp b/Problems/0817.cpp @@ -0,0 +1,19 @@ +class Solution { + public: + int numComponents(ListNode *head, vector<int> &nums) { + static int seen[10001]; + memset(seen, 0x00, sizeof(seen)); + for (const int n : nums) + seen[n] = true; + + int res = 0, started = 0; + for (const ListNode *crnt = head; crnt; crnt = crnt->next) { + if (!seen[crnt->val]) + res += started, started = 0; + else + started = 1; + } + res += started; + return res; + } +}; diff --git a/Problems/2109.cpp b/Problems/2109.cpp @@ -0,0 +1,14 @@ +class Solution { + public: + string addSpaces(const string &s, const vector<int> &spaces) const { + static char res[600001]; + int i = 0, j = 0, k = 0; + while (i < size(s)) { + if (j < size(spaces) && i == spaces[j]) + res[k++] = ' ', j++; + else + res[k++] = s[i++]; + } + return string(res, k); + } +}; diff --git a/README.md b/README.md @@ -419,6 +419,7 @@ for solving problems. | 0669 | Medium | [Trim a Binary Search Tree](Problems/0669.cpp) | | 0671 | Easy | [Second Minimum Node In a Binary Tree](Problems/0671.cpp) | | 0673 | Medium | [Number of Longest Increasing Subsequence](Problems/0673.cpp) | +| 0676 | Medium | [Implement Magic Dictionary](Problems/0676.cpp) | | 0684 | Medium | [Redundant Connection](Problems/0684.cpp) | | 0688 | Medium | [Knight Probability in Chessboard](Problems/0688.cpp) | | 0690 | Medium | [Employee Importance](Problems/0690.cpp) | @@ -464,6 +465,7 @@ for solving problems. | 0811 | Medium | [Subdomain Visit Count](Problems/0811.cpp) | | 0814 | Medium | [Binary Tree Pruning](Problems/0814.cpp) | | 0815 | Hard | [Bus Routes](Problems/0815.cpp) | +| 0817 | Medium | [Linked List Components](Problems/0817.cpp) | | 0820 | Medium | [Short Encoding of Words](Problems/0820.cpp) | | 0823 | Medium | [Binary Trees With Factors](Problems/0823.cpp) | | 0830 | Medium | [Kth Smallest Element in a BST](Problems/0230.cpp) | @@ -918,6 +920,7 @@ for solving problems. | 2095 | Medium | [Delete the Middle Node of a Linked List](Problems/2095.cpp) | | 2101 | Medium | [Detonate the Maximum Bombs](Problems/2101.cpp) | | 2104 | Medium | [Sum of Subarray Ranges](Problems/2104.cpp) | +| 2109 | Medium | [Adding Spaces to a String](Problems/2109.cpp) | | 2110 | Medium | [Number of Smooth Descent Periods of a Stock](Problems/2110.cpp) | | 2115 | Medium | [Find All Possible Recipes from Given Supplies](Problems/2115.cpp) | | 2120 | Medium | [Execution of All Suffix Instructions Staying in a Grid](Problems/2120.cpp) |