File tree 1 file changed +6
-35
lines changed
1 file changed +6
-35
lines changed Original file line number Diff line number Diff line change 2
2
3
3
# Dynamic Programming
4
4
5
- # N = int(input())
6
- # A = list(map(int, input().split()))
7
-
8
- # dp = [1 for _ in range(N)]
9
-
10
- # for i in range(N):
11
- # for j in range(i):
12
- # if A[i] > A[j]:
13
- # dp[i] = max(dp[i], dp[j]+1)
14
-
15
- # print(dp)
16
-
17
- # Binary Search, Stack
18
-
19
5
N = int (input ())
20
6
A = list (map (int , input ().split ()))
21
7
22
- stack = []
23
-
24
-
25
- def binarySearch (arr , start , end , target ):
26
- if (start >= end ):
27
- return start
28
- mid = (start + end ) // 2
29
-
30
- if (arr [mid ] == target ):
31
- return mid
32
- if (arr [mid ] < target ):
33
- return binarySearch (arr , mid + 1 , end , target )
34
- return binarySearch (arr , start , mid , target )
35
-
8
+ dp = [1 for _ in range (N )]
36
9
37
- for a in A :
38
- if not stack or (stack [- 1 ] < a ):
39
- stack .append (a )
40
- else :
41
- i = binarySearch (stack , 0 , len (stack ), a )
42
- stack [i ] = a
10
+ for i in range (N ):
11
+ for j in range (i ):
12
+ if A [i ] > A [j ]:
13
+ dp [i ] = max (dp [i ], dp [j ]+ 1 )
43
14
44
- print (len ( stack ))
15
+ print (max ( dp ))
You can’t perform that action at this time.
0 commit comments