|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.HashSet; |
| 3 | +import java.util.List; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +/* |
| 7 | + * @lc app=leetcode id=970 lang=java |
| 8 | + * |
| 9 | + * [970] Powerful Integers |
| 10 | + * |
| 11 | + * https://leetcode.com/problems/powerful-integers/description/ |
| 12 | + * |
| 13 | + * algorithms |
| 14 | + * Easy (39.90%) |
| 15 | + * Total Accepted: 26.4K |
| 16 | + * Total Submissions: 66.1K |
| 17 | + * Testcase Example: '2\n3\n10' |
| 18 | + * |
| 19 | + * Given two positive integers x and y, an integer is powerful if it is equal |
| 20 | + * to x^i + y^j for some integers i >= 0 and j >= 0. |
| 21 | + * |
| 22 | + * Return a list of all powerful integers that have value less than or equal to |
| 23 | + * bound. |
| 24 | + * |
| 25 | + * You may return the answer in any order. In your answer, each value should |
| 26 | + * occur at most once. |
| 27 | + * |
| 28 | + * |
| 29 | + * |
| 30 | + * |
| 31 | + * Example 1: |
| 32 | + * |
| 33 | + * |
| 34 | + * Input: x = 2, y = 3, bound = 10 |
| 35 | + * Output: [2,3,4,5,7,9,10] |
| 36 | + * Explanation: |
| 37 | + * 2 = 2^0 + 3^0 |
| 38 | + * 3 = 2^1 + 3^0 |
| 39 | + * 4 = 2^0 + 3^1 |
| 40 | + * 5 = 2^1 + 3^1 |
| 41 | + * 7 = 2^2 + 3^1 |
| 42 | + * 9 = 2^3 + 3^0 |
| 43 | + * 10 = 2^0 + 3^2 |
| 44 | + * |
| 45 | + * |
| 46 | + * |
| 47 | + * Example 2: |
| 48 | + * |
| 49 | + * |
| 50 | + * Input: x = 3, y = 5, bound = 15 |
| 51 | + * Output: [2,4,6,8,10,14] |
| 52 | + * |
| 53 | + * |
| 54 | + * |
| 55 | + * |
| 56 | + * |
| 57 | + * |
| 58 | + * Note: |
| 59 | + * |
| 60 | + * |
| 61 | + * 1 <= x <= 100 |
| 62 | + * 1 <= y <= 100 |
| 63 | + * 0 <= bound <= 10^6 |
| 64 | + * |
| 65 | + */ |
| 66 | +class Solution { |
| 67 | + public List<Integer> powerfulIntegers(int x, int y, int bound) { |
| 68 | + // Notes: |
| 69 | + // No duplicate values. Use HashSet to collect |
| 70 | + // exponent starts with 0. n^0 = 1 |
| 71 | + // List if integers should be <= bound |
| 72 | + |
| 73 | + // https://leetcode.com/problems/powerful-integers/discuss/296387 |
| 74 | + // |
| 75 | + // * Lang: java |
| 76 | + // * Author: 416486188 |
| 77 | + // * Votes: 1 |
| 78 | + // |
| 79 | + // **Analysis** |
| 80 | + // |
| 81 | + // This problem is not easy. I think it is one of the pattern where we consider the problem in opposite way: **we are not checking them, we are finding them**. |
| 82 | + // |
| 83 | + // For this problem, the key is that **we are not iterate `[1, bound]` and check if they are valid. Instead we are constructing all valid number in `[1, bound]` and add to result**. |
| 84 | + // |
| 85 | + // It is very similar to the problem **[#204](https://leetcode.com/problems/count-primes) Count Primes**. We are not checking if each number is prime, we are ruling out all composite number. That\'s the reason why I called it `opposite way`. |
| 86 | + // |
| 87 | + // --- |
| 88 | + // **Strategy** |
| 89 | + // |
| 90 | + // Basically, we are doing brute force, get all combination of `x^c1 + y^c2` who `<= bound`, and their sum is a candidate. Then we add it to the result list `res`. |
| 91 | + // |
| 92 | + // Pseudo code: |
| 93 | + // |
| 94 | + // ``` |
| 95 | + // for i = x ^ c1{ |
| 96 | + // for j = y ^ c2{ |
| 97 | + // if( i + j <= bound){ |
| 98 | + // // add to result |
| 99 | + // } |
| 100 | + // } |
| 101 | + // } |
| 102 | + // ``` |
| 103 | + // |
| 104 | + // **Take care** |
| 105 | + // |
| 106 | + // We need to take care of the case when **x == 1 || y == 1** to avoid infinite loop in contructing the number. |
| 107 | + |
| 108 | + Set<Integer> set = new HashSet<Integer>(); |
| 109 | + |
| 110 | + int i = 1; |
| 111 | + while (i < bound) { |
| 112 | + int j = 1; |
| 113 | + while (j < bound) { |
| 114 | + |
| 115 | + int sum = i + j; |
| 116 | + if (sum <= bound) |
| 117 | + set.add(sum); |
| 118 | + |
| 119 | + j *= y; // much like y^(n+1) to check the next exponent of y |
| 120 | + if (y == 1) break; |
| 121 | + } |
| 122 | + |
| 123 | + i *= x; // much like x^(n+1) to check the next exponent of x |
| 124 | + if (x == 1) break; |
| 125 | + } |
| 126 | + |
| 127 | + return new ArrayList<Integer>(set); |
| 128 | + } |
| 129 | +} |
0 commit comments