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 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
Awill 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- Split Array into Consecutive Subsequences
659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...
- leetcode659. Split Array into Consecutive Subsequences
leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- docker启动ubuntu的桌面环境
一.概述 由于最近一段时间在家办公,国内服务器在阿里云,国外站点在aws.家里的移动宽带比较差,无法访问aws. 所以尝试在阿里云启动docker,找到一个lxde桌面环境的ubuntu镜像. 二.启 ...
- iOS 14.5 有啥新功能?Apple Watch 也能解锁 iPhone 了
转: iOS 14.5 有啥新功能?Apple Watch 也能解锁 iPhone 了 苹果今天发布了即将发布的 iOS 14.5 和 iPadOS 14.5 更新的第一个 Beta 版本,我们在其中 ...
- 京东 Vue3 组件库闪亮登场
京东零售开源项目 NutUI 是一套京东风格的轻量级移动端 Vue 组件库,是开发和服务于移动 Web 界面的企业级产品.经过长时间的开发与打磨,NutUI 3.0 终于要和大家见面了!3.0 版本在 ...
- 蓝桥杯-分考场(dfs)
分考场 PREV-53 这题的解决方法使用dfs,因为数据很小,才100. 每次当前的人人是否可以和前面的组队,设置两个数组group和fri /*DFS求解:思路每次判断输入的人是否可以和前面的组队 ...
- pytorch(01)环境配置及安装
pytorch pytorch定位:深度学习框架 人工智能:多领域交叉科学技术 机器学习:计算机智能决策算法 深度学习:高效的机器学习算法 pytorch实现模型训练需要5个模块 数据 将数据从硬盘读 ...
- 测试平台系列(4) 使用Flask蓝图(blueprint)
使用Flask蓝图(blueprint) 回顾 先来看一下上一篇的作业吧,使用「logbook」的时候,遇到了时区不对的情况.那么我们怎么去解决这个问题呢? 实际上logbook默认采用的是世界标准时 ...
- WPF 应用 - 使用 Properties.Settings 保存客户端密码
1. 先在项目的 Settings.settings 新建需要的字段和类型 有需要还可设置初始默认值 2. 启动客户端时,获取 Properties.Settings 的属性值 public void ...
- 【odoo14】第八章、服务侧开发-进阶
本章代码位于作为GITHUB库 https://github.com/PacktPublishing/Odoo-14-Development-Cookbook-Fourth-Edition 在第五章( ...
- 分形、分形几何、数据可视化、Python绘图
本系列采用turtle.matplotlib.numpy这三个Python工具,以分形与计算机图像处理的经典算法为实例,通过程序和图像,来帮助读者一步步掌握Python绘图和数据可视化的方法和技巧,并 ...
- 记录Java注解在JavaWeb中的一个应用实例
概述 在学习注解的时候,学了个懵懵懂懂.学了JavaWeb之后,在做Demo项目的过程中,借助注解和反射实现了对页面按钮的权限控制,对于注解才算咂摸出了点味儿来. 需求 以"角色列表&quo ...