Skip to main content
edited tags
Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 101
added 10 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I'm looking to learn more about data structures, time complexity, and efficient algorithms. I have solved the [Left Rotation Problem][1] [1]: https://www.hackerrank.com/challenges/array-left-rotation/problem on HackerRank using JavaScript. I am looking to see ways to optimize the time complexity. I feel like I have a lot to learn, as don't grok Big O.

My solution:

function main() {
    const nd = readLine().split(' ')
    const numbers = parseInt(nd[0], 10)
    const rotations = parseInt(nd[1], 10)
    const arr = readLine().split(' ').map(aTemp => parseInt(aTemp, 10))
    const front = arr.slice(0, rotations)
    const back = arr.slice(rotations, numbers)
    const new_arr = back.concat(front)
    console.log(new_arr.join(' '))
}
function main() {
    const nd = readLine().split(' ')
    const numbers = parseInt(nd[0], 10)
    const rotations = parseInt(nd[1], 10)
    const arr = readLine().split(' ').map(aTemp => parseInt(aTemp, 10))
    const front = arr.slice(0, rotations)
    const back = arr.slice(rotations, numbers)
    const new_arr = back.concat(front)
    console.log(new_arr.join(' '))
}

I feel like this likely has bad time complexity, as under the hood, I believe the slice function uses a loop and has a time complexity of O(n), in\$O(n)\$. In addition, I'm unsure of how JavaScript implements the merge from the  concat method under the hood.

I'm looking to learn more about data structures, time complexity, and efficient algorithms. I have solved the [Left Rotation Problem][1] [1]: https://www.hackerrank.com/challenges/array-left-rotation/problem on HackerRank using JavaScript. I am looking to see ways to optimize the time complexity. I feel like I have a lot to learn, as don't grok Big O.

My solution:

function main() {
    const nd = readLine().split(' ')
    const numbers = parseInt(nd[0], 10)
    const rotations = parseInt(nd[1], 10)
    const arr = readLine().split(' ').map(aTemp => parseInt(aTemp, 10))
    const front = arr.slice(0, rotations)
    const back = arr.slice(rotations, numbers)
    const new_arr = back.concat(front)
    console.log(new_arr.join(' '))
}

I feel like this likely has bad time complexity, as under the hood, I believe the slice function uses a loop and has a time complexity of O(n), in addition, I'm unsure how JavaScript implements the merge from the  concat method under the hood.

I'm looking to learn more about data structures, time complexity, and efficient algorithms. I have solved the [Left Rotation Problem][1] [1]: https://www.hackerrank.com/challenges/array-left-rotation/problem on HackerRank using JavaScript. I am looking to see ways to optimize the time complexity. I feel like I have a lot to learn, as don't grok Big O.

function main() {
    const nd = readLine().split(' ')
    const numbers = parseInt(nd[0], 10)
    const rotations = parseInt(nd[1], 10)
    const arr = readLine().split(' ').map(aTemp => parseInt(aTemp, 10))
    const front = arr.slice(0, rotations)
    const back = arr.slice(rotations, numbers)
    const new_arr = back.concat(front)
    console.log(new_arr.join(' '))
}

I feel like this likely has bad time complexity, as under the hood, I believe the slice function uses a loop and has a time complexity of \$O(n)\$. In addition, I'm unsure of how JavaScript implements the merge from the concat method under the hood.

Source Link

HackerRank - Left Rotation in JavaScript

I'm looking to learn more about data structures, time complexity, and efficient algorithms. I have solved the [Left Rotation Problem][1] [1]: https://www.hackerrank.com/challenges/array-left-rotation/problem on HackerRank using JavaScript. I am looking to see ways to optimize the time complexity. I feel like I have a lot to learn, as don't grok Big O.

My solution:

function main() {
    const nd = readLine().split(' ')
    const numbers = parseInt(nd[0], 10)
    const rotations = parseInt(nd[1], 10)
    const arr = readLine().split(' ').map(aTemp => parseInt(aTemp, 10))
    const front = arr.slice(0, rotations)
    const back = arr.slice(rotations, numbers)
    const new_arr = back.concat(front)
    console.log(new_arr.join(' '))
}

I feel like this likely has bad time complexity, as under the hood, I believe the slice function uses a loop and has a time complexity of O(n), in addition, I'm unsure how JavaScript implements the merge from the concat method under the hood.