월 17,600원
5개월 할부 시다른 수강생들이 자주 물어보는 질문이 궁금하신가요?
- 미해결자바 코딩테스트 - it 대기업 유제
알파코드 질문드립니다.
import java.io.*; import java.util.*; public class Main { public static int[] dy; public int solution(String s){ int answer = 0; dy= new int[101]; answer = dfs(0,s); return answer; } public static int dfs(int s, String str) { if(dy[s]>0 ) return dy[s]; if(s<str.length() && str.charAt(s)=='0') return 0; if(s==str.length()-1 || s==str.length()) return 1; else { int res = dfs(s+1, str); String num = str.substring(s,s+2); if(Integer.parseInt(num)>26) return 0; //26보다 크면 리턴 else return res += dfs(s+2, str); //그렇지 않다면 재귀 } } public static void main(String[] args){ Main T = new Main(); System.out.println(T.solution("25114")); System.out.println(T.solution("23251232")); System.out.println(T.solution("21020132")); System.out.println(T.solution("21350")); System.out.println(T.solution("120225")); System.out.println(T.solution("232012521")); } } 이 코드에서 if(Integer.parseInt(num)>25) return 0 코드랑if(Integer.parseInt(num)<=26) res+=dfs(s+2, str)랑 차이가 있나요?? num이 25보다 크면 리턴해주고 그렇지 않을때 res+=dfs(s+2, str)해준다는 거랑 다른 의미인가요??
- 해결됨자바 코딩테스트 - it 대기업 유제
청소 관련 문제 질문
안녕하세요. 강의 잘 보고 있습니다.다름이 아니라 제가 코드를 작성해보다가if(nx < 0 || ny < 0 || nx >= n || ny >= n || board[nx][ny] == 1){ d=(d+1)%4; continue; }이 부분을 다르게 작성했더니 범위를 벗어났다는 오류가 떴습니다.오류가 뜬 코드는 다음(아래 코드블럭)과 같습니다.if(board[nx][ny] == 1|| nx < 0 || ny < 0 || nx >= n || ny >= n ){ d = (d + 1) % 4; continue; }오류 내용은 이렇습니다.Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at Solution2.solution(Solution2.java:13) at Solution2.main(Solution2.java:32) 요약하면 다른 모든 게 같아도, if문 안의 순서로 결과가 다르게 나온 것인데,여기에 질문이 있습니다.왜 board[nx][ny] == 1 부분을 맨 앞으로 보냈더니 오류가 뜨는 걸까요?or연산에서 조건1 or 조건2일 때 조건1이 참이라면 조건2는 확인하지 않는 것과 연관이 있는 걸까요? 검색이나 고민해봐도 잘 모르겠습니다. 답변부탁드립니다!
- 미해결자바 코딩테스트 - it 대기업 유제
BFS로 풀이
강사님, 안녕하세요. 강의 잘 듣고 있습니다.공굴리기 문제를 간선의 가중치가 모두 1이라고 간주하고,한 칸씩 이동하면서 distance[다음 정점] = distance[현재 정점] + 1 을 해주는 식으로 BFS를 활용해 풀어도 되는지 궁금합니다.다익스트라와 BFS 2 개 방식의 효율성(시간복잡도) 차이는 어떻게 되나요?
- 해결됨자바 코딩테스트 - it 대기업 유제
잃어버린 강아지 방향 문제
문제에서 북쪽(지도에서 위쪽)으로 이동한다고 했는데북쪽으로 이동이면 이동하게되는 처음 방향은x = 0 , y = -1 이 아닌가요? x = -1 , y = 0 인 이유를 모르겠습니다.
- 미해결자바 코딩테스트 - it 대기업 유제
다이나믹 프로그래밍 알고리즘에 대해 질문드립니다.
문제를 보고 이 문제가 dp문제다라는 것을 어떻게 알고 진행하는지 궁금합니다. 다양한 알고리즘이 존재하는데, 따로 문제에서 dp문제라는 주목할만한 힌트가 있나요???
- 미해결자바 코딩테스트 - it 대기업 유제
바둑대회 질문드립니다.
import java.util.*; import javax.security.auth.Subject; import java.awt.List; import java.io.*; class Main { public static boolean[] visit; public static int len,answer=Integer.MAX_VALUE; public int solution(int[][] cans){ len=cans.length; visit =new boolean[len]; dfs(0,0,cans); return answer; } public static void dfs(int L,int s,int[][] cans) { if(L==len/2) { ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i=0; i<len; i++) { if(visit[i]) a.add(i); else b.add(i); } int sum1=0,sum2=0; for(int i=0; i<L; i++) { sum1+=cans[a.get(i)][0]; sum2+=cans[b.get(i)][1]; } answer =Math.min(answer, Math.abs(sum1-sum2)); } else { for(int i=s; i<len; i++) { visit[i] = true; dfs(L+1, i+1,cans); visit[i] = false; } } } public static void main(String[] args){ Main T = new Main(); System.out.println(T.solution(new int[][]{{87, 84}, {66, 78}, {94, 94}, {93, 87}, {72, 92}, {78, 63}})); System.out.println(T.solution(new int[][]{{10, 20}, {15, 25}, {35, 23}, {55, 20}})); System.out.println(T.solution(new int[][]{{11, 27}, {16, 21}, {35, 21}, {52, 21}, {25, 33},{25, 32}, {37, 59}, {33, 47}})); } }이렇게 작성했는데 마지막 테스트 케이스가 1이 아닌 0이 나옵니다. 어디가 잘못된건지 모르겠습니다.
- 미해결자바 코딩테스트 - it 대기업 유제
알파코드 질문드립니다.
이렇게 작성하니까 오류가 나는데 뭐가 잘못된건지 모르겠습니다.import java.util.*; import javax.security.auth.Subject; import java.awt.List; import java.io.*; class Main { public static int[]dy; public int solution(String s){ int answer = 0; dy = new int[101]; answer = dfs(0,s); return answer; } public static int dfs(int s, String str) { if(dy[s]>0) return dy[s]; if(str.charAt(s)=='0' && s<str.length()) return 0; if(s==str.length() || s==str.length()-1) return 1; else { int res = dfs(s+1, str); int num = Integer.parseInt(str.substring(s,s+2)); if(num<=26) res+=dfs(s+2, str); return dy[s] = res; } } public static void main(String[] args){ Main T = new Main(); System.out.println(T.solution("25114")); System.out.println(T.solution("23251232")); System.out.println(T.solution("21020132")); System.out.println(T.solution("21350")); System.out.println(T.solution("120225")); System.out.println(T.solution("232012521")); } }
- 미해결자바 코딩테스트 - it 대기업 유제
IP주소 질문드립니다.
코드에서if(Integer.parseInt(num) > 255) return;이 부분이 일종의 메모이제이션 역할을 하니까 알파코드 문제처럼 따로 배열을 잡아서 방문처리를 안하는건가요???
- 미해결자바 코딩테스트 - it 대기업 유제
알파코드 질문드립니다.
import java.util.*; import javax.security.auth.Subject; import java.awt.List; import java.io.*; class Main { public static int[] dy; public int solution(String s){ int answer = 0; dy = new int[101]; answer = dfs(0,s); return answer; } public static int dfs(int s, String str) { if(dy[s]>0) return dy[s]; //메모이제이션 //if(s<str.length() && str.charAt(s)=='0') return 0; if(s==str.length() || s==str.length()-1) return 1; if(s<str.length() && str.charAt(s)=='0') return 0; else { int res = dfs(s+1, str); int num = Integer.parseInt(str.substring(s, s+2)); if(num<=26) res+=dfs(s+2, str); return dy[s] = res; } } public static void main(String[] args){ Main T = new Main(); System.out.println(T.solution("25114")); System.out.println(T.solution("23251232")); System.out.println(T.solution("21020132")); System.out.println(T.solution("21350")); System.out.println(T.solution("120225")); System.out.println(T.solution("232012521")); } } if(s<str.length() && str.charAt(s)=='0') return 0; 이 구문을 if(s==str.length() || s==str.length()-1) return 1;위 아래에 배치하는 순서에 따라 답이 다르던데왜 그런거죠???
- 미해결자바 코딩테스트 - it 대기업 유제
혹시 이 문제가 문자를 지우거나 추가헤서 라고 출제된다면 어떻게 풀어야할까요 ?
소문자로 이루어진 문자열이 주어지면 해당 문자열의 문자를 지우거나 추가하여 모든 문자의 빈도수가 서 로 다르게 만들려고 합니다. 만약 주어진 문자열이 매개변수 s에 문자열이 주어지면 s의 모든 문자의 빈도수가 서로 다르도록 하기 위해 지우거나 추가해야 할 최소 개수를 반환하는 프로그램을 작성하세요. 만약 문제가 이렇게 나오다면 어떤 방법으로 풀어야할까요 ..? 생각해봤는데 방법이 안 떠올라서 질문 드려봅니다 .. 이런 문제는 없으려나용 ,,
- 미해결자바 코딩테스트 - it 대기업 유제
피부과 질문드립니다.
기존의 조건문 코드인if(pos < n && t == inList[pos][0]){ if(Q.isEmpty() && t > fT) fT= t; Q.offer(inList[pos][1]); pos++; }이 부분을while(pos<n && list[pos][0]==t) { if(q.isEmpty() && t>ft) ft = t; q.offer(list[pos][1]); pos++; }이처럼 반복문으로 코드구현해도 되나요??답은 맞게 나옵니다.
- 미해결자바 코딩테스트 - it 대기업 유제
바둑대회 질문드립니다.
이처럼 코드를 작성했는데 마지막 테스트 케이스 답이 0이 나옵니다. 어디가 잘못된건지 모르겠습니다. import java.util.*; import javax.security.auth.Subject; import java.awt.List; import java.io.*; class Main { public static int n,answer=10000000; public static boolean[] visit; public int solution(int[][] cans){ n=cans.length; visit = new boolean[n]; dfs(0,0,cans); return answer; } public static void dfs(int L, int s, int[][] cans) { if(L==n/2) { ArrayList<Integer> a =new ArrayList<>(); ArrayList<Integer> b =new ArrayList<>(); for(int i=0; i<n; i++) { if(visit[i]) a.add(i); else b.add(i); } int sum1=0,sum2=0; for(int i=0; i<L; i++) { sum1+=cans[a.get(i)][0]; sum2+=cans[b.get(i)][1]; } answer = Math.min(answer, Math.abs(sum1-sum2)); } else { for(int i=s; i<n; i++) { visit[i] = true; dfs(L+1, i+1, cans); visit[i] = false; } } } public static void main(String[] args){ Main T = new Main(); System.out.println(T.solution(new int[][]{{87, 84}, {66, 78}, {94, 94}, {93, 87}, {72, 92}, {78, 63}})); System.out.println(T.solution(new int[][]{{10, 20}, {15, 25}, {35, 23}, {55, 20}})); System.out.println(T.solution(new int[][]{{11, 27}, {16, 21}, {35, 21}, {52, 21}, {25, 33},{25, 32}, {37, 59}, {33, 47}})); } }
- 미해결자바 코딩테스트 - it 대기업 유제
회의실 만남(구현부분)과 현관문 출입순서(자료구조) 질문드립니다.
회의실 만남은 구현으로 풀고 현관문 출입순서는 큐로 풀었는데, 회의실 만남은 큐로 못 푸나요??다른 차이가 있을까요???
- 미해결자바 코딩테스트 - it 대기업 유제
피부과 문제 질문입니다.
강의 해설 잘 보았습니다!저는 강사님 코드랑 다르게 풀어보았는데 이렇게 풀면 시간초과 문제가 있을까요??public class 피부과 { static int getTime(String time){ int H = Integer.parseInt(time.split(":")[0]) * 60; int M = Integer.parseInt(time.split(":")[1]); return H+M; } public static int solution(int[] laser, String[] enter){ int answer = 0; Queue<Integer> queue = new LinkedList<>(); ArrayList<Integer> info = new ArrayList<>(); for(String x : enter){ int t = getTime(x.split(" ")[0]); int n = Integer.parseInt(x.split(" ")[1]); info.add(laser[n]); queue.add(t); } int idx = 0; while(!queue.isEmpty()){ int temp = queue.poll() + info.get(idx); int res = 0; for(int x : queue){ if(temp > x){ res++; } else break; } answer = Math.max(answer,res); idx++; } return answer; } public static void main(String[] args){ System.out.println(solution(new int[]{30, 20, 25, 15}, new String[]{"10:23 0", "10:40 3", "10:42 2", "10:52 3", "11:10 2"})); System.out.println(solution(new int[]{30, 20, 25, 15}, new String[]{"10:23 0", "10:40 3", "10:42 2", "10:52 3", "15:10 0", "15:20 3", "15:22 1", "15:23 0", "15:25 0"})); System.out.println(solution(new int[]{30, 20, 25, 15}, new String[]{"10:20 1", "10:40 1", "11:00 1", "11:20 1", "11:40 1"})); } }
- 미해결자바 코딩테스트 - it 대기업 유제
구현 - 과일 가져가기 문제
public int getMinIndex(int[] fruit) { int min = getMin(fruit); for(int i = 0; i < 3; i++) { if(fruit[i] == min) return i; } return 0; }아니라public int getMinIndex(int[] fruit) { int min = getMin(fruit); int i; for(int i = 0; i < 3; i++) { if(fruit[i] == min) return i; } return i; }해야 되는거 아닌가요?첫번째 코드처럼 하면 무조건 최솟값인 과일의 인덱스로 0(사과)가 반환되는거 아닌가요??첫번째 코드가 맞는 이유는 처음 등장하는 return이 메소드를 종료시키므로 이후의 구문은 실행되지 않기 때문인가요?!
- 미해결자바 코딩테스트 - it 대기업 유제
이 문제 기본 코드가 답 코드입니닷
기본 코드 파일에서 파일 열었는데 답 코드인 것 같아용
- 미해결자바 코딩테스트 - it 대기업 유제
if-else를 활용한 코드
import java.util.*; import java.io.*; public class P03_잃어버린강아지 { static int[] dx = {-1, 0, 1, 0}; static int[] dy = {0, 1, 0, -1}; public static int solution(int[][] board){ int px = 0; int py = 0; int ax = 0; int ay = 0; int pd = 0; int ad = 0; for (int i=0; i<board.length; i++) { for (int j=0; j<board.length; j++) { if (board[i][j]==2) { px = i; py = j; } else if (board[i][j]==3) { ax = i; ay = j; } } } int answer = 0; for (int i=0; i<10000; i++) { int npx = px + dx[pd%4]; int npy = py + dy[pd%4]; if (!(0<=npx && npx<10 && 0<=npy && npy<10) || board[npx][npy]==1) { pd++; } else { px = npx; py = npy; } int nax = ax + dx[ad%4]; int nay = ay + dy[ad%4]; if (!(0<=nax && nax<10 && 0<=nay && nay<10) || board[nax][nay]==1) { ad++; } else { ax = nax; ay = nay; } if (px==ax && py==ay) { answer = i+1; break; } } return answer; } public static void main(String[] args){ int[][] arr1 = {{0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 2, 0, 0}, {1, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 3, 0, 0, 0, 1}, {0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 0, 0, 0, 0, 0}}; System.out.println(solution(arr1)); int[][] arr2 = {{1, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 1, 1, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 2, 1}, {0, 0, 0, 1, 0, 1, 0, 0, 0, 1}, {0, 1, 0, 1, 0, 0, 0, 0, 0, 3}}; System.out.println(solution(arr2)); } } 어차피 1분동안은 이동 혹은 회전 둘 중 하나만 가능한 것 같아 if-else로 풀고따로 boolean flag는 두지 않았는데 이렇게 풀어도 문제 없는거 맞나요 ?혹시 flag 변수를 두었을 때 얻는 이점이 따로 있는지 궁금합니다.
- 해결됨자바 코딩테스트 - it 대기업 유제
[그리디] 스프링쿨러 입출력 예 answer이 잘못된건가요?
안녕하세요스프링쿨러 입출력 예 answer가 잘못된건지, 제가 문제이해를 잘못한건지 하여 질문드립니다입출력 예 3번째 예제에서n=5, nums = {2, 0, 0, 0, 0, 2} 이고 답 answer은 -1로 기재되어 있습니다nums[0] 과 nums[5] 모두 작동시켰을때(-2,2), (3, 7)에 물을 줄 수 있어, nums[0]가 (0, 1, 2 )에 nums[5]가 (3, 4, 5) 에 물을 줄 수 있고연결해보면 (0,5) 모든 잔디밭에 물을 줄 수 있어 답안이 2가 되어야 한다고 생각합니다혹시 제가 잘 못 이해하고 있는건지, 답이 잘못 기재되어 있는건지 답변 부탁드립니다감사합니다!
- 미해결자바 코딩테스트 - it 대기업 유제
바둑대회 PDF답/ 정답코드 답이 다릅니다 ㅠ
바둑대회answer=10000000; 의 위치가PDF 와 정답코드가 서로 다릅니다.위치가 서로 달라도 정답은 일치할 줄 알았는데정답이 다르게 나옵니다.왜 그럴까요 ㅠ
- 미해결자바 코딩테스트 - it 대기업 유제
공 굴리기 질문드립니다.
이렇게 작성하니까 배열 범위가 벗어낫다고 하는데, 어디가 벗어나는지 잘 모르겠습니다. import java.awt.List; import java.util.*; class Node implements Comparable<Node>{ int x; int y; int c; Node(int x, int y, int c){ this.x=x; this.y=y; this.c=c; } @Override public int compareTo(Node o) { return this.c - o.c; } } class Main { public static int n,m; public static int INF = (int)1e9; public static int[][] cost; public static int[] dx = {0,0,1,-1}; public static int[] dy = {1,-1,0,0}; public int solution(int[][] board, int[] s, int[] e){ int answer; int n = board.length; int m = board[0].length; cost = new int[n][m]; for(int i = 0; i < n; i++) Arrays.fill(cost[i],INF); answer = dij(s[0],s[1],board,e[0],e[1]); if(answer == INF) return -1; else return answer; } public static int dij(int s,int e, int [][]board, int e1, int e2) { PriorityQueue<Node> q = new PriorityQueue<>(); q.offer(new Node(s,e,0)); cost[s][e] = board[s][e]; while(!q.isEmpty()) { Node tmp = q.poll(); int nowx = tmp.x; int nowy = tmp.y; int nowcnt = tmp.c; if(nowcnt>cost[nowx][nowy]) continue; for(int i=0; i<4; i++) { int nx = nowx; int ny = nowy; int len =nowcnt; while(nx>=0 && ny>=0 && nx<n && ny<m && board[nx][ny]==0) { nx+=dx[i]; ny+=dy[i]; len++; } nx-=dx[i]; ny-=dy[i]; len--; if(cost[nx][ny]>len) { cost[nx][ny] = len; q.offer(new Node(nx,ny,cost[nx][ny])); } } } return cost[e1][e2]; } public static void main(String[] args){ Main T = new Main(); System.out.println(T.solution(new int[][]{{0, 0, 1, 0, 0, 0}, {0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0}, {1, 0, 1, 1, 1, 0}, {1, 0, 0, 0, 0, 0}}, new int[]{1, 0}, new int[]{4, 5})); System.out.println(T.solution(new int[][]{{0, 0, 1, 0, 0, 0}, {0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0}, {1, 0, 1, 1, 1, 0}, {1, 0, 0, 0, 0, 0}}, new int[]{0, 0}, new int[]{4, 2})); System.out.println(T.solution(new int[][]{{1, 0, 1, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 1, 0}, {1, 1, 0, 1, 1}, {0, 0, 0, 0, 0}}, new int[]{0, 3}, new int[]{4, 2})); System.out.println(T.solution(new int[][]{{0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 1, 1, 0, 1, 1}, {0, 0, 0, 0, 0, 0}}, new int[]{0, 0}, new int[]{4, 5})); System.out.println(T.solution(new int[][]{{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0}}, new int[]{0, 0}, new int[]{4, 3})); } }