Skip to content

Commit 0ab4804

Browse files
committed
Update README.md
1 parent 1cd38a7 commit 0ab4804

File tree

1 file changed

+35
-69
lines changed

1 file changed

+35
-69
lines changed

README.md

Lines changed: 35 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,98 +4,64 @@ This is my attempt to make the coding experience easier for you guys so that you
44

55
## Always here to assist you guys.
66

7-
## Today's 13-04-24 [Problem Link](https://leetcode.com/problems/maximal-rectangle/description/?envType=daily-question&envId=2024-04-13)
8-
## 85. Maximal Rectangle
7+
## Today's 14-04-24 [Problem Link](https://leetcode.com/problems/sum-of-left-leaves/description/?envType=daily-question&envId=2024-04-14)
8+
## 404. Sum of Left Leaves
99

1010
# Intuition
1111
<!-- Describe your first thoughts on how to solve this problem. -->
12-
The problem involves finding the maximal rectangle containing only 1s in a binary matrix. To solve this problem efficiently, I can utilize the concept of histograms. For each row in the matrix, I can treat it as the base of a histogram and calculate the height of bars in the histogram based on the consecutive 1s in that row. Then, I can apply the largest rectangle in histogram algorithm to find the maximal rectangle for each row. Finally, I return the maximum area among all rows as the result.
13-
12+
The goal is to find the sum of all left leaves in a binary tree. A left leaf is a leaf node that is the left child of its parent.
1413
# Approach
1514
<!-- Describe your approach to solving the problem. -->
1615

17-
- I initialized a variable to store the maximal rectangle area.
18-
- Iterate through each row in the matrix:
19-
- Update the histogram of heights for each row. Treat 1s as bars and consecutive 1s as increasing heights in the histogram.
20-
- Calculate the maximal rectangle area using the largest rectangle in histogram algorithm for the current histogram.
21-
- Update the maximal rectangle area if the current area is greater.
22-
- Return the maximal rectangle area as the result.
23-
24-
##### Largest Rectangle in Histogram Algorithm :
25-
- I nitialized a stack to store the indices of histogram bars.
26-
- Iterated through each bar in the histogram:
27-
- If the stack is empty or the current bar's height is greater than or equal to the height of the bar at the top of the stack, push the index of the current bar onto the stack.
28-
- If the current bar's height is less than the height of the bar at the top of the stack, keep popping bars from the stack until the stack is empty or the height of the bar at the top of the stack is less than the current bar's height. For each popped bar, calculate its area using the popped height as the height of the rectangle and the difference between the current index and the index of the bar at the top of the stack as the width of the rectangle. Update the maximal rectangle area if the calculated area is greater.
29-
- After iterating through all bars, if there are bars left in the stack, repeat step 2 for each remaining bar.
30-
- Returned the maximal rectangle area calculated.
16+
- I started with the root node.
17+
- If the current node is null, returned 0.
18+
- Check if the left child of the current node exists and if it is a leaf node (i.e., it has no left or right children).
19+
- If the left child is a leaf node, add its value to the result and continue recursively with the right child.
20+
- If the left child is not a leaf node, recursively call the function on both the left and right children.
21+
- Returned the sum obtained from the left subtree and the right subtree.
3122

3223
---
3324
Have a look at the code , still have any confusion then please let me know in the comments
3425
Keep Solving.:)
3526

3627
# Complexity
37-
- Time complexity : $O(m*n)$
28+
- Time complexity : $O(n)$
3829
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
39-
$m$ : number of rows
40-
41-
$n$ : number of columns
30+
$n$ : number of nodes in the binary tree
4231
- Space complexity : $O(n)$
4332
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
4433

4534
# Code
4635
```
47-
class Solution {
48-
49-
// Method to find the maximal rectangle area in a given matrix
50-
public int maximalRectangle(char[][] matrix) {
36+
/**
37+
* Definition for a binary tree node.
38+
* public class TreeNode {
39+
* int val;
40+
* TreeNode left;
41+
* TreeNode right;
42+
* TreeNode() {}
43+
* TreeNode(int val) { this.val = val; }
44+
* TreeNode(int val, TreeNode left, TreeNode right) {
45+
* this.val = val;
46+
* this.left = left;
47+
* this.right = right;
48+
* }
49+
* }
50+
*/
5151
52-
// Check if the matrix is null or empty
53-
if (matrix == null || matrix.length == 0) {
52+
class Solution {
53+
public int sumOfLeftLeaves(TreeNode root) {
54+
if (root == null) {
5455
return 0;
5556
}
56-
57-
// Initialize the maximal rectangle area
58-
int maxArea = 0;
59-
// Initialize an array to store the histogram of heights
60-
int[] heights = new int[matrix[0].length];
61-
62-
// Iterate through each row in the matrix
63-
for (char[] row : matrix) {
64-
// Update the histogram for each row
65-
for (int i = 0; i < row.length; i++) {
66-
heights[i] = row[i] == '0' ? 0 : heights[i] + 1;
67-
}
68-
// Update the maximal rectangle area
69-
maxArea = Math.max(maxArea, calculateMaxArea(heights));
57+
58+
// Checking if the left node is a leaf node
59+
if (root.left != null && root.left.left == null && root.left.right == null) {
60+
return root.left.val + sumOfLeftLeaves(root.right);
7061
}
71-
72-
return maxArea;
73-
}
74-
75-
// Method to calculate the maximal rectangle area using histogram
76-
private int calculateMaxArea(int[] heights) {
7762
78-
// Initialize the maximal rectangle area
79-
int maxArea = 0;
80-
// Initialize a stack to store the indices of heights
81-
Stack<Integer> stack = new Stack<>();
82-
83-
// Iterate through each height and calculate the maximal rectangle area
84-
for (int i = 0; i <= heights.length; i++) {
85-
// Check if the stack is not empty and the current height is less than the height at the top of the stack
86-
while (!stack.isEmpty() && (i == heights.length || heights[stack.peek()] > heights[i])) {
87-
// Get the height at the top of the stack
88-
int currentHeight = heights[stack.pop()];
89-
// Calculate the width of the rectangle
90-
int width = stack.isEmpty() ? i : i - stack.peek() - 1;
91-
// Update the maximal rectangle area
92-
maxArea = Math.max(maxArea, currentHeight * width);
93-
}
94-
// Push the index of the current height onto the stack
95-
stack.push(i);
96-
}
97-
98-
return maxArea;
63+
// Exploring the tree further
64+
return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
9965
}
10066
}
10167
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy