You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.
Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array fruits where fruits[i] = [position<sub>i</sub>, amount<sub>i</sub>] depicts amount<sub>i</sub> fruits at the position position<sub>i</sub>. fruits is already sorted by position<sub>i</sub> in ascending order, and each position<sub>i</sub> is unique.
You are also given an integer startPos and an integer k. Initially, you are at the position startPos. From any position, you can either walk to the left or right. It takes one step to move one unit on the x-axis, and you can walk at mostk steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.
Return the maximum total number of fruits you can harvest.
Example 1:
Input: fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4 Output: 9 Explanation: The optimal way is to: - Move right to position 6 and harvest 3 fruits - Move right to position 8 and harvest 6 fruits You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
Example 2:
Input: fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4 Output: 14 Explanation: You can move at most k = 4 steps, so you cannot reach position 0 nor 10. The optimal way is to: - Harvest the 7 fruits at the starting position 5 - Move left to position 4 and harvest 1 fruit - Move right to position 6 and harvest 2 fruits - Move right to position 7 and harvest 4 fruits You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
Example 3:
Input: fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2 Output: 0 Explanation: You can move at most k = 2 steps and cannot reach any position with fruits.
一开始考虑用BFS,但由于每个点可以走两次,如先往左再往右,所以不能用BFS 每个点不能走3次,因为贪婪法。所以只要计算单向路径的水果数,单向路径水果数只要计算[startPos - k - 1, startPos + k + 1]的这个区间即可 然后重复路径的范围是[0, k/2 + 1], 枚举这些值然后用presum得到单向路径水果数。
注意事项:
先判断不合法的情况sum(gas) < sum(cost)
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
defmaxTotalFruits(self, fruits: List[List[int]], startPos: int, k: int) -> int: pos_to_fruits = collections.defaultdict(int) for pair in fruits: pos_to_fruits[pair[0]] = pair[1] presum = collections.defaultdict(int) presum[startPos - k - 1] = pos_to_fruits[startPos - k - 1] for i inrange(startPos - k, startPos + k + 1): presum[i] += presum[i-1] + pos_to_fruits[i] res = 0 for i inrange(k//2 + 1): res = max(res, presum[startPos + k - i * 2] - presum[startPos - i - 1]) res = max(res, presum[startPos + i] - presum[startPos - k + i * 2 - 1]) return res
for i inrange(len(arr)): while stack and arr[i] > arr[stack[-1]]: prev_idx = stack.pop() res += arr[prev_idx] * (prev_idx - stack[-1]) * (i - prev_idx) stack.append(i) return res
deffindKthLargest(self, nums: List[int], k: int) -> int: res = [] # min heap for i inrange(len(nums)): if i < k: heapq.heappush(res, nums[i]) elif nums[i] > res[0]: heapq.heapreplace(res, nums[i]) return res[0]
deffindKthLargest(self, nums: List[int], k: int) -> int: m = len(nums) - k returnself.quick_select(nums, 0, len(nums) - 1, m)
defquick_select(self, nums, start, end, m): if start > end: return -1 pivot_pos = self.partition(nums, start, end) if m == pivot_pos: return nums[pivot_pos] elif m < pivot_pos: returnself.quick_select(nums, start, pivot_pos - 1, m) else: returnself.quick_select(nums, pivot_pos + 1, end, m) # remember use m not related to pivot_pos
defpartition(self, nums, start, end): pivot, no_smaller_index = nums[end], start for i inrange(start, end): # remember use start and end not len if nums[i] < pivot: nums[i], nums[no_smaller_index] = nums[no_smaller_index], nums[i] no_smaller_index += 1 nums[no_smaller_index], nums[end] = nums[end], nums[no_smaller_index] return no_smaller_index
There is a new alien language that uses the English alphabet. However, the order among the letters is unknown to you.
You are given a list of strings words from the alien language’s dictionary, where the strings in words are sorted lexicographically by the rules of this new language.
Return a string of the unique letters in the new alien language sorted in lexicographically increasing order by the new language’s rules. If there is no solution, return"". If there are multiple solutions, return any of them.
A string s is lexicographically smaller than a string t if at the first letter where they differ, the letter in s comes before the letter in t in the alien language. If the first min(s.length, t.length) letters are the same, then s is smaller if and only if s.length < t.length.
Example 1:
Input: words = [“wrt”,”wrf”,”er”,”ett”,”rftt”] Output: “wertf”
Example 2:
Input: words = [“z”,”x”] Output: “zx”
Example 3:
Input: words = [“z”,”x”,”z”] Output: “” Explanation: The order is invalid, so return "".
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 100 * words[i] consists of only lowercase English letters.
defalienOrder(self, words: List[str]) -> str: # graph = collections.defaultdict(list) graph = Counter({c: [] for word in words for c in word}) for i inrange(1, len(words)): ifnotself.populate_one_order(words[i - 1], words[i], graph): return'' in_degree = collections.defaultdict(int) for c in graph.keys(): in_degree[c] = 0 for key, li in graph.items(): for j inrange(len(li)): in_degree[li[j]] += 1 res = '' queue = deque([node for node, in_degree_num in in_degree.items() if in_degree_num == 0]) while queue: node = queue.popleft() res += node for neighbor in graph[node]: in_degree[neighbor] -= 1 if in_degree[neighbor] == 0: queue.append(neighbor) return res iflen(graph) == len(res) else''
defpopulate_one_order(self, word, word2, graph): for j inrange(min(len(word), len(word2))): if word[j] != word2[j]: graph[word[j]].append(word2[j]) returnTrue returnFalseiflen(word) > len(word2) elseTrue