Algorithm/Programmers 28

[Lv.3] 여행 경로 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 주어진 항공권을 모두 사용해야함 > BFS보다 DFS 백트래킹이 더 효율적 목적지를 사전순으로 정렬하는 법// 목적지를 사전순으로 정렬하기 Arrays.sort(tickets, (a,b)-> { if(a[0].equals(b[0])) return a[1].compareTo(b[1]); return a[0].compareTo(b[0]); }); import java.util.*;class Solution { static boolean[] visited; static List route; ..

[Lv.3] 단어 변환 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr begin이랑 target이 몇 개의 문자가 다른지 체크begin의 길이만큼 0번째 같고 1, 2번째...다른 words 찾기words에 target 단어 없으면 0 리턴 위처럼 별 생각없이 고민해봤다. 왜 dfs/bfs 파트에 있는 문제일까 고민하다가 gpt에게 도움을 구했다ㅠ 노드: begin과 words의 각 단어.간선: 두 단어가 한 글자만 다르면 연결.목표: begin에서 target까지의 최단 간선 수 → BFS로 레벨(깊이) 카운트.중요 포인트target이 words에 없으면 답은 0 (문제 조건).begin은 배열에 없어도 시작 노드로 큐에 넣으면 ..

[Lv.2] 최댓값과 최솟값 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 문자열 s 를 공백을 기준으로 분리하기하나씩 정수 형태로 비교하기최댓값, 최솟값 저장하기 import java.util.*;class Solution { // 최솟값, 최댓값 반환하기 static int max = Integer.MIN_VALUE; static int min = Integer.MAX_VALUE; public String solution(String s) { String answer = ""; String[] string = s.split(" "); // 공백으로 분리하기 ..

[Lv.2] 타겟 넘버 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr class Solution { static int target; static int cnt = 0; public int solution(int[] numbers, int target) { // DFS, 백트래킹 > 만들 수 있는 숫자 다 만들어보기 > target이랑 일치하면 카운트 this.target = target; dfs(numbers,0, 0); return cnt; } public static void dfs(int[] numbers, int idx, int..

[Lv.2] 모음 사전 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 만들 수 있는 문자열이 몇 개 없으니까 다 만들어보기DFS + 백트래킹 > 원하는 단어랑 일치하면 cnt 리턴 이미 찾은 경우 재귀 멈추는 flag 유의하기 : 조기 중단해야 하는 경우class Solution { static final char[] a = {'A', 'E', 'I', 'O', 'U'}; static String word; static int result = 0; public int solution(String word) { // DFS 백트래킹 이용 > AEIOU 순으로 깊이를 1~5로 확장하면서 단어 하나..

[Lv.2] 전력망을 둘로 나누기 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 트리 + BFSimport java.util.*;import java.io.*;class Solution { static boolean visited[]; public int solution(int n, int[][] wires) throws Exception { int answer = Integer.MAX_VALUE; // 최소 구해야하니까 MAX로 설정 // 트리 구성하기 ArrayList[] tree = new ArrayList[n+1]; // 노드는 1부터 시작하니까 n+1 ..

[Lv.2] 카펫 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 노란 격자(내부)의 가로·세로를 (w, h)라고 두고, 갈색 격자는 테두리 한 줄이므로 최종 카펫 크기는 (w+2, h+2)입니다.조건은w * h = yellow(w+2) * (h+2) = brown + yellow을 만족하는 (w, h)를 찾아 (w+2, h+2)를 반환class Solution { // w * h = yellow일 때 (w+2)*(h+2) = brown+yellow public int[] solution(int brown, int yellow) { int[] answer = {}; int tota..

[Lv.2] 게임 맵 최단거리 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 2번째 풀이import java.util.*;class Solution { static int[] dx = {-1,1,0,0}; static int[] dy = {0,0,-1,1}; static int[][] dist; static int N, M; static int[][] maps; // 0:벽, 1:이동가능 public int solution(int[][] maps) { int answer = 0; this.maps = maps; N = maps.length; M =..

[Lv.2] 프로세스 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 해당 순서(인덱스)가 몇 번째로 실행되는지 알아야 하니까 큐에 인덱스도 같이 넣기import java.util.*;import java.io.*;class Solution { public int solution(int[] priorities, int location) { int answer = 0; ArrayDeque q = new ArrayDeque(); // 큐 채우기 for(int i=0; i 실행 if(!flag){ answer++; ..

728x90