Skip to content

Commit c9e3b26

Browse files
solves #2526: Find the Array Concatenation Value in java
1 parent 2b59f60 commit c9e3b26

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,8 @@
799799
| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum) | [![Java](assets/java.png)](src/AlternatingDigitSum.java) | |
800800
| 2549 | [Count Distinct Numbers on Board](https://leetcode.com/problems/count-distinct-numbers-on-board) | [![Java](assets/java.png)](src/CountDistinctNumbersOnBoard.java) | |
801801
| 2553 | [Separate the Digits in an Array](https://leetcode.com/problems/separate-the-digits-in-an-array) | [![Java](assets/java.png)](src/SeparateTheDigitsInAnArray.java) | |
802-
| 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile) | | |
803-
| 2562 | [Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value) | | |
802+
| 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile) | [![Java](assets/java.png)](src/TakeGiftsFromTheRichestPile.java) | |
803+
| 2562 | [Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value) | [![Java](assets/java.png)](src/FindTheArrayConcatenationValue.java) | |
804804
| 2566 | [Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit) | | |
805805
| 2570 | [Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values) | | |
806806
| 2574 | [Left and Right Sum Differences](https://leetcode.com/problems/left-and-right-sum-differences) | | |
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/find-the-array-concatenation-value
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class FindTheArrayConcatenationValue {
6+
public long findTheArrayConcVal(int[] nums) {
7+
long result = 0;
8+
9+
for (int i = 0 ; i < nums.length / 2 ; i++) {
10+
result += Integer.parseInt(nums[i] + "" + nums[nums.length - 1 - i]);
11+
}
12+
13+
if (nums.length % 2 == 1) {
14+
result += nums[nums.length / 2];
15+
}
16+
17+
return result;
18+
}
19+
}

src/TakeGiftsFromTheRichestPile.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// https://leetcode.com/problems/take-gifts-from-the-richest-pile
2+
// T: O(k * log(N))
3+
// S: O(N)
4+
5+
import java.util.Comparator;
6+
import java.util.PriorityQueue;
7+
import java.util.Queue;
8+
9+
public class TakeGiftsFromTheRichestPile {
10+
public long pickGifts(int[] gifts, int k) {
11+
final Queue<Integer> maxHeap = maHeapFrom(gifts);
12+
13+
for (int i = 0 ; i < k ; i++) {
14+
final int maxPile = maxHeap.poll();
15+
final int giftsToLeaveBehind = (int) Math.max(Math.sqrt(maxPile), 0);
16+
maxHeap.add(giftsToLeaveBehind);
17+
}
18+
19+
return sum(maxHeap);
20+
}
21+
22+
private long sum(Queue<Integer> heap) {
23+
long sum = 0;
24+
while (!heap.isEmpty()) {
25+
sum += heap.poll();
26+
}
27+
return sum;
28+
}
29+
30+
private Queue<Integer> maHeapFrom(int[] array) {
31+
final Queue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
32+
for (int element : array) {
33+
maxHeap.add(element);
34+
}
35+
return maxHeap;
36+
}
37+
}

0 commit comments

Comments
 (0)