본문 바로가기
Problem Solving/Baekjoon

[백준] 1487 물건 팔기 - Brute Force / Java

by graycode 2023. 10. 28.

 문제 링크

 

1487번: 물건 팔기

첫째 줄에 최대 이익을 만들어주는 가격을 출력한다. 이익이 최대인 가격이 여러개라면, 가장 낮은 가격을 출력한다. 또, 어떤 가격으로 팔아도 이익을 남길 수 없다면 0을 출력한다.

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Set;
import java.util.TreeSet;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = read();

        Pair[] arr = new Pair[n];
        Set<Integer> set = new TreeSet<>();
        while (n-- > 0) set.add((arr[n] = new Pair(read(), read())).in);

        int max = 0, res = 0;
        for (int in : set) {
            int sum = 0;
            for (Pair p : arr) if (in <= p.in && in > p.out) sum += in - p.out;

            if (max < sum) {
                max = sum;
                res = in;
            }
        }

        bw.write(String.valueOf(res));
        bw.flush();
    }

    private static class Pair {
        int in, out;

        Pair(int in, int out) {
            this.in = in;
            this.out = out;
        }
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글