Last active
June 7, 2023 17:45
-
-
Save gulbaki/4fe9875783e10e10c18169d480d8a81e to your computer and use it in GitHub Desktop.
arrayManipulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // brute force | |
| function arrayManipulation(arr, n) { | |
| const temp = new Array(n).fill(0); | |
| for (let i = 0; i < arr.length; i++) { | |
| for (let index = arr[i][0]; index <= arr[i][1]; index++) { | |
| temp[index - 1] += arr[i][2] | |
| } | |
| } | |
| console.log( Math.max(...temp)) | |
| } | |
| const arr = [[1,2,100],[2,5,100],[3,4,100]] | |
| arrayManipulation(arr, 53) | |
| //.end/ brute force | |
| // prefix sum method | |
| function arrayManipulation(arr, n) { | |
| const newmap = new Map(); | |
| for (let i = 0; i < arr.length; i++) { | |
| const start = arr[i][0]; | |
| const end = arr[i][1]; | |
| const value = arr[i][2] | |
| if(newmap.get(start) == undefined){ | |
| newmap.set(start, 0); | |
| } | |
| if(newmap.get(end) == undefined){ | |
| newmap.set(end, 0); | |
| } | |
| newmap.set(start, newmap.get(start) + value); | |
| newmap.set(end + 1, newmap.get(end + 1) + (-value)); | |
| } | |
| let max = 0; | |
| let itt = 0; | |
| for (const [key, value] of newmap) { | |
| itt += value | |
| if (itt > max ) { | |
| max = itt | |
| } | |
| } | |
| return max | |
| } | |
| const arr = [[1,2,100],[2,5,100],[3,4,100]] | |
| arrayManipulation(arr, 53) | |
| //.end/ prefix sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment