목록코딩테스트 대비 (60)
채린씨의 티스토리
문제 설명 함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다. 제한 조건 n은 1이상 8000000000 이하인 자연수입니다. 입출력 예 n return 118372 873211 나의 풀이 function solution(n) { // 정수 -> 문자열 -> 배열 let arrN = String(n).split(""); // 배열 내림차순 정렬 arrN.sort((a, b) => b - a); // 배열 -> 문자열 -> 정수 return parseInt(arrN.join("")); } 좋았던 다른 사람의 풀이 정수 + "" 을 이용해 문자열로 형 변환을 한 것과..
문제 설명 임의의 양의 정수 n에 대해, n이 어떤 양의 정수 x의 제곱인지 아닌지 판단하려 합니다. n이 양의 정수 x의 제곱이라면 x+1의 제곱을 리턴하고, n이 양의 정수 x의 제곱이 아니라면 -1을 리턴하는 함수를 완성하세요. 제한 사항 n은 1이상, 50000000000000 이하인 양의 정수입니다. 입출력 예 n return 121 144 3 -1 입출력 예 설명 입출력 예#1 121은 양의 정수 11의 제곱이므로, (11+1)를 제곱한 144를 리턴합니다. 입출력 예#2 3은 양의 정수의 제곱이 아니므로, -1을 리턴합니다. 나의 풀이 function solution(n) { // n의 제곱근값을 x에 저장 let x = Math.sqrt(n); // x가 정수이면 x+1의 제곱 반환, x가..
문제 설명 정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요. 단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 -1을 채워 리턴하세요. 예를들어 arr이 [4,3,2,1]인 경우는 [4,3,2]를 리턴 하고, [10]면 [-1]을 리턴 합니다. 제한 조건 arr은 길이 1 이상인 배열입니다. 인덱스 i, j에 대해 i ≠ j이면 arr[i] ≠ arr[j] 입니다. 입출력 예 arr return [4,3,2,1] [4,3,2] [10] [-1] 나의 풀이 function solution(arr) { // arr에서 가장 최솟값의 위치를 찾아 해당 위치의 값을 삭제 arr.splice(arr.indexOf(Math.min(...arr)), 1);..
문제 설명 두 수를 입력받아 두 수의 최대공약수와 최소공배수를 반환하는 함수, solution을 완성해 보세요. 배열의 맨 앞에 최대공약수, 그다음 최소공배수를 넣어 반환하면 됩니다. 예를 들어 두 수 3, 12의 최대공약수는 3, 최소공배수는 12이므로 solution(3, 12)는 [3, 12]를 반환해야 합니다. 제한 사항 두 수는 1이상 1000000이하의 자연수입니다. 입출력 예 n m return 3 12 [3, 12] 2 5 [1, 10] 입출력 예 설명 입출력 예 #1 위의 설명과 같습니다. 입출력 예 #2 자연수 2와 5의 최대공약수는 1, 최소공배수는 10이므로 [1, 10]을 리턴해야 합니다. 나의 풀이 function solution(n, m) { // 최대공약수 구하기 let gc..
Problem You are given two arrays each of size n, a and b consisting of the first n positive integers each exactly once, that is, they are permutations. Your task is to find the minimum time required to make both the arrays empty. The following two types of operations can be performed any number of times each taking 1 second: In the first operation, you are allowed to rotate the first array clock..
Problem You are given an array A which consists of N elements. Each element of array is either zero or one. Your task is to convert the given array into a good array with a minimum cost. Good array: In a good array, select any subarray of even length M and the sum of elements in the subarray will be M/2. In order to transform an array to good array, you can perform the following two operations a..
Problem A zoo have two types of creatures, type A has a hands and type B has b hands. Spawn the smallest number of creatures so they can grab each other hands in the following conditions: Each creature should only grab the hands of another creature type. Each creature should grab with all of its hands. What is the smallest number of creature needed? Note: It is guaranteed that under the given co..
문제 설명 두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요. 예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다. 제한 조건 a와 b가 같은 경우는 둘 중 아무 수나 리턴하세요. a와 b는 -10,000,000 이상 10,000,000 이하인 정수입니다. a와 b의 대소관계는 정해져있지 않습니다. 입출력 예 a b return 3 5 12 3 3 3 5 3 12 나의 풀이 function solution(a, b) { let answer = 0; for (let i = Math.min(a, b); i