From a817b92867bd24d5637c7a094b6e4b55d1ade9a0 Mon Sep 17 00:00:00 2001 From: sumitjhaleriya Date: Sun, 29 Oct 2023 19:21:08 +0530 Subject: [PATCH 1/3] Added Solution to Leetcode Problem 515 --- Hard/1425. Constrained Subsequence Sum/sol.md | 2 +- .../sol.cpp | 52 +++++++++++++++++++ .../sol.md | 28 ++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 Medium/515. Find Largest Value in Each Tree Row/sol.cpp create mode 100644 Medium/515. Find Largest Value in Each Tree Row/sol.md diff --git a/Hard/1425. Constrained Subsequence Sum/sol.md b/Hard/1425. Constrained Subsequence Sum/sol.md index 46a5eda9..9695c82f 100644 --- a/Hard/1425. Constrained Subsequence Sum/sol.md +++ b/Hard/1425. Constrained Subsequence Sum/sol.md @@ -1,4 +1,4 @@ -## Approach: Dynamic Programming with Deque +d## Approach: Dynamic Programming with Deque ### Intuition diff --git a/Medium/515. Find Largest Value in Each Tree Row/sol.cpp b/Medium/515. Find Largest Value in Each Tree Row/sol.cpp new file mode 100644 index 00000000..5d16732c --- /dev/null +++ b/Medium/515. Find Largest Value in Each Tree Row/sol.cpp @@ -0,0 +1,52 @@ +#include +#include +using namespace std; + +struct TreeNode +{ + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} +}; + +class Solution +{ +public: +vector largestValues(TreeNode *root) +{ + if (!root) + return {}; + + std::vector result; + std::queue queue; + queue.push(root); + + while (!queue.empty()) + { + int curr_level_size = queue.size(); + int max_val = INT_MIN; + + for (int i = 0; i < curr_level_size; ++i) + { + TreeNode *node = queue.front(); + queue.pop(); + max_val = std::max(max_val, node->val); + + if (node->left) + queue.push(node->left); + if (node->right) + queue.push(node->right); + } + + result.push_back(max_val); + } + + return result; +} +}; +int main() +{ + //run the function + return 0; +} \ No newline at end of file diff --git a/Medium/515. Find Largest Value in Each Tree Row/sol.md b/Medium/515. Find Largest Value in Each Tree Row/sol.md new file mode 100644 index 00000000..686822ca --- /dev/null +++ b/Medium/515. Find Largest Value in Each Tree Row/sol.md @@ -0,0 +1,28 @@ +## Approach + +The approach used here is to perform a level-wise traversal of the binary tree while keeping track of the largest value in each level. This is achieved using a queue for level order traversal. + +### Initialization +- Begin with initializing an empty queue. +- Add the root node of the binary tree to the queue. This will serve as the starting point for the traversal. + +### Level-wise Traversal +- Continue processing nodes while the queue is not empty. +- For each level, calculate the size of the current level (i.e., the number of nodes in that level). This helps in distinguishing between nodes of different levels. + +### Capture Maximum +- For each level, initialize a variable `max_val` with the smallest possible integer (INT_MIN). This variable is used to keep track of the maximum value in the current level. +- As we process each node in the current level, we update `max_val` to be the maximum between the node's value and the current `max_val`. + +### Child Processing +- After processing a node, add its left and right children (if they exist) to the queue. This ensures that the next level of nodes will be processed in the subsequent iterations. + +### Result Update +- Once all nodes of a level are processed, the maximum value for that level (`max_val`) is added to the result list. + +### Complexity + +- Time complexity: O(n) where n is the number of nodes in the binary tree. Each node is processed once, and the maximum value at each level is determined in constant time. So, the overall time taken is linear with respect to the number of nodes. +- Space complexity: O(n). In the worst case, the queue can hold all leaf nodes. In a balanced binary tree, the last level can have n/2 nodes. Thus, the space complexity is linear. + + From caa48a7dc7ed4e97b478d91a0febc52113e23217 Mon Sep 17 00:00:00 2001 From: Jarrian Gojar Date: Mon, 30 Oct 2023 08:56:59 +0800 Subject: [PATCH 2/3] Delete Hard/1425. Constrained Subsequence Sum/sol.md --- Hard/1425. Constrained Subsequence Sum/sol.md | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 Hard/1425. Constrained Subsequence Sum/sol.md diff --git a/Hard/1425. Constrained Subsequence Sum/sol.md b/Hard/1425. Constrained Subsequence Sum/sol.md deleted file mode 100644 index 9695c82f..00000000 --- a/Hard/1425. Constrained Subsequence Sum/sol.md +++ /dev/null @@ -1,22 +0,0 @@ -d## Approach: Dynamic Programming with Deque - -### Intuition - -To find the maximum sum of a non-empty subsequence, we can use dynamic programming (DP). We define `dp[i]` as the maximum sum of a non-empty subsequence of `nums[:i+1]`. The result we seek is the maximum value in the `dp` array. Initially, we can compute `dp[i]` using the recurrence `dp[i] = nums[i] + max(dp[i-k], dp[i-k+1], ..., dp[i-1])`. However, this approach would be too slow and result in a Time Limit Exceeded (TLE) error. To optimize it, we use a deque data structure to keep track of the maximum value within a sliding window. - -### Algorithm - -1. Initialize a deque `q` to store indices. -2. Create a `dp` array with the same length as `nums` and initialize it with the values of `nums`. -3. Iterate through the `nums` array from left to right (index `i` from 1 to `nums.size()`): - - While the deque `q` is not empty and the index at the back of `q` is out of the window of size `k`, pop the back element from `q`. - - While the deque is not empty and the `dp` value at the front index of `q` is smaller than `dp[i]`, pop the front element from `q`. - - Append the current `dp[i]` value to the deque `q`. - - Update `dp[i]` by taking the maximum between its current value and `dp[q[0]] + nums[i]`. This step captures the idea of maximizing the subsequence sum while adhering to the constraint. -4. After the loop, the result is the maximum value in the `dp` array, which can be found using the `max_element` function. -5. Return the result as the answer. - -### Complexity - -- Time complexity: O(N) where N is the length of the `nums` array. -- Space complexity: O(K), where K is the constraint that limits the size of the deque. From 260184b2a2f69700699bcdf3bd5f978bddb510df Mon Sep 17 00:00:00 2001 From: Jarrian Gojar Date: Mon, 30 Oct 2023 08:59:50 +0800 Subject: [PATCH 3/3] Update sol.cpp --- .../sol.cpp | 67 +++++++------------ 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/Medium/515. Find Largest Value in Each Tree Row/sol.cpp b/Medium/515. Find Largest Value in Each Tree Row/sol.cpp index 5d16732c..1ff02108 100644 --- a/Medium/515. Find Largest Value in Each Tree Row/sol.cpp +++ b/Medium/515. Find Largest Value in Each Tree Row/sol.cpp @@ -1,52 +1,35 @@ -#include -#include -using namespace std; - -struct TreeNode -{ - int val; - TreeNode *left; - TreeNode *right; - TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} -}; - class Solution { public: -vector largestValues(TreeNode *root) -{ - if (!root) - return {}; - - std::vector result; - std::queue queue; - queue.push(root); - - while (!queue.empty()) + vector largestValues(TreeNode *root) { - int curr_level_size = queue.size(); - int max_val = INT_MIN; + if (!root) + return {}; - for (int i = 0; i < curr_level_size; ++i) - { - TreeNode *node = queue.front(); - queue.pop(); - max_val = std::max(max_val, node->val); + std::vector result; + std::queue queue; + queue.push(root); - if (node->left) - queue.push(node->left); - if (node->right) - queue.push(node->right); + while (!queue.empty()) + { + int curr_level_size = queue.size(); + int max_val = INT_MIN; + + for (int i = 0; i < curr_level_size; ++i) + { + TreeNode *node = queue.front(); + queue.pop(); + max_val = std::max(max_val, node->val); + + if (node->left) + queue.push(node->left); + if (node->right) + queue.push(node->right); + } + + result.push_back(max_val); } - result.push_back(max_val); + return result; } - - return result; -} }; -int main() -{ - //run the function - return 0; -} \ No newline at end of file