ARRANGE -Arranging Amplifiers
Pattern: Candy and assign cookies
To make the tower a1^(a2^(…^an)) as large as possible: 1s go at the end (a 1 as the base makes everything 1, while x^1 = x), the rest in increasing order, with one exception: if the two largest are 2 and 3 at the end, put 3 before 2, since 3^2 = 9 beats 2^3 = 8.
The problem
Order the numbers to make a[0] ^ (a[1] ^ (… ^ a[n - 1])), evaluated from the right, as large as possible, and return
the order. [1, 3, 2] gives [3, 2, 1]; [2, 3, 4] gives [2, 3, 4] (2^81 is the largest). The tests avoid inputs
where two different orders give the same value, such as [2, 4]. The linked page could not be read when this guide was written, so the rules below are this guide's own statement of the task.
Think first
- Where can a 1 do the least damage?
- Is a bigger base or a bigger exponent worth more?
- Is there a small exception?
Hints
Brute force, and why it fails
Try every order and compare the towers: factorial, and the numbers get huge.
The key insight
Exponents grow towers much faster than bases, so increasing order wins, apart from the one pair where the base wins (3 over 2) and the 1s, which belong at the top.
Dry run
| order | 2 3 1 |
|---|
a = [1, 3, 2]. The 1 goes last. The rest, ascending: 2, 3.
The plan
big = sorted(values except 1s)
if the last two of big are 2, 3: make them 3, 2
return big + the 1s
Complexity
- Time
O(n log n)- Space
O(n)
Sorting dominates.
Edge cases
- All 1s.
- One number.
- Ties such as 2 and 4 (2^4 = 4^2), which the tests avoid.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public int[] arrange(int[] a) {
List<Integer> big = new ArrayList<>();
int ones = 0;
for (int x : a) {
if (x == 1) ones++; // a 1 at the bottom makes everything 1, so ones go last
else big.add(x);
}
Collections.sort(big); // smaller bases with bigger exponents above them grow fastest
if (big.size() >= 2 && big.get(big.size() - 2) == 2 && big.get(big.size() - 1) == 3) {
big.set(big.size() - 2, 3); // the one exception: 3 ^ 2 = 9 beats 2 ^ 3 = 8 at the top
big.set(big.size() - 1, 2);
}
int[] out = new int[a.length];
for (int i = 0; i < big.size(); i++) out[i] = big.get(i);
for (int i = big.size(); i < a.length; i++) out[i] = 1;
return out;
}
}C++
class Solution {
public:
vector<int> arrange(vector<int>& a) {
vector<int> big;
int ones = 0;
for (int x : a) {
if (x == 1) ones++; // a 1 at the bottom makes everything 1, so ones go last
else big.push_back(x);
}
sort(big.begin(), big.end()); // smaller bases with bigger exponents above them grow fastest
int m = static_cast<int>(big.size());
if (m >= 2 && big[m - 2] == 2 && big[m - 1] == 3) swap(big[m - 2], big[m - 1]); // 3 ^ 2 = 9 beats 2 ^ 3 = 8
for (int i = 0; i < ones; i++) big.push_back(1);
return big;
}
};Python
class Solution:
def arrange(self, a: list[int]) -> list[int]:
big = sorted(x for x in a if x != 1) # smaller bases with bigger exponents above them grow fastest
if len(big) >= 2 and big[-2:] == [2, 3]:
big[-2:] = [3, 2] # the one exception: 3 ^ 2 = 9 beats 2 ^ 3 = 8 at the top
return big + [1] * (len(a) - len(big)) # a 1 at the bottom makes everything 1, so ones go lastCommon mistakes
- Sorting descending.
- Putting 1s first, which makes the whole tower 1.