Advertisement
imashutosh51

Increasig Triplet Subsequence

Oct 6th, 2022 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. /*
  2. Logic:
  3. IF we get a number less than or equal to i then update i.
  4. if element is greater than i but less than equal to j,then update j.
  5. and if less than j also then we have k also so return k.
  6.  
  7. try to deny this algo:
  8. first we get 2 then 4 so i=2 and j=4
  9. then again we get 1 so i=1 and j=4 now we get 6 so now k=6 and
  10. we got the triplet. so it works in all case.
  11. ie. 2,4,1,6
  12. */
  13.  
  14. class Solution:
  15.     def increasingTriplet(self, nums: List[int]) -> bool:
  16.         first=math.inf
  17.         second=math.inf
  18.         third=math.inf
  19.         for i in nums:
  20.             if i<=first:
  21.                 first=i
  22.             elif i<=second:
  23.                 second=i
  24.             else:
  25.                 return True
  26.         return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement