forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarcsCakewalk.java
More file actions
39 lines (33 loc) ยท 1.7 KB
/
MarcsCakewalk.java
File metadata and controls
39 lines (33 loc) ยท 1.7 KB
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
package hackerrank;
import java.util.Arrays;
import java.util.Collections;
public class MarcsCakewalk {
static long marcsCakewalk(int[] calorie) {
//๋งํฌ๋ ์นผ๋ก๋ฆฌ c๋งํผ์ธ ์ปต์ผ์ต j๋ฅผ ํ๋ณตํ๊ฒ ๋จน์ ๋ค ํด๋น ์นผ๋ก๋ฆฌ๋ฅผ ๋ถํ์ฐ๊ธฐ์ํด 2์ j์น * c ๋ง์ผ๋งํผ ๊ฑธ์ผ๋ ค๊ณ ํ๋ค.
//๊ทธ๊ฐ ๊ฑธ์ด์ผ ํ๋ ๋ง์ผ์ ์ค ๊ฐ์ฅ ์์ ๋ง์ผ์๋ฅผ ๋ฆฌํดํ๋ ๋ฌธ์ .
// ์ญ์์ ๋ ฌ์ ํ๋ฉด ๊ฐ์ฅ ์์ ๋ง์ผ ์๋ฅผ ์ป์ ์ ์๋ค.
// int[]๋ Collections ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก Integer[]๋ก ๋ณ๊ฒฝ
Integer[] caloriesInteger = new Integer[calorie.length];
for(int i=0; i< calorie.length; i++){
caloriesInteger[i] = calorie[i];
}
Arrays.sort(caloriesInteger, Collections.reverseOrder()); //์ญ์์ ๋ ฌ
System.out.println(Arrays.toString(caloriesInteger));
//๊ฐ์ฅ ์์ ๋ง์ผ์ ๊ตฌํ๊ธฐ
long miles = 0;
for(int i =0; i< caloriesInteger.length; i++){
miles += Math.pow(2, i) * caloriesInteger[i];
}
return miles;
}
public static void main(String[] args) {
int[] a = {1, 3, 2};
int[] b = {7, 4, 9, 6};
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};
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};
//System.out.println(marcsCakewalk(a)+", ans: 11");
//System.out.println(marcsCakewalk(b)+", ans: 79");
System.out.println(marcsCakewalk(c)+", ans: 59715404338867");
System.out.println(marcsCakewalk(d)+", ans: 84350424920174");
}
}