File tree 2 files changed +26
-1
lines changed
2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,214 LeetCode solutions in JavaScript
1
+ # 1,215 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
982
982
1296|[ Divide Array in Sets of K Consecutive Numbers] ( ./solutions/1296-divide-array-in-sets-of-k-consecutive-numbers.js ) |Medium|
983
983
1297|[ Maximum Number of Occurrences of a Substring] ( ./solutions/1297-maximum-number-of-occurrences-of-a-substring.js ) |Medium|
984
984
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|
985
986
1304|[ Find N Unique Integers Sum up to Zero] ( ./solutions/1304-find-n-unique-integers-sum-up-to-zero.js ) |Easy|
986
987
1309|[ Decrypt String from Alphabet to Integer Mapping] ( ./solutions/1309-decrypt-string-from-alphabet-to-integer-mapping.js ) |Easy|
987
988
1313|[ Decompress Run-Length Encoded List] ( ./solutions/1313-decompress-run-length-encoded-list.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments