当前位置:网站首页>Rule method: number of effective triangles
Rule method: number of effective triangles
2022-07-01 04:16:00 【Xiaolingbao】
Ruler method : The number of effective triangles
List of articles
problem :
Ideas :
This problem needs to be solved by using the rule taking method of three elements , The rule method of three elements is to enumerate one , Then use the double pointer to scan the rest of the interval , The other two elements are the left end point and the right end point of the interval , Move one of the two elements according to the different conditions in the question
Let's sort the array in the question first , The three sides grow from small to large a、b、c, If and only if a + b > c
These three sides can form a triangle . This problem can only enumerate the largest edges for reverse scanning , If you are enumerating the shortest edge forward scan , If the sum of 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 , The situation of movement is uncertain . When enumerating the maximum edge reverse scanning , If the sum of the two sides is less than the third side , It can only be left pointer to right , The movement is certain , If the sum of two sides is greater than or equal to the third side , It can only be the right pointer to the left .
So we enumerate the rightmost maximum values of the array , Subscript k, In the remaining interval , The right end of the interval is the second largest value , Subscript to be j = k - 1, Minimum subscript i = 0 Start , if nums[i] + nums[j] > nums[k], that i From the present i Start , Until j - 1, Can meet this condition , So we found j - i Valid triangles , At the same time, turn the right pointer to the left ,j - -, Look for other conditions . if nums[i] + nums[j] <= nums[k], Turn the left pointer to the right ,i ++. stay i >= j when , We will traverse all the cases in the interval .
Code :
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;
}
边栏推荐
- 一些小知识点
- OSPF notes [multiple access, two multicast addresses with OSPF]
- 互联网行业最佳产品开发流程 推荐!
- 431. 将 N 叉树编码为二叉树 DFS
- CF1638E colorful operations
- NFT: start NFT royalty journey with eip-2981
- Coinbase in a bear market: losses, layoffs, stock price plunges
- Redis (VII) optimization suggestions
- JMeter学习笔记2-图形界面简单介绍
- TASK04|数理统计
猜你喜欢
Millet College wechat scanning code login process record and bug resolution
TASK04|数理统计
定了!2022京东全球科技探索者大会之京东云峰会7月13日北京见
Volley parsing data shows networking failure
After many job hopping, the monthly salary is equal to the annual salary of old colleagues
Do280 management application deployment --rc
It's settled! 2022 JD cloud summit of JD global technology Explorer conference see you in Beijing on July 13
JMeter学习笔记2-图形界面简单介绍
小程序中自定义组件
25.k sets of flipped linked lists
随机推荐
陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
LetCode 1829. Maximum XOR value per query
mysql 函数 变量 存储过程
Embedded System Development Notes 81: Using Dialog component to design prompt dialog box
Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services
LeetCode 1400. Construct K palindrome strings
PageObject模式解析及案例
25.k sets of flipped linked lists
为什么香港服务器最适合海外建站使用
不同性能测试工具的并发模式
【TA-霜狼_may-《百人計劃》】1.2.1 向量基礎
MySQL function variable stored procedure
嵌入式系统开发笔记80:应用Qt Designer进行主界面设计
ThreeJS开篇
[EI conference] the Third International Conference on nanomaterials and nanotechnology in 2022 (nanomt 2022)
318. Maximum word length product
小程序中自定义组件
Embedded System Development Notes 79: why should I get the IP address of the local network card
Obtain detailed ideas for ABCDEF questions of 2022 American Games
Analyse et cas du modèle pageobject