|
6 | 6 | import java.util.Queue;
|
7 | 7 | import java.util.Set;
|
8 | 8 |
|
9 |
| -/**Given a positive integer n and you can do operations as follow: |
| 9 | +/** |
| 10 | + * 397. Integer Replacement |
| 11 | + * |
| 12 | + * Given a positive integer n and you can do operations as follow: |
10 | 13 |
|
11 | 14 | If n is even, replace n with n/2.
|
12 | 15 | If n is odd, you can replace n with either n + 1 or n - 1.
|
|
36 | 39 | 7 -> 6 -> 3 -> 2 -> 1*/
|
37 | 40 | public class _397 {
|
38 | 41 |
|
39 |
| - public static int integerReplacement(int n) { |
40 |
| - long min = Long.MAX_VALUE; |
41 |
| - Set<long[]> set = new HashSet(); |
42 |
| - Queue<long[]> q = new LinkedList(); |
43 |
| - long[] pair = new long[]{n, 0}; |
44 |
| - q.offer(pair); |
45 |
| - while (!q.isEmpty()) { |
46 |
| - int size = q.size(); |
47 |
| - for (int i = 0; i < size; i++) { |
48 |
| - long[] curr = q.poll(); |
49 |
| - if (curr[0] == 1) { |
50 |
| - set.add(curr); |
51 |
| - } else { |
52 |
| - |
53 |
| - if (curr[0] % 2 == 0) { |
54 |
| - curr[0] /= 2; |
55 |
| - curr[1]++; |
56 |
| - q.offer(curr); |
| 42 | + public static class Solution1 { |
| 43 | + public int integerReplacement(int n) { |
| 44 | + long min = Long.MAX_VALUE; |
| 45 | + Set<long[]> set = new HashSet(); |
| 46 | + Queue<long[]> q = new LinkedList(); |
| 47 | + long[] pair = new long[] {n, 0}; |
| 48 | + q.offer(pair); |
| 49 | + while (!q.isEmpty()) { |
| 50 | + int size = q.size(); |
| 51 | + for (int i = 0; i < size; i++) { |
| 52 | + long[] curr = q.poll(); |
| 53 | + if (curr[0] == 1) { |
| 54 | + set.add(curr); |
57 | 55 | } else {
|
58 |
| - long[] minus = new long[2]; |
59 |
| - minus[0] = curr[0] - 1; |
60 |
| - minus[1] = curr[1] + 1; |
61 |
| - q.offer(minus); |
62 | 56 |
|
63 |
| - long[] plus = new long[2]; |
64 |
| - plus[0] = curr[0] + 1; |
65 |
| - plus[1] = curr[1] + 1; |
66 |
| - q.offer(plus); |
| 57 | + if (curr[0] % 2 == 0) { |
| 58 | + curr[0] /= 2; |
| 59 | + curr[1]++; |
| 60 | + q.offer(curr); |
| 61 | + } else { |
| 62 | + long[] minus = new long[2]; |
| 63 | + minus[0] = curr[0] - 1; |
| 64 | + minus[1] = curr[1] + 1; |
| 65 | + q.offer(minus); |
| 66 | + |
| 67 | + long[] plus = new long[2]; |
| 68 | + plus[0] = curr[0] + 1; |
| 69 | + plus[1] = curr[1] + 1; |
| 70 | + q.offer(plus); |
| 71 | + } |
67 | 72 | }
|
68 | 73 | }
|
69 | 74 | }
|
70 |
| - } |
71 | 75 |
|
72 |
| - Iterator<long[]> it = set.iterator(); |
73 |
| - while (it.hasNext()) { |
74 |
| - min = Math.min(min, it.next()[1]); |
| 76 | + Iterator<long[]> it = set.iterator(); |
| 77 | + while (it.hasNext()) { |
| 78 | + min = Math.min(min, it.next()[1]); |
| 79 | + } |
| 80 | + return (int) min; |
75 | 81 | }
|
76 |
| - return (int) min; |
77 | 82 | }
|
78 | 83 |
|
79 |
| - public static void main(String... strings) { |
80 |
| - System.out.println(integerReplacement(2147483647)); |
81 |
| - System.out.println(integerReplacement(65535));//should be 17 |
82 |
| - System.out.println(integerReplacement(1234));//should be 14 |
83 |
| -// System.out.println(integerReplacement(1)); |
84 |
| -// System.out.println(integerReplacement(2)); |
85 |
| -// System.out.println(integerReplacement(3)); |
86 |
| - System.out.println(integerReplacement(5));//should be 3 |
87 |
| -// System.out.println(integerReplacement(7)); |
88 |
| - } |
89 | 84 | }
|
0 commit comments