프로그래머스

    미로탈출

    미로탈출

    현재 방 == TRAP 이고 이동할 방 == TRAP 인 경우 현재 방 == TRAP 이고 이동할 방 != TRAP 인 경우 현재 방 != TRAP 이고 이동할 방 == TRAP 인 경우 현재 방 != TRAP 이고 이동할 방 != TRAP 인 경우 크게 네가지로 나눠서 생각했다. 그 다음 각 경우 내부에서 현재 방, 이동할 방 중 TRAP이 2개 또는 0개 인 경우와 1개인 경우 이 두가지로 나눠서 풀이 했다. package com.company; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new Bu..

    경주로 건설

    경주로 건설

    내 코드 package com.company; import java.io.*; import java.util.*; public class Solution { public int solution(int[][] board) { int answer = Integer.MAX_VALUE; int N = board.length; // 방향 아래 0, 오른쪽 1, 위 2, 왼쪽 3 int[][] d = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int[][] arr = new int[N][N]; boolean[][][] visited = new boolean[N][N][4]; for (int i = 0; i < N; i++) { Arrays.fill(arr[i], Integer.MAX_VALUE..

    보석 쇼핑

    보석 쇼핑

    풀이 방법 투포인터 방법을 이용해서 풀이했다. set으로 등장하는 보석의 갯수를 구해주고 포인터를 이동하면서 map을 이용해서 보석의 갯수를 세어준다. start 포인터가 이동하면 map에서 갯수를 줄여주고 end 포인터가 이동하면 map에 갯수를 증가시켜주었다. 내 코드 import java.util.*; class Solution { public int[] solution(String[] gems) { int[] answer = {100000, 100000}; HashSet set = new HashSet(Arrays.asList(gems)); HashMap map = new HashMap(); int size = set.size(); int start = 0; int end = 0; int coun..

    2 x n 타일링

    2 x n 타일링

    내 코드 class Solution { public int solution(int n) { int[] dp = new int[n + 1]; dp[0] = 1; dp[1] = 1; for (int i = 2; i

    124 나라의 숫자

    124 나라의 숫자

    class Solution { public String solution(int n) { StringBuilder sb = new StringBuilder(); int temp = 0; while (n > 0){ temp = n % 3; n /= 3; if (temp == 0){ n -= 1; sb.insert(0, "4"); } else { sb.insert(0, temp); } } return sb.toString(); } }

    구명보트

    구명보트

    최대 2명이라는 조건을 보지 못해서 잘못된 풀이를 하고 있었다.. 풀이 방법 정렬해준 다음 최대 2명이라서 가장 작은 사람과 가장 큰 사람을 묶어서 생각했다. 두 사람이 limit를 초과하면 큰사람만 계산해 주었다. 내 코드 import java.util.*; class Solution { public int solution(int[] people, int limit) { int answer = 0; int n = people.length; Arrays.sort(people); int left = 0; int right = n - 1; while(left

    조이스틱

    조이스틱

    내 코드 import java.util.*; class Solution { public int solution(String name) { int answer = 0; int cnt = 0; int len = name.length(); int[] arr = new int[len]; for(int i = 0; i 13) arr[i] = 26 - x; else arr[i] = x; answer += arr[i]; if(arr[i] != 0) cnt++; } int index = 0; int start = 0; if(arr[0] != 0){ start = 1; arr[0] = 0; } for(int i = start; i <..

    등굣길

    등굣길

    내 코드 import java.util.*; class Solution { public int solution(int m, int n, int[][] puddles) { int[][] arr = new int[m + 1][n + 1]; arr[0][1] = 1; for(int i = 0; i< puddles.length; i++){ arr[puddles[i][0]][puddles[i][1]] = -1; } for(int i = 1; i

    N으로 표현

    N으로 표현

    내 코드 import java.util.*; class Solution { public int solution(int N, int number) { int temp = 0; ArrayList list = new ArrayList(); for(int i = 0; i < 9; i++){ list.add(new ArrayList()); list.get(i).add(temp); temp = temp * 10 + N; } for(int i = 1; i < 9; i++){ for(int j = 1; j < i; j++){ for(int k = 0; k < list.get(j).size(); k++){ for(int u = 0; u < list.get(i - j).size(); u++){ list.get(i).add..

    피로도

    피로도

    풀이 방법 dfs를 이용해서 모든 경로를 탐색하는 방법을 선택했다. 내 코드 class Solution { private static int max = -1; private static boolean[] check; public int solution(int k, int[][] dungeons) { check = new boolean[dungeons.length]; dfs(dungeons, k, 0); return max; } private static void dfs(int[][] dungeons, int k, int count){ if(count > max){ max = count; } for(int i = 0; i < dungeons.length; i++){ if(!check[i] && dungeo..