[Leetcode]334. Increasing Triplet Subsequence(x)
2024. 7. 27. 12:01ㆍAlgorithm
1. problem :
2. solution1 :
def increasingTriplet(nums):
first = float('inf')
second = float('inf')
for num in nums:
if num <= first:
first = num # 첫 번째 최소값 갱신
elif num <= second:
second = num # 두 번째 최소값 갱신
else:
# num이 첫 번째 및 두 번째 최소값보다 크면 삼중 수열이 존재
return True
return False
이 코드는 chat gpt 4o가 짜준 코드이다. 나는 풀지 못했다. 그래서 도움을 받았다.
이 풀이는 first , second를 양의 무한대로 설정하고 , first값을 최솟값에 대응한다. 그다음은 second에 최솟값의 다음값을 대응한다. frist와 second가 채워지고, 마지막으로 if와 elif를 만족하지 않는다면, 즉, first와 second보다 크다면 True를 반환한다. 그렇지 않다면 , False를 반환한다.
'Algorithm' 카테고리의 다른 글
[Leetcode]443. String Compression(x) (0) | 2024.07.28 |
---|---|
[BOJ]2231번 분해합 (0) | 2024.07.27 |
[Leetcode]238. Product of Array Except Self (0) | 2024.07.27 |
[Leetcode]151. Reverse Words in a String (0) | 2024.07.26 |
[Leetcode]345. Reverse Vowels of a String (0) | 2024.07.26 |