[英]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
.问题是min
和max
的初始值。
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
被重置为循环内的第一个值,因此它可能仅在第一个或最后一个值是最小值时才有效;
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)提示:在第一次迭代时设置min
和max
( i == 0
) 或者,按照建议,分别使用 Integer.MAX_VALUE 和 Integer.MIN_VALUE 作为初始值(实际上min
和max
不需要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),即线性。
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.这将为您提供所有元素的总和,除了将是最大总和的最小元素。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。我们可以使用五个整数中的四个来计算以下总和:
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.