首页 > 其他分享 >【1114】

【1114】

时间:2022-11-14 11:34:55浏览次数:44  
标签:arr false nums int sum 1114 数组

805. 数组的均值分割     困难     相关企业

给定你一个整数数组 nums

我们要将 nums 数组中的每个元素移动到 A 数组 或者 B 数组中,使得 A 数组和 B 数组不为空,并且 average(A) == average(B) 。

如果可以完成则返回true , 否则返回 false  。

注意:对于数组 arr ,  average(arr) 是 arr 的所有元素除以 arr 长度的和。

 

示例 1:

输入: nums = [1,2,3,4,5,6,7,8]
输出: true
解释: 我们可以将数组分割为 [1,4,5,8] 和 [2,3,6,7], 他们的平均值都是4.5。

示例 2:

输入: nums = [3,1]
输出: false

 

提示:

  • 1 <= nums.length <= 30
  • 0 <= nums[i] <= 104

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 1 class Solution {
 2     public boolean splitArraySameAverage(int[] nums) {
 3         if (nums.length == 1) {
 4             return false;
 5         }
 6         int n = nums.length, m = n / 2;
 7         int sum = 0;
 8         for (int num : nums) {
 9             sum += num;
10         }
11         for (int i = 0; i < n; i++) {
12             nums[i] = nums[i] * n - sum;
13         }
14 
15         Set<Integer> left = new HashSet<Integer>();
16         for (int i = 1; i < (1 << m); i++) {
17             int tot = 0;
18             for (int j = 0; j < m; j++) {
19                 if ((i & (1 << j)) != 0) {
20                     tot += nums[j];
21                 }
22             }
23             if (tot == 0) {
24                 return true;
25             }
26             left.add(tot);
27         }
28         int rsum = 0;
29         for (int i = m; i < n; i++) {
30             rsum += nums[i];
31         }
32         for (int i = 1; i < (1 << (n - m)); i++) {
33             int tot = 0;
34             for (int j = m; j < n; j++) {
35                 if ((i & (1 << (j - m))) != 0) {
36                     tot += nums[j];
37                 }
38             }
39             if (tot == 0 || (rsum != tot && left.contains(-tot))) {
40                 return true;
41             }
42         }
43         return false;
44     }
45 }
46 
47 作者:力扣官方题解
48 链接:https://leetcode.cn/problems/split-array-with-same-average/solutions/1966163/shu-zu-de-jun-zhi-fen-ge-by-leetcode-sol-f630/
49 来源:力扣(LeetCode)
50 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

标签:arr,false,nums,int,sum,1114,数组
From: https://www.cnblogs.com/wzxxhlyl/p/16888462.html

相关文章

  • 今日笔记之20221114
    1.vue中刷新页面,echarts图表不显示问题? <divid="myChart"></div><script>exportdefault{data(){return{myChart:{}}},mounted:{this.init......
  • HDU 1114 Piggy-Bank
    题目链接:​​传送门​​Piggy-BankProblemDescriptionBeforeACMcandoanything,abudgetmustbepreparedandthenecessaryfinancialsupportobtained.Them......
  • LeetCode 1114. Print in Order
    原题链接在这里:https://leetcode.com/problems/print-in-order/题目:Supposewehaveaclass:publicclassFoo{publicvoidfirst(){print("first");}public......
  • 洛谷 P1114 “非常男女”计划 (前缀和)
    https://www.luogu.com.cn/problem/P1114题目大意:给定一排数字,让我们求出最大的区间内1和0的个数相等时的区间长度。输入9010001100输出6输入10011......
  • CF1114F Please, another Queries on Array?
    CF1114FPlease,anotherQueriesonArray?题目大意你有一个数组\(a_1,a_2,\dots,a_n\)。现在你需要完成\(q\)次操作,有以下两种操作形式:MULTIPLYlrx,对于所有\(i(......
  • P1114 “非常男女”计划
    https://www.luogu.com.cn/problem/P1114前缀和,相对差黄色题思路:引入相对差的概念。即a[i]表示第i个位置男生人数-女生人数的差值。那么差值相等的两个位置之间的人数......