목록leet code (85)
N
https://leetcode.com/problems/can-place-flowers/ Can Place Flowers - 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 그리디 알고리즘. n개의 꽃을 flowerbed에 심어야 하는데, 근접한 위치에 꽃이 없어야 한다. for문을 이용해 flowerbed를 순회. 만약 현재 n이 0이라면 모두 심었기 때문에 바로 true를 리턴한다. 그렇지 않고 n이 남아있다면 최대한 꽃을 심어본다. 맨 앞에서부터 꽃을 심으면 ..
https://leetcode.com/problems/decode-ways/ Decode Ways - 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 numDecodings = (s) => { const length = s.length; const dp = Array(length + 1).fill(0); if(s[0] === "0"){ return 0; } dp[length] = 1; dp[length - 1] = parseInt(s[length - 1..
https://leetcode.com/problems/unique-paths-ii/ Unique Paths 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 알고리즘. Unique Path 문제에서 장애물이 추가된 문제다. m x n 크기로 DP 배열을 선언하여 0으로 초기화한다. 만약 obstacleGrid[0][0]이 1이라면 출발점이 장애물로 막혀있고, 더 이상 진행할 수 없다는 뜻이된다. 그렇다면 0을 바로 리턴하여 종료한다. 그렇지 않다면 이..
https://leetcode.com/problems/unique-paths/ Unique Paths - 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 알고리즘. m x n 크기의 배열을 선언하고 0으로 초기화한다. [0, 0] 좌표에서 [m - 1, n - 1] 좌표까지 갈 수 있는 방법을 모두 구하는데, [0, 0]은 1로 시작하고 이중 for문으로 DP를 적용한다. 로봇은 오른쪽 또는 아래로 가기 때문에 순회를 하면서 점화식을 적용한 값을 배열에 저장..
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..