鱼C论坛

 找回密码
 立即注册
查看: 799|回复: 1

[技术交流] 【朱迪的LeetCode刷题笔记】】334. Increasing Triplet Subsequence #Medium #Python

[复制链接]
发表于 2023-6-4 13:28:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Judie 于 2023-6-4 11:24 编辑

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?

Judy
Python taught by Mike

  1. class Solution(object):
  2.     def increasingTriplet(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: bool
  6.         """
  7.         i = None
  8.         j = None
  9.         for n in nums:
  10.             if i == None or n < i:
  11.                 i = n
  12.             elif j == None:
  13.                 if n > i:
  14.                     j = n   
  15.                 else:
  16.                     continue
  17.             elif n > j:
  18.                 return True   
  19.             elif i < n < j:
  20.                 j = n
  21.         return False
复制代码


Sol1
Python
https://leetcode.com/problems/in ... p;envId=leetcode-75
  1. def increasingTriplet(nums):
  2.     first = second = float('inf')
  3.     for n in nums:
  4.         if n <= first:
  5.             first = n
  6.         elif n <= second:
  7.             second = n
  8.         else:
  9.             return True
  10.     return False
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-4 13:30:58 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 02:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表