当前位置:网站首页>Big meal count (time complexity) -- leetcode daily question
Big meal count (time complexity) -- leetcode daily question
2022-06-11 05:21:00 【Food can be very delicious】
Title Description

Example

Their thinking
The problem is simple , Finding statistics in an array and adding two numbers is 2 Group number of powers of
The difficulty is how to find the right two numbers in an array of 100000 numbers 
The first point is also easy to think of , The hash table will record the number of meals with the same delicious degree , This will slightly reduce the size of the target array
Record different tastes in the array , And sort . The purpose of this is to facilitate us to traverse the array in reverse order , This can avoid repetition caused by different order
according to a+b=c,c-a=b The principle is to directly query whether there is a target in the hash table , First, set the upper limit Double the current value 2 The logarithmic +1, Delimit the lower limit Double the current value 2 The logarithmic , Of course, it should be converted to an integer . Then traverse the integer between the upper and lower bounds , According to the changing c, Use c-a Look for b
find b Remember to multiply the values of the two hash tables
When double a number, it happens to be 2 And the number of meals is greater than 1, It is also what we need to record , It is necessary to add judgment to the program .
Code
Python Realization
import math
class Solution:
def countPairs(self, deliciousness: List[int]) -> int:
hasmap = dict()
for i in deliciousness:
if i not in hasmap:
hasmap[i] = 1
else:
hasmap[i] += 1
answer=0
l=sorted(list(hasmap.keys()))
for i in range(len(l)-1,-1,-1):
if l[i]>0:
Max=int(math.log(l[i]*2,2))+1
Min=int(math.log(l[i],2))
for j in range(Min,Max+1):
temp=2**j-l[i]
if temp in hasmap and temp!=l[i] and temp<l[i]:
answer += hasmap[l[i]] * hasmap[temp]
if l[i] != 0:
if math.log(2 * l[i], 2) % 1 == 0:
answer += (hasmap[l[i]] * (hasmap[l[i]] - 1) // 2)
return answer%(10**9+7)

Shortcomings are welcome to be pointed out in the comment area
边栏推荐
- jvm调优五:jvm调优工具和调优实战
- [entry level basics] node basic knowledge summary
- Yolov5 training personal data set summary
- Feynman learning method
- 49. grouping of acronyms
- The central rural work conference has released important signals. Ten ways for AI technology to help agriculture can be expected in the future
- Deep search + backtracking
- Huawei equipment is configured to access the virtual private network through GRE tunnel
- Following the wave of lack of core, Lianrui launched a number of gigabit network card solutions
- Basics of customized view
猜你喜欢

Preliminary test of running vins-fusion with zed2 binocular camera

Wechat applet uploads the data obtained from database 1 to database 2

Recommend a free intranet penetration open source software that can be used in the local wechat official account under test

高斯白噪声(white Gaussian noise,WGN)

Apply the intelligent OCR identification technology of Shenzhen Yanchang technology to break through the bottleneck of medical bill identification at one stroke. Efficient claim settlement is not a dr

Huawei equipment is configured to access the virtual private network through GRE tunnel

Linked list de duplication

Top 100 video information of station B

Games101 job 7-path tracing implementation process & detailed interpretation of code

如何让灯具智能化,单火、零火智能开关怎么选!
随机推荐
The central rural work conference has released important signals. Ten ways for AI technology to help agriculture can be expected in the future
1. use alicloud object OSS (basic)
截取文件扩展名
Project - Smart City
go单元测试实例;文件读写;序列化
oh my zsh正确安装姿势
Customize the layout of view Foundation
New product release: Lianrui launched a dual port 10 Gigabit bypass network card
Analyze while doing experiments -ndk article -jni uses registernatives for explicit method registration
Restoration of binary tree -- number restoration
Sealem finance builds Web3 decentralized financial platform infrastructure
White Gaussian noise (WGN)
JS promise, async, await simple notes
Lr-link Lianrui fully understands the server network card
Inventory | ICLR 2022 migration learning, visual transformer article summary
Opencv learning path (2-4) -- Deep parsing cvtcolor function
22、生成括号
Basics of customized view
Section II: structural composition characteristics of asphalt pavement (2) structural layer and performance requirements
Chapter I: Net architecture (1)