Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update 1299-Replace-Elements-with-Greatest-Element-on-Right-Side.js#1470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Ahmad-A0 merged 1 commit intoneetcode-gh:mainfromaadil42:patch-15
Nov 19, 2022
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update 1299-Replace-Elements-with-Greatest-Element-on-Right-Side.js
Added BruteForce approach for reference's sake.
  • Loading branch information
@aadil42
aadil42 authoredNov 16, 2022
commit108fe42a7ac3cf7dabab08c20335c19779667f47
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,3 +33,24 @@ var replaceElements = (arr, max = -1) => {

return arr;
};
// This is brute force with O(n^2). Just for reference's sake.
// submission link: https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/submissions/844439163/
var replaceElementsBrute = function(arr) {

for(let i = 0; i < arr.length; i++) {
arr[i] = biggestElement(i, arr);
}

arr[arr.length - 1] = -1;
return arr;
};

function biggestElement(index, arr) {

let biggest = 0;
for(let i = index + 1; i < arr.length; i++) {
biggest = Math.max(biggest, arr[i]);
}

return biggest;
}

[8]ページ先頭

©2009-2025 Movatter.jp