当前位置:网站首页>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
边栏推荐
- Negative number storage and type conversion in programs
- Mathematical analysis_ Notes_ Chapter 8: multiple integral
- Target detection series - detailed explanation of the principle of fast r-cnn
- golang定时器使用踩的坑:定时器每天执行一次
- [untitled]
- Powermanagerservice (I) - initialization
- 目标检测系列——Faster R-CNN原理详解
- Implementation of one-dimensional convolutional neural network CNN based on FPGA (VIII) implementation of activation layer
- Use of Pai platform
- 【无标题】
猜你喜欢
1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
U-Boot初始化及工作流程分析
Clickhouse database installation deployment and remote IP access
window navicat连接阿里云服务器mysql步骤及常见问题
Rough notes of C language (1)
Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
[idea] efficient plug-in save actions to improve your work efficiency
[software testing] 02 -- software defect management
DataGrid offline installation of database driver
SOC_ SD_ CMD_ FSM
随机推荐
Idea push project to code cloud
C learning notes
【idea】Could not autowire. No beans of xxx type found
苏打粉是什么?
SD_ CMD_ SEND_ SHIFT_ REGISTER
Oracle code use
Batch convert txt to excel format
公安专业知识--哔哩桐老师
[software testing] 02 -- software defect management
Binary search (half search)
Microservice registry Nacos introduction
Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no
Unconventional ending disconnected from the target VM, address: '127.0.0.1:62635', transport: 'socket‘
Rough notes of C language (1)
What is sodium hydroxide?
1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
剑指 Offer 56 数组中数字出现的次数(异或)
Course learning accumulation ppt
SOC_ SD_ CMD_ FSM
[vscode] prohibit the pylance plug-in from automatically adding import