Increasing Triplet Subsequence - LeetCode
문제 번역: 정수 배열 nums가 주어진다. i < j < k를 만족하는 인덱스 트리플 (i, j, k)가 존재하면 nums[i] < nums[j] < nums[k]인 경우 true를 반환하라. 그러한 인덱스가 존재하지 않으면 false를 반환하라.
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.
문제 요약:
정수 배열에서 연속된 세 숫자를 찾아야 하며, 이들이 증가하는 순서를 가져야 합니다. 즉, i < j < k 이고 nums[i] < nums[j] < nums[k]인 경우 true를 반환합니다. 그렇지 않으면 false를 반환합니다.
풀이 과정:
class Solution {
public boolean increasingTriplet(int[] nums) {
int min1 = Integer.MAX_VALUE;
int min2 = Integer.MAX_VALUE;
for(int num : nums){
if(num <= min1){
min1 = num;
}else if(num <= min2){
min2 = num;
}else{
return true;
}
}
return false;
}
}
1431. Kids With the Greatest Number of Candies (0) | 2023.11.01 |
---|---|
443. String Compression (0) | 2023.10.30 |
547. Number of Provinces (0) | 2023.10.30 |
841. Keys and Rooms (0) | 2023.10.29 |
450. Delete Node in a BST (0) | 2023.10.29 |