classSolution:defcountPairs(self, nums: List[int], target:int)->int:
nums.sort()
ans = left =0
right =len(nums)-1while left < right:if nums[left]+ nums[right]< target:
ans += right - left
left +=1else:
right -=1return ans
classSolution:defcanMakeSubsequence(self, str1:str, str2:str)->bool:
n =len(str2)
j =0for c in str1:if j < n:
d =ord(c)+1if d >ord('z'):
d =ord('a')if c == str2[j]orchr(d)== str2[j]:
j +=1return j == n
classSolution:defminimumOperations(self, nums: List[int])->int:
dp =[]for v in nums:ifnot dp or v >=dp[-1]:
dp.append(v)else:
p = bisect_right(dp,v)
dp[p]= v
returnlen(nums)-len(dp)# n = len(nums)# f = [1]*n # for i in range(1,n):# for j in range(i):# if nums[i] >= nums[j]:# f[i] = max(f[i],f[j]+1)# return len(nums) - max(f)
classSolution:defnumberOfBeautifulIntegers(self, low:int, high:int, k:int)->int:deff(s):
n =len(s)@cachedeff(i, p, even, odd, is_limit):if i == n:returnint(even+odd>0and even == odd and p ==0)
up =int(s[i])if is_limit else9
down =0if even+odd else1
ans =0ifnot even+odd:
ans += f(i +1,0,0,0,False)for j inrange(down, up +1):
ans += f(i +1,(p *10+ j)% k, even +(not j &1), odd +(j &1), is_limit and j == up)return ans
return f(0,0,0,0,True)return f(str(high))- f(str(low -1))