'drawRect'에 해당되는 글 1건

  1. 2021.12.27[DRAWRECT] 사각형 그리기

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/DRAWRECT

 

algospot.com :: DRAWRECT

사각형 그리기 문제 정보 문제 AdbyMe, Inc. 의 인턴인 A.I.는 웹 브라우저에 직사각형을 그리는 코드를 작성해야 한다. 웹 브라우저는 직사각형 모양의 뷰포트를 가지고 있고, 그려지는 직사각형의

www.algospot.com


풀이
입니다. 🤔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Map<Integer, Integer> xCounts = new HashMap<>(), yCounts = new HashMap<>();
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            for (int i = 0; i < 3++i) {
                input(xCounts, scanner.nextInt());
                input(yCounts, scanner.nextInt());
            }
 
            System.out.printf("%d %d\n", getAnswer(xCounts), getAnswer(yCounts));
 
            xCounts.clear();
            yCounts.clear();
        }
    }
 
    private static void input(Map<Integer, Integer> counts, int num) {
        if (counts.containsKey(num)) counts.put(num, counts.get(num) + 1);
        else counts.put(num, 1);
    }
 
    private static int getAnswer(Map<Integer, Integer> countGroup) {
        Set<Integer> keySet = countGroup.keySet();
        if (countGroup.size() == 1return keySet.iterator().next();
        else {
            for (Integer n : keySet) {
                if (countGroup.get(n) == 1return n;
            }
        }
 
        throw new IllegalStateException("Cannot found answer");
    }
}
 
cs

 


이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[ENCRYPT] 문자열 암호화  (0) 2021.12.27
[LECTURE] Lecture Note  (0) 2021.12.27
[ENDIANS] Endians  (0) 2021.12.27
[MERCY] Merciful Algospot  (0) 2021.12.27
[HELLOWORLD] Hello World  (0) 2021.12.27
Posted by N'