Algorithm
-
프로그래머스 - 폰켓몬Algorithm 2023. 5. 25. 18:27
문제 풀이 먼저 nums 배열에서 중복제거한 배열을 만들어 둔다 가져갈 수 있는 절반의 개수가 중복제거된 배열길이보다 작다면 nums의 절반길이를 리턴하고, 아니라면 중복제거된 배열길이 리턴한다 function solution(nums) { // nums 배열 중복제거 let setNums = [...new Set(nums)]; // 가져갈 수 있는 절반이 중복제거된 배열길이보다 작다면 nums의절반길이 리턴 아니라면 중복제거된 배열길이 리턴 return (nums.length/2 < setNums.length) ? Math.floor(nums.length/2) : setNums.length; }
-
프로그래머스 - 성격 유형 검사하기Algorithm 2023. 5. 23. 15:18
문제 풀이 성격 유형의 디폴트 값을 'RCJA'로 지정해준다 각 성격유형 쌍을 이루는 배열 ['RT', 'CF', 'JM', 'AN'] 과 이 배열에 매칭되는 [0, 0, 0, 0] 배열을 선언해준다. 이 배열은 각 인덱스의 값이 음수일 경우 각각 [R, C, J, A] 양수일 경우 [T, F, M, N]을 표현한다 survey 배열을 순회하며 survey[i]에 매칭되는 choices[i]의 값이 4보다 클경우 즉, 동의를 선택한 경우와 비동의를 선택한 경우 두가지로 나눈다 이 때 survey[i]의 값이 typeArr에 존재하는 값이라면 즉, 정렬이 되어 있는 상태라면 survey[i]에 매칭되는 typeNum의 인덱스에 choices[i]-4 를 더해준다 반면 survey[i]의 값이 typeArr..
-
프로그래머스 - 짝지어 제거하기Algorithm 2023. 5. 21. 21:26
문제 풀이 입력받은 문자열 s를 배열로 변환한 뒤 순회하면서 각 요소를 새로운 배열 newArr에 push한다 이 때, newArr의 마지막요소와 배열 s의 현재 요소가 다른 값인 경우에 배열 s의 현재요소를 newArr에 push하고, 같은 값인 경우에는 newArr의 마지막 요소를 pop한다 function solution(s) { let newArr = []; s = s.split(''); for(let i = 0; i < s.length; i++){ // s[i] 값이 newArr배열의 마지막값과 다르다면, newArr 배열에 s[i] push if(s[i] !== newArr[newArr.length-1]) newArr.push(s[i]) // s[i] 값이 newArr배열의 마지막값과 같다면,..
-
프로그래머스 - [1차] 비밀지도Algorithm 2023. 5. 20. 16:55
문제 풀이 먼저 arr1과 arr2의 각요소마다 비트 or 연산을 한 뒤 sumArr 에 추가한다 이때, or 연산된 최상위 비트가 0이라 생략된 경우 n 길이가 될 때까지 최상위 비트에 0을 추가한다 배열의 요소가 1이면 '#', 0이면 ' '으로 변환한다 function solution(n, arr1, arr2) { let answer = []; let sumArr = []; // arr1과 arr2 or 연산하기 for(let i = 0; i < n; i++) { sumArr.push((arr1[i] | arr2[i]).toString(2)); } // 위의 연산결과의 배열 길이가 n보다 작을경우 즉, 최상위 비트가 0이라 생략된 경우 for(let i = 0; i < sumArr.length; i..
-
프로그래머스 - 신규 아이디 추천Algorithm 2023. 5. 15. 19:16
문제 풀이 주어진 문제조건에서 단계별로 친절하게 설명되어 있어 단계별로 진행하였다. 1단계 : new_id 문자열을 소문자로 변환 2단계 : '-', '.', '_'을 제외한 모든 특수문자을 찾아내는 정규식으로 replaceAll을 이용해 특수문자를 제거 3단계 : 문자열에 점이 두번 연속 존재하는 ".." 문자열이 존재하지 않을 때 까지 문자열에서 ".."문자열을 "."로 변환 4단계 : 문자열의 첫 글자나 마지막 글자가 "." 특수문자일 경우 마지막 글자 제거 5단계 : 문자열 길이가 0이라면 문자열에 "a" 추가 6단계 : 문자열의 길이가 15글자를 넘는다면 15글자까지 잘라내고 마지막 글자가 "." 특수기호일 경우 마지막 글자 제거 7단계 : 문자열의 길이가 3보다 작다면 문자열의 길이가 3 이상..
-
프로그래머스 - 과일 장수Algorithm 2023. 5. 12. 16:00
문제 풀이 처음 풀고자 했던 의도는 score 배열을 내림차순으로 정렬한 뒤, 0번째 인덱스부터 m개씩 만큼 뽑아 최솟값을 구한뒤 m을 곱한 값들을 다 더하면 될 거라고 생각했다 function solution(k, m, score) { let answer = 0; let slicedScore = []; // 내림차순 정렬 score.sort((a, b) => b - a); for(let i = 0; i m-1)) { // score 배열의 i번째 인덱스 값부터 i+m-1번째 인덱스 값까지 뽑아 ..
-
프로그래머스 - 신고 결과 받기Algorithm 2023. 5. 12. 14:53
문제 풀이 먼저 한 유저가 같은 유저를 여러번 신고를 해도 1회 신고로 처리하기 위해 report 배열에서 중복제거를 해준다. 이 후 id_list와 매칭할 수 있는 유저당 신고 당한횟수 배열과 유저당 받은 메일 횟수 배열을 만들어 유저당 신고 당한횟수 배열에 유저당 신고당한 횟수를 채운다. 이 후 신고당한 횟수가 정지 조건인 k보다 많은 인덱스에 해당하는 유저당 받은 메일 횟수 배열 인덱스를 증가시킨다. 코드를 보면서 예시 1번을 진행해보자. function solution(id_list, report, k) { // report 배열에서 중복 제거 let setReportArray = [...new Set(report)]; // 각 유저당 신고당한 횟수 이 배열은 id_list 유저 순서와 동일 ex..
-
프로그래머스 - 체육복Algorithm 2023. 5. 6. 23:01
문제 풀이 이번 문제는 제법 시간이 꽤 걸렸었다. 풀고자 했던 의도는 먼저 reserve 배열의 요소중 lost 배열의 요소와 같은것이 있는지 즉, 여분의 체육복이 있으면서 도난당한 학생이 있는지 확인하고 해당 학생번호를 lost 배열과 reserve 배열 각각 제거시켜 주었다. 그 후 reserve 배열의 학생번호 앞 뒤 번호가 lost 배열에 있는지 체크하고 있다면 체육복을 가진 학생 수를 증가시켜준다. 하지만, 이론으로는 그럴듯 해 보였으나 계속 한 두 케이스에서 실패가 나서 왜 그런지 이유를 도저히 모르던 와중 lost 배열이나 reserve 배열의 학생 번호가 정렬되어 있지 않아서 발생하는 문제가 있었다. 5명의 학생이 있고, lost 배열에는 [2, 4], reserve 배열에는 [3, 1]이..