Skip to content

Commit 7772719

Browse files
committedApr 8, 2025
Add solution #1299
1 parent 7593186 commit 7772719

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,214 LeetCode solutions in JavaScript
1+
# 1,215 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -982,6 +982,7 @@
982982
1296|[Divide Array in Sets of K Consecutive Numbers](./solutions/1296-divide-array-in-sets-of-k-consecutive-numbers.js)|Medium|
983983
1297|[Maximum Number of Occurrences of a Substring](./solutions/1297-maximum-number-of-occurrences-of-a-substring.js)|Medium|
984984
1298|[Maximum Candies You Can Get from Boxes](./solutions/1298-maximum-candies-you-can-get-from-boxes.js)|Hard|
985+
1299|[Replace Elements with Greatest Element on Right Side](./solutions/1299-replace-elements-with-greatest-element-on-right-side.js)|Easy|
985986
1304|[Find N Unique Integers Sum up to Zero](./solutions/1304-find-n-unique-integers-sum-up-to-zero.js)|Easy|
986987
1309|[Decrypt String from Alphabet to Integer Mapping](./solutions/1309-decrypt-string-from-alphabet-to-integer-mapping.js)|Easy|
987988
1313|[Decompress Run-Length Encoded List](./solutions/1313-decompress-run-length-encoded-list.js)|Easy|
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 1299. Replace Elements with Greatest Element on Right Side
3+
* https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/
4+
* Difficulty: Easy
5+
*
6+
* Given an array arr, replace every element in that array with the greatest element among the
7+
* elements to its right, and replace the last element with -1.
8+
*
9+
* After doing so, return the array.
10+
*/
11+
12+
/**
13+
* @param {number[]} arr
14+
* @return {number[]}
15+
*/
16+
var replaceElements = function(arr) {
17+
let maxRight = -1;
18+
for (let i = arr.length - 1; i >= 0; i--) {
19+
const current = arr[i];
20+
arr[i] = maxRight;
21+
maxRight = Math.max(maxRight, current);
22+
}
23+
return arr;
24+
};

0 commit comments

Comments
 (0)
Please sign in to comment.