defget_ad_clicks_purchases(self, completed_purchase_user_ids, ad_clicks, all_user_ips): # get clicks product_to_clicks = collections.defaultdict(int) for ad_click in ad_clicks: parts = ad_click.split(',') product_to_clicks[parts[2]] += 1
# get purchases userid_to_ip = collections.defaultdict(str) for all_user_ip in all_user_ips: parts = all_user_ip.split(',') userid_to_ip[parts[0]] = parts[1]
ip_to_product = collections.defaultdict(str) for ad_click in ad_clicks: parts = ad_click.split(',') ip_to_product[parts[0]] = parts[2]
product_to_purchase = collections.defaultdict(int) for user_id in completed_purchase_user_ids: ip = userid_to_ip[user_id] product = ip_to_product[ip] product_to_purchase[product] += 1
res = [] for product, click in product_to_clicks.items(): res.append((product, click, product_to_purchase[product])) return res
算法分析:
时间复杂度为O(n + m + p),空间复杂度O(n + m + p), n, m, p分别每个表的大小
Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left-justified and no extra space is inserted between words.
Note:
A word is defined as a character sequence consisting of non-space characters only.
Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
The input array words contains at least one word.
Example 1:
<pre>Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]</pre>
Example 2:
<pre>Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.</pre>
Example 3:
<pre>Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
Output:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]</pre>
Constraints:
1 <= words.length <= 300
1 <= words[i].length <= 20
words[i] consists of only English letters and symbols.
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>] indicates that you must take course b<sub>i</sub> first if you want to take course a<sub>i</sub>.
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
Example 1:
<pre>Input: numCourses = 2, prerequisites = [[1,0]]
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
</pre>
Example 2:
<pre>Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
</pre>
All the pairs [a<sub>i</sub>, b<sub>i</sub>] are distinct.
</div>
题目大意:
课程有先修课要求,求修课的顺序
算法思路:
拓扑排序的经典题
注意事项:
注意题目要求课程可能存在循环,记得第四部侦测循环
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
deffindOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: in_degree = [0] * numCourses graph = [[] for _ inrange(numCourses)] for li in prerequisites: in_degree[li[0]] += 1 graph[li[1]].append(li[0]) queue = collections.deque([i for i inrange(len(in_degree)) if in_degree[i] == 0]) res = [] while queue: node = queue.popleft() res.append(node) for neighbor in graph[node]: in_degree[neighbor] -= 1 if in_degree[neighbor] == 0: queue.append(neighbor) return res if numCourses == len(res) else []
Implement a SnapshotArray that supports the following interface:
SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0.
void set(index, val) sets the element at the given index to be equal to val.
int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id
Example 1:
<pre>Input: ["SnapshotArray","set","snap","set","get"]
[[3],[0,5],[],[0,6],[0,0]]
Output: [null,null,0,null,5]
Explanation:
SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
snapshotArr.set(0,5); // Set array[0] = 5
snapshotArr.snap(); // Take a snapshot, return snap_id = 0
snapshotArr.set(0,6);
snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5</pre>
Constraints:
1 <= length <= 50000
At most 50000 calls will be made to set, snap, and get.
0 <= index < length
0 <= snap_id <(the total number of times we call snap())
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.
Return the minimum number of steps to walk from the upper left corner(0, 0)to the lower right corner(m - 1, n - 1)given that you can eliminate at mostkobstacles. If it is not possible to find such walk return -1.
Example 1:
<pre>Input: grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], k = 1
Output: 6
Explanation:
The shortest path without eliminating any obstacle is 10.
The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
</pre>
Example 2:
<pre>Input: grid = [[0,1,1],[1,1,1],[1,0,0]], k = 1
Output: -1
Explanation: We need to eliminate at least two obstacles to find such a walk.
</pre>
defshortestPath(self, grid: List[List[int]], k: int) -> int: m, n = len(grid), len(grid[0]) if k >= m + n - 2: # TLE return m + n - 2
queue = collections.deque([(0, 0, k, 0)]) # x, y, k, distance visited = set([(0, 0, k)]) # include k while queue: _x, _y, _k, _dis = queue.popleft() if _x == m - 1and _y == n - 1: return _dis for _dx, _dy in OFFSET: x, y = _x + _dx, _y + _dy if x < 0or x >= m or y < 0or y >= n: continue eliminations = _k - 1if grid[x][y] == 1else _k if (x, y, eliminations) notin visited and eliminations >= 0: queue.append((x, y, eliminations, _dis + 1)) visited.add((x, y, eliminations)) return -1