Skip to content

Commit 108fe42

Browse files
authored
Update 1299-Replace-Elements-with-Greatest-Element-on-Right-Side.js
Added BruteForce approach for reference's sake.
1 parent 82e9776 commit 108fe42

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

javascript/1299-Replace-Elements-with-Greatest-Element-on-Right-Side.js

+21
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,24 @@ var replaceElements = (arr, max = -1) => {
3333

3434
return arr;
3535
};
36+
// This is brute force with O(n^2). Just for reference's sake.
37+
// submission link: https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/submissions/844439163/
38+
var replaceElementsBrute = function(arr) {
39+
40+
for(let i = 0; i < arr.length; i++) {
41+
arr[i] = biggestElement(i, arr);
42+
}
43+
44+
arr[arr.length - 1] = -1;
45+
return arr;
46+
};
47+
48+
function biggestElement(index, arr) {
49+
50+
let biggest = 0;
51+
for(let i = index + 1; i < arr.length; i++) {
52+
biggest = Math.max(biggest, arr[i]);
53+
}
54+
55+
return biggest;
56+
}

0 commit comments

Comments
 (0)