본문 바로가기

Programmers/Level2

(76)
영어 끝말잇기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import java.util.*; class Solution { public int[] solution(int n, String[] words) { int[] answer = { 0, 0 }; Set set = new HashSet(); for (int i = 0; i 0 && words[i - 1].charAt(words[i - 1].length() - 1) != words[i].charAt(0)) { answer[0] = (i + 1) % n == 0 ? n : (i + 1) % n; // 번호 answer[1] = i / n + 1; // 차례 break; } else { set.add(words[i]); ..
가장 큰 정사각형 찾기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class Solution { public int solution(int [][]board) { int answer = 0; int max = 0; // 1행, 1열 체크 for (int i = 0; i
[1차] 캐시 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.util.*; class Solution { public int solution(int cacheSize, String[] cities) { int answer = 0; String[] newCities = new String[cities.length]; Queue queue = new LinkedList(); for (int i = 0; i
삼각 달팽이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 class Solution { public int[] solution(int n) { int[] answer = {}; int[][] snail = new int[n][n]; int i, j; int cnt = 1, row = 0, col = 0; if(n == 1){ answer = new int[1]; answer[0] = 1; return answer; } while (col != n / 2) { // 세로 for (i = row; i
다리를 지나는 트럭 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import java.util.*; class Solution { public int solution(int bridge_length, int weight, int[] truck_weights) { Queue queue = new LinkedList(); Queue time = new LinkedList(); int answer = 0, pos = 0; while (pos
멀쩡한 사각형 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public long solution(long w, long h) { long answer = w * h; long a = w, b = h; // 유클리드 호제법 while (b != 0) { long r = a % b; a = b; b = r; } answer -= w + h - a; return answer; } } Colored by Color Scripter cs 사각형이 색칠되는 규칙은 다음과 같다. 가로 w, 세로 h, 최대 공약수 a라고 했을 때, ( w / a + h / a - 1 ) * a 이다. 즉, w와 h의 비율로 가장 작은 사각형이 a개가 나오고 그 안에서 색칠이 된다. 색칠이 되는..
주식가격 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public int[] solution(int[] prices) { int len = prices.length; int[] answer = new int[len]; for (int i = 0; i
위장 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import java.util.*; class Solution { public int solution(String[][] clothes) { int answer = 1; Map map = new HashMap(); // 옷 종류에 따른 갯수 저장 for (int i = 0; i