Page tree
Skip to end of metadata
Go to start of metadata

1.  test 2개 타임아웃

MinimumAbsoluteDifference
static int minimumAbsoluteDifference(int[] arr) {
        int min = Math.abs(arr[0] - arr[1]);

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                int absValue = Math.abs(arr[i] - arr[j]);
                if (absValue < min) {
                    min = absValue;
                }
            }
        }
        return min;

    }


public int minimumAbsoluteDifference(int[] arr) {
        // arr[i] - arr[j] i != j
        final List<Integer> values = Arrays.stream(arr)
                .boxed()
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList());

        int maxValue = values.remove(0);
        int firstValue = values.remove(0);
        int min = Math.abs(firstValue - maxValue);

        for (final Integer value : values) {
            int absValue = Math.abs(value - maxValue);
            if (absValue < min) {
                min = absValue;
            }
        }

        return min;
    }

// 회고 코드
	public int minimumAbsoluteDifference(int[] arr) {
        // arr[i] - arr[j] i != j
        int min = Integer.MAX_VALUE;
        Arrays.sort(arr);

        for (int i = 0; i < arr.length - 1; i++) {
            min = Math.min(min, Math.abs(arr[i] - arr[i + 1]));
        }

        return min;
    }


2. 테스트 케이스 실패

static int luckBalance(int k, int[][] contests) {
        int sum = 0;
        for (int i = 0; i < contests.length; i++) {
            if (i != k - 1) {
                sum += contests[i][0];
            }
        }

        if (contests[k - 1][1] == 0) {
            return sum;
        }
        return sum - contests[k - 1][0];
    }

// 회고 코드
static int luckBalance(int k, int[][] contests) {
        int maxLuck = 0;
        int l = k;

        final List<int[]> collect = Arrays.stream(contests)
                .sorted((o1, o2) -> {
                    System.out.println(o1[0] + "::" + o2[0]);
                    if (o1[0] < o2[0]) {
                        return 1;
                    } else if (o1[0] > o2[0]) {
                        return -1;
                    }
                    return 0;
                })
                .collect(Collectors.toList());

        for (final int[] ints : collect) {
            if (ints[1] == 0) {
                maxLuck += ints[0];
            } else {
                if (l > 0) {
                    maxLuck += ints[0];
                    l--;
                } else {
                    maxLuck -= ints[0];
                }
            }
        }


        return maxLuck;
    }


3.

// k people group, c cost
    static int getMinimumCost(int k, int[] c) {

        final List<Integer> collect = Arrays.stream(c)
                .boxed()
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList());

        int index = 0;
        int value;
        int sum = 0;


        for (final Integer integer : collect) {
// 가중치...
            value = index / k;
            sum += (value + 1) * integer;
            index++;
        }

        return sum;
    }
  • No labels