class Solution:
def get_length(self, arr):
'''
1 <= n <= 10^5
only has values from 1 to n
[4,2,6,5,3,1] 4,2,6 or 4,2,5, or 4,2,3 return 3
0,1,2,3,4,5
[3,5,1,2,7,6,4] 【5,1,2,7】 5,1,2,6 5,1,2,4
smaller_than_left_set = [1,2,3,4]
smaller_than_left_set.remove(visit_element)
len(smaller_than_left_set) + 2
'''
n = len(arr)
if n == 1 or n == 2: return n
def left_max(arr):
re = 2
lv = set() # left visited
for i in range(n):
l = arr[i]
lv.add(l)
all_smaller_l = set(range(1, l))
rest = all_smaller_l - lv
for j in range(n-1, i, -1):
r = arr[j]
if r in rest:
rest.remove(r)
if l < r:
re = max(re, 2 + len(rest))
return re
l_max = left_max(arr)
r_max = left_max(arr[::-1])
return max(l_max, r_max)
if __name__ == '__main__':
s = Solution()
re = s.get_length([3,5,1,2,7,6,4])
print(re)
re = s.get_length([4,2,6,5,3,1])
print(re)
779. K-th Symbol in Grammar
class Solution:
def kthGrammar(self, n: int, k: int) -> int:
'''
0--------------------1
0 1. 2
0 1 1 0 3
0 1 1 0 1 0 0 1 4 # 后半部分的1001 其实是前面0110 的取反
- 1 2. 3 4. 5 6 7. 8
def divide(k):
# k = 1 return L, K=2, return R
mod = k % 2
if mod:
return "L"
else:
return "R"
path = []
for i in range(n-1):
path.append(divide(k))
k = k // 2 + 1 if k % 2 else k // 2
re = 0
# print(path)
for p in path:
if p == "L":
re = 0 if re == 0 else 1
else:
re = 1 if re == 0 else 0
return re
'''
N = n
K = k
def solver(N,K):
if N==1 and K == 1 :
return 0
mid = 2**(N-2)
if K <= mid:
return solver(N-1,K)
if K > mid:
return 1-solver(N-1,K-mid)
return solver(N,K)
869. Reordered Power of 2
class Solution:
def reorderedPowerOf2(self, n: int) -> bool:
# 1, 2 4 8, 4
# 16 32 64, 3
# 128 256 512, 3
# 1024 2048 4096 8192, 4
# 16384 32768 65536 3
# 131072 262144 524288 3
# 1048576 2097152 4194304 8388608 4
'''
size = len(str(n))
s = sorted(str(n))
count = [4, 3, 3]
j = -1
x = 0 # 2**x
for i in range(size):
j = j + 1 if j + 1 < 2 else 0
x += count[j]
# number_of_possible_values = count[j], start from x-count[j]-1 to x-1 include
for i in range(x-count[j]-1, x):
value = str(2 ** i)
if sorted(value) == s:
return True
return False
'''
# option2 is the other way around, 10**9 < 2**30. so loop from 2**0 to 2**30, sort the value and compare with givin sort value
s = sorted(str(n))
for i in range(31):
value = str(2**i)
if sorted(value) == s:
return True
return False
1131. Maximum of Absolute Value Expression
class Solution:
def maxAbsValExpr(self, A: List[int], B: List[int]) -> int:
# abs(A) + abs(B) = max(A+B, A-B, -A+B, -A-B)
# So if we plug in A and B, for i < j the score of a pair
# is max of the following four:
# A[j]-A[i]+B[j]-B[i]+j-i = A[j]+B[j]+j - (A[i]+B[i]+i)
# A[j]-A[i]-B[j]+B[i]+j-i = A[j]-B[j]+j - (A[i]-B[i]+i)
# -A[j]+A[i]+B[j]-B[i]+j-i =-A[j]+B[j]+j - (-A[i]+B[i]+i)
# -A[j]+A[i]-B[j]+B[i]+j-i =-A[j]-B[j]+j - (-A[i]-B[i]+i)
# ------------ --------------
# current index subtract lowest
# value we've seen
# over all i < j
# (cumulative min)
pp = A[0]+B[0] + 0 # i = 0
pm = A[0]-B[0]
mp = -A[0]+B[0]
mm = -A[0]-B[0]
best = 0
for j in range(1, len(A)): # because of the for loop, j will > i
ppj = A[j]+B[j]+j
pmj = A[j]-B[j]+j
mpj = -A[j]+B[j]+j
mmj = -A[j]-B[j]+j
best = max(best, ppj-pp, pmj-pm, mpj-mp, mmj-mm)
pp = min(pp, ppj)
pm = min(pm, pmj)
mp = min(mp, mpj)
mm = min(mm, mmj)
return best
1860. Incremental Memory Leak
class Solution: