File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 468
468
605|[ Can Place Flowers] ( ./0605-can-place-flowers.js ) |Easy|
469
469
606|[ Construct String from Binary Tree] ( ./0606-construct-string-from-binary-tree.js ) |Easy|
470
470
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|
471
472
617|[ Merge Two Binary Trees] ( ./0617-merge-two-binary-trees.js ) |Easy|
472
473
621|[ Task Scheduler] ( ./0621-task-scheduler.js ) |Medium|
473
474
628|[ Maximum Product of Three Numbers] ( ./0628-maximum-product-of-three-numbers.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments