Skip to content

Commit a39ff21

Browse files
2551_Put_Marbel_in_Bags.java
1 parent d00bf46 commit a39ff21

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

JAVA/2551_Put_Marbel_in_Bags.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Problem Number: 2551
2+
3+
// Put Marbels in Bags.
4+
5+
class Solution {
6+
public long putMarbles(int[] weights, int k) {
7+
8+
int[] arr = new int[weights.length - 1];
9+
long mn = 0;
10+
long mx = 0;
11+
12+
for (int i = 0; i < arr.length; ++i)
13+
arr[i] = weights[i] + weights[i + 1];
14+
15+
Arrays.sort(arr);
16+
17+
for (int i = 0; i < k - 1; ++i) {
18+
mn += arr[i];
19+
mx += arr[arr.length - 1 - i];
20+
}
21+
22+
return mx - mn;
23+
}
24+
}

0 commit comments

Comments
 (0)