We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You can indeed walk right to left in O(N). The logic is to find the maximum number of items that accumulate beyond day1, but can be ultimately removed. Here is the implementation:
Scanner sc = new Scanner(System.in);
int[] ar = new int[sc.nextInt()];
for (int i =0; i < ar.length; i++){
ar[i] = sc.nextInt();
}
Stack<Integer> st = new Stack<Integer>();
int i = ar.length-1;
boolean day1 = false;
int maxsize = 0, minsize = 0, days = 0;
while (i >= 0)
{
while (i > 0 && ar[i] > ar[i-1]){
i--;
day1 = true;
}
// TODO Setup your maxsize and minsize
while (st.size() > 0 && ar[i] < st.peek())
st.pop();
// TODO Calc days
st.push(ar[i--]);
}
System.out.println(days + (day1 ? 1 : 0));
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Poisonous Plants
You are viewing a single comment's thread. Return to all comments →
You can indeed walk right to left in O(N). The logic is to find the maximum number of items that accumulate beyond day1, but can be ultimately removed. Here is the implementation: