当前位置:网站首页>【二分搜索-中等】1498. 满足条件的子序列数目
【二分搜索-中等】1498. 满足条件的子序列数目
2022-04-21 15:08:00 【菜菜2022】
题目
注意:
- 题目是计算子序列的数目
这里要明确一个概念,子序列是原序列中的可非连续的元素列,假设有个数列[1,2,3,4,5,6]则[1,2,3],[1,5,6]都是原数列的子数列 - 程序输入的数列nums是无序的,可以先对nums进行排序再操作,对结果没有什么影响,但是可以简化程序代码
【代码】双指针
执行用时:7380 ms, 在所有 Python3 提交中击败了27.50% 的用户
内存消耗:24.6 MB, 在所有 Python3 提交中击败了50.00% 的用户
通过测试用例:62 / 62
class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
nums.sort()
left,right=0,len(nums)-1
ans=0
while left<=right:
if nums[left]+nums[right]<=target:
ans+=2**(right-left)
left+=1
else:
right-=1
return ans%(1000000007)
【方法2】二分法
执行用时:7992 ms, 在所有 Python3 提交中击败了14.17% 的用户
内存消耗:24.9 MB, 在所有 Python3 提交中击败了5.83% 的用户
通过测试用例:62 / 62
class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
n = len(nums)
P = 10**9 + 7
nums.sort()
ans = 0
for i, num in enumerate(nums):
if nums[i] * 2 > target:
break
maxValue = target - nums[i]
pos=-1
left,right=i,n-1
while left<=right:
mid=left+(right-left)//2
if nums[mid]<=(target-num):
pos=mid
left=mid+1
else:
right=mid-1
contribute = 2**(pos - i) if pos >= i else 0
ans += contribute
return ans % P
【方法2的加速版本】
执行用时:580 ms, 在所有 Python3 提交中击败了58.33% 的用户
内存消耗:24.5 MB, 在所有 Python3 提交中击败了69.17% 的用户
通过测试用例:62 / 62
class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
n = len(nums)
P = 10**9 + 7
nums.sort()
ans = 0
f = [1] + [0] * (n - 1)
for i in range(1, n):
f[i] = f[i - 1] * 2 % P
for i, num in enumerate(nums):
if nums[i] * 2 > target:
break
maxValue = target - nums[i]
pos=-1
left,right=i,n-1
while left<=right:
mid=left+(right-left)//2
if nums[mid]<=(target-num):
pos=mid
left=mid+1
else:
right=mid-1
contribute = f[pos - i] if pos >= i else 0
ans += contribute
return ans % P
【方法3】二分
执行用时:220 ms, 在所有 Python3 提交中击败了95.83% 的用户
内存消耗:24.5 MB, 在所有 Python3 提交中击败了70.83% 的用户
通过测试用例:62 / 62
class Solution:
def numSubseq(self, nums: List[int], target: int) -> int:
n = len(nums)
P = 10**9 + 7
f = [1] + [0] * (n - 1)
# print(f)
for i in range(1, n):
f[i] = f[i - 1] * 2 % P
# print(f)
nums.sort()
ans = 0
for i, num in enumerate(nums):
if nums[i] * 2 > target:
break
maxValue = target - nums[i]
pos = bisect.bisect_right(nums, maxValue) - 1
# print("maxValue:",maxValue," pos:",pos,' i:',i)
contribute = f[pos - i] if pos >= i else 0
# print(contribute)
ans += contribute
return ans % P
版权声明
本文为[菜菜2022]所创,转载请带上原文链接,感谢
https://blog.csdn.net/kz_java/article/details/124295065
边栏推荐
- lightGBM专题5:pyspark表数据处理之数据合并
- 使用wx.showActionSheet选择框修改数据库中的信息,为什么会报data未定义的错呢
- Lightgbm topic 2: detailed explanation of lightgbm training based on pyspark platform
- Druid database link problem
- [cloud based co creation] Huawei cloud database - basic knowledge
- C language preprocessing problem
- VMware horizon 8 2111 deployment series (XIII) creating application pool
- Software testing (III) p51-p104 software test case methods and defects
- The conversion between RDD and dataframe in pyspark is realized by RDD processing dataframe: data segmentation and other functions
- 架构实战毕业总结
猜你喜欢
随机推荐
lightGBM专题4:pyspark平台下lightgbm模型保存
期货开户找哪家公司?哪家期货公司最安全?
软件测试(三)p51-p104 软件测试用例方法、缺陷
武汉科技大学C语言上机实验题(第一第二第三部分)
想请问一下如何从数据库中找到软件注册码
【天梯赛】L2-040 哲哲打游戏 (25 point(s))(模拟)
elemetn表单控件---未通过校验字段提交时自动定位到该字段位置
Golang zap log
虫子 12864
Legendary server setup tutorial, legendary GM permission command setting tutorial
干货 | 环境问题还是测试的老大难?两个步骤轻松搞定
pytorch图像分类篇:pytorch官方demo实现一个分类器(LeNet)
SAP UI5 应用开发教程之七十 - 如何使用按钮控件触发页面路由跳转试读版
ENSP三层交换机连接二层交换机及路由器的做法
The worm inserted hill
Excel小技巧-VLOOKUP自动匹配
Spark SQL底层执行流程详解
如何选择合适的 Neo4j 版本(2022)
亚马逊测评自养号,卖家想要获得review应该怎么做?
Lightgbm topic 3: implementation of stringindexer and pipeline functions in pyspark



![[today in history] April 21: microprocessor pioneer was born; Winamp release; Coppa comes into force](/img/71/6f61fb0d0a46a25d92bdb48d02f413.png)





