본문 바로가기

코딩테스트/Programmers

(43)
[JAVA] N개의 최소공배수 class Solution { public int solution(int[] arr) { int answer = arr[0]; for (int i = 1; i < arr.length; i++) { answer = lcm(answer, arr[i]); } return answer; } // 최대공약수 static int gcd(int a, int b) { if (a % b == 0) { return b; } return gcd(b, a % b); } // 최소공배수 static int lcm(int a, int b) { return a * b / gcd(a, b); } } ✏️ 풀이 과정 처음 0번째 인덱스와 1번째 인덱스의 최소공배수를 구하고, 그 최소공배수와 그 다음 인덱스와의 최소공배수를..
[JAVA] 카펫 class Solution { public int[] solution(int brown, int yellow) { int width=0,length=0; for(int i=3;i ✏️ 풀이 과정 1. i (세로의 길이) 를 3부터 시작하여 반복문을 돈다.2. 가로의 길이는 (brown+yellow) / 세로의 길이   => brown+yellow가 전체 칸의 개수이다. 예를 들어 brown이 10, yellow 가 2라고 했을 때 전체 칸의 개수, 즉 카펫의 넓이가 12이다.    가로*세로 (직사각형의 넓이) 가 12가 되어야하므로 ( 1,12 ), ( 2, 6 ), ( 3,4 ),( 4,3 ),( 6,2 ),( 12,1 )  중에서 조건에 맞는 가로 세로 ..
[JAVA] 짝지어 제거하기 import java.util.*; class Solution { public int solution(String s) { Stackstack=new Stack(); // char 형 스택 생성 for(char c:s.toCharArray()){ // 문자열을 char 형으로 쪼개기 if(!stack.isEmpty() && stack.peek()==c) // 스택이 비어있지 않고, 스택의 top이 c이면 stack.pop(); // 스택에서 빼기 else stack.push(c); // 스택에 값 넣기 } return stack.isEmpty() ? 1 : 0; // 스택이 비어있으면 1, 비어있지 않으면 0 리턴 } } ✏️ 풀이 과정 1. 스택 생성 2. s= "baabaa"의 경우 : ..
[JAVA] 피보나치 수 class Solution { public int solution(int n) { int arr[]=new int[100001]; //배열 값[100000]에 접근하려면 크기가 1이라도 더 커야함 arr[0]=0; arr[1]=1; for(int i=2;i
[JAVA] 다음 큰 숫자 class Solution { static String binary; static int count_1; static int count_1(int num){ binary=Integer.toBinaryString(num); binary=binary.replace("0",""); //0을 제거하기 count_1=binary.length(); //1만 남았으므로 1의 개수==문자열의 길이 return count_1; // 개수 반환 } public int solution(int n) { int answer = 0; int num=count_1(n); // n의 1의 개수 세기 for(int i=n+1;i 1만 남음 3. 문자열의 길이 (1의 개수) 를 반환한다. - main : n의 1의 개수를 ..
[JAVA] 숫자의 표현 class Solution { public int solution(int n) { int answer = 0; for(int i=1;i sum이 n보다 커지면 반복문② 종료, 반복문 ①의 다음 i 값으로 넘어감 - 자기 자신일 경우를 포함해야 하므로 answer에 1을 더한 후 return 해준다. ✍🏻 느낀점 이번 문제는 다른 사람들 풀이를 봤을 때도 다 비슷하게 푼 것 같다. 이러한 비슷한 문제를 백준에서 푼 적이 있는 것 같은데, 처음에는 어? 이거 어떻게 풀었더라? 했지만 금방 풀이 방법이 생각나서 비교적 쉽게 문제를 해결할 수 있었다. "연속된 자연수" 라는 것에 초점을 맞추면 쉽게 풀 수 있는 문제 같다. 항상 느끼는 거지만, 문제를 꼼꼼히 정확하게 읽는게 코테에서 가장 중요한 것..
[JAVA] 이진 변환 반복하기 class Solution { static int removeNum=0; static String remove_0(String s){ //0을 제거하는 함수 String str=""; for(int i=0;i class Solution { public int[] solution(String s) { int[] answer = new int[2]; int temp; while( !s.equals("1") ) { answer[1] += s.length(); s = s.replaceAll("0", ""); temp = s.length(); s = Integer.toBinaryString(temp); answer[0]++; // 이진 변환의 횟수 answer[1] -= temp; // 제거한 0..
[JAVA] JadenCase 문자열 만들기 import java.util.*; class Solution { public String solution(String s) { StringBuilder sb=new StringBuilder(s); boolean isEmpty=false; for(int i=0;i class Solution { public String solution(String s) { String answer = ""; String[] sp = s.toLowerCase().split(""); boolean flag = true; for(String ss : sp) { answer += flag ? ss.toUpperCase() : ss; flag = ss.equals(" ") ? true : false; } retur..