当前位置:网站首页>611. Number of effective triangles
611. Number of effective triangles
2022-07-05 07:24:00 【BugMaker-shen】
611. The number of effective triangles
here n u m s [ l ] + n u m s [ m i d ] > n u m s [ r ] nums[l] + nums[mid] > nums[r] nums[l]+nums[mid]>nums[r], l l l take [ l , m i d − 1 ] [l,mid-1] [l,mid−1] Any number in satisfies the condition , There are a n s + = ( m i d − l ) ans += (mid - l) ans+=(mid−l)
Our current triple (2,6,7) The conditions are satisfied , Due to the inequality on the right nums[r] Fix , What we need to do is to adjust the size of the left side of the inequality , Try to satisfy the inequality
Because we let l l l take [ l , m i d − 1 ] [l,mid-1] [l,mid−1] Any number within is an attempt to make the left side of the inequality larger ( Still meet the conditions ), We should now make the left side of the inequality smaller , Continue to check whether the triangle condition is met , That is to move to the left mid, Continue looking for triples
When the conditions are not met , The sum of two small numbers is too small , To make the left side of the inequality larger , There is only one choice : To the right l l l Take a larger number
class Solution {
public:
int triangleNumber(vector<int>& nums) {
sort(nums.begin(), nums.end(), less<int>());
int n = nums.size();
int ans = 0;
// Fix the maximum number in each cycle
for (int r = n - 1; r >= 2; r--){
int l = 0;
int mid = r - 1;
while(l < mid){
if(nums[l] + nums[mid] > nums[r]){
ans += (mid - l);
// mid It's from r-1 At the beginning ,mid The initial value is large , Now the triangle condition is satisfied , You need to find the number that meets the triangle condition among the smaller numbers
mid--;
}else{
// The sum of two small numbers is too small
l++;
}
}
}
return ans;
}
};
Then we can fix the minimum value , use mid and r Looking for triples ?
In this case , Triangle condition is not satisfied , The sum of two small numbers is too small , We can adjust to the right m i d mid mid Give Way n u m s [ l ] + n u m s [ m i d ] nums[l]+nums[mid] nums[l]+nums[mid] It's worth more , Or left adjustment r r r, Give Way n u m s [ r ] nums[r] nums[r] It's worth less , To satisfy the triangle inequality . This operation is more difficult :
Let's adjust to the right mid,mid Point to 3, We will miss (2,2,3) This combination ; To the left r,r Point to 3, We will miss (2,3,4) This combination
So we should put two pointers for searching triples on the same side of the inequality , When triangle inequality is not satisfied, there is only one choice
边栏推荐
- Netease to B, soft outside, hard in
- Three body goal management notes
- PowerManagerService(一)— 初始化
- Hdu1232 unimpeded project (and collection)
- [solved] there is something wrong with the image
- Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
- I can't stand the common annotations of idea anymore
- Docker installs MySQL and uses Navicat to connect
- 行测--资料分析--fb--高照老师
- Database SQL practice 4. Find the last of employees in all assigned departments_ Name and first_ name
猜你喜欢
Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
【idea】Could not autowire. No beans of xxx type found
SD_ CMD_ SEND_ SHIFT_ REGISTER
并查集理论讲解和代码实现
M2DGR 多源多场景 地面机器人SLAM数据集
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
[software testing] 03 -- overview of software testing
Solve tensorfow GPU modulenotfounderror: no module named 'tensorflow_ core. estimator‘
Hdu1231 maximum continuous subsequence (divide and conquer or dynamic gauge or double pointer)
(tool use) how to make the system automatically match and associate to database fields by importing MySQL from idea and writing SQL statements
随机推荐
[software testing] 04 -- software testing and software development
氫氧化鈉是什麼?
What is soda?
Interpretation of the earliest sketches - image translation work sketchygan
Application of MATLAB in Linear Algebra (4): similar matrix and quadratic form
Executealways of unity is replacing executeineditmode
借助 Navicat for MySQL 软件 把 不同或者相同数据库链接中的某数据库表数据 复制到 另一个数据库表中
[software testing] 02 -- software defect management
Concurrent programming - how to interrupt / stop a running thread?
[untitled]
Don't confuse the use difference between series / and / *
Import CV2 prompt importerror: libgl so. 1: Cannot open shared object file: no such file or directory
Chapter 2: try to implement a simple bean container
The SQL implementation has multiple records with the same ID, and the latest one is taken
Brief description of inux camera (Mipi interface)
【obs】x264编码:“buffer_size“
【无标题】
Tshydro tool
Reading literature sorting 20220104
行测--资料分析--fb--高照老师