805. Split Array With Same Average

标签: leetcode-java
29人阅读 评论(0) 收藏 举报
分类:

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].
import java.awt.Point;

class Solution {
    public boolean splitArraySameAverage(int[] A) {
        int N = A.length;
        int S = 0;
        for (int x: A) S += x;
        if (N == 1) return false;

        int g = gcd(S, N);
        Point mu = new Point(-(S/g), N/g);
        // A[i] -> fracAdd(A[i], mu)
        List<Point> A2 = new ArrayList();
        for (int x: A)
            A2.add(fracAdd(new Point(x, 1), mu));

        Set<Point> left = new HashSet();
        left.add(A2.get(0));
        for (int i = 1; i < N/2; ++i) {
            Set<Point> left2 = new HashSet();
            Point z = A2.get(i);
            left2.add(z);
            for (Point p: left) {
                left2.add(p);
                left2.add(fracAdd(p, z));
            }
            left = left2;
        }

        if (left.contains(new Point(0, 1))) return true;

        Set<Point> right = new HashSet();
        right.add(A2.get(N-1));
        for (int i = N/2; i < N-1; ++i) {
            Set<Point> right2 = new HashSet();
            Point z = A2.get(i);
            right2.add(z);
            for (Point p: right) {
                right2.add(p);
                right2.add(fracAdd(p, z));
            }
            right = right2;
        }

        if (right.contains(new Point(0, 1))) return true;

        Point sleft = new Point(0, 1);
        for (int i = 0; i < N/2; ++i)
            sleft = fracAdd(sleft, A2.get(i));

        Point sright = new Point(0, 1);
        for (int i = N/2; i < N; ++i)
            sright = fracAdd(sright, A2.get(i));

        for (Point ha: left) {
            Point ha2 = new Point(-ha.x, ha.y);
            if (right.contains(ha2) && (!ha.equals(sleft) || !ha2.equals(sright)))
                return true;
        }
        return false;
    }

    public Point fracAdd(Point A, Point B) {
        int numer = A.x * B.y + B.x * A.y;
        int denom = A.y * B.y;
        int g = gcd(numer, denom);
        numer /= g;
        denom /= g;

        if (denom < 0) {
            numer *= -1;
            denom *= -1;
        }

        return new Point(numer, denom);
    }

    public int gcd(int a, int b) {
       if (b==0) return a;
       return gcd(b, a%b);
    }
}
查看评论

对JAVA语言的十个常见误解

对JAVA语言的十个常见误解(辽宁 都 健  编译) JAVA 语 言 自 从 应 用 于 Internet, 迅 速 成 为 全 球 热 点。 它 的 平 台 无 关 性 仿 佛 成 为 解 决 互...
  • Virtual
  • Virtual
  • 2001-01-05 09:27:00
  • 875

[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...
  • magicbean2
  • magicbean2
  • 2018-04-08 10:54:39
  • 76

[Leetcode] 806. Number of Lines To Write String 解题报告

题目: We are to write the letters of a given string S, from left to right into lines. Each line ...
  • magicbean2
  • magicbean2
  • 2018-04-08 11:05:20
  • 18

leetcode805. Split Array With Same Average

leetcode805. Split Array With Same Average 原题地址:https://leetcode.com/problems/split-array-with-sa...
  • birdreamer
  • birdreamer
  • 2018-03-25 21:22:04
  • 35

Leetcode 410. Split Array Largest Sum 划分数组 解题报告

1 解题思想首先声明下,本文这些内容,基本是dicuss的来的题目意思是,给一个数组,需要划分成m份,让你找出一种划分方式,使得划分成m份后,最大的那一个区间的数值最小这道题discuss上说的是2分...
  • MebiuW
  • MebiuW
  • 2016-10-02 17:04:16
  • 4208

unity各种报错之:Mesh.colors is out of bounds. The supplied array needs ...

可以了解一个模型是有点线面体构成,显示就要配合shader
  • qq_28002559
  • qq_28002559
  • 2017-03-31 11:14:11
  • 753

659. Split Array into Consecutive Subsequences

You are given an integer array sorted in ascending order (may contain duplicates), you need to split...
  • zjucor
  • zjucor
  • 2017-08-14 09:00:54
  • 1340

js Array 数组相关的方法

1.contact()用于连接2个或者多个数组。该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。 var arr1 = [1,2,3], arr2 = [4,5,6], ...
  • wkyseo
  • wkyseo
  • 2016-04-28 15:18:48
  • 458

python入门opencv踩过的坑

在用python学习opencv的基本操作时候,dst = cv2.addWeighted(src1, alpha, src2, beta, 0.0)用这个函数总是报错The operation is...
  • legalhighhigh
  • legalhighhigh
  • 2017-12-08 11:28:04
  • 231

leetcode-410. Split Array Largest Sum

题目: Given an array which consists of non-negative integers and an integer m, you can split the arr...
  • caoyan_12727
  • caoyan_12727
  • 2016-10-22 11:58:41
  • 595
    个人资料
    持之以恒
    等级:
    访问量: 3万+
    积分: 6410
    排名: 4748
    最新评论