```
public int lengthOfLIS(int[] nums) {
int len=nums.length;
Stack<Integer> stack=new Stack<Integer>();
for(int i=len-1; i>=0; i--)
{
if(stack.isEmpty())
{
stack.push(nums[i]);
}else
{
int val= stack.pop();
if(stack.isEmpty())
{
if(nums[i]>=val)
{
val=nums[i];
}else
{
stack.push(val);
val=nums[i];
}
}else
{
int up=stack.peek();
if(nums[i]<val)
{
stack.push(val);
val=nums[i];
}else if(nums[i]>val && nums[i]<up)
{
val=nums[i];
}
}
stack.push(val);
}
}
return stack.size();
}
```