leetcode 3

Python 에서 많이 쓰이는 라이브러리 defaultdict

리트코드를 하다보면, 파이썬에서 많이 쓰이는 라이브러리들이 몇 가지 있는데, 그 중 많이 쓰는 라이브러리인 defaultdict에 대해서 알아보도록 하겠습니다. 리트코드를 하다보면, 대략 20프로, 30프로 정도의 문제에서 defaultdict를 쓰면 코드양을 확연하게 줄일 수 있는 좋은 함수라고 생각합니다. 1. import 방법 from collections import defaultdict 2. 활용 및 설명 (1) Counter로 활용! Python에서 자주 사용하는 Counter 모듈도 defaultdict로 쉽게 구현할 수 있습니다. lst에 수를 구하고 싶은 item들이 있다고 할 때, from collections import Counter lst = ["a", "a", "a", "b", ..

알고리즘 2022.08.31

Weekly Contest 305. (2367~2370) (list, graph, dp)

오랜만에 리트코드 컨테스트를 다 풀었습니다. problem 2367. Number of Arithmetic Triplets 코드 : class Solution: def arithmeticTriplets(self, nums: List[int], diff: int) -> int: res = 0 for i in range(len(nums)-2): for j in range(i+1, len(nums)-1): for k in range(j+1,len(nums)): if nums[j] - nums[i] == diff and nums[k] - nums[j] == diff: res += 1 return res 설명 : Brute force 알고리즘이 통할 것으로 보여서, Brute force를 사용하였습니다. O(n^..