LeetCode 题解工作台

分组得分最高的所有下标

给你一个下标从 0 开始的二进制数组 nums ,数组长度为 n 。 nums 可以按下标 i ( 0 )拆分成两个数组(可能为空): nums left 和 nums right 。 nums left 包含 nums 中从下标 0 到 i - 1 的所有元素 (包括 0 和 i - 1 ) ,而…

category

1

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·driven

bolt

答案摘要

我们从 $i = 0$ 开始,用两个变量 和 分别记录 左侧和右侧的 的个数,初始时 $\textit{l0} = 0$,而 $\textit{r1} = \sum \textit{nums}$。 我们遍历数组 ,对于每个 ,更新 和 ,计算当前分组得分 $t = \textit{l0} + \textit{r1}$,如果 等于当前最大分组得分 ,则将 加入答案数组,如果 大于 ,…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·driven 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的二进制数组 nums ,数组长度为 nnums 可以按下标 i0 <= i <= n )拆分成两个数组(可能为空):numsleftnumsright

  • numsleft 包含 nums 中从下标 0i - 1 的所有元素(包括 0i - 1,而 numsright 包含 nums 中从下标 in - 1 的所有元素(包括 in - 1 )。
  • 如果 i == 0numsleft ,而 numsright 将包含 nums 中的所有元素。
  • 如果 i == nnumsleft 将包含 nums 中的所有元素,而 numsright

下标 i 分组得分numsleft0 的个数和 numsright1 的个数之

返回 分组得分 最高所有不同下标 。你可以按 任意顺序 返回答案。

 

示例 1:

输入:nums = [0,0,1,0]
输出:[2,4]
解释:按下标分组
- 0 :numsleft 为 [] 。numsright 为 [0,0,1,0] 。得分为 0 + 1 = 1 。
- 1 :numsleft 为 [0] 。numsright 为 [0,1,0] 。得分为 1 + 1 = 2 。
- 2 :numsleft 为 [0,0] 。numsright 为 [1,0] 。得分为 2 + 1 = 3 。
- 3 :numsleft 为 [0,0,1] 。numsright 为 [0] 。得分为 2 + 0 = 2 。
- 4 :numsleft 为 [0,0,1,0] 。numsright 为 [] 。得分为 3 + 0 = 3 。
下标 2 和 4 都可以得到最高的分组得分 3 。
注意,答案 [4,2] 也被视为正确答案。

示例 2:

输入:nums = [0,0,0]
输出:[3]
解释:按下标分组
- 0 :numsleft 为 [] 。numsright 为 [0,0,0] 。得分为 0 + 0 = 0 。
- 1 :numsleft 为 [0] 。numsright 为 [0,0] 。得分为 1 + 0 = 1 。
- 2 :numsleft 为 [0,0] 。numsright 为 [0] 。得分为 2 + 0 = 2 。
- 3 :numsleft 为 [0,0,0] 。numsright 为 [] 。得分为 3 + 0 = 3 。
只有下标 3 可以得到最高的分组得分 3 。

示例 3:

输入:nums = [1,1]
输出:[0]
解释:按下标分组
- 0 :numsleft 为 [] 。numsright 为 [1,1] 。得分为 0 + 2 = 2 。
- 1 :numsleft 为 [1] 。numsright 为 [1] 。得分为 0 + 1 = 1 。
- 2 :numsleft 为 [1,1] 。numsright 为 [] 。得分为 0 + 0 = 0 。
只有下标 0 可以得到最高的分组得分 2 。

 

提示:

  • n == nums.length
  • 1 <= n <= 105
  • nums[i]01
lightbulb

解题思路

方法一:前缀和

我们从 i=0i = 0 开始,用两个变量 l0\textit{l0}r1\textit{r1} 分别记录 ii 左侧和右侧的 11 的个数,初始时 l0=0\textit{l0} = 0,而 r1=nums\textit{r1} = \sum \textit{nums}

我们遍历数组 nums\textit{nums},对于每个 ii,更新 l0\textit{l0}r1\textit{r1},计算当前分组得分 t=l0+r1t = \textit{l0} + \textit{r1},如果 tt 等于当前最大分组得分 mx\textit{mx},则将 ii 加入答案数组,如果 tt 大于 mx\textit{mx},则更新 mx\textit{mx}tt,并将答案数组清空,然后将 ii 加入答案数组。

遍历结束后,返回答案数组。

时间复杂度 O(n)O(n),其中 nn 为数组 nums\textit{nums} 的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def maxScoreIndices(self, nums: List[int]) -> List[int]:
        l0, r1 = 0, sum(nums)
        mx = r1
        ans = [0]
        for i, x in enumerate(nums, 1):
            l0 += x ^ 1
            r1 -= x
            t = l0 + r1
            if mx == t:
                ans.append(i)
            elif mx < t:
                mx = t
                ans = [i]
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidate should maintain running counts of 0's and 1's.

  • question_mark

    Look for clear explanation of optimizing through precomputation.

  • question_mark

    Expect understanding of array-driven solutions and space-time trade-offs.

warning

常见陷阱

外企场景
  • error

    Not maintaining running counts of 0's and 1's efficiently.

  • error

    Incorrect handling of boundary conditions for division (e.g., empty left or right subarrays).

  • error

    Failing to optimize by precomputing the right side counts.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Consider cases with very small arrays, including edge cases with 1 or 2 elements.

  • arrow_right_alt

    Test with large arrays near the maximum constraint to ensure performance.

  • arrow_right_alt

    Explore different ways of maintaining the counts (e.g., using two passes instead of one).

help

常见问题

外企场景

分组得分最高的所有下标题解:数组·driven | LeetCode #2155 中等