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. Kubernetes-2.组件

    内容主要摘自官网文档资料 官网地址 本文概述了交付正常运行的Kubernetes集群所需的各种组件. 本文编写基于kubernetes v1.17版本 目录 Kubernetes集群 Master组件 ...

  2. 后端程序员之路 53、A Tour of Go-3

    #method    - Methods        - Go does not have classes. However, you can define methods on types.    ...

  3. 翻译:《实用的Python编程》03_02_More_functions

    目录 | 上一节 (3.1 脚本) | 下一节 (3.3 错误检查) 3.2 深入函数 尽管函数在早先时候介绍了,但有关函数在更深层次上是如何工作的细节却很少提供.本节旨在填补这些空白,并讨论函数调用 ...

  4. Mac电脑管理员密码丢失解决办法

    1.重新启动电脑,并长按 Command (Win)+ S,并进入命令终端. 2.进入命令终端输入一下命令 /sbin/mount -uaw rm var/db/ .applesetupdone re ...

  5. 关于MarkDown语法

    Markdown语法 码云笔记链接:https://gitee.com/out_of_zi_wen/practical-experience/blob/master/Markdown%E8%AF%AD ...

  6. 翻译:《实用的Python编程》05_01_Dicts_revisited

    目录 | 上一节 (4.4 异常) | 下一节 (5.2 封装) 5.1 再谈字典 Python 对象系统主要基于字典实现.本节将对此进行讨论. 字典 字典是命名值(named values)的集合. ...

  7. CSS盒子的尺寸

    1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...

  8. Apache Pulsar 在能源互联网领域的落地实践

    关于 Apache Pulsar Apache Pulsar 是 Apache 软件基金会顶级项目,是下一代云原生分布式消息流平台,集消息.存储.轻量化函数式计算为一体,采用计算与存储分离架构设计,支 ...

  9. 对控制器类型“StudentController”的操作“Edit”的当前请求在下列操作方法之间不明确:

    "/"应用程序中的服务器错误. 对控制器类型"StudentController"的操作"Edit"的当前请求在下列操作方法之间不明确:类型 ...

  10. windows与远程linux服务器进行文件传输

    在学习pwn时找到了http://pwnable.kr这个网站,很多题目通过ssh连接, ssh otp@pwnable.kr -p2222 (pw:guest) 连接脚本: pwn_ssh=ssh( ...