1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_1299.java 962 Bytes
一键复制 编辑 原始数据 按行查看 历史
Fisher Coder 提交于 5年前 . add 1299
package com.fishercoder.solutions;
import java.util.PriorityQueue;
/**
* 1299. Replace Elements with Greatest Element on Right Side
*
* 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.
* After doing so, return the array.
*
* Example 1:
* Input: arr = [17,18,5,4,6,1]
* Output: [18,6,6,6,1,-1]
* */
public class _1299 {
public static class Solution1 {
public int[] replaceElements(int[] arr) {
PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
for (int num : arr) {
heap.offer(num);
}
int[] result = new int[arr.length];
for (int i = 0; i < arr.length - 1; i++) {
heap.remove(arr[i]);
result[i] = heap.peek();
}
result[arr.length - 1] = -1;
return result;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助