File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 738. Monotone Increasing Digits
3+ * https://leetcode.com/problems/monotone-increasing-digits/
4+ * Difficulty: Medium
5+ *
6+ * An integer has monotone increasing digits if and only if each pair of adjacent digits
7+ * x and y satisfy x <= y.
8+ *
9+ * Given an integer n, return the largest number that is less than or equal to n with
10+ * monotone increasing digits.
11+ */
12+
13+ /**
14+ * @param {number } n
15+ * @return {number }
16+ */
17+ var monotoneIncreasingDigits = function ( n ) {
18+ const digits = String ( n ) . split ( '' ) . map ( Number ) ;
19+ let offset = digits . length ;
20+
21+ for ( let i = digits . length - 1 ; i > 0 ; i -- ) {
22+ if ( digits [ i - 1 ] > digits [ i ] ) {
23+ offset = i ;
24+ digits [ i - 1 ] -- ;
25+ }
26+ }
27+
28+ for ( let i = offset ; i < digits . length ; i ++ ) {
29+ digits [ i ] = 9 ;
30+ }
31+
32+ return + digits . join ( '' ) ;
33+ } ;
Original file line number Diff line number Diff line change 559559733|[ Flood Fill] ( ./0733-flood-fill.js ) |Easy|
560560735|[ Asteroid Collision] ( ./0735-asteroid-collision.js ) |Medium|
561561736|[ Parse Lisp Expression] ( ./0736-parse-lisp-expression.js ) |Hard|
562+ 738|[ Monotone Increasing Digits] ( ./0738-monotone-increasing-digits.js ) |Medium|
562563739|[ Daily Temperatures] ( ./0739-daily-temperatures.js ) |Medium|
563564743|[ Network Delay Time] ( ./0743-network-delay-time.js ) |Medium|
564565744|[ Find Smallest Letter Greater Than Target] ( ./0744-find-smallest-letter-greater-than-target.js ) |Easy|
You can’t perform that action at this time.
0 commit comments