240630

Regular Kim·2024년 6월 30일
0

회고

목록 보기
47/59

240630 회고 💬

이전에는 공부한 내용이나 회고글 들을 모두 로컬로 관리했었다. 문서 도구로 옵시디언을 사용한다. 이번에 로컬로 관리하던 옵시디언 파일들을 모두 깃으로 관리하기로 했다. 아직까지는 대만족이다! 👍 하루 알고리즘 1문제를 풀어야 1일 커밋이 완료되기 때문에 이때까지는 문제풀이에 강박이 있었다. 알고리즘 문제를 풀지 못한다면 커밋이 안 된다는 불안감 때문에 신경이 많이 쓰였다. 이제는 다른 공부를 하더라도 해당 내용이 커밋되면서 커밋 기록이 초기화 되지 않는다. 강박 치료에 도움이 되지 않을까,,, 기대가 된다. 😀

Keep 👍

  • 알고리즘 문제풀이를 7일 연속으로 해냈다. 이번 주에는 DFS, BFS 문제를 골고루 해결했다. 이전에 집중적으로 공부했던 주제여서 그런지 오랜만에 풀이를 하는데도 기계처럼 코드가 나왔다.
    - 순열 입력된 값의 조합을 순서대로 구하는 문제다. 해당 입력의 경우의 수를 넘어가는 순서가 값으로 주어지면 예외처리고 답을 출력하면 연산을 줄일 수 있다. 백트래킹 복습용으로 좋은 문제다.
    - ABCDE 오랜만에 골드 문제에 도전했다. 골드라고 하지만 백트래킹을 잘 이해했다면 쉽게 풀 수 있는 문제다. 깊이가 5 이상인 그래프가 있는지 확인하는 문제이다. 포인트는 양방향 그래프로 네트워크를 만들어서 탐색하는 부분이다. 양방향으로 만들지 않으면 탐색이 적절하게 되지 않는다.
    - 현명한 나이트 체스 나이트로 넓이 우선 탐색을 하는 문제이다. 신경쓰였던 점이라면 타겟을 입력받은 순서대로 몇번째 이동에 잡히는지 값을 저장하는 부분이었다. 처음에는 타겟 하나당 탐색을 매번 새로 시작했었는데 시간 초과로 실패했다. 한번의 탐색으로 모든 타겟을 전부 처리해야한다. 좌표가 중첩되는 타겟은 없으므로 x, y 값을 키로 두고 타겟에 닿으면 해당 위치값에 몇번째에 도달했는지 저장하도록 로직을 구성했다.
  • "프로그래머의 길, 멘토에게 묻다"를 다 읽었다. 백수 된 기념으로 독서에 시간을 이전보다 더 많이 할애하고 있다. 따라서 서평을 써볼 예정이다. 독서에 좀 더 진지하게 접근하려고 독서 목록을 만들었다. 읽은 책, 읽고 있는 책, 읽을 예정인 책을 구분해서 리스트로 보여주는게 전부다. 그래도 쌓이다보면 리스트가 길어지면서 어떤 책들을 읽었는지 파악할 수 있게 되니 좋은 방법이라고 생각한다.
  • 자바 공부로 JPA를 시작했다. DB 최신기술 부터 공부한 다음에 모르는 부분들을 정리해서 그 부분들을 이후에 공부하는 방향으로 학습할 예정이다.
    - 이번 주에는 일단
    - 영속성 관리
    - 엔티티 매핑 (데이터베이스 스키마 자동 생성, 필드 & 컬럼 매핑, 기본키 매핑 전략)
    - 연관관계 매핑 (단방향, 양방향)
    - 프록시, 지연 로딩
    - 영속성 전이
    까지 공부했다.

Problem 🤢

Try 🧚

240630 독서 목록

서평 완료 목록

서평 예정 목록 (읽는 중)

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

독서 예정 목록

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

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

Extras 😀

ABCDE


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

public class Main {

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

            Solution s = new Solution();
            System.out.println(s.solution(getInput(br)));

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

    private static List<List<Integer>> getInput(BufferedReader br) throws IOException {
        String[] tokens = br.readLine().split(" ");
        int len = Integer.parseInt(tokens[0]);
        int numOfNodes = Integer.parseInt(tokens[1]);

        return connect(br, numOfNodes, getNetwork(len));
    }

    private static List<List<Integer>> getNetwork(int len) {
        List<List<Integer>> network = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            network.add(new ArrayList<>());
        }
        return network;
    }

    private static List<List<Integer>> connect(BufferedReader br, int numOfNodes, List<List<Integer>> network) throws IOException {
        for (int i = 0; i < numOfNodes; i++) {
            String[] tokens = br.readLine().split(" ");
            int from = Integer.parseInt(tokens[0]);
            int to = Integer.parseInt(tokens[1]);
            network.get(from).add(to);
            network.get(to).add(from);
        }
        return network;
    }
}

class Solution {

    public int solution(List<List<Integer>> network) {
        Calculator c = new Calculator(network);
        return c.getResult();
    }

    private class Calculator{

        List<List<Integer>> network;
        boolean[] isVisited;
        int result = 0;

        public Calculator(List<List<Integer>> network) {
            this.network = network;
            this.isVisited = new boolean[network.size()];
        }

        public int getResult() {
            for (int i = 0; i < network.size(); i++) {
                isVisited[i] = true;
                DFS(i, 0);
                isVisited[i] = false;
                if (result == 1) break;
            }
            return result;
        }

        private void DFS(int position, int cnt) {
            if (cnt == 4) {
                result = 1;
                return;
            }

            for (int nextFriend : network.get(position)) {
                if(isVisited[nextFriend]) continue;

                isVisited[nextFriend] = true;
                DFS(nextFriend, cnt + 1);
                isVisited[nextFriend] = false;
            }
        }
    }
}

현명한 나이트


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

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.board, ip.knight, ip.targets));

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

    private static Input getInput(BufferedReader br) throws IOException {
        String[] tokens = br.readLine().split(" ");
        int n = Integer.parseInt(tokens[0]);
        int m = Integer.parseInt(tokens[1]);
        int[][] targets = new int[m][2];

        int[][] board = new int[n + 1][n + 1];
        int[] knight = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();


        for (int i = 0; i < targets.length; i++) {
            targets[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        }

        return new Input(board, knight, targets);
    }

    private static class Input {

        int[][] board;
        int[] knight;
        int[][] targets;

        public Input(int[][] board, int[] knight, int[][] targets) {
            this.board = board;
            this.knight = knight;
            this.targets = targets;
        }
    }
}

class Solution {

    public String solution(int[][] board, int[] knight, int[][] targets) {
        Calculator c = new Calculator(board, knight, targets);
        return c.getResult();
    }

    private class Calculator {

        int[][] board;
        boolean[][] isVisited;
        int[] knight;
        int[][] targets;
        int[][] directions = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}};
        int[] result;

        public Calculator(int[][] board, int[] knight, int[][] targets) {
            this.board = board;
            this.isVisited = new boolean[board.length][board[0].length];
            this.knight = knight;
            this.targets = targets;
            this.result = new int[targets.length];
        }

        public String getResult() {
            for (int[] target : targets) {
                board[target[0]][target[1]] = -1;
            }
            BFS();
            return getAnswer(result);
        }

        private String getAnswer(int[] arr) {
            StringBuilder answer = new StringBuilder();
            for (int num : arr) {
                answer.append(num).append(" ");
            }
            return answer.toString();
        }

        private void BFS() {
            Queue<Location> q = new ArrayDeque<>();
            q.offer(new Location(knight[0], knight[1], 0));
            isVisited[knight[0]][knight[1]] = true;

            while (!q.isEmpty()) {
                Location currentLocation = q.poll();
                int currentX = currentLocation.x;
                int currentY = currentLocation.y;
                int currentStep = currentLocation.step;

                if (board[currentX][currentY] == -1) {
                    addResult(currentX, currentY, currentStep);
                }

                for (int[] direction : directions) {
                    int nx = currentX + direction[0];
                    int ny = currentY + direction[1];

                    if (isWithinBoard(nx, ny) && !isVisited[nx][ny]) {
                        isVisited[nx][ny] = true;
                        q.offer(new Location(nx, ny, currentStep + 1));
                    }
                }
            }
        }

        private void addResult(int x, int y, int curStep) {
            for (int i = 0; i < targets.length; i++) {
                int[] candidate = targets[i];

                if (candidate[0] == x && candidate[1] == y) {
                    result[i] = curStep;
                    return;
                }
            }
        }

        private boolean isWithinBoard(int x, int y) {
            return 1 <= x && x < board.length && 1 <= y && y < board[x].length;
        }
        private class Location {
            int x;
            int y;
            int step;

            public Location(int x, int y, int step) {
                this.x = x;
                this.y = y;
                this.step = step;
            }
        }
    }
}
profile
What doesn't kill you, makes you stronger

0개의 댓글