当前位置:网站首页>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;
}
边栏推荐
- Embedded System Development Notes 81: Using Dialog component to design prompt dialog box
- 浏览器顶部loading(来自知乎)
- One job hopping up 8K, three times in five years
- Jenkins自动清理构建历史
- [leetcode skimming] February summary (updating)
- MallBook:后疫情时代下,酒店企业如何破局?
- 互联网行业最佳产品开发流程 推荐!
- Leetcode learning - day 36
- Deep learning | rnn/lstm of naturallanguageprocessing
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
猜你喜欢

Use winmtr software to simply analyze, track and detect network routing

NFT: start NFT royalty journey with eip-2981

Deep learning | rnn/lstm of naturallanguageprocessing

Obtain detailed ideas for ABCDEF questions of 2022 American Games

Jenkins automatically cleans up construction history

Programs and processes, process management, foreground and background processes

熊市下的Coinbase:亏损、裁员、股价暴跌

MFC window scroll bar usage

【TA-霜狼_may-《百人计划》】2.2 模型与材质空间

After many job hopping, the monthly salary is equal to the annual salary of old colleagues
随机推荐
MySQL function variable stored procedure
Grid system in bootstrap
小程序中自定义组件
LetCode 1829. Maximum XOR value per query
PageObject模式解析及案例
【TA-霜狼_may-《百人计划》】1.2.2 矩阵计算
Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services
[leetcode skimming] February summary (updating)
The programmer's girlfriend gave me a fatigue driving test
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
Leetcode learning - day 36
After many job hopping, the monthly salary is equal to the annual salary of old colleagues
Libevent Library Learning
674. longest continuous increasing sequence force buckle JS
[recommended algorithm] C interview question of a small factory
318. Maximum word length product
Hololens2 development environment building and deploying apps
LeetCode 1380. Lucky number in matrix
类和对象收尾
OSPF notes [multiple access, two multicast addresses with OSPF]