목록알고리즘 (547)
N
https://leetcode.com/problems/network-delay-time/ Network Delay Time - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 다익스트라 알고리즘 활용. 우선 주어진 times 배열을 그래프로 나타내기 위해 n + 1 크기의 graph 2차원 배열을 선언. 또한, 노드 간 걸리는 시간을 dist 배열에 저장하기 위해 n + 1 크기의 1차원 배열을 선언하여 Infinity로 초기화한다. times를 순회하면서 노드를..
https://leetcode.com/problems/find-original-array-from-doubled-array/ Find Original Array From Doubled Array - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Greedy 알고리즘 적용. changed 배열을 오름차순으로 정렬한 후 순회를 한다. 순회를 하면서 number배열에 changed[i]의 갯수를 카운팅한다. 다음 for문으로 다시 changed를 순회한다. 현재 ch..
https://leetcode.com/problems/snapshot-array/ Snapshot Array - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Map을 이용한 문제 풀이. 생성자가 호출될 때 전체 스냅샷이 저장될 snapshots(Map), 현재 스냅샷을 나타내는 array(Map), 현재 스냅 Id를 나타내는 snapId를 선언한다. set(index, val) 함수. 현재 스냅샷에 key: index, value: val 로 저장한다. sna..
https://programmers.co.kr/learn/courses/30/lessons/12971?language=javascript 코딩테스트 연습 - 스티커 모으기(2) N개의 스티커가 원형으로 연결되어 있습니다. 다음 그림은 N = 8인 경우의 예시입니다. 원형으로 연결된 스티커에서 몇 장의 스티커를 뜯어내어 뜯어낸 스티커에 적힌 숫자의 합이 최대가 되도록 programmers.co.kr DP를 이용한 문제 풀이. 하나의 스티커를 땠을 때, 양 옆의 스티커를 사용하지 못하기 때문에 DP 테이블을 2개 사용해야 한다. 왜냐하면 아래처럼 두 가지 경우 중 합이 큰 값을 골라야 하기 때문이다. 1) 첫번째 스티커부터 땠을 때(가장 마지막에 있는 스티커를 사용하지 못함) 2) 두번째 스티커부터 땠을 때..
https://programmers.co.kr/learn/courses/30/lessons/12979?language=javascript 코딩테스트 연습 - 기지국 설치 N개의 아파트가 일렬로 쭉 늘어서 있습니다. 이 중에서 일부 아파트 옥상에는 4g 기지국이 설치되어 있습니다. 기술이 발전해 5g 수요가 높아져 4g 기지국을 5g 기지국으로 바꾸려 합니다. 그런데 5 programmers.co.kr 조건이 크지 않기 때문에 완전 탐색으로 풀 수 있다. 기지국 번호가 1부터 시작하니 start = 1로 시작. 인덱스는 0부터 시작한다. start { let answer = 0; let index = 0; let start = 1; while(start = stations[index] - w && start
https://programmers.co.kr/learn/courses/30/lessons/43163 코딩테스트 연습 - 단어 변환 두 개의 단어 begin, target과 단어의 집합 words가 있습니다. 아래와 같은 규칙을 이용하여 begin에서 target으로 변환하는 가장 짧은 변환 과정을 찾으려고 합니다. 1. 한 번에 한 개의 알파벳만 바꿀 수 programmers.co.kr BFS 풀이. countDiffer()함수. 인자로 받아온 word와 target의 다른 문자가 1개인지 확인하는 함수. for문으로 word의 길이만큼 반복하면서, 문자가 몇개나 다른지 count로 확인한다. 만약 count === 1이라면 true를, 아니라면 false를 리턴한다. solutino() 함수. 정답을..
https://leetcode.com/problems/reorganize-string/submissions/ Reorganize String - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com const asc = (a, b) => { if(a[1] === b[1]){ return a[0] - b[0]; } return b[1] - a[1]; } const reorganizeString = (s) => { const map = new Map(); const ans..
https://leetcode.com/problems/k-closest-points-to-origin/ K Closest Points to Origin - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com array 배열과 answer 배열을 선언. points 배열을 순회하면서 array에 유클리드 거리와 인덱스를 넣어준다. 유클리드 거리와 인덱스가 저장된 array 배열을 거리의 오름차순으로 정렬. k개 만큼 answer에 답을 넣어주면 된다. /** * @pa..