【问题标题】:Finding the array with the lowest max value of element查找元素最大值最小的数组
【发布时间】:2019-05-27 13:32:37
【问题描述】:

我有一个 int[] 数组的数组列表,我正在尝试找到最大元素最低的数组。

例如,从数组 [1,2,5], [0,1,2], [8,0,0] 将是数组 [0,1,2] 因为最大元素是 2 .

但是,我的代码无法正常工作。你能帮我修复我的代码吗?谢谢!

int min = Integer.MAX_VALUE;
for (int i=0; i<list.size(); i++) {
    for (int j=0; j<list.get(i).length; j++) {
        if (list.get(i)[j]<min) {
            min = list.get(i)[i]; 
            minIndex = i; //index of the array in arraylist
        }
    }
}

【问题讨论】:

  • 什么错误? list 是什么?请创建一个minimal reproducible example
  • 如果两个或多个具有相同的最低最大值,您打算返回什么数组?
  • 对不起,没有错误,卡在一个循环中。 List 是数组的arraylist。
  • @deHaar 如果发生这种情况,我想返回两个/更多数组,但我首先专注于只找到一个。

标签: java arrays


【解决方案1】:

您的代码会在所有数组中找到最小值(至少如果您将 list.get(i)[i] 的拼写错误修正为 list.get(i)[j] 会如此)。

您应该找到每个数组的最大元素,然后检查该最大值是否小于之前找到的所有最大值:

int minIndex = 0;
int min = Integer.MAX_VALUE;
for (int i=0; i<list.size(); i++) {
    int max = Integer.MIN_VALUE;
    for (int j=0; j<list.get(i).length; j++) { // find max element of current array
        if (list.get(i)[j] > max) {
            max = list.get(i)[j]; 
            if (max > min) {
                break; // we already know the max of this array is not the smallest max
            }
        }
    }
    if (max < min) { // check if the max of the current array is the smallest max so far
        min = max;
        minIndex = i; //index of the array in arraylist
    }
}

【讨论】:

  • 谢谢,但是我想我必须重新考虑我的方法,因为这对于我的数据量来说非常慢。我一定会在我重新设计的方法中使用您的解决方案。再次感谢
  • @Jane 在最坏的情况下你必须测试所有数组的所有元素,但你可以通过从内部循环中断一次 max > min 来优化。
  • 我使用了您的代码,只是删除了 ArrayList 以摆脱嵌套循环。区别是分钟:)。谢谢
【解决方案2】:

你可以试试这个例子:

import java.util.Arrays;
import java.util.Comparator;

public class MaxMinArray {

    public static int[]  maxMinFromArray(int[][] arrays){
       return  Arrays.stream(arrays).min(Comparator.comparingInt(x -> Arrays.stream(x).max().orElse(0))).orElse(null);
    }
}

我已经用您的示例对其进行了测试:):

import org.junit.Test;

import static org.junit.Assert.*;

public class MaxMinArrayTest {

    @Test
    public void testMaxMinArray(){
        int[][] arrays = new int[][] {{1,2,5}, {0,1,2}, {8,0,0}};
        int[] result = MaxMinArray.maxMinFromArray(arrays);
        assertArrayEquals(new int[]{0,1,2}, result);
    }
}

【讨论】:

    【解决方案3】:

    作为 for 循环的替代方法,您可以尝试使用 stream api 解决此问题。思路完全一样:

    • 在每个子列表中找到最大元素。
    • 使用Comparator 在最大元素中查找具有最小元素的子列表。
    List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0)) 
            .stream()
            .min((a, b) ->  
                   a.stream().max(Integer::compare).get()
                     .compareTo(
                        b.stream().max(Integer::compare).get()
                     )
            ).get(); 
    

    代码较少,可以说很容易理解代码的意图。 你甚至可以使用Comparator::comparing 方法来缩短代码:

    List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0))
            .stream()
            .min(Comparator.comparing(Collections::max))
            .get();
    

    让我们更详细地看看这里发生了什么。

    List.of(List.of(1, 2, 5), List.of(0, 1, 2), List.of(8, 0, 0))
            // lets get stream of list Stream<List<Integer>>. 
            .stream()
            // find sublist which has minimum maximum element. 
            .min((a, b) ->  
                   // a & b are sublist, for example: [1,2,5], [0,1,2]
                   // find maximum from a [1,2,5] which is [5]
                   a.stream().max(Integer::compare).get()
                   // compare maximum from a to maximum from b 
                     .compareTo(
                     // find maximum from a [0,1,2] which is [2]
                        b.stream().max(Integer::compare).get()
                     )
                   // get minimum out of [5,2]
            ).get(); // [0, 1, 2]
    

    所以执行可能看起来像这样:

    Initial list is: [1,2,5], [0,1,2], [8, 0, 0]
    find minimum list based on maximum: 
    min( max([1,2,5]), max([0,1,2])) 
    min( [5], [2]) 
    [2] -> list [0,1,2] contains minimum maximum element so far, go the the next iteration
    find minimum list based on maximum: 
    min( max([0,1,2]), max([8, 0, 0]) ) 
    min( [2], [8]) 
    [2] -> list [0,1,2] contains minimum maximum element so far, 
    there no other element in the stream, [0,1,2] is final result. 
    

    我希望你觉得这很有用。

    【讨论】:

      猜你喜欢
      • 2014-07-21
      • 2013-12-15
      • 2017-01-17
      • 1970-01-01
      • 2010-12-12
      • 2019-02-24
      • 2021-12-25
      • 2012-10-18
      相关资源
      最近更新 更多