fix bug in finding longest increasing/decreasing subsequence