Algorithm(53)
-
2008. Maximum Earnings From Taxi (Medium)
start, end 구간과 구간 내 얻을 수 있는 profit이 주어짐 구간이 겹치지 않게 profit이 최대가 되도록 각 구간을 선택 for i in range(len(rides)): rides[i][2] = rides[i][1] - rides[i][0] + rides[i][2] rides.sort(key=lambda p: p[1]) dp = [[0, 0]] for start, end, profit in rides: idx = bisect.bisect_left(dp, [start + 1]) if dp[-1][1] < dp[idx - 1][1] + profit: dp += [end, dp[idx - 1][1] + profit], return dp[-1][1]
2021.09.21 -
2006. Count Number of Pairs With Absolute Difference K
절대값이 k 만큼 차이나는 모든 pair의 수를 리턴 cnt = collections.Counter(nums) return sum(cnt[num - k] for num in nums)
2021.09.21 -
2014. Longest Subsequence Repeated k Times (hard)
s = "letsleetcode", k = 2 s에서 k번 반복하는 가장 긴 sub string을 찾아서 리턴 filtered = "".join(el*(freq//k) for el, freq in collections.Counter(s).items()) def dfs(n, sel, seq, res): if len(seq) == n: res.add(''.join(seq)) return for i in range(len(filtered)): if i in sel: continue dfs(n, sel + [i], seq + [filtered[i]], res) def is_subseq(sub, seq): seq = iter(seq) return all(c in seq for c in sub) combs = se..
2021.09.21 -
2013. Detect Squares (medium)
좌표들이 주어지고, 특정 좌표가 주어질 시 이 좌표로 square를 몇 개 만들수 있는지 리턴 def __init__(self): self.cnt = collections.defaultdict(int) self.coords = set() def add(self, point: List[int]) -> None: self.cnt[tuple(point)] += 1 self.coords.add(tuple(point)) def count(self, point: List[int]) -> int: cx, cy = point num = 0 for x, y in self.coords: if abs(cx - x) == 0 or abs(cy - y) == 0 or \ abs(cx - x) != abs(cy - y): con..
2021.09.21 -
2012. Sum of Beauty in the Array (Medium)
양끝 값을 위치를 제외하고, 중간의 어떤 값이 좌/우 보다 크면 1점 좌측의 모두보다 크고, 우측의 모두보다 작으면 2점 좌/우측의 비교는 O(N)으로 선형 탐색을 통해 찾고, 좌측모두보다 크고 우측 모두 보다 작은 것은 미리 만들어 놓은 max value list와 min value list를 사용 각각 만드는데 O(N)의 시간 소모 전체 알고리즘의 복잡도는 O(N) mx = [0]*len(nums) mn = [0]*len(nums) mx[0] = nums[0] for i in range(1, len(nums)): mx[i] = max(mx[i - 1], nums[i]) mn[-1] = nums[-1] for i in range(len(nums) - 2, -1, -1): mn[i] = min(mn[i ..
2021.09.21