본문 바로가기

프로그래머스6

[프로그래머스] n진수 게임 - 자바(Java), 2018 KAKAO BLIND RECRUITMENT 문제 https://programmers.co.kr/learn/courses/30/lessons/17687 풀이 코드 정수를 특정 진법으로 변환한 문자열을 반환해 주는 convert()메서드를 사용해서 t번째 튜브의 순서가 포함된 숫자까지 구한 후, 튜브가 말해야 되는 순서의 숫자들을 문자열로 만들어 반환한다. public class Solution { public String solution(int n, int t, int m, int p) { StringBuilder sb = new StringBuilder(); //t번째 튜브의 순서 int limit = m*(t-1) + p; int i = 0; //t번째 튜브의 순서가 포함된 숫자까지만 구한다. while(sb.length() < limit) {.. 2022. 1. 12.
[프로그래머스] 땅따먹기 - 자바(Java), DP(Dynamic Programming) 문제 https://programmers.co.kr/learn/courses/30/lessons/12913 풀이 DFS를 사용해 완전 탐색으로 풀이하면 시간 초과로 실패하기 때문에, DP(Dynamic Programming)를 사용하여 풀이해야 한다. import java.util.*; class Solution { int solution(int[][] land) { int n = land.length; for(int i = 1; i < n; i++) { //land[i][j] : i행 j열로 내려왔을 때의 최대 값 land[i][0] += Math.max(land[i-1][1], Math.max(land[i-1][2], land[i-1][3])); land[i][1] += Math.max(land[i-.. 2022. 1. 10.
[프로그래머스] 최솟값 만들기 - 자바(Java) 문제 https://programmers.co.kr/learn/courses/30/lessons/12941 풀이 두 배열 중 하나는 오름차순 정렬, 남은 하나는 내림차순 정렬한 뒤 같은 인덱스끼리 곱한 합이 최솟값이다. import java.util.*; class Solution { public int solution(int []A, int []B) { int answer = 0; //reverseOrder() 를 사용하려면 래퍼 타입이어야하기 때문에 int[] -> Integer[] 변환 //boxed() : 프리미티브 타입을 래퍼 타입으로 박싱한 스트림을 만들어준다. Integer[] BB = Arrays.stream(B).boxed().toArray(Integer[]::new); Arrays.so.. 2022. 1. 8.
[프로그래머스] 카펫 - 자바(Java) 문제 https://programmers.co.kr/learn/courses/30/lessons/42842 풀이 코드 public class Main { public int[] solution(int brown, int yellow) { //column, row : 노란 격자 컬럼, 행 길이 for(int column = 1; column yellow/column*2 + column*2 + 4 = (yellow/column + column)*2 + 4 = brown */ if(yellow%column == 0 && ((row+column)*2 + 4) == brown) { return new int[] {row+2, column+2}; } } return null; } } 2022. 1. 3.
[프로그래머스] 행렬 테두리 회전하기 - 자바(Java), 구현, 2021 Dev-Matching: 웹 백엔드(상반기) 문제 https://programmers.co.kr/learn/courses/30/lessons/77485 풀이 코드 - 구현 풀이 과정 좌측 상단 테두리 (x1, y1) 위치의 값을 저장해 놓는다. 행렬의 좌측 테두리 -> 하단 테두리 -> 우측 테두리 -> 상단 테두리 순으로 시계 방향으로 행렬의 값을 한칸씩 이동시킨다. 저장해 두었던 좌측 상단 테두리 값을 (x1, y1+1) 위치에 저장한다. class Solution { public int[] solution(int rows, int columns, int[][] queries) { int[] answer = new int[queries.length]; int[][] board = new int[rows+1][columns+1]; int num .. 2021. 10. 21.
[2019 KAKAO BLIND RECRUITMENT] 오픈채팅방 - 자바(Java) 문제 https://programmers.co.kr/learn/courses/30/lessons/42888 풀이 코드 import java.util.*; public class Solution { static class Message { String status; String id; public Message(String status, String id) { this.status = status; this.id = id; } } public String[] solution(String[] record) { //리스트에 메시지의 종류(Enter 또는 Leave)와 id 값을 저장한다. List messages = new ArrayList(); //맵에 아이디와 그에 맞는 닉네임을 저장한다. Map map .. 2021. 10. 15.