알고리즘
-
[구름 트레이닝] 막대 도형알고리즘 2024. 3. 20. 12:48
❓문제 설명 2줄로 구성된 input을 CL을 통해서 받는다 첫번째는 막대의 개수고 두번째는 높이인데 " "로구분된 문자열임 막대를 다 붙여서 하나의 도형이 생기는데 이 도형의 둘레를 구하는 것임 ✅ 문제 해결 막대를 통합적으로 보지말고 하나씩 나눠서 합쳤을 때 몇을 빼는지에 초점을 맞춤 한 변의 길이는 무조건 1로 고정이기에 막대 하나의 둘레를 구하는 공식은 (한변 * 2) + 2이다 그리고 막대가 서로 맞닿는 부분에 대해서만 각 막대의 둘레에서 빼주면 되니깐 Math.min()를 통해서 작은 수를 구하고 *2해줘서 빼줄 것임 이걸 다 반복문으로 돌려서 총합 만들어서 뺄 것임 const readline = require('readline'); (async () => { let rl = readline...
-
[프그래머스 2단계] 미로 탈출알고리즘 2024. 3. 18. 14:15
❓문제 설명 특정 의미를 가진 문자 구성된 문자열로 이루어진 배열을 하나 줌 미로탈출하는 것임 레버 당기고 S에서 E로 가는 최단시간을 구해야 함 탈출 못하는 경우에는 -1을 반환해야 함 ✅ 문제 해결 탈출 못하는 경우를 먼저 생각해서 예외처리로 처리한 다음에 나머지를 처리해보자 탈출 못하는 경우: 레버 상하좌우로 X가 있을 경우랑 S지점 상하좌우로 X가 있는 경우 최단거리를 찾는 것은 어떻게 해야할까??? - 검색해보자 최소시간을 구하는 문제는 BFS로 풀라고 한다. function solution(maps) { let cMaps = maps.map((e)=>[...e].map(a=>a)) const bfs = (start, init = 0) => { cMaps[start[0]][start[1]] = ..
-
[프로그래머스 2단계] 도넛과 막대 그래프알고리즘 2024. 3. 11. 08:28
❓문제 설명 3종류의 그래프가 있다 도넛과 막대와 8자 각각의 형태는 사이즈별로 간선과 정점의 개수가 일정하게 증가하는 추세이다 어떠한 연결점을 만들었다 치고 이 연결점이 무엇인지와 각각의 그래프는 몇개인지를 배열로 return하라고 한다 ✅ 문제 해결 문제를 어떻게 해결해야 할지 감이 안와서 이부분은 다른 사람의 풀이를 보고 분석을 하면서 어떻게 구상을 하게 되었는지를 파악해보자 간선의 개수와 특징을 통해서 문제를 파악하는 것이 중요하다고 생각된다. 찾을 수 있는 규칙으로는 생성된 연결점은 나가는 선만 있고 들어오는 선이 없다는 내용과 8자의 중심에 위치한 수는 나가는 수와 들어오는 수가 각각 2개씩이며 이 중심 개수를 통해서 8자의 개수를 파악할 수 있다 라인의 경우 받는 선이 1개만 있는 중심이 ..
-
[프로그래머스 2단계] 광물 캐기알고리즘 2024. 3. 8. 08:43
❓문제 설명 광석이랑 곡괭이가 주어질 것임 대충 내용 보면 암 곡괭이는 연속 5번 사용해야하므로 5개씩 끊어서 광석을 나눠야 할 듯 광석은 곡괭이별로 캐야하는 피로도가 주어지는데 피로도의 최솟값을 구하는 문제임 ✅ 문제 해결 최솟값을 구할 것이며 곡괭이 수가 광물을 캘 갯수만큼 안나오면 짤라야 함 ex) 곡괭이는 3개인데 광물은 300개면 어짜피 광물 중에 순서대로 15개밖에 못 캠 5개씩 배열로 만들어서 내용물을 이러쿵저러쿵할것임 function solution(picks, minerals) { var answer = 0; var cutCount = Math.ceil(minerals.length/5); let maxLen = picks.reduce((a, b) => a + b); if (maxLen ..