from collections import defaultdict classSolution: defdestCity(self, paths: List[List[str]]) -> str: res_map = defaultdict(int) tmp = set() for path in paths: tmp.add(path[0]) tmp.add(path[1]) res_map [path[0]] += 1 for x in tmp: if res_map[x] ==0: return x returnNone
classSolution: defkLengthApart(self, nums: List[int], k: int) -> bool: pre = -1 for i,num in enumerate(nums): if num == 1: if pre==-1or i-pre-1>=k: pre = i else: returnFalse returnTrue
classSolution { public: intlongestSubarray(vector<int> & nums, int limit){ deque<int> maxq; deque<int> minq; int n = nums.size(); int ans = 1; int left = 0; for (int i = 0; i < n; ++i) { int tmp = nums[i]; while (!maxq.empty() && nums[maxq.back()] < tmp) { maxq.pop_back(); } while (!minq.empty() && nums[minq.back()] > tmp) { minq.pop_back(); } maxq.push_back(i); minq.push_back(i); while ((nums[maxq.front()] - nums[minq.front()]) > limit) { ++left; if (maxq.front()<left){ maxq.pop_front(); } if (minq.front()<left){ minq.pop_front(); } } ans = max(ans, i - left + 1); } return ans; } };
classSolution: defkthSmallest(self, mat: List[List[int]], k: int) -> int: res = mat[0][:k] for x in mat[1:]: tmp = [] for t in x: tmp += [y+t for y in res] res = sorted(tmp)[:k] return res[-1]