Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Refer:
- * https://leetcode.com/problems/increasing-triplet-subsequence/discuss/79004/Concise-Java-solution-with-comments.
- *
- * Time Complexity: O(N)
- *
- * Space Complexity: O(1)
- *
- * N = Length of the input array.
- */
- class Solution {
- public boolean increasingTriplet(int[] nums) {
- if (nums == null || nums.length < 3) {
- return false;
- }
- int c1 = Integer.MAX_VALUE;
- int c2 = Integer.MAX_VALUE;
- for (int num : nums) {
- if (num <= c1) {
- // update c1 if num is smaller than both c1 & c2.
- c1 = num;
- } else if (num <= c2) {
- // update c2 only if num is greater than c1 but smaller than c2.
- c2 = num;
- } else {
- // return true if found a number bigger than both c1 & c2.
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement