File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 248
248
256|[ Paint House] ( ./solutions/0256-paint-house.js ) |Medium|
249
249
257|[ Binary Tree Paths] ( ./solutions/0257-binary-tree-paths.js ) |Easy|
250
250
258|[ Add Digits] ( ./solutions/0258-add-digits.js ) |Easy|
251
+ 259|[ 3Sum Smaller] ( ./solutions/0259-3sum-smaller.js ) |Medium|
251
252
260|[ Single Number III] ( ./solutions/0260-single-number-iii.js ) |Medium|
252
253
263|[ Ugly Number] ( ./solutions/0263-ugly-number.js ) |Easy|
253
254
264|[ Ugly Number II] ( ./solutions/0264-ugly-number-ii.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 259. 3Sum Smaller
3
+ * https://leetcode.com/problems/3sum-smaller/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an array of n integers nums and an integer target, find the number of index triplets
7
+ * i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.
8
+ */
9
+
10
+ /**
11
+ * @param {number[] } nums
12
+ * @param {number } target
13
+ * @return {number }
14
+ */
15
+ var threeSumSmaller = function ( nums , target ) {
16
+ nums . sort ( ( a , b ) => a - b ) ;
17
+ const n = nums . length ;
18
+ let result = 0 ;
19
+
20
+ for ( let i = 0 ; i < n - 2 ; i ++ ) {
21
+ let left = i + 1 ;
22
+ let right = n - 1 ;
23
+
24
+ while ( left < right ) {
25
+ const sum = nums [ i ] + nums [ left ] + nums [ right ] ;
26
+ if ( sum < target ) {
27
+ result += right - left ;
28
+ left ++ ;
29
+ } else {
30
+ right -- ;
31
+ }
32
+ }
33
+ }
34
+
35
+ return result ;
36
+ } ;
You can’t perform that action at this time.
0 commit comments