240804

Regular Kim·2024년 8월 4일
0

회고

목록 보기
53/59

240804 회고 💬

본격 백수 생활 2달 차, 8월이 시작됐다. 장마가 끝나도 습기는 좀 사라졌지만 그럼에도 엄청난 폭염으로 밖을 나가지 않았다. 하지만 재수가 없으면 뒤로 넘어져도 코가 깨진댔나? 집에만 있었음에도 코로나에 걸려버렸다. 우리 집에 누가 몰래 들어온 건가? 왜 코로나에 걸렸는지 알 수가 없다. 한밤중에 추워서 깼다 잠들기를 반복하다 보니 엄청나게 괴롭다. 그래서 한여름인데도 겨울 이불을 꺼내버렸다. 와 코로나 엄청 힘들다. 코로나로 시작하는 8월의 첫째 주를 되돌아본다.

Keep 👍

  • 알고리즘 문제풀이를 역시나 7일 연속으로 해냈다. 8월 4일 기준 연속 295일째, 697문제를 풀이 중이다. 요즘 다시 알고리즘 문제 풀기가 재밌어져서 하루에 3, 4문제씩 풀 때도 있다. 안 풀리면 너무 괴롭지만 또 그만큼 푼 다음의 쾌감이 커서 그만두기가 힘들다. 🏋️
    - 로봇청소기 조건이 좀 많지만 까다롭지 않기 때문에 천천히 구현하다 보면 쉽게 풀 수 있다. 골드와 실버 사이의 문제다. 객체지향 적으로 설계하려고 고민좀 했다.
    - 이중 우선순위 큐 아이디어만 떠올리면 쉽게 풀수 있는 문제다. 우선 순위 큐 2개를 사용해도 되지만 연산량이 늘어나기에 다른 자료구조 사용하기를 추천한다. 맨 앞과 맨 뒤를 한번에 접근할 수 있어야하고, 정렬이 빠르게 되는 자료구조를 사용해야한다.
  • "함께 자라기 애자일로 가는 길"을 계속 읽고 있다.
  • 알고리즘 문제 풀이를 하느라 요즘에는 알고리즘 공부에 집중 하고 있다. 스프링 공부도 해야하는데,,,🤦‍♂️
    - 그래프
    - 트리
    - 유니온 파인드
    - 우선순위 큐

Problem 🤢

Try 🧚

  • 함께 자라기 마무리하기
  • 최소신장트리 공부하기
    - 유니온파인드 (o)
    - 프림
  • 알고리즘 문제 풀이

Extras 😀

로봇청소기


import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

            Input ip = getInput(br);
            Solution s = new Solution();
            System.out.println(s.solution(ip.startLocation, ip.toward, ip.room));

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static Input getInput(BufferedReader br) throws IOException {
        String[] dimension = br.readLine().split(" ");
        int x = Integer.parseInt(dimension[0]);
        int y = Integer.parseInt(dimension[1]);

        int[][] room = new int[x][y];

        String[] info = br.readLine().split(" ");
        int startX = Integer.parseInt(info[0]);
        int startY = Integer.parseInt(info[1]);
        int toward = Integer.parseInt(info[2]);

        for (int i = 0; i < room.length; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < room[i].length; j++) {
                room[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        return new Input(new int[]{startX, startY}, toward, room);
    }

    private static class Input {

        int[] startLocation;
        int toward;
        int[][] room;

        public Input(int[] startLocation, int toward, int[][] room) {
            this.startLocation = startLocation;
            this.toward = toward;
            this.room = room;
        }
    }
}

class Solution {

    public int solution(int[] startLocation, int toward, int[][] room) {
        Calculator c = new Calculator(startLocation, toward, room);
        return c.getResult();
    }
}

class Calculator {

    VacuumCleaner vacuumCleaner;
    Room room;
    int[][] directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};  // UP, RIGHT, DOWN, LEFT

    public Calculator(int[] startLocation, int toward, int[][] room) {
        this.vacuumCleaner = new VacuumCleaner(startLocation, toward);
        this.room = new Room(room);
    }

    public int getResult() {
        cleanUp();
        return vacuumCleaner.cleaned;
    }

    private void cleanUp() {
        while (true) {
            if (room.getStatusOf(vacuumCleaner.location) == 0) {
                room.getClean(vacuumCleaner.location);
                vacuumCleaner.cleaned++;
            }

            if (noDirtAround()) {
                if (room.noWallBehind(vacuumCleaner.getBackward())) {
                    vacuumCleaner.goBackward();
                } else {
                    break;
                }
            } else {
                vacuumCleaner.turn();
                if (room.isWithinRoom(vacuumCleaner.getForward()) && room.getStatusOf(vacuumCleaner.getForward()) == 0) {
                    vacuumCleaner.goForward();
                }
            }
        }
    }

    public boolean noDirtAround() {
        int[] location = vacuumCleaner.location;
        for (int[] direction : directions) {
            int nx = location[0] + direction[0];
            int ny = location[1] + direction[1];

            if (room.isWithinRoom(nx, ny) && room.getStatusOf(nx, ny) == 0) {
                return false;
            }
        }
        return true;
    }
}

class VacuumCleaner {

    int[] location;
    Direction toward;
    int cleaned = 0;

    public VacuumCleaner(int[] location, int toward) {
        this.location = location;
        this.toward = Direction.setDirection(toward);
    }

    public int[] getForward() {
        int[] forward = toward.getForward();
        return new int[]{location[0] + forward[0], location[1] + forward[1]};
    }

    public void goForward() {
        int[] forward = toward.getForward();
        location = new int[]{location[0] + forward[0], location[1] + forward[1]};
    }

    public void goBackward() {
        int[] backward = toward.getBackward();
        location = new int[]{location[0] + backward[0], location[1] + backward[1]};
    }

    public int[] getBackward() {
        int[] backward = toward.getBackward();
        return new int[]{location[0] + backward[0], location[1] + backward[1]};
    }

    public void turn() {
        toward = Direction.turnLeft(toward);
    }

    private enum Direction {

        UPWARD(0, new int[]{-1, 0}, new int[]{1, 0}),
        RIGHTWARD(1, new int[]{0, 1}, new int[]{0, -1}),
        DOWNWARD(2, new int[]{1, 0}, new int[]{-1, 0}),
        LEFTWARD(3, new int[]{0, -1}, new int[]{0, 1}),
        ;

        private final int toward;
        private final int[] forward;
        private final int[] backward;

        Direction(int toward, int[] forward, int[] backward) {
            this.toward = toward;
            this.forward = forward;
            this.backward = backward;
        }

        public static Direction setDirection(int toward) {
            for (Direction d : Direction.values()) {
                if (d.toward == toward) return d;
            }
            return UPWARD;
        }

        public int[] getForward() {
            return forward;
        }

        public int[] getBackward() {
            return backward;
        }

        public static Direction turnLeft(Direction d) {
            switch(d) {
                case UPWARD -> {
                    return LEFTWARD;
                }
                case RIGHTWARD -> {
                    return UPWARD;
                }
                case DOWNWARD -> {
                    return RIGHTWARD;
                }
                case LEFTWARD -> {
                    return DOWNWARD;
                }
            }
            return d;
        }
    }
}

class Room {

    int[][] room;

    public Room(int[][] room) {
        this.room = room;
    }

    public int getStatusOf(int[] location) {
        int x = location[0];
        int y = location[1];
        return room[x][y];
    }

    public int getStatusOf(int x, int y) {
        return room[x][y];
    }

    public void getClean(int[] location) {
        int x = location[0];
        int y = location[1];
        room[x][y] = -1;
    }

    public boolean isWithinRoom(int x, int y) {
        return 0 <= x && x < room.length && 0 <= y && y < room[x].length;
    }

    public boolean noWallBehind(int[] location) {
        int x = location[0];
        int y = location[1];
        return isWithinRoom(x, y) && room[x][y] != 1;
    }

    public boolean isWithinRoom(int[] forward) {
        int x = forward[0];
        int y = forward[1];
        return 0 <= x && x < room.length && 0 <= y && y < room[x].length;
    }
}

이중 우선순위 큐


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeMap;

public class Main {

    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

            int TEST_CASE = Integer.parseInt(br.readLine());

            while (TEST_CASE-- > 0) {
                Solution s = new Solution();
                int orderLen = Integer.parseInt(br.readLine());
                while (orderLen-- > 0) {
                    String[] order = br.readLine().split(" ");
                    s.process(order[0], Integer.parseInt(order[1]));
                }
                System.out.println(s.getResult());
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

class Solution {

    DoublePriorityQueue dpq = new DoublePriorityQueue();

    public void process(String order, int num) {
        dpq.calc(order, num);
    }

    public String getResult() {
        return dpq.getResult();
    }
}

class DoublePriorityQueue {

    private TreeMap<Integer, Integer> map = new TreeMap<>();

    public String getResult() {
        if (map.isEmpty()) return "EMPTY";
        return map.lastKey() + " " + map.firstKey();
    }

    public void calc(String order, int num) {
        if (order.equals("I")) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        } else {
            deleteCalc(num);
        }
    }

    private void deleteCalc(int num) {
        if (map.isEmpty()) return;

        int key = (num == 1) ? map.lastKey() : map.firstKey();

        if (map.get(key) == 1) {
            map.remove(key);
        } else {
            map.put(key, map.get(key) - 1);
        }
    }
}

독서 목록

서평 완료 목록

서평 예정 목록 (읽는 중)

  • 프로그래머의 길, 멘토에게 묻다
  • 함께 자라기 애자일로 가는 길

독서 예정 목록

목록은 우선순위 큐이다. 상단에 있더라도 더 중요한 책이 들어온다면 순위가 뒤로 밀릴 수 있다.

  • 객체지향의 사실과 오해
  • 오브젝트
  • 파이브 라인스 오브 코드
  • HTTP 완벽 가이드
  • 자바/스프링 개발자를 위한 실용주의 프로그래밍
  • 모던 자바 인 액션
  • 자바 성능 튜닝 이야기
  • 자바 개발자와 시스템 운영자를 위한 트러블 슈팅 이야기 / scouter를 활용한 시스템 장애 진단 및 해결 노하우 자바 트러블슈팅
  • 헤드 퍼스트 서블릿
  • Hello Coding 그림으로 개념을 이해하는 알고리즘
profile
What doesn't kill you, makes you stronger

0개의 댓글