Skip to content

Commit

Permalink
Replace elements with greatest number to the right
Browse files Browse the repository at this point in the history
33rd exercise - leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/
  • Loading branch information
CharlesCreativeContent authored Jun 28, 2020
1 parent a680267 commit b808bb2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions greatestToTheRight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
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]
*/

//Answer//

/**
* @param {number[]} arr
* @return {number[]}
*/
var replaceElements = function(arr) {
return arr.map((x,i)=>i===(arr.length-1)?-1:Math.max(...arr.slice(i+1)))
};

0 comments on commit b808bb2

Please sign in to comment.