返回介绍

solution / 0300-0399 / 0334.Increasing Triplet Subsequence / README_EN

发布于 2024-06-17 01:04:02 字数 4661 浏览 0 评论 0 收藏 0

334. Increasing Triplet Subsequence

中文文档

Description

Given an integer array nums, return true_ if there exists a triple of indices _(i, j, k)_ such that _i < j < k_ and _nums[i] < nums[j] < nums[k]. If no such indices exists, return false.

 

Example 1:

Input: nums = [1,2,3,4,5]
Output: true
Explanation: Any triplet where i < j < k is valid.

Example 2:

Input: nums = [5,4,3,2,1]
Output: false
Explanation: No triplet exists.

Example 3:

Input: nums = [2,1,5,0,4,6]
Output: true
Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.

 

Constraints:

  • 1 <= nums.length <= 5 * 105
  • -231 <= nums[i] <= 231 - 1

 

Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space complexity?

Solutions

Solution 1

class Solution:
  def increasingTriplet(self, nums: List[int]) -> bool:
    mi, mid = inf, inf
    for num in nums:
      if num > mid:
        return True
      if num <= mi:
        mi = num
      else:
        mid = num
    return False
class Solution {
  public boolean increasingTriplet(int[] nums) {
    int n = nums.length;
    int[] lmi = new int[n];
    int[] rmx = new int[n];
    lmi[0] = Integer.MAX_VALUE;
    rmx[n - 1] = Integer.MIN_VALUE;
    for (int i = 1; i < n; ++i) {
      lmi[i] = Math.min(lmi[i - 1], nums[i - 1]);
    }
    for (int i = n - 2; i >= 0; --i) {
      rmx[i] = Math.max(rmx[i + 1], nums[i + 1]);
    }
    for (int i = 0; i < n; ++i) {
      if (lmi[i] < nums[i] && nums[i] < rmx[i]) {
        return true;
      }
    }
    return false;
  }
}
class Solution {
public:
  bool increasingTriplet(vector<int>& nums) {
    int mi = INT_MAX, mid = INT_MAX;
    for (int num : nums) {
      if (num > mid) return true;
      if (num <= mi)
        mi = num;
      else
        mid = num;
    }
    return false;
  }
};
func increasingTriplet(nums []int) bool {
  min, mid := math.MaxInt32, math.MaxInt32
  for _, num := range nums {
    if num > mid {
      return true
    }
    if num <= min {
      min = num
    } else {
      mid = num
    }
  }
  return false
}
function increasingTriplet(nums: number[]): boolean {
  let n = nums.length;
  if (n < 3) return false;
  let min = nums[0],
    mid = Number.MAX_SAFE_INTEGER;
  for (let num of nums) {
    if (num <= min) {
      min = num;
    } else if (num <= mid) {
      mid = num;
    } else if (num > mid) {
      return true;
    }
  }
  return false;
}
impl Solution {
  pub fn increasing_triplet(nums: Vec<i32>) -> bool {
    let n = nums.len();
    if n < 3 {
      return false;
    }
    let mut min = i32::MAX;
    let mut mid = i32::MAX;
    for num in nums.into_iter() {
      if num <= min {
        min = num;
      } else if num <= mid {
        mid = num;
      } else {
        return true;
      }
    }
    false
  }
}

Solution 2

class Solution {
  public boolean increasingTriplet(int[] nums) {
    int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
    for (int num : nums) {
      if (num > mid) {
        return true;
      }
      if (num <= min) {
        min = num;
      } else {
        mid = num;
      }
    }
    return false;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文