首页 > 其他分享 >[LeetCode] 2270. Number of Ways to Split Array

[LeetCode] 2270. Number of Ways to Split Array

时间:2022-12-06 03:22:06浏览次数:86  
标签:index 2270 nums Ways sum Number valid part its

You are given a 0-indexed integer array nums of length n.

nums contains a valid split at index i if the following are true:

  • The sum of the first i + 1 elements is greater than or equal to the sum of the last n - i - 1 elements.
  • There is at least one element to the right of i. That is, 0 <= i < n - 1.

Return the number of valid splits in nums.

Example 1:

Input: nums = [10,4,-8,7]
Output: 2
Explanation: 
There are three ways of splitting nums into two non-empty parts:
- Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 >= 3, i = 0 is a valid split.
- Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 >= -1, i = 1 is a valid split.
- Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 < 7, i = 2 is not a valid split.
Thus, the number of valid splits in nums is 2.

Example 2:

Input: nums = [2,3,1,0]
Output: 2
Explanation: 
There are two valid splits in nums:
- Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 >= 1, i = 1 is a valid split. 
- Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 >= 0, i = 2 is a valid split.

Constraints:

  • 2 <= nums.length <= 105
  • -105 <= nums[i] <= 105

分割数组的方案数。

给你一个下标从 0 开始长度为 n 的整数数组 nums 。
如果以下描述为真,那么 nums 在下标 i 处有一个 合法的分割 :

前 i + 1 个元素的和 大于等于 剩下的 n - i - 1 个元素的和。
下标 i 的右边 至少有一个 元素,也就是说下标 i 满足 0 <= i < n - 1 。
请你返回 nums 中的 合法分割 方案数。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/number-of-ways-to-split-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题的思路跟2256类似。2256求的是最小的满足条件的 index,这道题其实更简单,就是求符合题意的 index 一共有几个,而且只是单纯的求子数组的和,用前缀和的思路解决即可。

时间O(n)

空间O(1)

Java实现

 1 class Solution {
 2     public int waysToSplitArray(int[] nums) {
 3         long sum = 0;
 4         for (int num : nums) {
 5             sum += num;
 6         }
 7         
 8         int count = 0;
 9         int len = nums.length;
10         long left = 0;
11         long right = 0;
12         for (int i = 0; i < len - 1; i++) {
13             left += nums[i];
14             right = sum - left;
15             if (left >= right) {
16                 count++;
17             }
18         }
19         return count;
20     }
21 }

 

前缀和prefix sum题目总结

LeetCode 题目总结

标签:index,2270,nums,Ways,sum,Number,valid,part,its
From: https://www.cnblogs.com/cnoodle/p/16954115.html

相关文章

  • LeetCode: 313. Super Ugly Number
    LeetCode:313.SuperUglyNumberWriteaprogramtofindthenthsuperuglynumber.Superuglynumbersarepositivenumberswhoseallprimefactorsareinthegi......
  • LeetCode: 315. Count of Smaller Numbers After Self
    LeetCode:315.CountofSmallerNumbersAfterSelf题目描述Youaregivenanintegerarraynumsandyouhavetoreturnanewcountsarray.Thecountsarrayhast......
  • LeetCode: 241. Different Ways to Add Parentheses
    LeetCode:241.DifferentWaystoAddParentheses题目描述Givenastringofnumbersandoperators,returnallpossibleresultsfromcomputingallthedifferentp......
  • LeetCode: 260. Single Number III
    LeetCode:260.SingleNumberIII题目描述Givenanarrayofnumbersnums,inwhichexactlytwoelementsappearonlyonceandalltheotherelementsappearexactl......
  • LeetCode: 264. Ugly Number II
    LeetCode:264.UglyNumberII题目描述Writeaprogramtofindthen-thuglynumber.Uglynumbersarepositivenumberswhoseprimefactorsonlyinclude2,3,5.Ex......
  • LeetCode: 287. Find the Duplicate Number
    LeetCode:287.FindtheDuplicateNumber题目描述Givenanarraynumscontainingn+1integerswhereeachintegerisbetween1andn(inclusive),provethatat......
  • LeetCode: 306. Additive Number
    LeetCode:306.AdditiveNumber题目描述Additivenumberisastringwhosedigitscanformadditivesequence.Avalidadditivesequenceshouldcontainatleastthr......
  • 收集表单数据(包括输入number型如何保证输出还是number型)
    收集表单数据: 若<inputtype="text">,则v-model收集的是value值,用户输入的就是value值 若<inputtype="radio">,则v-model收集的是value值,且要给标签配置value值 若<inpu......
  • XAF Number(编号)
    前言编号在各类系统中都会存在,同时它还会根据业务场景的不同,会有不同的生成规则。XAF提供了一个编号生成助手(DistributedIdGeneratorHelper),它能在多并发的情况下,生成一个......
  • Typescript类型题材 - NumberRange
    题目中文有时我们需要限制数字的范围...示例:typeresult=NumberRange<2,9>//|2|3|4|5|6|7|8|9EnglishSometimeswewanttolimittheran......