코딩테스트/문제풀이

[코드트리]왔다 갔던 구역 2

알렉스 페레이라 2023. 4. 18. 15:39

설명을 봐도 잘 모르겠어서 기록하면서 분석하려고 쓴다..


위치 0에서 시작하여 n번의 명령에 걸쳐 움직인 뒤, 2번 이상 지나간 영역의 크기를 출력하는 프로그램을 작성해보세요. 단 명령은 “x L“, “x R” 형태로만 주어집니다. "x L" 의 경우 왼쪽으로 x만큼 이동해야 함을, "x R"의 경우 오른쪽으로 x만큼 이동해야 함을 뜻합니다.

 

입력 형식

첫 번째 줄에는 n이 주어집니다.

두 번째 줄 부터는 n개의 줄에 걸쳐 명령이 주어집니다. 형태는 “x L” 혹은 “x R” 입니다.

  • 1 ≤ n ≤ 100
  • 1 ≤ x ≤ 10

출력 형식

첫 번째 줄에 명령을 주어진 순서대로 수행했을 때, 2번 이상 지나간 영역의 크기를 출력합니다.

 

입출력 예제

예제1

입력:

6
2 R
6 L
1 R
8 L
1 R
2 R

 

출력:

6

 

예제 설명

2번 이상 지나간 구간은 아래와 같으므로 답은 6이 됩니다.

 


 

내 제출코드 : 

import java.util.*;

public class Main {
    static HashMap<Integer, Integer> map = new HashMap<>();
    static int location = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for(int i = 0; i < n; i++){
            move(sc.nextInt(), sc.next());
        }

        int count = 0;
        for(int key : map.keySet()){
            if(map.get(key) >= 2)count++;
        }

        System.out.print(count);
    }

    static void move(int num, String code){
        if("L".equals(code)){
            for(int i = location; i > location-num; i--){
                map.put(i, map.getOrDefault(i, 0) + 1);
            }
            location -= num;
        }else{
            for(int i = location; i < location+num; i++){
                map.put(i, map.getOrDefault(i, 0) + 1);
            }
            location += num;
        }
    }
}

 

 

첫번째 테스트케이스는 정답이지만 다른 테스트케이스에서 에러가난다.

 

입력:

28
7 L
1 R
3 R
1 R
6 L
2 L
2 L
5 L
3 L
3 R
10 L
1 R
6 L
6 L
2 L
1 L
4 R
7 R
2 R
6 L
10 L
7 L
6 R
9 L
7 R
10 R
4 R
7 R

 

정답 : 

36

출력 : 

38

 

해설은 다음과 같다

 

 

import java.util.*;

public class Main {
    static final int offset = 1000; // 100씩 10번움직이면 최대 1000까지 움직이므로 offset을 1000으로 설정.

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[] from = new int[n]; //시작점을 나타내는 배열
        int[] to = new int[n]; //끝점을 나타내는 배열
        int[] checked = new int[2001];//구역 체크 배열, 점의갯수는 2001개보다 크지 않다.
        int location = 0; //현재위치

        for (int i = 0; i < n; i++) {
            int distance = sc.nextInt();
            String direction = sc.next();

            if (direction.equals("L")) {
                from[i] = location - distance; //여기가 이해가 안됨. 왜 location-distance ~ location이지?
                                               //location ~ location-distance로 해야하는거 아닌가??
                                               //구역을 찍었다는 기준이 시작점이 찍혀있으면 다음칸 까지 찍혀서???
                                               //아니면 혹시 시작점에 찍는다는게 -4~-3이면 -4고 0~-4면 -4? 
                                               //단순히 왼쪽에 있는점? 더 작은 점???
                to[i] = location;
                location -= distance;
            } else {
                from[i] = location;
                to[i] = location + distance;
                location += distance;
            }

            from[i] += offset;
            to[i] += offset;
        }

        for (int i = 0; i < n; i++) {
            for (int j = from[i]; j < to[i]; j++) {
                checked[j]++;
            }
        }

        int count = 0;
        for (int i = 0; i < checked.length; i++) {
            if (checked[i] >= 2) count++;
        }
        System.out.print(count);

    }
}