Skip to content

Commit 2f04ea5

Browse files
[N-0] add 738
1 parent 16d8302 commit 2f04ea5

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2525
|739|[Daily Temperatures](https://leetcode.com/problems/daily-temperatures/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_739.java) | O(n^2) | O(1) | Medium|
26+
|738|[Monotone Increasing Digits](https://leetcode.com/problems/monotone-increasing-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_738.java) | O(n) | O(1) | Medium|
2627
|737|[Sentence Similarity II](https://leetcode.com/problems/sentence-similarity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_737.java) | O(nlogk + k) n is the length of max(words1, words2), k is the length of pairs| O(k) | Medium| Union Find
2728
|735|[Asteroid Collision](https://leetcode.com/problems/asteroid-collision/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_735.java) | O(n) | O(n) | Medium | Stack
2829
|734|[Sentence Similarity](https://leetcode.com/problems/sentence-similarity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_734.java) | O(n*k) | O(1) | Easy | HashTable
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 738. Monotone Increasing Digits
5+
*
6+
* Given a non-negative integer N, find the largest number that is less than or equal to N with monotone increasing digits.
7+
* (Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.)
8+
9+
Example 1:
10+
Input: N = 10
11+
Output: 9
12+
13+
Example 2:
14+
Input: N = 1234
15+
Output: 1234
16+
17+
Example 3:
18+
Input: N = 332
19+
Output: 299
20+
21+
Note: N is an integer in the range [0, 10^9].
22+
*/
23+
public class _738 {
24+
public static class Solution1 {
25+
/**
26+
* credit: https://discuss.leetcode.com/topic/112808/simple-python-solution-w-explanation/2
27+
*/
28+
public int monotoneIncreasingDigits(int N) {
29+
String s = Integer.toString(N);
30+
int index = -1;
31+
for (int i = s.length() - 2; i >= 0; i--) {
32+
if (s.charAt(i) > s.charAt(i + 1) || (index != -1 && s.charAt(index) == s.charAt(i))) {
33+
index = i;
34+
}
35+
}
36+
return index == -1 ? N : N - Integer.parseInt(s.substring(index + 1, s.length())) - 1;
37+
}
38+
}
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._738;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _738Test {
10+
private static _738.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _738.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(9, solution1.monotoneIncreasingDigits(10));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)