In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.)

Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and B and C are both non-empty.

Example :
Input:
[1,2,3,4,5,6,7,8]
Output: true
Explanation: We can split the array into [1,4,5,8] and [2,3,6,7], and both of them have the average of 4.5.

Note:

  • The length of A will be in the range [1, 30].
  • A[i] will be in the range of [0, 10000].

Approach #1: DP. [Java]

class Solution {
public boolean splitArraySameAverage(int[] A) {
int sum = 0;
for (int num : A) {
sum += num;
} boolean[][] dp = new boolean[sum+1][A.length/2+1];
dp[0][0] = true; for (int num : A) {
for (int i = sum; i >= num; --i) {
for (int j = 1; j <= A.length/2; ++j) {
dp[i][j] = dp[i][j] || dp[i-num][j-1];
}
}
} for (int i = 1; i <= A.length/2; ++i)
if (sum * i % A.length == 0 && dp[sum * i / A.length][i])
return true; return false;
}
}

  

Approach #2: DFS. [Java]

class Solution {
public boolean check(int[] A, int leftSum, int leftNum, int startIndex) {
if (leftNum == 0) return leftSum == 0;
if ((A[startIndex]) > leftSum / leftNum) return false;
for (int i = startIndex; i < A.length - leftNum + 1; i ++) {
if (i > startIndex && A[i] == A[i - 1]) continue;
if (check(A, leftSum - A[i], leftNum - 1, i + 1)) return true;
}
return false;
} public boolean splitArraySameAverage(int[] A) {
if (A.length == 1) return false;
int sumA = 0;
for (int a: A) sumA += a;
Arrays.sort(A);
for (int lenOfB = 1; lenOfB <= A.length / 2; lenOfB ++) {
if ((sumA * lenOfB) % A.length == 0) {
if (check(A, (sumA * lenOfB) / A.length, lenOfB, 0)) return true;
}
}
return false; }
}

  

Analysis:

Can't understanding.

805. Split Array With Same Average的更多相关文章

  1. [LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

  2. [LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

  3. [Swift]LeetCode805. 数组的均值分割 | Split Array With Same Average

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

  4. [Swift]LeetCode842. 将数组拆分成斐波那契序列 | Split Array into Fibonacci Sequence

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  5. Split Array into Consecutive Subsequences

    659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...

  6. leetcode659. Split Array into Consecutive Subsequences

    leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...

  7. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. Power BI成功的背后

    Power BI成功的背后 魔力象限 又是一年Gartner数据分析与BI魔力象限报告的发布,Power BI毫无悬念的第一,并且拉开与其他产品的差距越来越大.在Power BI dataflows( ...

  2. 001 说说Python中的深拷贝和浅拷贝

    在Python编程中忽略深拷贝和浅拷贝可能会造成未知的风险. 比如我们打算保存一份原始对象的副本作为上一状态的记录,此后修改原始对象数据时,若是副本对象的数据也发生改变,那么这就是一个严重的错误. 注 ...

  3. 基于Hi3559AV100的视频采集(VDEC-VPSS-VO)整体框图设计

    下面给出基于Hi3559AV100的视频采集整体设计,具体设计将在后续给出: 图形采集端整体设计 Hi3559AV100软件程序按结构划分可分为4层,第一层是硬件驱动层,第二层是操作系统层,第三层是媒 ...

  4. POJ-3080(KMP+多个字符串的最长公共子串)

    Blue Jeans HDOJ-3080 本题使用的是KMP算法加暴力解决 首先枚举第一个字符串的所有子串,复杂度为O(60*60),随后再将每个子串和所有剩下的m-1个字符串比较,看是否存在这个子串 ...

  5. HDOJ-6621(线段树+二分法)

    K-th Closest Distance HDOJ-6621 本题可以使用线段树解决,结点存本结点对应的所有元素,并按照从小打到排序 最后使用二分法求解答案.因为题目中有绝对值,所以需要使用两次查找 ...

  6. go中waitGroup源码解读

    waitGroup源码刨铣 前言 WaitGroup实现 noCopy state1 Add Wait 总结 参考 waitGroup源码刨铣 前言 学习下waitGroup的实现 本文是在go ve ...

  7. Linux-mysql服务级别对DB的操作要领[导出-导入(执行SQL)]及修改数据库名称

    A:docker容器的mysql docker exec -it mysql bash -- 进入容器 备份脚本 mysqldump -uroot -p123456 --databases dbNam ...

  8. python工业互联网应用实战8—django-simpleui

    笔者也使用过一段时间adminx组件,后来由于adminx停更,又遇到更简单的django-simpleui后,现在基本上只使用simpleui了,使用simpleui的几个好处,笔者认为排在第一位的 ...

  9. 设置beeline连接hive的数据展示格式

    问题描述:beeline -u 方式导出数据,结果文件中含有"|"(竖杠). 执行的sql为:beeline -u jdbc:hive2://hadoop1:10000/defau ...

  10. All I know about A/B Test (1) : 均值型指标与比值(率)型指标的计算区别

    因为最近在找实习,所以打算把自己之前学过的关数据分析的知识总结(复习)一下.在总结A/B test时,我发现中文互联网中关于A/B test的总结已经很多了,但是对于均值型指标和比值(率)型指标在设计 ...