Skip to content

Commit b94da20

Browse files
add 1299
1 parent 8c44f41 commit b94da20

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1302|[Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1302.java) | |Medium||
12+
|1299|[Replace Elements with Greatest Element on Right Side](https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1299.java) | |Easy||
1213
|1297|[Maximum Number of Occurrences of a Substring](https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1297.java) | |Medium||
1314
|1296|[Divide Array in Sets of K Consecutive Numbers](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1296.java) | |Medium||
1415
|1295|[Find Numbers with Even Number of Digits](https://leetcode.com/problems/find-numbers-with-even-number-of-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1295.java) | |Easy||
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* 1299. Replace Elements with Greatest Element on Right Side
7+
*
8+
* Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
9+
* After doing so, return the array.
10+
*
11+
* Example 1:
12+
* Input: arr = [17,18,5,4,6,1]
13+
* Output: [18,6,6,6,1,-1]
14+
* */
15+
public class _1299 {
16+
public static class Solution1 {
17+
public int[] replaceElements(int[] arr) {
18+
PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
19+
for (int num : arr) {
20+
heap.offer(num);
21+
}
22+
int[] result = new int[arr.length];
23+
for (int i = 0; i < arr.length - 1; i++) {
24+
heap.remove(arr[i]);
25+
result[i] = heap.peek();
26+
}
27+
result[arr.length - 1] = -1;
28+
return result;
29+
}
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1299;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1299Test {
10+
private static _1299.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1299.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{17, 18, 5, 4, 6, 1};
21+
assertArrayEquals(new int[]{18, 6, 6, 6, 1, -1}, solution1.replaceElements(arr));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)