File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } digits
3+ * @return {string }
4+ */
5+ const largestMultipleOfThree = function ( digits ) {
6+ const sum = digits . reduce ( ( a , c ) => a + c )
7+ if ( sum === 0 ) return '0'
8+ const remainder = sum % 3
9+ digits . sort ( ( a , b ) => b - a )
10+ if ( remainder === 0 ) return digits . join ( '' )
11+ const doubleRemainder = remainder === 1 ? 2 : 1
12+ const idxs = [ ]
13+ for ( let i = digits . length - 1 ; i >= 0 ; i -- ) {
14+ const numRemainder = digits [ i ] % 3
15+ if ( numRemainder === remainder ) {
16+ digits [ i ] = ''
17+ return digits . join ( '' )
18+ } else if ( numRemainder === doubleRemainder ) {
19+ idxs . push ( i )
20+ }
21+ }
22+ const [ idx1 , idx2 ] = idxs
23+ if ( idx2 === undefined ) return ''
24+
25+ digits [ idx1 ] = ''
26+ digits [ idx2 ] = ''
27+ return digits . join ( '' )
28+ }
You can’t perform that action at this time.
0 commit comments