Google Foobar power_hungry [英] Google Foobar power_hungry

查看:97
本文介绍了Google Foobar power_hungry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要解决我的一个Google foobar问题,这是我到目前为止所获得的。

Hello I need help with one of my Google foobar questions this is what I've got so far.

package com.google.challenges;
import java.math.BigInteger;

public class Answer{


    public static String answer (int[] xs){
        BigInteger result = new BigInteger("1");
        int xsLen = xs.length, pos = 0;
        int[] negatives = new int[xsLen];
        if (xsLen == 1){
            return Integer.toString(xs[0]);
        }
        // Split the input up into pos/negative. Pos get put onto the final value, as they don't need anything else.
        // they are all useful. negative to onto seperate array and get sorted later
        for (int n = 0;n < xsLen;n++){
            int val = xs[n];
            if (val == 0){
                continue;
            }
            if (val > 0){
                result = result.multiply(new BigInteger(Integer.toString(val)));
            } else {
                negatives[pos] = val;
                pos++;
            }
        }
        // even number of negatives means a full product will always be positive.
        // odd number means that we discard the smallest number to maximise the result.
        if ((pos % 2) == 0){
            // even number, so add to result
            for (int i = 0;i < pos;i++){
                result = result.multiply(new BigInteger(Integer.toString(negatives[i])));
            }
        } else {
            // sort then discard the minimum
            int min = -1000; int mPos = -1;
            for (int i = 0;i < pos;i++){
                if(negatives[i] > min){
                    min = negatives[i];
                    mPos = i;
                }
            }
            for (int j = 0;j < pos;j++){
                if(j == mPos){
                    continue;
                }
                result = result.multiply(new BigInteger(Integer.toString(negatives[j])));
            }
        }

        // done, return the string;
        return result.toString();
    }
}

这是问题,

您需要确定在给定阵列中可以脱机修复的几组面板,同时仍保持每个阵列的最大功率输出,要做到这一点,您首先需要弄清楚每个数组的最大实际输出是多少。编写一个函数answer(xs),该函数采用一个整数列表,这些整数表示一个数组中每个面板的功率输出水平,并返回这些数字的一些非空子集的最大乘积。因此,例如,如果一个数组包含功率输出级别为[2,-3、1、0,-5]的面板,则通过乘以子集xs [0] = 2,xs [1]可以找到最大乘积。 ] = -3,xs [4] = -5,乘积2 *(-3)*(-5)=30。因此,答案([2,-3,1,0,-5])为30。

You need to figure out which sets of panels in any given array you can take offline to repair while still maintaining the maximum amount of power output per array, and to do THAT, you'll first need to figure out what the maximum output of each array actually is. Write a function answer(xs) that takes a list of integers representing the power output levels of each panel in an array, and returns the maximum product of some non-empty subset of those numbers. So for example, if an array contained panels with power output levels of [2, -3, 1, 0, -5], then the maximum product would be found by taking the subset: xs[0] = 2, xs[1] = -3, xs[4] = -5, giving the product 2*(-3)*(-5) = 30. So answer([2,-3,1,0,-5]) will be "30".

每个太阳能电池板阵列至少包含1个且不超过50个面板,并且每个面板的功率输出电平的绝对值均不大于1000(某些面板的故障非常严重,以至于浪费了能量,但是您知道这些面板的波形稳定器有一个技巧,可以将两个负输出面板组合在一起,以产生其功率值倍数的正输出)。最终的乘积可能很大,因此以数字的字符串表示形式给出答案。

Each array of solar panels contains at least 1 and no more than 50 panels, and each panel will have a power output level whose absolute value is no greater than 1000 (some panels are malfunctioning so badly that they're draining energy, but you know a trick with the panels' wave stabilizer that lets you combine two negative-output panels to produce the positive output of the multiple of their power values). The final products may be very large, so give the answer as a string representation of the number.

要提供Python解决方案,请编辑solution.py
要提供Java解决方案,请编辑solution.java

To provide a Python solution, edit solution.py To provide a Java solution, edit solution.java

Inputs:
    (int list) xs = [2, 0, 2, 2, 0]
Output:
    (string) "8"

Inputs:
    (int list) xs = [-2, -3, 4, -5]
Output:
    (string) "60"

我已经呆了2天了,我很想知道答案,所以我可以了解我做错了什么并改进!感谢您的阅读,希望您能回答。 :)

I've been at it for 2 days and would really like the answer so I can learn what I have done wrong and improve! Thanks for reading and hopefully you answer. :)

推荐答案

您需要处理某些情况:

您的数组具有1到50个介于-1000到1000之间的Integer元素。如果您的输入是这样的:[0,0,-43,0]。在这种情况下,

your array has between 1 and 50 Integer elements ranging from -1000 to 1000. what if your input was like this: [0, 0, -43, 0]. In that case,

    if (xsLen == 1){
        return Integer.toString(xs[0]);
    }

没有道理。 (您不能给出否定的答案)。在这种情况下,您的答案应该为0。

Doesn't make sense. (You can't have a negative answer). your answer should be 0 in this case.

解决此问题的关键是要认识到您可以将两个负整数相乘得到一个正整数。 BigInteger很有用,因为您的最终答案可能会变得非常大。

The key to solving this problem is to recognize that You can Multiply two negative integers to get a positive one. BigInteger is useful because your final answer may get really really large.

我实现该解决方案的方式是,我将每个非零Integer相乘并将值存储为BigInteger结果变量。但是,我保留了另一个变量来跟踪最大负整数。最后,将结果除以最大负整数变量,即可得到答案。

The Way I implemented the solution is that I multiplied every non-zero Integer and stored the value as a BigInteger result variable. However, I kept another variable to keep track of "Greatest negative integer". At the end divide the result by your "Greatest Negative Integer" variable and there you have your answer.

我保存了一个正整数和一个负整数...希望这会有所帮助。

I kept a count of Positive Integers and a count of Negative Integers... Hope this helps.

这篇关于Google Foobar power_hungry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆