We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 7311a6f commit 35e7a37Copy full SHA for 35e7a37
Apothecary.cpp
@@ -17,30 +17,22 @@ typedef vector<vs> vvs;
17
18
class Apothecary{
19
public:
20
- int next(int x){
21
- int i = 1;
22
- while(i < x) i *= 3;
23
- return i;
24
- }
25
vi balance(int W){
26
- vi v[2];
27
- int n = W, m = 1;
28
- while(true){
29
- int k = next(n);
30
- if(k/2 < n){
31
- v[m].push_back(k);
32
- if(k == n) break;
33
- n = k-n;
34
- m = (m+1)%2;
+ vi r;
+ int n = W;
+ int c = 1;
+ while(n > 0){
+ if(n%3 == 1){
+ r.push_back(c);
+ n--;
35
}
36
- else{
37
- v[m].push_back(k/3);
38
- n = n-k/3;
+ else if(n%3 == 2){
+ r.push_back(-c);
+ n++;
39
+ c *= 3;
+ n /= 3;
40
41
- vi r;
42
- for(int p : v[0]) r.push_back(-p);
43
- for(int p : v[1]) r.push_back(p);
44
sort(r.begin(), r.end());
45
return r;
46
0 commit comments