当前位置:网站首页>[binary search - medium] 1498 Number of subsequences satisfying the condition
[binary search - medium] 1498 Number of subsequences satisfying the condition
2022-04-21 15:11:00 【Caicai 2022】
subject
Be careful :
- The topic is to calculate the number of subsequences
Here we need to clarify a concept , A subsequence is a non continuous sequence of elements in the original sequence , Suppose you have a sequence [1,2,3,4,5,6] be [1,2,3],[1,5,6] Are all subsequences of the original sequence - Program input sequence nums Is chaotic , You can do it first nums Sort and then operate , It has no effect on the results , But it can simplify the program code
【 Code 】 Double pointer
Execution time :7380 ms, In all Python3 Defeated in submission 27.50% Users of
Memory consumption :24.6 MB, In all Python3 Defeated in submission 50.00% Users of
Pass the test case :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)
【 Method 2】 Dichotomy
Execution time :7992 ms, In all Python3 Defeated in submission 14.17% Users of
Memory consumption :24.9 MB, In all Python3 Defeated in submission 5.83% Users of
Pass the test case :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
【 Method 2 The accelerated version of 】
Execution time :580 ms, In all Python3 Defeated in submission 58.33% Users of
Memory consumption :24.5 MB, In all Python3 Defeated in submission 69.17% Users of
Pass the test case :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
【 Method 3】 Two points
Execution time :220 ms, In all Python3 Defeated in submission 95.83% Users of
Memory consumption :24.5 MB, In all Python3 Defeated in submission 70.83% Users of
Pass the test case :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
版权声明
本文为[Caicai 2022]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211507332589.html
边栏推荐
- 【二分搜索-中等】1855. 下标对中的最大距离(需要复习双指针法)
- lightGBM专题4:pyspark平台下lightgbm模型保存
- Mysql8. Method for resetting initial password above 0
- Shang Silicon Valley smart campus - 6. Realization of administrator function
- MongoDB分组查询
- Would like to ask how to find the software registration code from the database
- 【C语言进阶】自定义类型:结构体,位段,枚举,联合
- 武汉科技大学C语言上机实验题(第一第二第三部分)
- Dry goods | hidden rules for workplace promotion of testers: workplace advice of Senior Test Manager with 15 years of experience
- 智慧党建系统开发,智慧组工党员信息管理平台建设
猜你喜欢

MySQL 8.0.11 installation tutorial (Windows version)
![[JS] urlsearchparams object (upload parameters to URL in the form of object)](/img/e1/5ff277e990f9aa61072c07142febca.png)
[JS] urlsearchparams object (upload parameters to URL in the form of object)

数字化时代,SaaS软件如何成为国产化替代的轻骑兵?

Software testing (III) p51-p104 software test case methods and defects

pytorch图像分类篇: 花分类数据集下载和AlexNet网络搭建训练

Detailed explanation of basic knowledge of database 5: index and its two engines in mysql, master-slave replication and relational / non relational database

突然掉电,为啥MySQL也不会丢失数据?(收藏)

Cousin node-c language of binary tree

怎么去约束代码的统一性

Amazon evaluates the autotrophic number. What should sellers do if they want to get a review?
随机推荐
js 百度地图定位
Dry goods | record your first web automation test case
VMware Horizon 8 2111 部署系列(十三)创建应用程序池
MySQL 8.0.11 installation tutorial (Windows version)
Mysql8. Method for resetting initial password above 0
Detailed explanation of spark SQL underlying execution process
【C语言】C语言标准库大梳理(超全)
Lightgbm topic 3: implementation of stringindexer and pipeline functions in pyspark
May day financial products have no income?
干货 | 录制你的第一个web 自动化测试用例
[binary search - simple] sword finger offer II 068 Find insertion location
SAP UI5 應用開發教程之七十 - 如何使用按鈕控件觸發頁面路由跳轉試讀版
【二分查找-简单】374. 猜数字大小
Minimum steps of manufacturing letter ectopic words - C language solution
&lt;译文&gt;设置Prometheus并将其与Grafana集成以进行监控
Insect idol Zhi Huijun, keep up with God
Lightgbm topic 2: detailed explanation of lightgbm training based on pyspark platform
如何在Nodejs项目中调用uniCloud云存储进行上传文件
干货 | 环境问题还是测试的老大难?两个步骤轻松搞定
期货开户找哪家公司?哪家期货公司最安全?