Skip to content

Commit 6fed8c7

Browse files
docs: add time & space complexity for 9 files in Stacks folder (#7132)"
1 parent 3f7abc5 commit 6fed8c7

File tree

9 files changed

+116
-26
lines changed

9 files changed

+116
-26
lines changed

src/main/java/com/thealgorithms/stacks/BalancedBrackets.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public static boolean isBalanced(String brackets) {
7878
return bracketsStack.isEmpty();
7979
}
8080
}
81+
8182
/**
8283
* Time Complexity: O(n)
8384
* Space Complexity: O(n)
@@ -87,4 +88,4 @@ public static boolean isBalanced(String brackets) {
8788
* length of the input string.
8889
* 2. The space complexity of the isBalanced method is O(n), where n is the
8990
* length of the input string.
90-
*/
91+
*/

src/main/java/com/thealgorithms/stacks/CelebrityFinder.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
/**
66
* Solves the celebrity problem using a stack-based algorithm.
77
*
8-
* <p>Celebrity is someone known by everyone but doesn't know anyone else.
9-
* <p>Applications: Graph theory and social network analysis.
8+
* <p>
9+
* Celebrity is someone known by everyone but doesn't know anyone else.
10+
* <p>
11+
* Applications: Graph theory and social network analysis.
1012
*
1113
* @author Hardvan
1214
*/
@@ -50,3 +52,15 @@ public static int findCelebrity(int[][] party) {
5052
return candidate;
5153
}
5254
}
55+
56+
/**
57+
* Time Complexity: O(n)
58+
* reason:- Because each person is processed a constant number of times:
59+
* once when pushed, once during candidate elimination, and once during
60+
* verification.
61+
* -----------------------------------------------------------------------------
62+
*
63+
* Space Complexity: O(n)
64+
* reason:- Due to the stack storing up to n people in the worst case.
65+
*
66+
*/

src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,38 @@
66
* Utility class for converting a non-negative decimal (base-10) integer
77
* to its representation in another radix (base) between 2 and 16, inclusive.
88
*
9-
* <p>This class uses a stack-based approach to reverse the digits obtained from
9+
* <p>
10+
* This class uses a stack-based approach to reverse the digits obtained from
1011
* successive divisions by the target radix.
1112
*
12-
* <p>This class cannot be instantiated.</p>
13+
* <p>
14+
* This class cannot be instantiated.
15+
* </p>
1316
*/
1417
public final class DecimalToAnyUsingStack {
1518

1619
private DecimalToAnyUsingStack() {
1720
}
1821

19-
private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
22+
private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
23+
'F' };
2024

2125
/**
2226
* Convert a decimal number to another radix.
2327
*
2428
* @param number the number to be converted
25-
* @param radix the radix
29+
* @param radix the radix
2630
* @return the number represented in the new radix as a String
27-
* @throws IllegalArgumentException if number is negative or radix is not between 2 and 16 inclusive
31+
* @throws IllegalArgumentException if number is negative or radix is not
32+
* between 2 and 16 inclusive
2833
*/
2934
public static String convert(int number, int radix) {
3035
if (number < 0) {
3136
throw new IllegalArgumentException("Number must be non-negative.");
3237
}
3338
if (radix < 2 || radix > 16) {
34-
throw new IllegalArgumentException(String.format("Invalid radix: %d. Radix must be between 2 and 16.", radix));
39+
throw new IllegalArgumentException(
40+
String.format("Invalid radix: %d. Radix must be between 2 and 16.", radix));
3541
}
3642

3743
if (number == 0) {
@@ -52,3 +58,10 @@ public static String convert(int number, int radix) {
5258
return result.toString();
5359
}
5460
}
61+
62+
/**
63+
* Time Complexity: O(log n) - The number of divisions by the radix determines
64+
* the iterations.
65+
* Space Complexity: O(log n) - Stack depth depends on the number of digits in
66+
* the new base.
67+
*/

src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.util.Stack;
44

55
/**
6-
* Class for detecting unnecessary or redundant brackets in a mathematical expression.
7-
* Assumes the expression is balanced (i.e., all opening brackets have matching closing brackets).
6+
* Class for detecting unnecessary or redundant brackets in a mathematical
7+
* expression.
8+
* Assumes the expression is balanced (i.e., all opening brackets have matching
9+
* closing brackets).
810
*/
911
public final class DuplicateBrackets {
1012
private DuplicateBrackets() {
@@ -42,3 +44,12 @@ public static boolean check(String expression) {
4244
return false;
4345
}
4446
}
47+
48+
/**
49+
* Time Complexity: O(n)
50+
* reason - Iterates through the expression once; each character is processed
51+
* constant times.
52+
* Space Complexity: O(n)
53+
* reason - Stack stores characters, growing linearly with expression length in
54+
* worst case.
55+
*/

src/main/java/com/thealgorithms/stacks/GreatestElementConstantTime.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public GreatestElementConstantTime() {
2727
* Pushes an element onto the top of the stack.
2828
* Checks if the element is the maximum or not
2929
* If so, then pushes to the maximum stack
30+
*
3031
* @param data The element to be pushed onto the stack.
3132
*/
3233
public void push(int data) {
@@ -63,7 +64,8 @@ public void pop() {
6364
/**
6465
* Returns the maximum element present in the stack
6566
*
66-
* @return The element at the top of the maxStack, or null if the stack is empty.
67+
* @return The element at the top of the maxStack, or null if the stack is
68+
* empty.
6769
*/
6870
public Integer getMaximumElement() {
6971
if (maxStack.isEmpty()) {
@@ -72,3 +74,15 @@ public Integer getMaximumElement() {
7274
return maxStack.peek();
7375
}
7476
}
77+
78+
/**
79+
* ime Complexity: O(1)
80+
* Reason: Detailed operations like push, pop, and getMaximumElement
81+
* only involve basic stack operations (push/pop/peek) and constant-time
82+
* comparisons, so they all run in constant time.
83+
* -----------------------------------------------------------------
84+
* Space Complexity: O(n)
85+
* Reason: An auxiliary stack (maxStack) is used. In the worst case (e.g.,
86+
* elements pushed in ascending order), it stores all n elements.
87+
*
88+
*/

src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
/**
88
* Utility class for evaluating postfix expressions using integer arithmetic.
99
* <p>
10-
* Postfix notation, also known as Reverse Polish Notation (RPN), is a mathematical notation in which operators follow their operands.
11-
* This class provides a method to evaluate expressions written in postfix notation.
10+
* Postfix notation, also known as Reverse Polish Notation (RPN), is a
11+
* mathematical notation in which operators follow their operands.
12+
* This class provides a method to evaluate expressions written in postfix
13+
* notation.
1214
* </p>
1315
* <p>
1416
* For more information on postfix notation, refer to
15-
* <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish Notation (RPN) on Wikipedia</a>.
17+
* <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse
18+
* Polish Notation (RPN) on Wikipedia</a>.
1619
* </p>
1720
*/
1821
public final class StackPostfixNotation {
@@ -22,16 +25,16 @@ private StackPostfixNotation() {
2225
private static BiFunction<Integer, Integer, Integer> getOperator(final String operationSymbol) {
2326
// note the order of operands
2427
switch (operationSymbol) {
25-
case "+":
26-
return (a, b) -> b + a;
27-
case "-":
28-
return (a, b) -> b - a;
29-
case "*":
30-
return (a, b) -> b * a;
31-
case "/":
32-
return (a, b) -> b / a;
33-
default:
34-
throw new IllegalArgumentException("exp contains an unknown operation.");
28+
case "+":
29+
return (a, b) -> b + a;
30+
case "-":
31+
return (a, b) -> b - a;
32+
case "*":
33+
return (a, b) -> b * a;
34+
case "/":
35+
return (a, b) -> b / a;
36+
default:
37+
throw new IllegalArgumentException("exp contains an unknown operation.");
3538
}
3639
}
3740

@@ -70,3 +73,10 @@ public static int postfixEvaluate(final String exp) {
7073
return s.pop();
7174
}
7275
}
76+
/**
77+
* Time Complexity: O(n)
78+
* Reason - The expression is tokenized and scanned once. Each operation takes
79+
* constant time.
80+
* Space Complexity: O(n)
81+
* Reason - The stack can store up to O(n) operands in the worst case.
82+
*/

src/main/java/com/thealgorithms/stacks/StackUsingTwoQueues.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,11 @@ public int size() {
8989
return mainQueue.size();
9090
}
9191
}
92+
/*
93+
* push: O(n) — requires moving all existing elements to maintain LIFO order.
94+
* pop: O(1) — direct removal from the main queue.
95+
* peek: O(1).
96+
* isEmpty / size: O(1).
97+
* -----------------------------------------------------------------
98+
* Space Complexity: O(n) — total elements stored across two queues.
99+
*/

src/main/java/com/thealgorithms/stacks/TrappingRainwater.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.thealgorithms.stacks;
2+
23
/**
34
* Trapping Rainwater Problem
45
* Given an array of non-negative integers representing the height of bars,
@@ -46,3 +47,10 @@ public static int trap(int[] height) {
4647
return result;
4748
}
4849
}
50+
51+
/**
52+
* Time Complexity - O(n)
53+
* reason:- The algorithm scans the array once.
54+
* Space Complexity - O(1)
55+
* reason:- Constant space is used.
56+
*/

src/main/java/com/thealgorithms/stacks/ValidParentheses.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
/**
88
* Valid Parentheses Problem
99
*
10-
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']',
10+
* Given a string containing just the characters '(', ')', '{', '}', '[' and
11+
* ']',
1112
* determine if the input string is valid.
1213
*
1314
* An input string is valid if:
@@ -72,3 +73,13 @@ public static boolean isValid(String s) {
7273
return stack.isEmpty();
7374
}
7475
}
76+
77+
/**
78+
* Time Complexity: O(n)
79+
* The algorithm scans the string once, and all operations inside
80+
* the loop (map lookup, stack push/pop) run in constant time.
81+
*
82+
* Space Complexity: O(n)
83+
* In the worst case, the stack holds half of the characters, growing linearly
84+
* with input size.
85+
*/

0 commit comments

Comments
 (0)