Algorithm 121

[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 최단 거리 구하기 > dist[][] 이용import java.io.*;import java.util.*;class Solution { static int N, M; static int[][] dist; // 거리 저장, 미방문이면 0 static int[] dx = {-1,1,0,0}; static int[] dy = {0,0,-1,1}; static int answer = 0; public int solution(int[][] maps) throws Exception{ N = maps.length; ..

[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++; ..

[Lv.2] 괄호 회전하기 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.krimport java.io.*;import java.util.*;class Solution { public int solution(String s) { int answer = 0; // 올바른 괄호 문자열 개수 // x칸만큼 회전시키기 int x = s.length(); while(x>0){ // 올바른 괄호인지 판별하기 > 큐 사용 > 짝 맞추기 if(isValid(s)) answer++; // 회전시키..

[Lv.2] 문자열 압축 : Java

프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr import java.util.*;class Solution { public int solution(String s) { int n = s.length(); if(n==1) return 1; // 문자열 길이가 1인 경우 바로 1 리턴 int answer = n; // 초기값은 원본 길이로 설정 // 문자 조각 단위를 1~n/2까지 시도 for(int k=1; k1) sb.append(cnt); sb.append(pre); ..

[Lv.2] 다리를 지나는 트럭 : Java

https://school.programmers.co.kr/learn/courses/30/lessons/42583 프로그래머스SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 모든 트럭이 다리를 건너려면 최소 몇 초?최대 bride_length대 올라갈 수 있음다리는 최대 weight 무게까지 가능 다리 길이만큼 이동해야하니까 > 초반에 큐를 다리 길이만큼 0으로 채워서 시작하기import java.util.*;class Solution { public int solution(int bridge_length, int weight, int[] truck_weights) { ArrayDeque bridge = ..

728x90