알고리즘 풀이/프로그래머스

3진법을 조금 응용한 문제인데 나머지가 0이 되게 떨어지는 구간만 따로 처리해주면 된다 나머지가 0일때 몫을 하나 떨궈주고 나머지를 4로 교체시켜줌 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19class Solution { public String solution(int n) { String answer = ""; int rest=0; while(n>0){ rest = n%3; n /= 3; if(rest == 0) { rest = 4; n-=1; } answer = rest + answer; } return answer; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17class Solution { public static int[] solution(int[] heights) { int N = heights.length; int[] answer = new int[N]; for(int i = N-1; i >= 1; i--) { for(int j = i-1; j >= 0; j--) { if(heights[i]
선행 스킬을 찍기전에 나오면 안되는 스킬이 나온다면 flag를 활용해 false처리해줌 for문을 돌고난 후에도 flag값이 여전히 참일 경우 문제에서 요구하는 스킬트리 조건을 달성하므로 answer를 counting해줌 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 27class Solution { public int solution(String skill, String[] skill_trees) { int answer = 0; for(int i=0; i
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19class Solution { public int[] solution(int[] prices) { int len = prices.length; int[] answer = new int[len]; for(int i = 0; i
문제에서 budget에 딱 맞춰서 부서에 나눠주는 문제인줄 알았지만 그냥 예산 범위 초과만 안하게 나눠주면 되는 문제였음 for문으로 차근차근 밑에서부터 담다가 예산초과하면 break써서 리턴시켜주면됨 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19import java.util.*; class Solution { public int solution(int[] d, int budget) { int answer = 0; int result = 0; Arrays.sort(d); for(int i=0; i budget) { answer = i; break; } } if(result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); for (int i = 0; i
solution 옆의 인자값 x의 클래스를 long으로 바꿔줌 (int->long) 1 2 3 4 5 6 7 8 9class Solution { public long[] solution(long x, int n) { long[] answer = new long[n]; for(int i = 0; i
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15class Solution { public int[][] solution(int[][] arr1, int[][] arr2) { int[][] answer = {}; answer = new int[arr1.length][arr1[0].length]; for(int i = 0; i
div변수에 10으로 나눠가면서 자릿수를 더해줌 문제에서 요구하는대로 자리수를 더한 div가 주어진 값 x의 약수일 경우 true리턴 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16class Solution { public boolean solution(int x) { int div = 0; int minX = x; while(true){ div+=(minX%10); minX/=10; if(minX==0) break; } System.out.println(x+ " "+ div); if(x%div == 0) return true; else return false; } }
for문, if문쓰는 문제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18class Solution { public int solution(int num) { int answer=0; int i=0; for(; i
배게
'알고리즘 풀이/프로그래머스' 카테고리의 글 목록 (11 Page)