Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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: One of the valid triplet is (1, 4, 5), because nums[1] == 1 < nums[4] == 4 < nums[5] == 6.
Constraints:
1 <= nums.length <= 5 * 105-231 <= nums[i] <= 231 - 1O(n) time complexity and O(1) space complexity?Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,2,3,4,5]
[5,4,3,2,1]
[2,1,5,0,4,6]
longest-increasing-subsequence)count-special-quadruplets)count-good-triplets-in-an-array)count-increasing-quadruplets)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #334: Increasing Triplet Subsequence
class Solution {
public boolean increasingTriplet(int[] nums) {
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
for (int num : nums) {
if (num > mid) {
return true;
}
if (num <= min) {
min = num;
} else {
mid = num;
}
}
return false;
}
}
// Accepted solution for LeetCode #334: Increasing Triplet Subsequence
func increasingTriplet(nums []int) bool {
min, mid := math.MaxInt32, math.MaxInt32
for _, num := range nums {
if num > mid {
return true
}
if num <= min {
min = num
} else {
mid = num
}
}
return false
}
# Accepted solution for LeetCode #334: Increasing Triplet Subsequence
class Solution:
def increasingTriplet(self, nums: List[int]) -> bool:
mi, mid = inf, inf
for num in nums:
if num > mid:
return True
if num <= mi:
mi = num
else:
mid = num
return False
// Accepted solution for LeetCode #334: Increasing Triplet Subsequence
impl Solution {
pub fn increasing_triplet(nums: Vec<i32>) -> bool {
let n = nums.len();
if n < 3 {
return false;
}
let mut min = i32::MAX;
let mut mid = i32::MAX;
for num in nums.into_iter() {
if num <= min {
min = num;
} else if num <= mid {
mid = num;
} else {
return true;
}
}
false
}
}
// Accepted solution for LeetCode #334: Increasing Triplet Subsequence
function increasingTriplet(nums: number[]): boolean {
let n = nums.length;
if (n < 3) return false;
let min = nums[0],
mid = Number.MAX_SAFE_INTEGER;
for (let num of nums) {
if (num <= min) {
min = num;
} else if (num <= mid) {
mid = num;
} else if (num > mid) {
return true;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.