File tree Expand file tree Collapse file tree 2 files changed +20
-20
lines changed Expand file tree Collapse file tree 2 files changed +20
-20
lines changed Original file line number Diff line number Diff line change @@ -4,5 +4,16 @@ def majorityElement(self, nums):
4
4
:type nums: List[int]
5
5
:rtype: int
6
6
"""
7
- nums .sort ()
8
- return nums [len (nums ) // 2 ]
7
+ vote = None
8
+ vote_cnt = 0
9
+
10
+ for num in nums :
11
+ if not vote or num == vote :
12
+ vote = num
13
+ vote_cnt += 1
14
+ else :
15
+ vote_cnt -= 1
16
+ if vote_cnt == 0 :
17
+ vote = num
18
+ vote_cnt = 1
19
+ return vote
Original file line number Diff line number Diff line change @@ -4,22 +4,11 @@ def lengthOfLIS(self, nums):
4
4
:type nums: List[int]
5
5
:rtype: int
6
6
"""
7
- l = len (nums )
8
- if not l :
9
- return 0
10
- dp = [1 for _ in range (l )]
7
+ dp = [1 for _ in nums ]
11
8
12
- for index , item in enumerate (nums ):
13
- dp [index ] = self .find (nums [:index + 1 ], dp ) + 1
14
- # print dp
15
- return max (dp )
16
-
17
-
18
- def find (self , nums , dp ):
19
- max_element = - 1 * 2 << 31
20
-
21
- for i in range (len (nums ) - 2 , - 1 , - 1 ):
22
- if nums [i ] < nums [- 1 ]:
23
- max_element = max (max_element , dp [i ])
24
-
25
- return max_element if max_element != - 1 * 2 << 31 else 0
9
+ for i in range (len (nums )):
10
+ for j in range (i ):
11
+ if nums [i ] > nums [j ]:
12
+ dp [i ] = max (dp [i ], dp [j ] + 1 )
13
+
14
+ return max (dp ) if dp else 0
You can’t perform that action at this time.
0 commit comments