Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
1 <= preorder.length <= 3000inorder.length == preorder.length -3000 <= preorder[i], inorder[i] <= 3000preorder and inorder consist of unique values. Each value of inorder also appears in preorder.
preorder is guaranteed to be the preorder traversal of the tree. inorder is *guaranteed to be the inorder traversal of the tree.
We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i].
You’re given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.
If you choose a job that ends at time X you will be able to start another job that starts at time X.
Example 1:
Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70] Output: 120 Explanation: The subset chosen is the first and fourth job. Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
Example 2:
Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60] Output: 150 Explanation: The subset chosen is the first, fourth and fifth job. Profit obtained 150 = 20 + 70 + 60.
A certain bug’s home is on the x-axis at position x. Help them get there from position 0.
The bug jumps according to the following rules:
It can jump exactly a positions forward (to the right).
It can jump exactly b positions backward (to the left). It cannot jump backward twice in a row.
It cannot jump to any forbidden positions.
The bug may jump forward beyond its home, but it cannot jump to positions numbered with negative integers.
Given an array of integers forbidden, where forbidden[i] means that the bug cannot jump to the position forbidden[i], and integers a, b, and x, return the minimum number of jumps needed for the bug to reach its home. If there is no possible sequence of jumps that lands the bug on position x, return -1.
Example 1:
Input: forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9 Output: 3 Explanation: 3 jumps forward (0 -> 3 -> 6 -> 9) will get the bug home.
Example 2:
Input: forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11 Output: -1
Example 3:
Input: forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7 Output: 2 Explanation: One jump forward (0 -> 16) then one jump backward (16 -> 7) will get the bug home.
Constraints:
1 <= forbidden.length <= 10001 <= a, b, forbidden[i] <= 2000 0 <= x <= 2000 All the elements in forbidden are distinct. * Position x is not forbidden.
用BFS模板,但此题到了某个位置可以有两个状态:向前跳和向后跳。所以visited不能只含位置,必须包含方向,(position, is_backward). if (neighbor, neighbor_is_backward) in visited也记得包含方向,否则LTE,因为Python不会检查是否tuple
Give a binary string s, return the number of non-empty substrings that have the same number of 0‘s and 1‘s, and all the 0‘s and all the 1‘s in these substrings are grouped consecutively.
Substrings that occur multiple times are counted the number of times they occur.
Example 1:
Input: s = “00110011” Output: 6 Explanation: There are 6 substrings that have equal number of consecutive 1’s and 0’s: “0011”, “01”, “1100”, “10”, “0011”, and “01”. Notice that some of these substrings repeat and are counted the number of times they occur. Also, “00110011” is not a valid substring because all the 0’s (and 1’s) are not grouped together.
Example 2:
Input: s = “10101” Output: 4 Explanation: There are 4 substrings: “10”, “01”, “10”, “01” that have equal number of consecutive 1’s and 0’s.
Constraints:
1 <= s.length <= 10<sup>5</sup>s[i] is either '0' or '1'.
defcountBinarySubstrings(self, s: str) -> int: presum, count, res = [], 1, 0 for i in range(1, len(s)): # if s[i - 1] == s[i]: count += 1 else: presum.append(count) #[2, 2,2,2] count = 1 if count > 0: presum.append(count) for i in range(1, len(presum)): res += min(presum[i - 1], presum[i]) return res
There is a directed graph of n colored nodes and m edges. The nodes are numbered from 0 to n - 1.
You are given a string colors where colors[i] is a lowercase English letter representing the color of the i<sup>th</sup> node in this graph (0-indexed). You are also given a 2D array edges where edges[j] = [a<sub>j</sub>, b<sub>j</sub>] indicates that there is a directed edge from node a<sub>j</sub> to node b<sub>j</sub>.
A valid path in the graph is a sequence of nodes x<sub>1</sub> -> x<sub>2</sub> -> x<sub>3</sub> -> ... -> x<sub>k</sub> such that there is a directed edge from x<sub>i</sub> to x<sub>i+1</sub> for every 1 <= i < k. The color value of the path is the number of nodes that are colored the most frequently occurring color along that path.
Return the largest color value of any valid path in the given graph, or-1if the graph contains a cycle.
Example 1:
Input: colors = “abaca”, edges = [[0,1],[0,2],[2,3],[3,4]] Output: 3 Explanation: The path 0 -> 2 -> 3 -> 4 contains 3 nodes that are colored "a" (red in the above image).
Example 2:
Input: colors = “a”, edges = [[0,0]] Output: -1 Explanation: There is a cycle from 0 to 0.
Constraints:
n == colors.lengthm == edges.length 1 <= n <= 10<sup>5</sup>0 <= m <= 10<sup>5</sup> colors consists of lowercase English letters.
0 <= a<sub>j</sub>, b<sub>j</sub> < n
拓扑排序的同时,如果知道父亲节点有最大的同种颜色数,容易计算儿子的同种颜色数dp[child] dp[child] = max(dp[parent]) for all the parents for the child 由于有26种颜色,扩展到最大的第i种颜色数dp[child][i]表示以child为结尾的路径上第i种颜色的累计最大节点数
1 2
dp[child][i] = max(dp[parent][i] + 1) if color[child] == color[parent] for all the immediate parents for the child, i = 1..26 = max(dp[parent][i]) if color[child] != color[parent]
deflargestPathValue(self, colors: str, edges: List[List[int]]) -> int: graph = [[] for _ in range(len(colors))] in_degree = [0] * len(colors) for _edge in edges: graph[_edge[0]].append(_edge[1]) in_degree[_edge[1]] += 1 res = [] dp = [[0for _ in range(26)] for _ in range(len(colors))] initial_nodes = [i for i in range(len(in_degree)) if in_degree[i] == 0] for _node in initial_nodes: dp[_node][ord(colors[_node]) - ord('a')] = 1 queue = collections.deque(initial_nodes) while queue: node = queue.popleft() res.append(node) for j in graph[node]: for i in range(26): dp[j][i] = max(dp[j][i], dp[node][i] + (1if ord(colors[j]) - ord('a') == i else0)) # remember max(dp[j][i] in_degree[j] -= 1 if in_degree[j] == 0: queue.append(j) return-1if len(colors) != len(res) else max(map(max, dp))