imashutosh51

Increasig Triplet Subsequence

Oct 6th, 2022 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 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. class Solution:
  14.     def increasingTriplet(self, nums: list[int]) -> bool:
  15.         a=math.inf
  16.         b=math.inf
  17.         for i in nums:
  18.             if i<=a:
  19.                 a=i
  20.             elif i<=b:
  21.                 b=i
  22.             else:
  23.                 return True
  24.         return False
  25.        
  26.        
  27.  
  28.  
  29.        
Advertisement
Add Comment
Please, Sign In to add comment