분류 전체보기
-
프로그래머스 - 구명보트Algorithm 2023. 6. 7. 18:16
문제 풀이 사람들을 담은 배열 people을 몸무게가 가벼운 순인 오름차순으로 정렬시킨다 보트에 people 배열에서 가장 무거운 사람을 태운 뒤, people 배열에서 가장 가벼운 사람을 태울 수 있는지 확인한다 두 사람이 탈 수 있다면 두 사람을 태워 보내고 불가능하다면 다음으로 무거운 사람을 태운 뒤 확인한다 이 과정을 people 배열에 아무도 남지 않을 때 까지 반복한다 function solution(people, limit) { let answer = 0; let boat = []; people.sort((a, b) => a - b); while(people.length) { boat.push(people.pop()); // 보트에 탄 사람의 무게 + 가장 가벼운사람의 무게가 limit보다 ..
-
Javascript - Event LoopJavascript 2023. 6. 5. 20:56
프로그래머스 데브코스 프론트엔드 과정을 이수하던 중 Event Loop의 동작 과정에 대해 접하게 되었다. 상당히 예전에 Javascript를 처음 공부하던 당시에 접했던 키워드라 기억이 가물가물해서 이번 기회에 다시 한번 정리해보기로 했다. Event Loop가 Javascript 엔진에 포함되어 있다고 착각하는 경우가 종종 있다고들 하는데, 사실은 그렇지가 않다. Event Loop는 Javascript 엔진에 포함되어 있지않고, 브라우저나 Node.js에서 자체적으로 관리하고 있다. WebAPI는 브라우저에서 제공하는 API이고, 클릭과 같은 DOM event, 네트워크 호출 혹은 Timer 등을 실행시킬 경우 브라우저에 위임되는데, 보통 이런 WebAPI들은 Callback 함수를 넘기기 마련이다..
-
프로그래머스 - 게임 맵 최단거리Algorithm 2023. 5. 30. 13:22
문제 풀이 큐에 x, y의 좌표값과 방문한 칸의 개수 answer을 초기값으로 각각 0, 0, 1씩 넣어준다 큐에 현재 좌표의 상하좌우의 좌표값과 answer+1이 든 배열을 push해준다 shift()로 큐에서 하나씩 빼내서 해당 좌표가 갈 수 없는 길이라면 다음 요소를 확인하고 갈 수 있는 길이라면 큐에 남은 요소가 없어질 때 까지 이 과정을 반복한다 반복하는 과정중 x, y 좌표가 상대 진영에 도달하면 answer을 리턴한다 큐에 남은 요소가 없어진다면 상대 진영에 도착하지 못한다는 의미이기에 -1을 리턴한다 function solution(maps) { let xLength = maps[0].length; let yLength = maps.length; let xGoal = xLength-1; l..
-
프로그래머스 - 바탕화면 정리Algorithm 2023. 5. 26. 11:00
문제 풀이 이 문제는 드래그를 시작할 좌표와 드래그를 마칠 좌표를 구하는게 관건이다 wallpaper 배열의 요소들 중에서 '#' 문자가 나온 가장 첫번째 요소의 인덱스가 드래그를 시작할 좌표의 y좌표가 된다 wallpaper 배열의 요소에서 가장 첫번째로 존재하는 '#' 문자가 몇 번째 인덱스에 있는지 확인하고 그중 최솟값이 드래그를 시작할 좌표의 x좌표가 된다 wallpaper 배열의 요소들 중에서 '#' 문자가 나온 요소들 중 마지막 요소의 인덱스 + 1 이 드래그를 마칠 좌표의 y좌표가 된다 wallpaper 배열의 요소에서 마지막에 존재하는 '#' 문자가 몇 번째 인덱스에 있는지 확인하고 그중 최댓값 + 1 이 드래그를 마칠 좌표의 x좌표가 된다 function solution(wallpaper..
-
프로그래머스 - 폰켓몬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..