Skip to content

Commit d7b8a72

Browse files
authored
Create 1323-maximum-69-number.java
1 parent fc1977d commit d7b8a72

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Easy/1323-maximum-69-number.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 1323. Maximum 69 Number
3+
* Difficulty: Easy
4+
* URL: https://leetcode.com/problems/maximum-69-number/description/
5+
*
6+
* ---------------------
7+
* Approach: Greedy (First '6' to '9')
8+
* ---------------------
9+
* - Convert the number to a character array.
10+
* - Traverse from left to right and change the **first '6'** to '9'.
11+
* - Break immediately since changing the leftmost '6' maximizes the number.
12+
* - Convert the updated character array back to an integer and return it.
13+
*
14+
* Time Complexity: O(n) // where n = number of digits
15+
* Space Complexity: O(n) // for storing char array (can be seen as O(1) since digits ≤ 4)
16+
*/
17+
18+
class Solution {
19+
public int maximum69Number(int num) {
20+
char[] digits = String.valueOf(num).toCharArray();
21+
for (int i = 0; i < digits.length; i++) {
22+
if (digits[i] == '6') {
23+
digits[i] = '9';
24+
break;
25+
}
26+
}
27+
return Integer.parseInt(String.valueOf(digits));
28+
}
29+
}

0 commit comments

Comments
 (0)