관리 메뉴

bright jazz music

334. Increasing Triplet Subsequence 본문

LeetCode

334. Increasing Triplet Subsequence

bright jazz music 2026. 1. 17. 22:34
function increasingTriplet(nums: number[]): boolean {
    // first: 지금까지 본 가장 작은 값
    // second: first보다 크면서 가장 작은 값
    
    let first = Infinity;
    let second = Infinity;
    
    for(let num of nums) {
        if(num <= first) {
            // 더 작은 first 발견
            first = num;
        } else if(num <= second) {
            // first < num <= second
            // 더 나은 second 발견
            second = num;
        } else {
            // num > second > first
            // triplet 완성!
            return true;
        }
    }
    
    return false;
}


// 잘못된 접근
// function increasingTriplet(nums: number[]): boolean {
//     // 각 인덱스를 시작점으로 triplet 찾기
    
//     function seekTriplet(arr: number[], startIdx: number): boolean {
//         let count = 1;
//         let val = arr[startIdx];
        
//         for(let i = startIdx + 1; i < arr.length; i++) {
//             if(count === 3) return true;
//             if(arr[i] > val) {
//                 val = arr[i];
//                 count++;
//                 if(count === 3) return true;  // 여기서도 체크
//             }
//         }
//         return false;
//     }
    
//     // 모든 시작점 시도
//     for(let i = 0; i < nums.length - 2; i++) {
//         if(seekTriplet(nums, i)) {
//             return true;
//         }
//     }
    
//     return false;
// }

'LeetCode' 카테고리의 다른 글

238. Product of Array Except Self  (0) 2026.01.16
151. Reverse Words in a String  (0) 2026.01.15
345. Reverse Vowels of a String  (0) 2026.01.14
605. Can place flowers  (0) 2026.01.13
Comments