본문 바로가기

leet code41

[LEET CODE] 39. Combination Sum # Problem Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order. The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.. 2021. 4. 9.
[LEET CODE] 77. Combinations # Problem Given two integers n and k, return all possible combinations of k numbers out of the range [1, n]. You may return the answer in any order. Constraints: 1 2021. 4. 9.
❗️[LEET CODE] 46. Permutations ( 객체 복사 ) # Problem Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Constraints: 1 List[List[int]]: return list(itertools.permutations(nums)) itertools 모듈의 permutations 함수는 튜플 모음을 반환 # 객체 복사 파이썬에서는 모든 것이 객체이기 때문에 '=' 를 이용하면 객체의 참조가 복사되기 때문에 원래의 값을 변경하면 참조한 값도 변경된다. 리스트의 경우 a[:], a.copy() 를 이용하면 값을 복사한 다른 객체를 생성 복잡한 리스트의 경우 a.copy.deep.. 2021. 4. 7.
[LEET CODE] 17. Letter Combinations of a Phone Number # Problem Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. Constraints: 0 List[str]: def dfs(index, path): # 끝까지 탐색하면 백트래킹 if len(path) == len(digits): result.append.. 2021. 4. 7.
[LEET CODE] 200. Number of Islands # Problem Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Constraints: m == grid.length n == grid[i].length 1 = len(grid) or \ j = len(grid[.. 2021. 4. 6.
[LEET CODE] 347. Top K Frequent Elements( zip 함수, * (아스테리스크)) # Problem Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Constraints: 1 List[int]: freqs = collections.Counter(nums) freqs_heap = [] # 힙에 음수로 삽입 for f in freqs: heapq.heappush(freqs_heap, (-freqs[f], f)) topk = list() # k번 만큼 추출, 민 힙 이므로 가장 작은 음수 순으로 추출 for _ in range(k): topk.append(heapq.heappop(freqs_heap)[1]) return .. 2021. 4. 5.
반응형