当前位置:网站首页>Number of effective triangles (double pointer (reverse scanning) + bisection)
Number of effective triangles (double pointer (reverse scanning) + bisection)
2022-04-21 13:40:00 【4nc414g0n】
The number of effective triangles
Topic link
Title Description:
Given an array of non negative integers nums , Returns the number of triples that can form three sides of a triangle
Input :
nums = [2,2,3,4]
Output :
3
Ideas 1:
Sort + Two points: Prioritize , Ascending order guarantees the first two conditions for forming a triangle a+c>b b+c>a- Double cycle ( Satisfy a<b)
- Dichotomy from b The next start of , Find the most satisfying condition, that is a+b>c Of c, From this position to b Next in position (k-j) It is the trilateral case that meets the conditions
- Continue to cycle
Ideas 2:
Double pointer + Two points ( Reverse scanning ): Prioritize , And then traverse in reverse order nums Array ( First determine the maximum edge a, As long as the remaining two sides meet b + c > a, You must be satisfied a + c > b and a + b > c)- nums[i] For certain a edge ,l=0(b edge , Left pointer )r=i-1(c edge , Right pointer )
- Combine dichotomy while(l<r)
As long as meet b + c > a Just r–, c The edge is fixed to nums[r], meanwhile b The edge can be seen from l Fetch r-1 common r-l In this case ,cnt+=l-r;
otherwise l++Be careful : Why not use forward scanning here ?
answer : When scanning forward , If the sum of the two sides is less than the third side , Then there are two situations , One is to move the left pointer to the right , One is to move the right pointer to the left . Reverse scanning can only say that the left pointer is to the right
The code is as follows Sort + Two points :class Solution { public: int triangleNumber(vector<int>& nums) { // Sort + Two points sort(nums.begin(),nums.end());//less Ascending order guarantees the first two conditions for forming a triangle a+c>b b+c>a // All that remains is to meet a+b>c int cnt=0;// Count for(int i=0;i<nums.size();i++)// Double cycle ( Satisfy a<b) { int a=nums[i]; for(int j=i+1;j<nums.size();j++) { int b=nums[j]; int l=j+1;// from j+1 Start int r=nums.size()-1;// To the end of the array int k=j;// Final satisfaction c The number of may be 0 individual while(l<=r)// Split template { int mid=(l+r)/2; if(a+b>nums[mid]) { k=mid; l=mid+1; } else r=mid-1; } cnt+=(k-j);// The maximum value that satisfies the condition j+1~max It's all possible c Value obtained } } return cnt; } };
The code is as follows Double pointer + Two points :class Solution { public: int triangleNumber(vector<int>& nums) { sort(nums.begin(),nums.end()); int n=nums.size(); int cnt=0; for(int i=n-1;i>=2;i--) { int l=0; int r=i-1; while(l<r) { if(nums[l]+nums[r]>nums[i]) { cnt+=r-l; r--; } else l++; } } return cnt; } };
版权声明
本文为[4nc414g0n]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211217327598.html
边栏推荐
- If the field lines are the same, they will be merged into another field SQL statement?
- Number II that occurs only once (hash, bit operation, logic circuit, finite state automata)
- MySQL uses PIP and binlog2sql to install
- Open mmlab / mmpose installation and use tutorial
- After the completion of hundreds of millions of yuan of financing, smart bank plans to land urban intelligent driving products in more than 100 cities
- 做自媒体、短视频,不要再相信那些互关、互赞了
- LLVM之父Chris Lattner:编译器的黄金时代
- 前馈神经网络
- sql数据库入门-what&MySQL&按照MySQL
- metasploit渗透
猜你喜欢

Access的BOM开发(3)BOM展开

哈夫曼編碼

An example of expert system and its skeleton system

Upgrade the jdbc driver to version 8.0.28 and connect the pit record of MySQL

Go language file operation

What kind of comfortable sports earphones are recommended

Achieve random labels, font size, color random display

no server suitable for synchronization found

CV technical guide free knowledge planet

Actual ora2pg is migrated from Oracle11g to PG12 four
随机推荐
数字IC入门工具大全之 英特尔 Quartus Prime是什么?三个版本有什么区别
The difference between thread library and ASIO Library
Could not load dynamic library ‘libcusolver. so. 11‘
Detailed explanation and demonstration of 3-4dom shaped XSS
What about first-class insurance? Is there a charge? What are the waiting requirements?
Tailwind core concept - responsive design
单体测试使用Assert.assertThat(expected,Matcher matcher)来对比结果和预期
MySQL uses PIP and binlog2sql to install
npm---package.json
二叉树创建及其线索化
How to uninstall openjdk and install Oracle JDK under CentOS
Route selection, anti ring and re release of hcip OSPF
3-4Dom形XSS详解以及演示
Dynamic implementation of address book
Tailwind核心理念——响应式设计
运动耳机什么样的舒服、运动健身耳机推荐
Forced to choose an outsourcing company
promise---几个关键问题
Servlet中关于web.xml的测试
Longest common subsequence (I) (dynamic gauge)