리트코드 40

[Python] 리트코드 Hash Table, Sorting문제(791. Custom Sort String)

https://leetcode.com/problems/custom-sort-string/ Custom Sort 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 우선 s의 문자들 중 order에 존재하는 문자를 order에서의 정렬 순서대로 정렬한 후 나머지 문자를 s에 있던 순서대로 정렬한다. 여기서 주의해야 할 점은 order는 모든 문자가 유니크하지만 s는 중복이 허용되기 때문에 s에 있는 모든 중복 문자는 order에 있는 순서대로 정렬되어야..

알고리즘 2022.03.14

[leetcode] 아스키코드를 이용한 알고리즘 풀이(389. Find the Difference)

https://leetcode.com/problems/find-the-difference/ Find the Difference - 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 문제를 풀다가 풀이 방법이 이마를 탁 치게 만들어서 가져와봤다. 딕셔너리로 빈도수를 체크해서 풀어야 하나 고민하고 있었는데 의외로 쉽게 풀 수 있단 걸 알게 되었다. s와 t의 모든 문자가 똑같은데(예시에는 나와있지 않지만 s와 t의 문자들은 순서가 다를 수도 있다.) 단 한 가지만 t에..

알고리즘 2022.03.09

[Sliding Window] with 예시문제 2가지

슬라이딩 윈도우는 네트워크를 공부할 때 배웠던 개념인데 이 방식을 알고리즘 문제에 적용시켜 풀기도 한다. 범위를 일정하게 고정시켜서 그 범위를 이동시켜가면서 데이터를 비교한다. two pointer알고리즘과 비슷한 알고리즘이다. [문제 1] 문자열 s에서 반복되는 알파벳이 없는 가장 긴 substring의 길이를 구하라. var lengthOfLongestSubstring = function (s) { let max = 0; let l = 0; for (let r = 0; r < s.length; r++) { const idx = s.slice(l, r).indexOf(s[r]); if (idx === -1) { max = Math.max(max, r + 1 - l); } else { l += idx +..

알고리즘 2022.02.16

[Two Pointer #1] 대표 문제 + 풀이

[문제 1] 오름차순 정렬된 nums배열의 모든 요소를 제곱한 값을 정렬된 배열로 반환하라. (ex. nums = [-4,-1,0,3,10] 일 때 정답은 [0,1,9,16,100] 이다.) var sortedSquares = function(nums) { let left = 0; let right = nums.length-1; const answer = [] while(left Math.abs(nums[right])){ answer.push(Math.pow(nums[left], 2)); left ++ }else{ answer.push(Math.pow(nums[right], 2)); right -- } } return answer.reverse() }; class Solution: def sortedSq..

알고리즘 2022.02.13

[이진탐색 알고리즘] with 예시 문제

이진 탐색 알고리즘은 검색 범위를 절반씩 줄여나가면서 탐색하는 알고리즘이다. 흔히 술자리에서 소주 뚜껑에 있는 숫자를 맞추는 게임을 할 때의 방식과 동일하다. 1. 25 제시. 타깃 숫자는 더 작음 => 범위를 0~25로 줄임 2. 12 제시. 타깃 숫자는 더 큼 => 범위를 13~25로 줄임 3. 19를 제시 => 정답 이렇게 숫자가 오름차순 혹은 내림차순의 일정한 순서로 정렬되어 있는 경우 이진 탐색 알고리즘을 사용하면 탐색 시간을 많이 줄일 수 있다. 이 방법을 응용한 문제 3가지를 풀어볼 것이다. [문제 1] 배열 nums에서 target숫자가 위치한 인덱스를 반환하라. (target이 nums에 존재하지 않으면 -1 반환) // JavaScript const searchIndex = (nums,..

알고리즘 2022.02.11

[Python] 리트코드 139. Word Break

https://leetcode.com/problems/word-break/ Word Break - 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 class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: @lru_cache(None) # 결과 캐싱 def isInDict(start): # 4. 3번에서 재귀돌렸을때 현재문자열위치+1을 보내니까 # 그걸 시작위치로 받았을 때 그게 전체 문자열..

알고리즘 2022.01.10

[Python] 리트코드 583. Delete Operation for Two Strings

https://leetcode.com/problems/delete-operation-for-two-strings/ Delete Operation for Two Strings - 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 먼저 word1이랑 word2에서 공통된 알파벳들을 알아낸다. 공통되지 않은 알파벳들은 조작되어야 하는 알파벳이므로 word1과 word2의 각 length를 비교해 그 차이만큼 답으로 리턴한다. 공통된 알파벳을 가려내는데 고려해야 하는 사항..

알고리즘 2021.12.02

[Python] 리트코드 309. Best Time to Buy and Sell Stock with Cooldown

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ Best Time to Buy and Sell Stock with Cooldown - 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 이 문제는 [121. Best Time to Buy and Sell Stock] 이랑 [122. Best Time to Buy and Sell Stock II] 의 다음 단계라고 할 수 있다. 이..

알고리즘 2021.10.27

[Python] 리트코드 463. Island Perimeter

https://leetcode.com/problems/island-perimeter/ Island Perimeter - 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 grid = [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] 그림처럼 1은 땅을 0은 물을 뜻한다. 이때 땅의 둘레를 구하는 문제 ==풀이== 1. 땅 두 칸이 붙어 있으면 겹치는 선이 한 개일 것이다. 2. 겹치는 것 생각하지 않고 각 네모의 둘레를 모두 구했을 때..

알고리즘 2021.10.19