-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.java
47 lines (41 loc) · 1.33 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//Problem: https://www.hackerrank.com/challenges/marcs-cakewalk
//Java 8
/*
Initial Thoughts:
Since our calorie range is only 1000 which
is relatively small, we can do a counting sort
in linear time and beat out a n log n sorting
algo like qsort, also since our 2^j is always
increasing, we can just track it as we go and
not do pow(2,j) every time
Time Complexity: O(n + k) //We have to see every cupcake's calories
Space Complexity: O(k) //We allocate an array of size k which is max calories
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] calories = new int[1000];//999 index is calorie 1 and 0 index is calorie 1000
int maxCalories = 0;
//Sort the calories from largest to smallest
for(int i=0; i < n; i++){
calories[calories.length - in.nextInt()]++;
}
long miles = 0;
long pow = 1;
for(int i = 0; i < calories.length; i++)
{
for(int j = 0; j < calories[i]; j++)
{
miles += (calories.length - i) * pow;
pow *= 2;
}
}
System.out.println(miles);
}
}