File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments