목록Leet Code 알고리즘 (179)
N
https://leetcode.com/problems/jump-game-ii/ Jump Game II - 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 DP 알고리즘 사용. dp 배열을 nums의 length 만큼 선언하고, Infinity로 초기화 한다. 시작은 0번째 인덱스부터 시작하기 때문에 dp[0] = 0으로 저장하고 for문을 시작한다. nums 배열을 순회. nums[i]는 점프할 수 있는 최대 거리를 나타내기 때문에 이중 for문으로 끝까지 갈 수..
https://leetcode.com/problems/min-cost-climbing-stairs/ Min Cost Climbing Stairs - 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 dp 알고리즘 사용. cost의 길이를 나타내는 length. dp를 length의 길이만큼 할당하고 0으로 초기화. 계단은 1칸 또는 2칸 오를 수 있다. 그렇기 때문에 dp[0] = cost[0], dp[1] = cost[0] + cost[1] 과 cost[1] 중 ..
https://leetcode.com/problems/find-and-replace-in-string/ Find And Replace in 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 문자열과 hash map을 활용. indices 배열이 오름차순으로 정렬되어있지 않다. 그래서 인덱스에 따른 source와 target을 map에 저장 후 문자열을 변환하면 된다. map에 저장 후 indices를 오름차순으로 정렬 후, s 문자열을 순회 만약 i..
https://leetcode.com/problems/rearrange-spaces-between-words/submissions/ Rearrange Spaces Between Words - 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 /** * @param {string} text * @return {string} */ const reorderSpaces = (text) => { let space = 0; let string = ""; const splite..
https://leetcode.com/problems/path-with-minimum-effort/ Path With Minimum Effort - 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 우선순위큐로를 이용한 풀이. MinHeap으로 우선순위 큐를 구현. BFS 방식과 비슷하게 PriorityQueue에 방문할 좌표와 minimum effort를 넣으면 된다. pq에 push하면 알아서 minHeap으로 정렬해주기 때문에 방문할 좌표와 방문 여부를 확인해..
https://leetcode.com/problems/minimum-path-sum/ Minimum Path Sum - 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 DP 알고리즘 이용. 주어진 grid의 크기는 m x n 이므로 각 변수에 length를 저장한다. dp 테이블은 grid와 마찬가지로 m x n의 크기로 선언하여 0으로 초기화한다. 시작점인 [0, 0]에서 끝점 [m - 1, n - 1]로 향하기 때문에 dp[0][0]은 grid[0][0]으로 ..
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..