Solving LeetCode 396 with O(n) Time Complexity

🚀 Day 248 of #500DaysOfCode Solved LeetCode 396 – Rotate Function 🔄 💡 Approach: Instead of recomputing every rotation (which would be O(n²)), we use a mathematical relation Let: sum = total sum of array F(0) = initial rotation value Then: F(k) = F(k-1) + sum - n * nums[n-k] ⚡ Key Insight: Each rotation shifts weights → reuse previous result This reduces redundant computation drastically ⏱️ Complexity: Time: O(n) Space: O(1) ✨ Takeaway: Whenever rotations are involved, look for a pattern / recurrence instead of brute force. Consistency continues 🔥 #LeetCode #DSA #Arrays #Math #Optimization #Consistency

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories