Last active
January 28, 2023 15:16
-
-
Save kenneropia/a1841c492ed061fdf5b672b9e1e2ad72 to your computer and use it in GitHub Desktop.
This file contains 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
function isMonotonic(arr: Number[]): Boolean { | |
let monoLeft = true; | |
let monoRight = true; | |
// loop thru the arr and check for the side by side items that makes the array non-monotonic | |
for (let i = 0; i < arr.length - 1; i++) { | |
// To check if | |
// array is not increasing | |
if (arr[i] > arr[i + 1]) { | |
monoLeft = false; | |
} | |
if (arr[i] < arr[i + 1]) { | |
monoRight = false; | |
} | |
} | |
// Pick one whether left or right | |
return monoLeft || monoRight; | |
} |
This file contains 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
function rotateArr(arr: Number[], step: number): Number[] { | |
// I could have mutated the original arr too | |
const outputArr = [...arr]; | |
//loop throught the array and push the rotated items into it | |
for (let i = 0; i < step; i++) { | |
outputArr.push(outputArr[i]); | |
} | |
//delete the stale array items | |
for (let j = 0; j < step; j++) { | |
outputArr.shift(); | |
} | |
return outputArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment