알고리즘
-
[프로그래머스 1단계] 크레인 인형뽑기 게임알고리즘 2024. 3. 6. 08:56
❓문제 설명 2중배열 형태의 배열 하나를 제공하는데 이걸 결국 n x n 형태로 만드는 것임 주어진 moves라는 배열의 순서대로 2중 배열에서 특정 수를 찾는 것임 그래서 찾은 값을 스택에 저장함 스택에 이전값과 추가되는 값이 같은 시 2개를 없애고 count를 올려서 최종적으로 count를 return하는 것임 ✅ 문제 해결 2중 반복문에 대해 사용을 최대한 지양하고 싶어서 고민을 하다가 도저히 풀리지 않아서 풀이를 보고 해석하였음 기본적으로 배열 탐색이라는 키워드를 풀면서 공부를 하게 되면 자연스럽게 알고리즘을 이해할 수 있는 구조라고 한다. 배열에서 찾은 특정 수를 변수에 저장하고 그 수를 0으로 만드는 작업 이부분이 좀 막혔었는데 이런식으로 간단히 구현할 수 있다. function solutio..
-
[프로그래머스 1단계] 명예의 전당 (1)알고리즘 2024. 2. 29. 09:51
❓문제 설명 명예의 전당이라고 수를 배열에 하나씩 추가하다가 length가 k만큼 된다면 이후로는 명예의 전당에 있는 최소값보다 큰수가 추가될 시 명예의 전당에서 교체되는 것임 이때 수가 추가될 때마다 커트라인을 배열에 추가해서 그 배열의 최종 값을 보여주면 됨 ✅ 문제 해결 function solution(k, score) { // 명예의 전당 배열 const honor = [] // 결괏값 const result = [] // 모든 점수 순회 for(let i = 0 ; i Math.min(..
-
[프로그래머스 1단계] 체육복알고리즘 2024. 2. 28. 09:56
❓문제 설명 최대 인원인 n: number, 체육복이 없는 인원의 명단인 lost: number[], 여분을 가지고 있는 명단 reserve: number[] 여분을 가지고 있는 학생은 자기 +-1 인원에게만 체육복을 빌려줄 수 있음 최대로 많이 수업을 들을 수 있는 인원의 수는? ✅ 문제 해결 먼저 가장 큰 예외 상황을 구별해야 한다. 그렇기에 도난당했는데 여분을 가지고 있는 학생을 제외하고 나머지에 대해서 공통적인 식을 대입을 해보자 let lost_student = lost.filter(v => !reserve.includes(v)).sort(); let reserve_student = reserve.filter(v => !lost.includes(v)).sort(); 제외한 이후 정렬에 대해서 문..
-
[프로그래머스 1단계] 기사단원의 무기알고리즘 2024. 2. 21. 17:18
❓문제 설명 주어진 number만큼 오름차순으로 array를 만들어야 함 그 array가 각각 몇개의 약수를 가지는 지 알아야 함 그 약수의 개수가 limit보다 크다면 power로 교체해주고 넘지 않는다면 그대로 사용하면 됨 최종적으로 나온 array의 합을 구하면 됨 ✅ 문제 해결 순차적으로 진행을 하면 되기에 아래와 같이 만들었더니 시간초과가 떠서 어디서 시간을 많이 잡아먹는지를 알아야겠다. function 약수구하기 (number) { let totalNumber = 0 for (let i = 1; i 약수구하기(index + 1) > limit ? power : 약수구하기(index + 1)).reduce((a, b) => a + b, 0) return answer; } 약수를 구하는 과정에서 ..