santosh.b
BAN USERAlgo:
Have a custom stack object and always have minimum value along with actual value on the stack top
package com.santosh.career;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Career14462745 {
public static void main(String[] args) {
Stack<CustomStack> stack = new Stack<CustomStack>();
CustomStack s1 = new CustomStack();
CustomStack s2 = new CustomStack();
CustomStack s3 = new CustomStack();
s1.currentValue = 5;
s2.currentValue = 3;
s3.currentValue = 1;
List<CustomStack> stacks = new ArrayList<CustomStack>();
stacks.add(s1);
stacks.add(s2);
stacks.add(s3);
for (CustomStack customStack : stacks) {
if (stack.isEmpty()) {
customStack.minimumValue = customStack.currentValue;
stack.push(customStack);
} else {
CustomStack temp = stack.pop();
if (temp.minimumValue < customStack.currentValue) {
customStack.minimumValue = temp.minimumValue;
} else {
customStack.minimumValue = customStack.currentValue;
}
stack.push(temp);
stack.push(customStack);
}
}
System.out.println(stack.pop().minimumValue);
}
}
class CustomStack {
int currentValue;
int minimumValue = Integer.MAX_VALUE;
}
public static void stringReplace(char[] input, char[] replace, char[] output) {
for (int i = input.length - 1, j = output.length - 1; i >= 0; i--, j--) {
if (input[i] != ' ') {
output[j] = input[i];
} else {
for (int k = replace.length - 1; k >= 0; k--) {
output[j] = replace[k];
j--;
}
j++;
}
}
System.out.println(output);
}
package com.santosh;
- santosh.b June 01, 2016public class Sample {
public static void main(String[] args)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
sqrt(6);
}
static int sqrt(double num) {
if (num == 1) {
System.out.println(1);
return 1;
}
if (num == 0) {
return 1;
}
Integer sqrt = (int) Math.sqrt(num);
System.out.println(sqrt);
num = num - sqrt * sqrt;
return sqrt(num);
}
}