Home > Article > Web Front-end > Typescript Coding Chronicles: Increasing Triplet Subsequence
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 exist, return false.
Can you implement a solution that runs in O(n) time complexity and O(1) space complexity?
To solve this problem efficiently, we need to keep track of the smallest and second smallest values encountered so far. If we find a third value that is greater than the second smallest, then we have found an increasing triplet.
The brute force solution involves checking all possible triplets to see if there exists one that satisfies the condition i < j < k and nums[i] < nums[j] < nums[k]. This approach has a time complexity of O(n^3), which is not efficient for large input sizes.
function increasingTripletBruteForce(nums: number[]): boolean { const n = nums.length; for (let i = 0; i < n - 2; i++) { for (let j = i + 1; j < n - 1; j++) { for (let k = j + 1; k < n; k++) { if (nums[i] < nums[j] && nums[j] < nums[k]) { return true; } } } } return false; }
The brute force solution is not efficient and is not suitable for large input sizes.
The optimized solution involves iterating through the array while maintaining two variables, first and second, which represent the smallest and second smallest values encountered so far. If we find a value greater than second, then we return true.
function increasingTriplet(nums: number[]): boolean { let first = Infinity; let second = Infinity; for (let num of nums) { if (num <= first) { first = num; // smallest value } else if (num <= second) { second = num; // second smallest value } else { return true; // found a value greater than second smallest, thus an increasing triplet exists } } return false; }
console.log(increasingTripletBruteForce([1,2,3,4,5])); // true console.log(increasingTripletBruteForce([5,4,3,2,1])); // false console.log(increasingTripletBruteForce([2,1,5,0,4,6])); // true console.log(increasingTripletBruteForce([1,1,1,1,1])); // false console.log(increasingTripletBruteForce([1,2])); // false console.log(increasingTripletBruteForce([1,2,3])); // true console.log(increasingTripletBruteForce([1,5,0,4,1,3])); // true console.log(increasingTriplet([1,2,3,4,5])); // true console.log(increasingTriplet([5,4,3,2,1])); // false console.log(increasingTriplet([2,1,5,0,4,6])); // true console.log(increasingTriplet([1,1,1,1,1])); // false console.log(increasingTriplet([1,2])); // false console.log(increasingTriplet([1,2,3])); // true console.log(increasingTriplet([1,5,0,4,1,3])); // true
Subarray Problems:
Two-Pointer Technique:
In-Place Algorithms:
By practicing such problems and strategies, you can improve your problem-solving skills and be better prepared for various coding challenges.
The above is the detailed content of Typescript Coding Chronicles: Increasing Triplet Subsequence. For more information, please follow other related articles on the PHP Chinese website!