파이썬 48

[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] 리트코드 202. Happy Number(two pointer)

https://leetcode.com/problems/happy-number/ Happy Number - 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 문제는 간단하다. 숫자를 각 자리수별로 쪼개서 제곱한다음 모두 더해 1을 만들 수 있다면 True를 반환하면 된다. 아주 간단해보이지만 문제는 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 -> ... 처럼 계속 루프에 갇혀 빠져나가지 못하는 경우가 있다는 것이다. 이 ..

알고리즘 2022.01.05

[Python] 리트코드 763. Partition Labels(Hash)

https://leetcode.com/problems/partition-labels/ Partition Labels - 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 문제의 첫 번째 예시에서(ababcbacadefegdehijhklij) 제일 처음 알파벳인 a를 자르려면 최소 [8]까지는 한 파트로 묶어 잘라야 나머지 파트에 a가 포함되지 않는다. 그렇게 0~8까지를 한 파트로 묶으면 그 안의 알파벳들 중 나머지 부분에 포함되는 알파벳은 하나도 없다. 그러니 이..

알고리즘 2021.12.31

[Python] 리트코드 313. Super Ugly Number(Hash)

https://leetcode.com/problems/super-ugly-number/ Super Ugly Number - 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 이 문제는 예전에 풀었던 264. Ugly Number II (https://leetcode.com/problems/ugly-number-ii/) 이 문제와 비슷하다. 그래서 그 문제를 응용해서 풀어보았다. 결국 primes factors를 구하는 문제인건데 주어지는 primes에 있는 숫자들로..

알고리즘 2021.12.31

[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] 리트코드 1014. Best Sightseeing Pair

https://leetcode.com/problems/best-sightseeing-pair/ Best Sightseeing Pair - 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 values[i] + values[j] + i - j 를 계산했을 때 최댓값을 구하는 문제이다. values[i] + values[j] + i - j 값이 최대가 되려면 values[i] + i는 최대가 되어야 하고 values[j] - j도 최대가 되어야 한다. values의 임..

알고리즘 2021.10.26

[Python] 리트코드 1567. Maximum Length of Subarray With Positive Product (누적 곱셈)

https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/ Maximum Length of Subarray With Positive Product - 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 이 문제는 https://leetcode.com/problems/maximum-product-subarray/ 문제의 응용 버전이다. Maximum Product Subarray는 su..

알고리즘 2021.10.25

[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