有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java下面的代码问题解决方案在本地IDE中对我有效,但在Hackerrank IDE中不知何故失败了,我的代码有什么问题吗

最小启动值

Start with a given array of integer and an arbitrary initial value x.Calculate the running sum of x plus each array element,left to right. The running sum must never get below 1.Determine the minimum value of x.

示例arr=[4,-2,3,1,-5]第一个元素是大小,因此需要处理的数组是arr=[-2,3,1,-5],大小n=4

如果x=4,将得到以下结果

sum     arr[i]
-----   ------ 
4        -2
2         3
5         1
6        -5
1

所以最小值是4

类似地,对于数组,arr=[10,-5,4,-2,3,1,-1,-6,-1,0,-5],大小将是10,因此实际数组是arr=[-5,4,-2,3,1,-1,-6,-1,0,-5]

if x=6, following result would be obtained.

 sum     arr[i]
        -----   ------
        6       -5
        1        4
        5       -2
        3        3
        6        1
        7       -1
        0       -6
                -1
                 0
                -5

所以,在运行sum时,我们得到了一个小于1的0和,所以x=6不是最小值

如果x=11,将得到以下结果

   sum     arr[i]
    -----   ------
    11       -5
    6         4
    10       -2
    8         3
    11        1
    12       -1
    11       -6
    5        -1
    4         0
    4        -5
   -1  

这是不正确的,我们得到的数字小于1,所以它不是最小值

如果x=13,将获得以下结果

    sum     arr[i]
    -----   ------
    13       -5
    8         4
    12       -2
    10        3
    13        1
    14       -1
    13       -6
    7        -1
    6         0
    6        -5
    1

所以,最小值是13

下面是我为上述问题编写的代码。该测试有5个测试用例,它们都在我的本地IDE中成功,但在hackerrank IDE中都失败了

public static int minX(List<Integer> arr) {
        int x=0;
        arr.remove(0);
        boolean limitFound = false;
        while(!limitFound){
            int sum=x;
            for(Integer i: arr){
                sum+=i;
                if(sum<1){
                    break;
                }
            }
            limitFound = sum<1?false:true;
            if(limitFound){
                break;
            }
            x++;
        }
        return x;
        }

我真的没有发现我的解决方案有任何问题,另外,请帮助我理解为什么它在那里失败了,当我在本地IDE中尝试相同的方法时,它通过了所有相同的测试场景。此外,如何改进代码也会很有帮助


共 (6) 个答案

  1. # 1 楼答案

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int minX(vector<int> &arr)
    {
        int sumwX = 1;
        for (auto it = arr.rbegin(); it != arr.rend(); ++it)
        {
            if(sumwX < 1) sumwX = 1;
            sumwX -= *it;
        }
        
        return sumwX;
    }
    
    int main()
    {
        
        vector<int> M = {-5,4,-2,3,29,-1,-6,-1,0,-5}; // 6
        vector<int> N = {-2,3,1,-5}; //4
        vector<int> V = {-5,4,-2,3,1,-1,-6,-1,0,-5}; //13
        cout << minX(M) << endl;
        cout << minX(N) << endl;
        cout << minX(V) << endl;
        return 0;
    }
    
  2. # 2 楼答案

    以下代码解决了该问题:

    public int minX(int[] nums) {
        int startValue = 0; //Integer.MIN_VALUE
        for (int x = startValue; x <= Integer.MAX_VALUE; x++) {
            int sum = x;
            for (int n : nums) {
                sum += n;
                if (sum < 1) {
                    break;
                }
            }
            if (sum >= 1) {
                return x;
            }
        }
        return 0;
    }
    
  3. # 3 楼答案

    #include <iostream>
    
    using namespace std;
    
    int main() {
        int a[]={-2,3,1,-5};
        int sum=0;
        int ans=0;
        int n=sizeof(a)/sizeof(a[0]);
        for(int i=0;i<n;i++){
            sum+=a[i];
            if(sum<1){
                sum=-sum;
                ans+=sum+1;
                sum=1;
            }
        }
        cout << ans << "\n";
        return 0;
    }
    
  4. # 4 楼答案

    import java.util.List;
    import java.util.Arrays;
    import static java.lang.System.out;
    class Playground {
        private static boolean doesRunningTotalNeverGoBelowOneForSeed(
          List<Integer> list, 
          Integer seed 
        ) {
            boolean result = true;
            int total = seed;
            for (Integer i : list) {
                total += i;
                if (total < 1) {
                    result = false;
                    break;
                }
            }
            return result;
        }
        public static void main(String[ ] args) {
            Integer[] a = {-5, 4, -2, 3, 1, -1, -6, -1, 0, -5};//{-2,3,1,-5};
            List<Integer> list = Arrays.asList(a);
            out.println(doesRunningTotalNeverGoBelowOneForSeed(list, 13)); //4, 6
        }
          
    }
    

    这是我应该做的事情

  5. # 5 楼答案

    在删除第三行中的arr.remove(0);时,它起作用。 我认为HackerRank IDE计算的值包括0

  6. # 6 楼答案

    试过这个。。它起作用了(是的,在当地)。如果您可以提供更多关于TCs的详细信息,而TCs在黑客排名中可能失败,那么您也可以尝试添加一些sysout来更好地了解。可能你也可以发布黑客等级的链接:)

    public static int minX(List<Integer> arr) {
            int x = 0;
            arr.remove(0);
            while (true) {
                int sum = x;
                for (Integer i : arr) {
                    sum += i;
                }
                if (sum == 1) {
                    return x;
                } else {
                    x++;
                }
            }
        }