Google Foobar 动力饥渴问题

3

你好,我需要在谷歌foobar项目的一个问题上寻求帮助,以下是我目前的进展。

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。因此,answer([2,-3,1,0,-5])将为“30”。

每组太阳能电池板至少包含1个且不超过50个面板,并且每个面板的功率输出级别的绝对值不大于1000(有些面板的故障很严重,它们正在消耗能量,但是您知道面板的波形稳定器技巧,可让你将两个负输出面板组合起来,以产生其功率值的多倍的正输出)。最终产品可能非常大,因此将答案作为数字的字符串表示给出。

语言

要提供Python解决方案,请编辑solution.py 要提供Java解决方案,请编辑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"

我已经在进行这项工作两天了,希望能得到答案,以便我知道自己做错了什么并加以改进!感谢您的阅读,希望您能回答:)
3个回答

3

您需要处理以下情况:

您的数组包含1到50个整数元素,范围从-1000到1000。如果您的输入如下:[0, 0, -43, 0]。在这种情况下,

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

没有意义(你不能有一个负的答案),在这种情况下,你的答案应该是0。

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

我实现这个解决方案的方法是将每个非零整数相乘,并将值存储为BigInteger结果变量。然而,我保留了另一个变量来跟踪“最大负整数”。最后,将结果除以您的“最大负整数”变量,就可以得到您的答案。

我记录了正整数和负整数的数量……希望这对你有所帮助。


1

你的代码很好,但是需要进行小的修改,如果数组中所有元素都为零或负数,则应返回0。以下是代码。

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,ng=0;
    int[] negatives = new int[xsLen];
    if (xsLen == 1){
        return Integer.toString(xs[0]);
    }

    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)));
            ng++;
        } else {
            negatives[pos] = val;
            pos++;
        }
    }
    if(ng==0)
    {
        int l=0;
        return Integer.toString(l);
    }
    if ((pos % 2) == 0){

        for (int i = 0;i < pos;i++){
            result = result.multiply(new BigInteger(Integer.toString(negatives[i])));
        }
    } else {

        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])));
        }
    }


    return result.toString();
}
}

0

尝试编写代码以满足这些测试用例或类似的测试用例。

{-2} -> -2

{2,3,2,2} -> 24

{-2,-2,-3} -> 6

{-2,-2,-2,-3} -> 24

{-2,-2,0,0,-2,-3} -> 24

{2,3,0,0,2,2} -> 24

{-2,-2,0,0,2,3} -> 24

{0,0} -> 0

在foobar挑战中,返回结果的大小可能达到10^50。

因此,建议使用字符串实现乘积。

import java.util.Arrays;

public class Main {

public static String result(int[] xs) {

// System.out.println("你好,世界"); int i = 0, j;

    String pwr = "1";

    Arrays.sort(xs);
    while(i < xs.length && xs[i] < 0 ){
        i++;
    }
    if(i != 0){
        if (i == 1){
            if(xs[xs.length - 1] == 0){
                pwr = "0";
            } else if (xs.length == 1){
                return Arrays.valueOf(xs[0]);
            }
        }
        else if(i % 2 == 0){
            for (j = 0; j < i ;j++ ) {
                pwr = multiply(pwr, String.valueOf(xs[j]));
            }
        } else {
            for (j = 0; j < i - 1; j++ ) {
                pwr = multiply(pwr, String.valueOf(xs[j]));
            }
        }
    } else {
        if(xs[xs.length - 1] == 0){
            pwr = "0";
        }
    }

    for(;i < xs.length; i++){
        if(xs[i] != 0)
            pwr = multiply(pwr, String.valueOf(xs[i]));
    }


    return (pwr);     
}

public static String multiply(String num1, String num2){
                String tempnum1 = num1; 
                String tempnum2 = num2; 

                // Check condition if one string is negative 
                if(num1.charAt(0) == '-' && num2.charAt(0)!='-') 
                { 
                    num1 = num1.substring(1); 
                } 
                else if(num1.charAt(0) != '-' && num2.charAt(0) == '-') 
                { 
                    num2 = num2.substring(1); 
                } 
                else if(num1.charAt(0) == '-' && num2.charAt(0) == '-') 
                { 
                    num1 = num1.substring(1); 
                    num2 = num2.substring(1); 
                } 
                String s1 = new StringBuffer(num1).reverse().toString(); 
                String s2 = new StringBuffer(num2).reverse().toString(); 

                int[] m = new int[s1.length()+s2.length()]; 

                        // Go from right to left in num1 
                for (int k = 0; k < s1.length(); k++)  
                { 
                    // Go from right to left in num2 
                    for (int j = 0; j < s2.length(); j++)  
                    { 
                        m[k+j] = m[k+j]+(s1.charAt(k)-'0')*(s2.charAt(j)-'0'); 

                    } 
                } 


                String product = new String(); 
                // Multiply with current digit of first number 
                // and add result to previously stored product 
                // at current position.  
                for (int l = 0; l < m.length; l++) 
                { 
                    int digit = m[l]%10; 
                    int carry = m[l]/10; 
                    if(l+1<m.length) 
                    { 
                        m[l+1] = m[l+1] + carry; 
                    } 
                    product = digit+product; 

                } 

                // ignore '0's from the right 
                while(product.length()>1 && product.charAt(0) == '0') 
                { 
                    product = product.substring(1); 
                } 

                // Check condition if one string is negative 
                if(tempnum1.charAt(0) == '-' && tempnum2.charAt(0)!='-') 
                { 
                    product = new StringBuffer(product).insert(0,'-').toString(); 
                } 
                else if(tempnum1.charAt(0) != '-' && tempnum2.charAt(0) == '-') 
                { 
                    product = new StringBuffer(product).insert(0,'-').toString(); 
                } 
                else if(tempnum1.charAt(0) == '-' && tempnum2.charAt(0) == '-') 
                { 
                    product = product; 
                } 
                // System.out.println("Product of the two numbers is :"+"\n"+product); 
                return product;
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接