Meta Data#
Difficulty: easy First Attempt: 2024-03-03
- Submissions: Accepted (first try)
Approach#
Simulate the rule directly (Array Template 5): arr1 = [nums[0]], arr2 = [nums[1]], then each number goes by arr1[-1] > arr2[-1], and the two are joined at the end.
My Solution#
class Solution:
def resultArray(self, nums: List[int]) -> List[int]:
a, b = [nums[0]], [nums[1]]
for x in nums[2:]:
(a if a[-1] > b[-1] else b).append(x)
return a + b
Complexity#
Time O(n), space O(n).
Mistakes to Avoid#
- Compare the arrays’ last elements, not their sums.
Interview Explanation#
I simulate the rule, appending each number to the array whose last element is larger, and join them at the end. Time O(n).
