Stack find min max efficient O-n
Below are examples of Stack find minimum and maximum value more efficient than O(n).
Example 1: Stack find minimum and maximum value more efficient than O(n). This example will find minimum and maximum value when pushing elements on the stack and calculate minimum and maximum value where we are not touching its n (parameter which we are adding) on the stack:
package com.javahonk; import java.util.Stack; public class FindMinMaxStackFasterThanOBigN { public static void main(String[] args) { int valueToPush[] = { 2, 7, 1, 8, 3, 9 }; int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; Stack<Integer> stack = new Stack<Integer>(); for (int i = 0; i < valueToPush.length; i++) { int j = valueToPush[i]; if (max < j) { max = j; } if (min > j) { min = j; } stack.push(j); } System.out.println("min value on stack: " + min); System.out.println("max value on stack: " + max); } }
Output:
Example 2: Find Min Max Stack BigO O(n)
package com.javahonk; import java.util.Stack; public class FindMinMaxStackBigO { public static void main(String[] args) { Stack<Integer> stack = new Stack<Integer>(); for (int i = 1; i < 10; i++) { stack.push(i); } int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (Integer integer : stack) { if (integer > max) { max = integer; } if (integer < min) { min = integer; } } System.out.println("min value on stack: " + min); System.out.println("max value on stack: " + max); } }
Example 3: Find Min Max Stack Using Clone:
package com.javahonk; import java.util.Stack; public class FindMinMaxStackUsingClone { public static void main(String[] args) { Stack<Integer> stack = new Stack<Integer>(); for (int i = 1; i < 10; i++) { stack.push(i); } Stack<Integer> lifo = stack; Stack<Integer> lifoCopy = (Stack<Integer>) stack.clone(); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; while (!lifoCopy.isEmpty()) { if (lifoCopy.peek() < min) { min = lifoCopy.peek(); } if (lifoCopy.peek() > max) { max = lifoCopy.pop(); } else { lifoCopy.pop(); } } System.out.println("min value on stack: " + min); System.out.println("max value on stack: " + max); } }
Example 4: Find Min Max Stack Using Collections:
package com.javahonk; import java.util.Collections; import java.util.Stack; public class FindMinMaxStackUsingCollections { public static void main(String[] args) { Stack<Integer> stack = new Stack<Integer>(); for (int i = 1; i < 10; i++) { stack.push(i); } if (!stack.isEmpty()) { System.out.println("min value on stack: " + Collections.min(stack)); System.out.println("max value on stack: " + Collections.max(stack)); } } }
Example 5: Find Min Max Stack Using Loop:
package com.javahonk; import java.util.Stack; public class FindMinMaxStackUsingLoop { public static void main(String[] args) { Stack<Integer> stack = new Stack<Integer>(); for (int i = 1; i < 10; i++) { stack.push(i); } int minValue = Integer.MAX_VALUE; int maxValue = Integer.MIN_VALUE; for (Integer integer : stack) { if (minValue > integer) { minValue = integer; } if (maxValue < integer) { maxValue = integer; } } System.out.println("min value on stack: " + minValue); System.out.println("max value on stack: " + maxValue); } }