Skip to content

Commit fdf9441

Browse files
committed
Add solution #738
1 parent e5a4b07 commit fdf9441

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@
559559
733|[Flood Fill](./0733-flood-fill.js)|Easy|
560560
735|[Asteroid Collision](./0735-asteroid-collision.js)|Medium|
561561
736|[Parse Lisp Expression](./0736-parse-lisp-expression.js)|Hard|
562+
738|[Monotone Increasing Digits](./0738-monotone-increasing-digits.js)|Medium|
562563
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
563564
743|[Network Delay Time](./0743-network-delay-time.js)|Medium|
564565
744|[Find Smallest Letter Greater Than Target](./0744-find-smallest-letter-greater-than-target.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
};

0 commit comments

Comments
 (0)