Algorithms/- 프로그래머스
-
프로그래머스 - 가장 큰 수 [자바]Algorithms/- 프로그래머스 2022. 2. 17. 23:34
문제 링크 : 가장 큰 수 코딩테스트 연습 - 가장 큰 수 0 또는 양의 정수가 주어졌을 때, 정수를 이어 붙여 만들 수 있는 가장 큰 수를 알아내 주세요. 예를 들어, 주어진 정수가 [6, 10, 2]라면 [6102, 6210, 1062, 1026, 2610, 2106]를 만들 수 있고, 이중 가장 큰 programmers.co.kr 정답(Solution) import java.util.*; class Solution { public String solution(int[] numbers) { String[] arr = Arrays.stream(numbers) .boxed() .map(String::valueOf) .sorted((a, b) -> (b+a).compareTo(a+b)) .toArray(S..
-
프로그래머스 - 평균 구하기 [자바]Algorithms/- 프로그래머스 2022. 2. 17. 01:19
문제 링크 : 평균 구하기 코딩테스트 연습 - 평균 구하기 정수를 담고 있는 배열 arr의 평균값을 return하는 함수, solution을 완성해보세요. 제한사항 arr은 길이 1 이상, 100 이하인 배열입니다. arr의 원소는 -10,000 이상 10,000 이하인 정수입니다. 입출력 예 arr programmers.co.kr 정답(Solution) class Solution { public double solution(int[] arr) { double answer = 0; double sum = 0; for(int num : arr){ sum += num; } answer = sum / arr.length; return answer; } } 분석 단순사칙 연산 double 자료형 아는지 묻는 문제
-
프로그래머스 - 카카오 프렌즈 컬러링북 [자바]Algorithms/- 프로그래머스 2022. 2. 14. 22:30
문제 링크 : 카카오 프렌즈 컬러링북 코딩테스트 연습 - 카카오프렌즈 컬러링북 6 4 [[1, 1, 1, 0], [1, 2, 2, 0], [1, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 3], [0, 0, 0, 3]] [4, 5] programmers.co.kr 정답 class Solution { private int[] x = {1, -1, 0, 0}; private int[] y = {0, 0, 1, -1}; private int temp = 0; public int[] solution(int m, int n, int[][] picture) { int numberOfArea = 0; int maxSizeOfOneArea = 0; boolean[][] v = new boolean[m..
-
프로그래머스 - 최대공약수와 최소공배수 [자바]Algorithms/- 프로그래머스 2022. 2. 13. 23:35
문제 링크 : 최대공약수와 최소공배수 코딩테스트 연습 - 최대공약수와 최소공배수 두 수를 입력받아 두 수의 최대공약수와 최소공배수를 반환하는 함수, solution을 완성해 보세요. 배열의 맨 앞에 최대공약수, 그다음 최소공배수를 넣어 반환하면 됩니다. 예를 들어 두 수 3, 12의 programmers.co.kr 정답(Solution) import java.util.*; class Solution { public int[] solution(int n, int m) { int[] answer = new int[2]; answer[0] = maxSame(n, m); answer[1] = minSame(n, m); return answer; } private int maxSame(int first, int ..
-
프로그래머스 - 콜라츠 추측 [자바]Algorithms/- 프로그래머스 2022. 2. 12. 01:49
문제 링크 : 콜라츠 추측 코딩테스트 연습 - 콜라츠 추측 1937년 Collatz란 사람에 의해 제기된 이 추측은, 주어진 수가 1이 될때까지 다음 작업을 반복하면, 모든 수를 1로 만들 수 있다는 추측입니다. 작업은 다음과 같습니다. 1-1. 입력된 수가 짝수라면 2 programmers.co.kr 정답(Solution) class Solution { public int solution(int num) { if(num == 1){ return 0; } long n = (long) num; for(int i=0; i
-
프로그래머스 - 제일 작은 수 제거하기 [자바]Algorithms/- 프로그래머스 2022. 2. 10. 22:09
문제 링크 : 제일 작은 수 제거하기 코딩테스트 연습 - 제일 작은 수 제거하기 정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요. 단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 -1을 채워 리턴하세요. 예를들어 arr이 [4,3,2,1 programmers.co.kr 정답(Solution) class Solution { public int[] solution(int[] arr) { if(arr.length == 1){ return new int[]{-1}; } int[] answer = new int[arr.length - 1]; int min = Integer.MAX_VALUE; int idx = 0; for(int i=0; i
-
프로그래머스 - 단어 변환 [자바]Algorithms/- 프로그래머스 2022. 2. 8. 23:19
문제 링크 : 단어 변환 코딩테스트 연습 - 단어 변환 두 개의 단어 begin, target과 단어의 집합 words가 있습니다. 아래와 같은 규칙을 이용하여 begin에서 target으로 변환하는 가장 짧은 변환 과정을 찾으려고 합니다. 1. 한 번에 한 개의 알파벳만 바꿀 수 programmers.co.kr 정답(Solution) 1. 재귀를 사용한 풀이 import java.util.*; class Solution { private List iList = new ArrayList(); public int solution(String begin, String target, String[] words) { boolean f = false; for(String str : words){ if(target..
-
프로그래머스 - 단체사진 찍기 [자바] (feat. += , + 차이)Algorithms/- 프로그래머스 2022. 2. 6. 22:18
문제 링크 : 단체사진 찍기 코딩테스트 연습 - 단체사진 찍기 단체사진 찍기 가을을 맞아 카카오프렌즈는 단체로 소풍을 떠났다. 즐거운 시간을 보내고 마지막에 단체사진을 찍기 위해 카메라 앞에 일렬로 나란히 섰다. 그런데 각자가 원하는 배치가 모두 programmers.co.kr 정답 class Solution { private int answer = 0; private String[] arr = {"A", "C", "F", "J", "M", "N", "R", "T"}; public int solution(int n, String[] data) { boolean[] v = new boolean[arr.length]; dfs("", v, data); return answer; } private void df..