当前位置:网站首页>Leetcode:1498. Number of subsequences that meet the conditions [sort + bisection + power hash table]
Leetcode:1498. Number of subsequences that meet the conditions [sort + bisection + power hash table]
2022-07-27 20:36:00 【White speed Dragon King's review】

analysis
Subsequence should be maximum and minimum , In fact, it doesn't matter if you sort
Fixed the smallest , You can find the largest
Then the middle option is optional
So there is 2 The power of
And then in order to accelerate ,2 To hash and save
Don't count every time
ac code
class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
MOD = 10 ** 9 + 7
n = len(nums)
nums.sort()
# Hash computing power memory
f = [1] * (n + 1)
for i in range(1, n + 1):
f[i] = f[i - 1] * 2 % MOD
# Fix the smallest
ans = 0
for i in range(n):
minn = nums[i]
maxn = target - minn
if maxn < minn:
break
idx = bisect_right(nums, maxn) - 1
if idx >= i:
ans += f[idx - i]
ans %= MOD
return ans
summary
Hash memory power acceleration
边栏推荐
- ES6--拓展运算符运用
- Preprocessing and macro definition
- C语言pow函数(c语言中指数函数怎么打)
- IE11 下载doc pdf等文件的方法
- 为什么需要第三方支付?
- es6删除对象的属性_ES6删除对象中的某个元素「建议收藏」
- [rctf2015]easysql-1 | SQL injection
- 图解LeetCode——剑指 Offer II 115. 重建序列(难度:中等)
- Understand │ what is cross domain? How to solve cross domain problems?
- JS realizes video recording - Take cesium as an example
猜你喜欢

antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key

预处理与宏定义

Ten year test old bird talk about mobile terminal compatibility test

Two years after its release, the price increased by $100, and the reverse growth of meta Quest 2

Datepicker date selector in viewui compatible solution in ie11 browser

Pyqt5 rapid development and practice 4.5 button controls and 4.6 qcombobox (drop-down list box)

Check the internship salary of Internet companies: with it, you can also enter the factory

Redis queue、rdb学习

一看就懂的ESLint

Redis thing learning
随机推荐
What app should individuals use for stock speculation to be safer and faster
Konka semiconductor's first storage master chip was mass produced and shipped, with the first batch of 100000 chips
Unity fairygui play video (Lua)
ES6 deleting attributes of objects_ ES6 delete an element "suggested collection" in the object
Codeworks round 810 (Div. 2) B.Party super detailed problem solution
Standing on the shoulders of giants to learn, jd.com's popular architect growth manual was launched
JS jump to the page and refresh (jump to this page)
一个程序员的水平能差到什么程度?
antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
MySQL 日志错误日志
In 2019, China's smart machine Market: Huawei won nearly 4 components, firmly ranking first in China
ES6--解构赋值
继华为、联发科之后,这家手机芯片厂商宣布向武汉捐款700万
Mlx90640 infrared thermal imager temperature sensor module development notes (VII)
IE11 下载doc pdf等文件的方法
发布2年后涨价100美元,Meta Quest 2的逆生长
Under the epidemic, I left my job for a year, and my income increased 10 times
Why do we need third-party payment?
Anfulai embedded weekly report no. 275: 2022.07.18--2022.07.24
shell