Algorithm/프로그래머스
-
[JAVA/Level1] K번째수Algorithm/프로그래머스 2021. 6. 23. 11:13
문제 설명 배열 array의 i번재 숫자부터 j번째 숫자까지 자르고 정렬했을 때, K번째에 있는 수를 구하려 합니다. 예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면 array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다. 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다. 2에서 나온 배열의 3번째 숫자는 5입니다. 배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return하도록 solution함수를 작성해주세요. 제한 조건 array의 길이는 1이상 100이하입니다. arra..
-
[JAVA/Level1] 완주하지 못한 선수Algorithm/프로그래머스 2021. 6. 22. 17:45
문제 설명 수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return하도록 solution함수를 작성해주세요. 제한 조건 마라톤 경기에 참여한 선수의 수는 1명 이상 100,000명 이하입니다. completion의 길이는 participant의 길이보다 1작습니다. 참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자로 이루어져 있습니다. 참가자 중에는 동명이인이 있을 수 있습니다. 입출력 예 풀이 HashMap자료형은 의 형태로 이루어진 자료형으로 처음 접하게 되었다. ..
-
[JAVA/Level1] x만큼 간격이 있는 n개의 숫자Algorithm/프로그래머스 2021. 6. 14. 21:47
문제 설명 함수 solution은 정수 x와 자연수 n을 입력 받아, x부터 시작해 x씩 증가하는 숫자를 n개 지니는 리스트를 리턴해야 합니다. 다음 제한 조건을 보고, 조건을 만족하는 함수 solution을 완성해주세요. 제한 조건 x는 -10000000이상, 10000000이하인 정수입니다. n은 1000이하인 자연수입니다. 입출력 예 x n answer 2 5 [2,4,6,8,10] 4 3 [4,8,12] -4 2 [-4,-8] 풀이 long타입의 배열을 선언하고, 파라미터로 받은 n의 길이만큼 배열의 길이를 설정한다. 반복문을 이용해, 배열 answer의 길이만큼 answer[i]에 x의 배수를 n번까지 넣는다. 배열 answer를 리턴한다. 코드
-
[JAVA/Level2] 정렬된 수에서 하나의 수 위치 찾기Algorithm/프로그래머스 2021. 6. 11. 23:33
문제 설명 여러 개의 수가 정렬된 순서로 있을 때 특정한 수를 찾으세요. 수의 예: [12, 25, 31, 48, 54, 66, 70, 83, 95, 108] 단순 반복문을 이용하면 수의 개수에 따라 비교 횟수가 증가하는 O(n)의 수행이 이루어진다. 수가 정렬된 상태에서 이진 탐색을 활용할 경우 매번 비교되는 요소의 수가 절반으로 감소될 수 있으므로 O(logN)의 수행으로 원하는 수를 찾을 수 있다. 입출력 예 찾고자하는 수의 위치를 찾을 경우 배열 내 인덱스의 값을 출력하세요. 찾고자하는 수가 존재하지 않을 경우 "찾는 숫자가 존재하지 않습니다."를 출력하세요. 풀이 수가 정렬된 상태이므로 중간의 값을 하나 선택하고, 찾으려는 값이 그보다 크면 범위를 오른쪽으로 그보다 작으면 범위를 왼쪽으로 좁히며..
-
[JAVA/Level1] 최솟값과 최댓값Algorithm/프로그래머스 2021. 6. 11. 23:01
문제 설명 여러 개의 수가 배열에 있을 때 그 중 가장 큰 값과 가장 작은 값을 찾는다. 배열의 몇 번째에 있는지 순서를 찾는다. 반복문을 한번만 사용하여 문제를 해결한다. 수의 예 : [10, 55, 23, 2, 79, 101, 16, 82, 30, 45] 풀이 1. 배열 내 인덱스 0에 위치한 값을 maxNumber변수와 minNumber변수에 저장한다. 2. 배열의 크기만큼 반복문을 이용하여 더 큰수나 작은 수가 나올 경우 maxNumber의 값과 minNumber의 값을 변경시켜준다. 3. 변경될 경우 해당 인덱스의 위치를 저장한다. 코드
-
[JAVA/Level1] 문자열을 정수로 바꾸기Algorithm/프로그래머스 2021. 6. 1. 23:49
문제 설명 문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요. 제한 조건 s의 길이는 1 이상 5이하입니다. s의 맨앞에는 부호(+, -)가 올 수 있습니다. s는 부호와 숫자로만 이루어져있습니다. s는 "0"으로 시작하지 않습니다. 입출력 예 예를들어 str이 "1234"이면 1234를 반환하고, "-1234"이면 -1234를 반환하면 됩니다. str은 부호(+,-)와 숫자로만 구성되어 있고, 잘못된 값이 입력되는 경우는 없습니다. 풀이 1. String을 int로 변경하기 위하여 Integer.parseInt를 사용하였다. 2. 변경된 값을 answer에 저장하고, 해당 값을 리턴하였다. 코드
-
[JAVA/Level1] 수박수박수박수박수박수?Algorithm/프로그래머스 2021. 6. 1. 23:39
문제 설명 길이가 n이고, "수박수박수박..."와 같은 패턴을 유지하는 문자열을 리턴하는 함수, solution을 완성하세요. 예를들어 n이 4이면 "수박수박"을 리턴하고 3이라면 "수박수"를 리턴하면 됩니다. 제한 조건 n은 길이 10,000이하인 자연수입니다. 입출력 예 n return 3 "수박수" 4 "수박수박" 풀이 1. 파라미터 값 n개 만큼 수박을 반복하기 위해 StringBuilder를 사용하여 answer변수를 선언한다. 2. for문을 사용하여 n까지 반복을 수행한다. 3. 삼항연산자를 사용하여 i가 홀수인 경우 "수"를 추가하고, 아닐경우 "박"을 추가한다. 4. answer값을 리턴한다. 코드