-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path334.py
55 lines (38 loc) · 1.07 KB
/
334.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#encoding=utf-8
class Solution(object):
def binary_search(self,num):
start = 0
end = len(self.LIS) - 1
if self.LIS[0] >= num:
return 0
while end-start>1:
middle = (start+end)/2
if self.LIS[middle] > num:
end = middle
elif self.LIS[middle] < num:
start = middle
else:
return middle
return end
def increasingTriplet(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if len(nums) == 0:
return False
self.LIS = [nums[0]]
for i in range(1,len(nums)):
num = nums[i]
if num > self.LIS[-1]:
self.LIS.append(num)
else:
index = self.binary_search(num)
self.LIS[index] = num
if len(self.LIS) > 2:
return True
return False
if __name__ == '__main__':
testset = [1,2,1,2,1,1]
wds = Solution()
print wds.increasingTriplet(testset)