Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Increasing Triplet Subsequence in Python
Suppose there is an unsorted array. We have to check whether an increasing subsequence of length 3 exists or not in that array.
Formally the function should −
- Return true if there exists i, j, k
- such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
To solve this, we will follow these steps −
- small := infinity, big := infinity
- for each element i in array
- if i <= small, then small := i, otherwise when i <= big, then big := i, otherwise return true
- return false
Let us see the following implementation to get better understanding −
Example
class Solution(object): def increasingTriplet(self, nums): small,big = 100000000000000000000,100000000000000000000 for i in nums: if i <= small: small = i elif i<=big: big = i else : return True return False ob1 = Solution() print(ob1.increasingTriplet([5,3,8,2,7,9,4]))
Input
[5,3,8,2,7,9,4]
Output
True
Advertisements
