Skip to content

Latest commit

 

History

History
42 lines (35 loc) · 895 Bytes

334.递增的三元子序列.md

File metadata and controls

42 lines (35 loc) · 895 Bytes

334.递增的三元子序列

/*
 * @lc app=leetcode.cn id=334 lang=typescript
 *
 * [334] 递增的三元子序列
 */

// @lc code=start
function increasingTriplet(nums: number[]): boolean {}
// @lc code=end

解法 1: 使用栈

function increasingTriplet(nums: number[]): boolean {
  const stack: number[] = []
  for (const num of nums) {
    let i = stack.length
    while (i > 0 && stack[i - 1] >= num) i--
    stack[i] = num

    if (stack.length > 2) return true
  }
  return false
}

Case

test.each([
  { input: { nums: [1, 1, 1, 1, 1, 1, 1] }, output: false },
  { input: { nums: [1, 2, 3, 4, 5] }, output: true },
  { input: { nums: [5, 4, 3, 2, 1] }, output: false },
  { input: { nums: [2, 1, 5, 0, 4, 6] }, output: true },
])('input: nums = $input.nums', ({ input: { nums }, output }) => {
  expect(increasingTriplet(nums)).toEqual(output)
})