Skip to content

Commit 5410bc4

Browse files
committed
cleanup
1 parent 8f2dae9 commit 5410bc4

11 files changed

+56
-12
lines changed

102.LevelOrder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public IList<IList<int>> LevelOrder(TreeNode root) {
4646
level.Add(currNode.val);
4747
if (currNode.right != null)
4848
queue.Enqueue(currNode.right);
49-
if (currNode.left != null)
49+
if (currNode.left != null)
5050
queue.Enqueue(currNode.left);
5151
}
5252
res.Add(level);

103.ZigzagLevelOrder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// 103. Binary Tree Zigzag Level Order Traversal
2-
// Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
2+
// Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right,
3+
// then right to left for the next level and alternate between).
34

45
// Example 1:
56
// Input: root = [3,9,20,null,null,15,7]

116.Connect.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
// 116. Populating Next Right Pointers in Each Node
22

3-
// You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
3+
// You are given a perfect binary tree where all leaves are on the same level, and every parent has two children.
4+
// The binary tree has the following definition:
45

56
// struct Node {
67
// int val;
78
// Node *left;
89
// Node *right;
910
// Node *next;
1011
// }
11-
// Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
12+
// Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be
13+
// set to NULL.
1214

1315
// Initially, all next pointers are set to NULL.
1416

1517
// Example 1:
1618
// Input: root = [1,2,3,4,5,6,7]
1719
// Output: [1,#,2,3,#,4,5,6,7,#]
18-
// Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
20+
// Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to
1921
// Example 2:
2022
// Input: root = []
2123
// Output: []

122.MaxProfit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
44

55
// On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time.
6-
//However, you can buy it then immediately sell it on the same day.
6+
// However, you can buy it then immediately sell it on the same day.
77

88
// Find and return the maximum profit you can achieve.
99

124.MaxPathSum.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// 124. Binary Tree Maximum Path Sum
2-
// A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
2+
// A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them.
3+
// A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
34
// The path sum of a path is the sum of the node's values in the path.
45
// Given the root of a binary tree, return the maximum path sum of any non-empty path.
56
// Example 1:

152.MaxProduct.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// 152. Maximum Product Subarray
22

33
// Given an integer array nums, find a subarray
4-
// that has the largest product, and return the product.
4+
// that has the largest product, and return the product.
55

66
// The test cases are generated so that the answer will fit in a 32-bit integer.
77

173.BSTIterator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// 173. Binary Search Tree Iterator
22
// Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):
33

4-
// BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
4+
// BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the
5+
// constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
56
// boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false.
67
// int next() Moves the pointer to the right, then returns the number at the pointer.
78
// Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST.

198.Rob.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// it will automatically contact the police if two adjacent houses were broken into on the same night.
66

77
// Given an integer array nums representing the amount of money of each house, return the maximum amount of money you
8-
// can rob tonight without alerting the police.
8+
// can rob tonight without alerting the police.
99

1010
// Example 1:
1111

203.RemoveElements.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//203. Remove Linked List Elements
2-
//Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and
2+
// Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and
33
// return the new head.
4-
//definition for singly-linked list.
4+
// definition for singly-linked list.
55
public class listnode
66
{
77
public int val;
File renamed without changes.

84.LargestRectangleArea.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,43 @@ public int LargestRectangleArea(int[] heights) {
5353
return maxarea;
5454

5555
}
56+
}
57+
58+
59+
public class Solution {
60+
public int LargestRectangleArea(int[] heights)
61+
{
62+
// Edge case: if heights array is empty or null
63+
if (heights == null || heights.Length == 0)
64+
return 0;
65+
66+
// Stack to store indices of histogram bars
67+
Stack<int> stack = new Stack<int>();
68+
int maxArea = 0;
69+
70+
// Traverse all bars and an extra iteration to process remaining stack
71+
for (int i = 0; i <= heights.Length; i++)
72+
{
73+
// Handle the final case where we treat the "last" height as 0
74+
int currentHeight = (i == heights.Length) ? 0 : heights[i];
75+
76+
// Maintain a non-decreasing height stack; calculate area for taller bars
77+
while (stack.Count > 0 && currentHeight < heights[stack.Peek()])
78+
{
79+
// Pop the top element which represents the height of a rectangle
80+
int height = heights[stack.Pop()];
81+
82+
// Calculate the width of the rectangle
83+
int width = stack.Count == 0 ? i : i - stack.Peek() - 1;
84+
85+
// Update max area if a larger rectangle is found
86+
maxArea = Math.Max(maxArea, height * width);
87+
}
88+
89+
// Push the current bar's index onto the stack
90+
stack.Push(i);
91+
}
92+
93+
return maxArea;
94+
}
5695
}

0 commit comments

Comments
 (0)