diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/3940. Limit Occurrences in Sorted Array.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/3940. Limit Occurrences in Sorted Array.py" new file mode 100644 index 00000000..f3730905 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/3940. Limit Occurrences in Sorted Array.py" @@ -0,0 +1,12 @@ +from collections import defaultdict +class Solution: + def limitOccurrences(self, nums: list[int], k: int) -> list[int]: + counter = defaultdict(int) + ans = [] + for num in nums: + if counter[num] == k: + continue + counter[num]+=1 + ans.append(num) + + return ans diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/4024. Nearest Available Drone.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/4024. Nearest Available Drone.py" new file mode 100644 index 00000000..abfa299d --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/4024. Nearest Available Drone.py" @@ -0,0 +1,29 @@ +# + +''' +1. 아이디어 : +- + +2. 시간복잡도 : + O(n) + +3. 자료구조/알고리즘 : +- + +''' +class Solution: + def nearestDrone(self, drones: list[list[int]], target: list[int]) -> int: + + def get_manhattan_distance(x1, y1, x2, y2): + return abs(x1-x2) + abs(y1-y2) #|xi - xj| + |yi - yj| + + min_distance = float('inf') + ans = -1 + + for i in range(len(drones)): + x, y, distance = drones[i] + m_distance = get_manhattan_distance(x, y, target[0], target[1]) + if m_distance < min_distance and m_distance<=distance: + min_distance = m_distance + ans = i + return ans diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/907. Sum of Subarray Minimums.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/907. Sum of Subarray Minimums.py" new file mode 100644 index 00000000..a4e92181 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/907. Sum of Subarray Minimums.py" @@ -0,0 +1,37 @@ +''' +1. 아이디어 : +최대 nlogn 시간복잡도로 문제를 풀어야한다. (캐싱 또는 DP로 값 재사용) +i보다 왼쪽에 있는 인덱스 j 중, arr[i]보다 작은 arr[j]의 위치를 구한다. (monotonic stack) +dp[i]는 0~i까지 윈도우를 설정했을때의 총합을 유지. +j value를 포함하는 윈도우는 0~j까지의 합을 미리 구했기때문에 i-j 범위 * arr[i] + +2. 시간복잡도 : + O(2n) + +3. 자료구조/알고리즘 : +dp + monotonic stack + +''' +class Solution: + def sumSubarrayMins(self, arr: List[int]) -> int: + MOD = 1000000007 + n = len(arr) + + dp = [0] * n + small_indexes = [] + + for i in range(n): + cval = arr[i] + while small_indexes and arr[small_indexes[-1]] > cval: + small_indexes.pop() + + if small_indexes: + j = small_indexes[-1] + dp[i] = dp[j] + cval * (i-j) + else: + dp[i] = cval * (i+1) + + small_indexes.append(i) + + return sum(dp) % MOD +