Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
T N
<Python> O(1) space, better than the official solution

T N

Jul 9, 2024

The solution to the problem is not very efficient nor helpful. Why is there O(n) extra space? It's like the monotonic stack pattern.

Typically result space is not calculated in the Big O, so that's why res is not included as space complexity. You can note this to your interviewer if anyone isn't onboard.

def leftRightDifference(self, nums: List[int]) -> List[int]: leftsum, rightsum, res = 0, sum(nums), [] for n in nums: rightsum -= n res.append(abs(leftsum - rightsum)) leftsum += n return res

1

0

Comments
Comments

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity