|
1 | 1 | package hackerrank; |
2 | 2 |
|
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Collections; |
| 5 | + |
3 | 6 | public class MarcsCakewalk { |
4 | 7 | static long marcsCakewalk(int[] calorie) { |
5 | | - //will write my code |
| 8 | + //마크는 칼로리 c만큼인 컵케익 j를 행복하게 먹은 뒤 해당 칼로리를 불태우기위해 2의 j승 * c 마일만큼 걸으려고한다. |
| 9 | + //그가 걸어야 하는 마일수 중 가장 작은 마일수를 리턴하는 문제. |
| 10 | + |
| 11 | + //int[]는 Collections이 아니므로 Integer[]로 변경 |
| 12 | + Integer[] caloriesInteger = new Integer[calorie.length]; |
| 13 | + for(int i=0; i< calorie.length; i++){ |
| 14 | + caloriesInteger[i] = calorie[i]; |
| 15 | + } |
| 16 | + Arrays.sort(caloriesInteger, Collections.reverseOrder()); |
| 17 | + System.out.println(Arrays.toString(caloriesInteger)); |
| 18 | + |
| 19 | + //가장 작은 마일수 구하기 |
| 20 | + long miles = 0; |
| 21 | + for(int i =0; i< caloriesInteger.length; i++){ |
| 22 | + miles += Math.pow(2, i) * caloriesInteger[i]; |
| 23 | + } |
6 | 24 |
|
7 | | - return 1; |
| 25 | + return miles; |
8 | 26 | } |
9 | 27 |
|
10 | 28 | public static void main(String[] args) { |
11 | 29 | int[] a = {1, 3, 2}; |
12 | 30 | int[] b = {7, 4, 9, 6}; |
13 | | - System.out.println(marcsCakewalk(a)+", ans: 11"); |
14 | | - System.out.println(marcsCakewalk(b)+", ans: 79"); |
| 31 | + int[] c ={819,701,578,403,50,400,983,665,510,523,696,532,51,449,333,234,958,460,277,347,950,53,123,227,646,190,938,61,409,110,61,178,659,989,625,237,944,550,954,439}; |
| 32 | + int[] d={801,234,536,747,172,590,833,847,509,429,666,411,609,894,348,254,814,767,647,965,711,801,852,781,972,390,218,290,508,174,55,714,442,284,745,872,46,131,833,315}; |
| 33 | + //System.out.println(marcsCakewalk(a)+", ans: 11"); |
| 34 | + //System.out.println(marcsCakewalk(b)+", ans: 79"); |
| 35 | + System.out.println(marcsCakewalk(c)+", ans: 59715404338867"); |
| 36 | + System.out.println(marcsCakewalk(d)+", ans: 84350424920174"); |
15 | 37 | } |
16 | 38 | } |
0 commit comments