From a4cf7ca2aa4bbf053cdba2deab9a41c3d975c81d Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 4 May 2025 18:52:21 -0700 Subject: [PATCH 1/5] Valid Parentheses solution --- valid-parentheses/PDKhan.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 valid-parentheses/PDKhan.cpp diff --git a/valid-parentheses/PDKhan.cpp b/valid-parentheses/PDKhan.cpp new file mode 100644 index 000000000..400a78286 --- /dev/null +++ b/valid-parentheses/PDKhan.cpp @@ -0,0 +1,25 @@ +class Solution { + public: + bool isValid(string s) { + stack st; + + for(char ch : s){ + if(ch == ')'){ + if(st.empty() || st.top() != '(') + return false; + st.pop(); + }else if(ch == '}'){ + if(st.empty() || st.top() != '{') + return false; + st.pop(); + }else if(ch == ']'){ + if(st.empty() || st.top() != '[') + return false; + st.pop(); + }else + st.push(ch); + } + + return st.empty(); + } + }; From 8edebd31a9c22bcfa47ae8d514d98691a3bb1e0c Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 4 May 2025 18:52:40 -0700 Subject: [PATCH 2/5] container with most watter solution --- container-with-most-water/PDKhan.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 container-with-most-water/PDKhan.cpp diff --git a/container-with-most-water/PDKhan.cpp b/container-with-most-water/PDKhan.cpp new file mode 100644 index 000000000..388d6c44f --- /dev/null +++ b/container-with-most-water/PDKhan.cpp @@ -0,0 +1,22 @@ +class Solution { + public: + int maxArea(vector& height) { + int result = 0; + int left = 0; + int right = height.size() - 1; + + while(left < right){ + int len = right - left; + + if(height[left] < height[right]){ + result = max(result, height[left] * len); + left++; + }else{ + result = max(result, height[right] * len); + right--; + } + } + + return result; + } + }; From a705e6f8c19d35f77dd1cc91a33826e1467b3f18 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 4 May 2025 18:53:02 -0700 Subject: [PATCH 3/5] Design Add And Search Words Data Structure solution --- .../PDKhan.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 design-add-and-search-words-data-structure/PDKhan.cpp diff --git a/design-add-and-search-words-data-structure/PDKhan.cpp b/design-add-and-search-words-data-structure/PDKhan.cpp new file mode 100644 index 000000000..a525dde3d --- /dev/null +++ b/design-add-and-search-words-data-structure/PDKhan.cpp @@ -0,0 +1,60 @@ +class Trie{ + public: + Trie* children[26]; + bool isEnd; + + Trie(){ + for(int i = 0; i < 26; i++) + children[i] = nullptr; + + isEnd = false; + } + }; + + class WordDictionary { + private: + Trie* trie; + public: + WordDictionary() { + trie = new Trie(); + } + + void addWord(string word) { + Trie* node = trie; + + for(char ch : word){ + int index = ch - 'a'; + + if(node->children[index] == nullptr) + node->children[index] = new Trie(); + + node = node->children[index]; + } + + node->isEnd = true; + } + + bool dfs(Trie* node, int index, string word){ + if(node == nullptr) + return false; + + if(index == word.length()) + return node->isEnd; + + char ch = word[index]; + + if(ch == '.'){ + for(int i = 0; i < 26; i++){ + if(dfs(node->children[i], index + 1, word) == true) + return true; + } + }else + return dfs(node->children[ch-'a'], index + 1, word); + + return false; + } + + bool search(string word) { + return dfs(trie, 0, word); + } + }; From d5b3ef2b3f2ca24375e6fcffc180e452f447f96c Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 4 May 2025 18:53:58 -0700 Subject: [PATCH 4/5] Longest Increasing Subsequence solution --- longest-increasing-subsequence/PDKhan.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 longest-increasing-subsequence/PDKhan.cpp diff --git a/longest-increasing-subsequence/PDKhan.cpp b/longest-increasing-subsequence/PDKhan.cpp new file mode 100644 index 000000000..629b7c5b6 --- /dev/null +++ b/longest-increasing-subsequence/PDKhan.cpp @@ -0,0 +1,17 @@ +class Solution { + public: + int lengthOfLIS(vector& nums) { + vector sub; + + for(int num : nums){ + auto it = lower_bound(sub.begin(), sub.end(), num); + + if(it == sub.end()) + sub.push_back(num); + else + *it = num; + } + + return sub.size(); + } + }; From e6c45108cf22c5d20c28c3d9059efed145bff5c7 Mon Sep 17 00:00:00 2001 From: PDKhan Date: Sun, 4 May 2025 18:54:18 -0700 Subject: [PATCH 5/5] Spiral Matrix solution --- spiral-matrix/PDKhan.cpp | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 spiral-matrix/PDKhan.cpp diff --git a/spiral-matrix/PDKhan.cpp b/spiral-matrix/PDKhan.cpp new file mode 100644 index 000000000..81d99e7c7 --- /dev/null +++ b/spiral-matrix/PDKhan.cpp @@ -0,0 +1,57 @@ +class Solution { + public: + vector spiralOrder(vector>& matrix) { + enum direction { R, D, L, U }; + + enum direction dir = R; + vector result; + int min_R = 0; + int max_R = matrix.size() - 1; + int min_C = 0; + int max_C = matrix[0].size() - 1; + int r = 0; + int c = 0; + int size = matrix.size() * matrix[0].size(); + + while(result.size() < size){ + result.push_back(matrix[r][c]); + + switch(dir){ + case R: + if(c == max_C){ + dir = D; + min_R++; + r++; + }else + c++; + break; + case D: + if(r == max_R){ + dir = L; + max_C--; + c--; + }else + r++; + break; + case L: + if(c == min_C){ + dir = U; + max_R--; + r--; + }else + c--; + break; + case U: + if(r == min_R){ + dir = R; + min_C++; + c++; + }else + r--; + break; + } + } + + return result; + } + };