• [LeetCode] 456. 132 Pattern


    Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i]nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].

    Return true if there is a 132 pattern in nums, otherwise, return false.

    Follow up: The O(n^2) is trivial, could you come up with the O(n logn) or the O(n) solution?

    Example 1:

    Input: nums = [1,2,3,4]
    Output: false
    Explanation: There is no 132 pattern in the sequence.
    

    Example 2:

    Input: nums = [3,1,4,2]
    Output: true
    Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
    

    Example 3:

    Input: nums = [-1,3,2,0]
    Output: true
    Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].

    Constraints:

    • n == nums.length
    • 1 <= n <= 104
    • -109 <= nums[i] <= 109

    132模式。

    给你一个整数数组 nums ,数组中共有 n 个整数。132 模式的子序列 由三个整数 nums[i]、nums[j] 和 nums[k] 组成,并同时满足:i < j < k 和 nums[i] < nums[k] < nums[j] 。

    如果 nums 中存在 132 模式的子序列 ,返回 true ;否则,返回 false 。

    进阶:很容易想到时间复杂度为 O(n^2) 的解决方案,你可以设计一个时间复杂度为 O(n logn) 或 O(n) 的解决方案吗?

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/132-pattern
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    我这里提供两种思路,一种利用了treemap,一种是单调栈。

    treemap的做法,首先我们遍历input数组,统计每个数字出现了几次,用treemap记录好。再一次遍历input数组,此时遇到一个数字,就从treemap中减去这个数字,直到把这个数字在treemap中的key移除为止。在第二次遍历的过程中,记录一个最小值min。如果在过程中发现有一个数字num比min大,同时在treemap中可以找到另一个数字既严格大于min(treemap.higherKey(min)),又小于当前的num,则说明找到了这个132模式。其中treemap的higherKey()函数是表示找一个严格大于当前数字的key,注意它和ceilingKey()的区别。

    时间O(nlogn) - treemap找higherKey的时间复杂度

    空间O(n)

    Java实现

     1 class Solution {
     2     public boolean find132pattern(int[] nums) {
     3         TreeMap<Integer, Integer> treemap = new TreeMap<>();
     4         for (int num : nums) {
     5             treemap.put(num, treemap.getOrDefault(num, 0) + 1);
     6         }
     7         
     8         int min = Integer.MAX_VALUE;
     9         for (int num : nums) {
    10             int count = treemap.get(num);
    11             if (count > 1) {
    12                 treemap.put(num, count - 1);
    13             } else {
    14                 treemap.remove(num);
    15             }
    16             if (num <= min) {
    17                 min = num;
    18             } else {
    19                 // min is 1
    20                 // num is 3
    21                 // target is 2
    22                 Integer target = treemap.higherKey(min);
    23                 if (target != null && target < num) {
    24                     return true;
    25                 }
    26             }
    27         }
    28         return false;
    29     }
    30 }

    第二种方法是单调栈,而且是从后往前遍历数组。遍历过程中,记录一个变量mid表示中间那个数字。因为这个数字需要出现在1和3之后,所以从后往前遍历是make sense的。一开始stack为空的时候就往stack放入nums[i]。当stack不为空的时候,判断当前数字nums[i]是否大于stack.peek(),若大于则stack.pop()并且把pop出来的元素赋给mid。此时pop出来的元素因为是比较靠后的,所以其实mid记录的是132模式里的那个2。我们试图找一个3之后最大的2,这样会留更多余地给1。所以再往前扫描的时候,一旦发现有nums[i] < mid则return true。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public boolean find132pattern(int[] nums) {
     3         Stack<Integer> stack = new Stack<>();
     4         int mid = Integer.MIN_VALUE;
     5         for (int i = nums.length - 1; i >= 0; i--) {
     6             if (nums[i] < mid) {
     7                 return true;
     8             } else {
     9                 while (!stack.isEmpty() && nums[i] > stack.peek()) {
    10                     mid = stack.pop();
    11                 }
    12                 stack.push(nums[i]);
    13             }
    14         }
    15         return false;
    16     }
    17 }

    LeetCode 题目总结

  • 相关阅读:
    CSS选择器及优先级
    iframe中跨域页面访问parent的方法
    网页外链用了 target=_blank,结果悲剧了
    CarbonData:大数据融合数仓新一代引擎
    补习系列(14)-springboot redis 整合-数据读写
    python读写配置文件使用总结与避坑指南
    【云速建站】后台配置邮费
    解析:区块链的本质是伴随信息社会产生的一种新型生产关系
    用“黑科技”产放心粮,种地竟然和想象中有点不一样
    【云速建站】按照给定模板从0开始编辑网站页面
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13867139.html
Copyright © 2020-2023  润新知