【问题标题】:Google Foobar power_hungryGoogle Foobar power_hungry
【发布时间】:2017-02-27 16:20:59
【问题描述】:

您好,我需要帮助解决我的一个 Google 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"

我已经用了 2 天了,我真的很想得到答案,这样我就可以了解我做错了什么并改进!感谢您的阅读,希望您能回答。 :)

【问题讨论】:

    标签: java python arrays computer-science


    【解决方案1】:

    您需要处理某些情况:

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

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

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

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

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

    我记录了正整数和负整数的计数...希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      你的代码很好,但你只需要做一个小的加法,如果数组中的所有元素都是零或负数,那么它应该返回 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();
      }
      }
      

      【讨论】:

        【解决方案3】:

        尝试编写满足这些测试用例或类似情况的代码。 {-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。

        所以也推荐使用String来实现一个产品。

        导入 java.util.Arrays;

        公共类主要 {

        public static String result(int[] xs) {
        

        // System.out.println("Hello World"); 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;
        }
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-10-19
          • 1970-01-01
          • 2021-09-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多