当前位置:网站首页>611. 有效三角形的个数
611. 有效三角形的个数
2022-07-05 07:18:00 【BugMaker-shen】

此时 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取 [ l , m i d − 1 ] [l,mid-1] [l,mid−1]内任何一个数都满足条件,故有 a n s + = ( m i d − l ) ans += (mid - l) ans+=(mid−l)
我们现在的三元组(2,6,7)满足条件了,由于不等式右侧nums[r]固定,我们需要做的是调整不等式左侧的大小,尽可能让不等式满足
由于我们让 l l l取 [ l , m i d − 1 ] [l,mid-1] [l,mid−1]内任何一个数就是尝试了让不等式左侧变得更大(依然满足条件),我们现在应该让不等式左侧变得更小,继续查看是否满足三角形条件,即向左移动mid,继续查找三元组

不满足条件时,两个小数字的和太小了,要让不等式左侧变大,只有一个选择:向右移动 l l l取更大的数
class Solution {
public:
int triangleNumber(vector<int>& nums) {
sort(nums.begin(), nums.end(), less<int>());
int n = nums.size();
int ans = 0;
// 每轮循环固定最大的数字不动
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是从r-1开始的,mid初始值较大,现在已经满足三角形条件,需要在较小数字中找满足三角形条件的数字
mid--;
}else{
// 两个小数字的和太小了
l++;
}
}
}
return ans;
}
};
那我们可以固定最小的值,用mid和r寻找三元组吗?
这种情况下,不满足三角形条件,两个小数字之和太小了,我们可以向右调整 m i d mid mid让 n u m s [ l ] + n u m s [ m i d ] nums[l]+nums[mid] nums[l]+nums[mid]的值更大,或向左调整 r r r,让 n u m s [ r ] nums[r] nums[r]的值更小,以满足三角形不等式。这操作起来就比较困难了:
我们向右调整mid,mid指向3,我们会漏掉(2,2,3)这个组合;向左调整r,r指向3,我们会漏掉(2,3,4)这个组合
所以我们应该把用于搜索三元组的两个指针放在不等式的同一侧,使得不满足三角形不等式时只有一个选择
边栏推荐
- Energy conservation and creating energy gap
- Using GEE plug-in in QGIS
- Course learning accumulation ppt
- [idea] efficient plug-in save actions to improve your work efficiency
- HDU1232 畅通工程(并查集)
- Mid 2022 documentary -- the experience of an ordinary person
- PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
- C#学习笔记
- SOC_ SD_ DATA_ FSM
- How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
猜你喜欢

C learning notes

PHY drive commissioning - phy controller drive (II)

Using GEE plug-in in QGIS

Three body goal management notes

Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘

Chapter 2: try to implement a simple bean container

Ros2 - common command line (IV)

目标检测系列——Faster R-CNN原理详解
![[idea] efficient plug-in save actions to improve your work efficiency](/img/6e/49037333964865d9900ddf5698f7e6.jpg)
[idea] efficient plug-in save actions to improve your work efficiency

SD_CMD_RECEIVE_SHIFT_REGISTER
随机推荐
2022.06.27_ One question per day
Delayqueue usage and scenarios of delay queue
Database SQL practice 4. Find the last of employees in all assigned departments_ Name and first_ name
Concurrent programming - deadlock troubleshooting and handling
The SQL implementation has multiple records with the same ID, and the latest one is taken
【Node】nvm 版本管理工具
[untitled]
MySQL setting trigger problem
Unity ugui how to match and transform coordinates between different UI panels or uis
The difference between NPM install -g/-save/-save-dev
ROS2——node节点(七)
Machine learning Seaborn visualization
SOC_ SD_ CMD_ FSM
Mipi interface, DVP interface and CSI interface of camera
arcgis_ spatialjoin
二分查找(折半查找)
基于Cortex-M3、M4的GPIO口位带操作宏定义(可总线输入输出,可用于STM32、ADuCM4050等)
2022.06.27_每日一题
Ros2 - ros2 vs. ros1 (II)
DataGrid offline installation of database driver
