filmov
tv
LeetCode 189: Rotate Array - Interview Prep Ep 52
Показать описание
⭐ Support my channel and connect with me:
Solutions explained:
Solution1: we could use a nested for loop, rotate every single element to the right one at a time, total k times, this could potential results in TLE (Time Limit Exceeded exception on LeetCode OJ), time complexity: O(n*k), space complexity: O(1);
Solution 2: we could trade off space for time: use an extra array to help, assign all elements into its final correct position in the extra array, then copy the extra array into the input array. Time: O(n), Space: O(n);
Solution 3: we could reverse the input array three times:
1. first reverse this entire array,
2. secondly, reverse the array from 0 to k - 1
3. lastly, reverse the array from k to length - 1
Time: O(n), Space: O(1)
// TOOLS THAT I USE:
// MY FAVORITE BOOKS:
My ENTIRE Programming Equipment and Computer Science Bookshelf:
And make sure you subscribe to my channel!
#softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures
Your comments/thoughts/questions/advice will be greatly appreciated!
Комментарии