Java, straightforward...


  • 0
    C
    public boolean find132pattern(int[] nums) {
            int len = nums.length;
            if(len < 3) return false;
            int[] max_cache = new int[len];
            int[] min_cache = new int[len];
            
            min_cache[0] = nums[0];
            for(int j = 1; j < len ; j++){
                min_cache[j] = Math.min(min_cache[j-1], nums[j]);
            }
            max_cache[len-1] = nums[len-1];
            for(int j = len-2; j >= 0; j--){
                max_cache[j] = Math.max(max_cache[j+1], nums[j]);
            }
            for(int i = 1; i < len-1; i++){
                int val = nums[i];
                int left = min_cache[i-1];
                if(max_cache[i+1] > left && val > max_cache[i+1]) return true;
                for(int j = i+1; j < len; j++){
                    if(nums[j] > left && val > nums[j]) return true;
                }
            }
            return false;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.