Last active
June 8, 2018 03:40
-
-
Save bilbo3000/ecdd633889ff8097f4fec45b972f00ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean increasingTriplet(int[] nums) { | |
if (nums == null || nums.length < 3) return false; | |
int small = Integer.MAX_VALUE, mid = Integer.MAX_VALUE; | |
// invariant 1: both small and mid can only go down | |
// invariant 2: mid will always greater than smaller | |
// ==> If find a number greater than both, true | |
for (int n : nums) { | |
if (n <= small) { // Find a better small | |
small = n; | |
} else if (n <= mid) { // Find a better mid, small larger than small | |
mid = n; | |
} else { // Greater than mid (must also greater than mid), find a solution | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment