简体   繁体   English

HackerRank 任务“Mini Max Sum”解决方案未通过 13 个测试用例中的 3 个,有人能告诉我我做错了什么吗

[英]HackerRank Task “Mini Max Sum” solution not passing 3 of the 13 test cases, can someone tell me what i'm doing wrong

This is for the "Mini Max Sum" problem on HackerRank, I can't see why it doesn't have a check mark on all of the test cases.这是针对 HackerRank 上的“Mini Max Sum”问题,我不明白为什么所有测试用例上都没有复选标记。 Can someone tell me where my problem lies at.谁能告诉我我的问题出在哪里。 The question is:问题是:

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers.打印两个空格分隔的长整数,分别表示可以通过将五个整数中的四个相加来计算的相应最小值和最大值。 (The output can be greater than 32 bit integer.) (输出可以大于 32 位整数。)

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long[] arr = new long[5];
        long total = 0, max = 0, min = 0;
        for(int arr_i=0; arr_i < 5; arr_i++){
            arr[arr_i] = in.nextLong();
            min = arr[0];
            total += arr[arr_i];
            if (arr[arr_i] > max)
                max = arr[arr_i];
            if (arr[arr_i] <= min)
                min = arr[arr_i];
        }
        System.out.println((total - max) + " " + (total - min));
    }
}

The problem are the initial values of both min and max .问题是minmax的初始值。

  1. min is being reset to the first values inside the loop, so it will probably only work if the first or the last value is the minimum one; min被重置为循环内的第一个值,因此它可能仅在第一个或最后一个值是最小值时才有效;

  2. max starts with zero, so if all values are negative, max will stay at zero (instead of one of the input values). max从零开始,因此如果所有值都为负,则max将保持为零(而不是输入值之一)。

Hints: set min and max on the first iteration ( i == 0 ) or, as suggested, use Integer.MAX_VALUE and Integer.MIN_VALUE respectively as initial value (actually long is not needed for min and max , neither is the array)提示:在第一次迭代时设置minmax ( i == 0 ) 或者,按照建议,分别使用 Integer.MAX_VALUE 和 Integer.MIN_VALUE 作为初始值(实际上minmax不需要long ,数组也不需要)

This also worked.这也奏效了。 Thanks!!谢谢!!

static void miniMaxSum(int[] arr) {
    List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
    Collections.sort(list); 
    long x=0, y=0;
    for(int j=0;j<(list.size()-1);j++){
            x = x + list.get(j);
            y = y + list.get(j+1);
    }
    System.out.println(x +" "+y);
}
static void miniMaxSum(int[] arr) {
       int temp = 0;
      for (int i = 0; i < arr.length; i++) 
        {
            for (int j = i + 1; j < arr.length; j++) { 
                if (arr[i] > arr[j]) 
                {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        long minSum = 0;
        long maxSum = 0;
      for(int i = 1; i< arr.length; i++){
         maxSum = maxSum + arr[i];
      }
      for(int i = 0; i< arr.length-1; i++){
         minSum = minSum + arr[i];
      }
      System.out.print(minSum+ " " +maxSum);
    }

This worked for me.这对我有用。

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.stream.LongStream;

public class Solution {

// Complete the miniMaxSum function below.
static void miniMaxSum(int[] arr) {
long[] longs = Arrays.stream(arr).asLongStream().toArray();

Arrays.sort(longs);
long sum = LongStream.of(longs).sum();

long min = sum - longs[4];
long max = sum - longs[0];

System.out.println(min + " " + max);
}


private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    int[] arr = new int[5];

    String[] arrItems = scanner.nextLine().split(" ");
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    for (int i = 0; i < 5; i++) {
        int arrItem = Integer.parseInt(arrItems[i]);
        arr[i] = arrItem;
    }

    miniMaxSum(arr);

    scanner.close();
   }
}

Here's a hint in the question: minimum and maximum values that can be calculated by summing exactly four of the five integers .这是问题中的一个提示:可以通过summing exactly four of the five integers来计算的最小值和最大值。

Just sort the array first, assuming the array is not sorted.假设数组未排序,只需先对数组进行排序。 Take two for loop because we want to keep the complexity up to O(n) ie Linear.取两个for loop因为我们希望将复杂度保持在 O(n),即线性。

  1. from 1st index to n - 1 .1st索引到n - 1 Assuming index starts from 0 .假设 index 从0开始。 This will give you sum of all the element except the smallest element which will be the largest sum.这将为您提供所有元素的总和,除了将是最大总和的最小元素。
  2. from 0th index to n - 2 .0th索引到n - 2 Assuming index starts from 0 .假设 index 从0开始。 This will give you sum of all the element except the largest element which will be the least sum.这将为您提供所有元素的总和,除了最大的元素将是最小的总和。

Let's say, Our initial numbers are 1, 2, 3, 4 and 5. We can calculate the following sums using four of the five integers:假设我们的初始数字是 1、2、3、4 和 5。我们可以使用五个整数中的四个来计算以下总和:

  • If we sum everything except 1, our sum is 2 + 3 + 4 + 5 =14.如果我们对除 1 之外的所有内容求和,我们的总和为 2 + 3 + 4 + 5 =14。
  • If we sum everything except 2, our sum is 1 + 3 + 4 + 5 =13.如果我们对除 2 之外的所有内容求和,我们的总和为 1 + 3 + 4 + 5 =13。
  • If we sum everything except 3, our sum is 1 + 2 + 4 + 5 =12.如果我们对除 3 之外的所有内容求和,我们的总和为 1 + 2 + 4 + 5 =12。
  • If we sum everything except 4, our sum is 1 + 2 + 3 + 5 =11.如果我们对除 4 之外的所有内容求和,我们的总和为 1 + 2 + 3 + 5 =11。
  • If we sum everything except 5, our sum is 1 + 2 + 3 + 4 =10.如果我们对除 5 之外的所有内容求和,我们的总和为 1 + 2 + 3 + 4 =10。
public static void minmaxsum(int[] ar1) {
    long a, b, c, d, e;

    a = (long) ar1[1] + (long) ar1[2] + (long) ar1[3] + (long) ar1[4];
    System.out.println(a);

    b = (long) ar1[0] + (long) ar1[2] + (long) ar1[3] + (long) ar1[4];
    System.out.println(b);

    c = (long) ar1[0] + (long) ar1[1] + (long) ar1[3] + (long) ar1[4];
    System.out.println(c);

    d = (long) ar1[0] + (long) ar1[1] + (long) ar1[2] + (long) ar1[4];
    System.out.println(d);

    e = (long) ar1[0] + (long) ar1[1] + (long) ar1[2] + (long) ar1[3];
    System.out.println(e);

    long[] df = new long[] { a, b, c, d, e };

    long max = df[0];
    for (int i = 0; i < df.length; i++) {
        if (df[i] > max) {
            max = df[i];
        }
    }

    long min = df[0];
    for (int i = 0; i < df.length; i++) {
        if (df[i] < min) {
            min = df[i];
        }
    }
    System.out.println(min + " " + max);

}

Worked for me on Python3!在 Python3 上为我工作!

def miniMaxSum(arr):

    total = sum(arr)
    low = total - min(arr)
    high = total - max(arr)

    print(high, low)

    return
import math
import os
import random
import re
import sys

def miniMaxSum(arr):
   arr.sort()
   m = sum(arr)
   
   max_num = m - arr[-1]
   min_num = m - arr[0]
   
   print(max_num, min_num)

if __name__ == '__main__':

    arr = list(map(int, input().rstrip().split()))

    miniMaxSum(arr)
static void miniMaxSum(int[] arr) {
    long[] longs = Arrays.stream(arr).asLongStream().toArray();

    Arrays.sort(longs);
    long sum = LongStream.of(longs).sum();

    long min = sum - longs[4];
    long max = sum - longs[0];

    System.out.println(min + " " + max);
}

This answer is in PYTHON language.这个答案是用 PYTHON 语言编写的。 I am a beginner and any improvements are welcome我是初学者,欢迎任何改进

n = input().split(" ")
n=list(n)
n1 = list(map(int,n))
n2 = list(map(int,n))
n1.sort()
n1.pop()
min =0
max=0
for i in n1:
min+=i
n2.sort()
n2.reverse()
n2.pop()
for j in n2:
max+=j
print(min, max)

This is what worked for me in JavaScript:这就是在 JavaScript 中对我有用的东西:

var sum = 0;

var arry = [];

function miniMaxSum(arr) {

    for (var i = 0; i < arr.length; i++) {

        for (var j = 0; j < arr.length; j++) {

            if (arr[j] !== arr[i]) {
                sum += arr[j];
            }

        }
        arry.push(sum);

        sum = 0;

    }


    var min = Math.min(...arry);
    var max = Math.max(...arry);

    console.log(min, max);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以告诉我在Java中设置此Robot类时我做错了什么吗? - Can someone tell me what I'm doing wrong to setup this Robot class in Java? 有人能告诉我我做错了什么吗? 通过LinkedList计数和循环 - Can someone tell me what i'm doing wrong? Counting and looping through LinkedList 谁能告诉我我在做什么错? -堆栈 - Can anyone tell me what I'm doing wrong? - Stacks 有一种有效的“合并排序”功能,但不是完全可以,有人可以告诉我我做错了什么吗? - Have a sort of working Merge Sort, but not quite, could someone tell me what I am doing wrong? 请告诉我我做错了什么,if 语句和转换不起作用 - Please tell me what I'm doing wrong, the if statements and the conversions are not working 二叉树的高度:为什么这个解决方案没有通过hackerrank中的2/6测试用例? - Height of a binary tree: why is this solution not passing 2/6 test cases in hackerrank? 当尝试根据给定查询查找给定字符串中的子字符串计数时,有人能告诉我我在 Java 代码中做错了什么吗? - Can someone tell me what am I doing wrong here in my Java code when trying to find count of substrings in given string according to given queries? 膨胀 class android.fragment.app.FragmentContainerView 时出错,有人可以告诉我我缺少什么 - Error inflating class android.fragment.app.FragmentContainerView, can someone tell me what I'm missing 有人可以告诉我for循环怎么了吗? - Can someone tell me what's wrong with the for loops? 有人可以告诉我for循环有什么问题吗 - Can someone please tell me what is wrong with the for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM