Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Note:
Each of the array element will not exceed 100.
The array size will not exceed 200.
Example 1:
1
2
3
4
5
Input:[1,5,11,5]
Output:true
Explanation:The arraycan be partitioned as[1,5,5]and[11].
Example 2:
1
2
3
4
5
Input:[1,2,3,5]
Output:false
Explanation:The arraycannot be partitioned into equal sum subsets.
Idea:
DP 动态规划
Solution:
Time complexity: O(n*sum)
Space complexity: O(sum)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Author: Huahua
// Running time: 16 ms
classSolution{
public:
boolcanPartition(vector<int>& nums) {
const int sum = std::accumulate(nums.begin(), nums.end(), 0);
There are a total of n courses you have to take, labeled from 0 to n - 1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
For example:
1
2,[[1,0]]
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]
1
4,[[1,0],[2,0],[3,1],[3,2]]
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].
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
You may assume that there are no duplicate edges in the input prerequisites.
Given a binary tree where every node has a unique value, and a target key k, find the value of the closest leaf node to target k in the tree.
Here, closest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children.
In the following examples, the input tree is represented in flattened form row by row. The actual root tree given will be a TreeNode object.
Example 1:
1
2
3
4
5
6
7
8
9
10
Input:
root = [1, 3, 2], k = 1
Diagram of binary tree:
1
/ \
3 2
Output: 2 (or 3)
Explanation: Either 2 or 3 is the closest leaf node to the target of 1.
Example 2:
1
2
3
4
5
Input:
root = [1], k = 1
Output: 1
Explanation: The closest leaf node is the root node itself.
Example 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input:
root = [1,2,3,4,null,null,null,5,null,6], k = 2
Diagram of binary tree:
1
/ \
2 3
/
4
/
5
/
6
Output: 3
Explanation: The leaf node with value 3 (and not the leaf node with value 6) is closest to the node with value 2.
Note:
root represents a binary tree with at least 1 node and at most 1000 nodes.
Every node has a unique node.val in range [1, 1000].
There exists some node in the given binary tree for which node.val == k.
题目大意:
给你一棵树,每个节点的值都不相同。
给定一个节点值,让你找到离这个节点距离最近的叶子节点的值。
Idea:
Shortest path from source to any leaf nodes in a undirected unweighted graph.
Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the target node, and w is the time it takes for a signal to travel from source to target.
Now, we send a signal from a certain node K. How long will it take for all nodes to receive the signal? If it is impossible, return -1.
Note:
N will be in the range [1, 100].
K will be in the range [1, N].
The length of times will be in the range [1, 6000].
All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 1 <= w <= 100.
Idea:
Construct the graph and do a shortest path from K to all other nodes.
Solution 2:
C++ / Bellman-Ford
Time complexity: O(ne)
Space complexity: O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Author: Huahua
// Runtime: 92 ms
classSolution{
public:
intnetworkDelayTime(vector<vector<int>>& times, int N, int K) {
constexpr int MAX_TIME = 101 * 100;
vector<int>dist(N,MAX_TIME);
dist[K-1]=0;
for(inti=1;i<N;++i)
for(constauto& time : times) {
int u = time[0] - 1, v = time[1] - 1, w = time[2];