나의 발자취
334. Increasing Triplet Subsequence 본문
문제 링크
문제 접근 방법
greedy
고민한 부분
처음에는 루프 순회를 역으로 하려다가.. 아닌것같아서 바로 접었다.
코드
class Solution:
def increasingTriplet(self, nums: List[int]) -> bool:
first = second = float('inf') # first와 second 초기화
for num in nums:
if num <= first: # num이 first보다 작거나 같으면 first 갱신
first = num
elif num <= second: # num이 first보다 크고, second보다 작거나 같으면 second 갱신
second = num
else: # num이 first, second보다 크면 triplet 존재
return True
return False
'알고리즘' 카테고리의 다른 글
[Two Pointer, ::] 125. Valid Palindrome (0) | 2025.02.13 |
---|---|
Two Pointers 기법이란? (Why / When / How to use) (0) | 2025.02.13 |
[그리디] 605. Can Place Flowers (2) | 2025.01.04 |
[Two Pointers] 345. Reverse Vowels of a String (0) | 2025.01.04 |
[Ad-Hoc] 1431. Kids With the Greatest Number of Candies (1) | 2025.01.03 |
Comments