Skip to content

Commit 77ef1b5

Browse files
committedMar 7, 2025
Add solution #611
1 parent 2c2d29d commit 77ef1b5

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@
468468
605|[Can Place Flowers](./0605-can-place-flowers.js)|Easy|
469469
606|[Construct String from Binary Tree](./0606-construct-string-from-binary-tree.js)|Easy|
470470
609|[Find Duplicate File in System](./0609-find-duplicate-file-in-system.js)|Medium|
471+
611|[Valid Triangle Number](./0611-valid-triangle-number.js)|Medium|
471472
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
472473
621|[Task Scheduler](./0621-task-scheduler.js)|Medium|
473474
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 611. Valid Triangle Number
3+
* https://leetcode.com/problems/valid-triangle-number/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums, return the number of triplets chosen from the array that
7+
* can make triangles if we take them as side lengths of a triangle.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
var triangleNumber = function(nums) {
15+
let count = 0;
16+
17+
nums.sort((a, b) => a - b);
18+
19+
for (let k = nums.length - 1; k >= 2; k--) {
20+
for (let i = 0, j = k - 1; i < j;) {
21+
if (nums[i] + nums[j] > nums[k]) {
22+
count += j - i;
23+
j--;
24+
} else {
25+
i++;
26+
}
27+
}
28+
}
29+
30+
return count;
31+
};

0 commit comments

Comments
 (0)
Please sign in to comment.