当前位置:网站首页>尺取法:有效三角形的个数
尺取法:有效三角形的个数
2022-07-01 04:14:00 【小灵宝】
尺取法:有效三角形的个数
问题:

思路:
该题需要使用三个元素的尺取法求解,三个元素的尺取法就是需要枚举一个,然后利用双指针扫描剩下的区间,其余两个元素分别为区间左端点与右端点,根据题中的不同条件移动这两个元素之一
我们先对题中数组进行排序,三条边长从小到大为 a、b、c,当且仅当 a + b > c 这三条边能组成三角形。这道题只能枚举最大边进行反向扫描,如果是枚举最短边正向扫描,若两边之和小于第三边,那么会有两种情况,一种是左指针往右移动,一种是右指针往左移动,移动的情况是不确定的。枚举最大边反向扫描时,如果两边之和小于第三边,只能是左指针往右,移动的情况是确定的,如果两边之和大于等于第三边,只能是右指针往左。
因此我们枚举数组最右边的最大值,下标k,剩下的区间中,区间右端点为第二大的值,下标为j = k - 1,最小值从下标i = 0开始,若nums[i] + nums[j] > nums[k],那么i从当前的i开始,一直到j - 1,都能满足这个条件,因此我们找到了j - i个有效三角形,同时将右指针往左,j - -,寻找其他满足条件的情况。若nums[i] + nums[j] <= nums[k],将左指针往右,i ++。在i >= j时,我们就遍历了区间中的所有情况。
代码:
public int triangleNumber(int[] nums) {
// write code here
Arrays.sort(nums);
int ans = 0;
int i, j, k;
for (k = nums.length - 1; k >= 2; k--) {
j = k - 1;
i = 0;
while (i < j) {
if (nums[i] + nums[j] > nums[k]) {
ans += (j - i);
j--;
} else {
i++;
}
}
}
return ans;
}
边栏推荐
- Loop filtering based on Unet
- Programs and processes, process management, foreground and background processes
- 【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
- Use of JMeter counters
- What is uid? What is auth? What is a verifier?
- 京东智能客服言犀意图体系搭建和意图识别技术介绍
- TS type gymnastics: illustrating a complex advanced type
- One job hopping up 8K, three times in five years
- [recommended algorithm] C interview question of a small factory
- Obtain detailed ideas for ABCDEF questions of 2022 American Games
猜你喜欢
![[leetcode skimming] February summary (updating)](/img/62/0d0d9f11434e49d33754a2e4f2ea65.jpg)
[leetcode skimming] February summary (updating)

NFT:使用 EIP-2981 开启 NFT 版税之旅

Possible problems and solutions of using scroll view to implement slider view

Introduction of Spock unit test framework and its practice in meituan optimization___ Chapter I

Hololens2 development environment building and deploying apps
![[ta- frost wolf \u may- hundred people plan] 2.2 model and material space](/img/93/95ee3d4389ffd227559dc8b3207e1d.png)
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space

This may be your last chance to join Tencent

【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
![[ta- frost wolf \u may- hundred people plan] 1.1 rendering pipeline](/img/af/4498382bc47d8c9ae41c407b9d1265.png)
[ta- frost wolf \u may- hundred people plan] 1.1 rendering pipeline
![[human version] Web3 privacy game in the dark forest](/img/89/e16789b7f3892002748aab309c45e6.png)
[human version] Web3 privacy game in the dark forest
随机推荐
使用scroll-view实现滑块视图可能遇到的问题及其解决方法
Browser top loading (from Zhihu)
Knowledge supplement: redis' basic data types and corresponding commands
One job hopping up 8K, three times in five years
有效的 @SuppressWarnings 警告名称
TASK04|数理统计
Embedded System Development Notes 80: using QT designer to design the main interface
JMeter learning notes 2 - brief introduction to graphical interface
Visit the image URL stored by Alibaba cloud to preview the thumbnail directly on the web page instead of downloading it directly
类和对象收尾
创新界,聚势行 | 2022人大金仓“百城巡展”火热开启
浏览器顶部loading(来自知乎)
166. fractions to decimals
After many job hopping, the monthly salary is equal to the annual salary of old colleagues
Spock单元测试框架介绍及在美团优选的实践___第一章
409. longest palindrome
Jenkins automatically cleans up construction history
205. isomorphic string
LeetCode 1400. Construct K palindrome strings
Mallbook: how can hotel enterprises break the situation in the post epidemic era?