- 博客(20)
- 收藏
- 关注
原创 课后练习8.15:最大公共子图问题
课后练习8.15:证明最大公共子图问题是NP-完全问题根据题意,即提供两个图G1和G2和预算b,要求分别去掉一些顶点后两个图都得到结点数至少为b的子图,且两个子图完全相同。首先,若存在最大公共子图,并已知其顶点,可以在多项式时间内检验是否正确,因此它是NP问题。下面证明其为NP-难问题。已知求一个图的具有b个顶点的独立集的问题是NP-完全问题,现在从这个问题归约到最大公共子图问题,若成
2017-06-18 17:09:31
655
原创 Week Training: 226 Invert Binary Tree
A classic problem, in which we have to invert every subtree of the binary tree. Using the knowledge of binary tree and recursion can we solve it easily, but the awareness of such problems is important
2017-06-17 15:35:26
185
原创 Week Training: 96 Unique Binary Search Trees
Using dynamic programming, we can ignore the tree struct of BST, but just use the property: every BST's left and right subtrees are also BST. Let dp[i] be the number of BST when n is i, and the number
2017-06-10 13:23:15
2917
原创 Week Training: 63 Unique Paths II
An simple enhanced vision of last problem, we just need to judge whether add the path if there's an obstacle. However, the initial state should be paid attention, which wasted me some time.class Sol
2017-06-04 14:05:21
197
原创 Week Training: 62 Unique Paths
An easy problem to solve, we have made it before, but didn't realize it's a dp solution.class Solution {public: int uniquePaths(int m, int n) { int dp[105][105]; for(int i=0;i<1
2017-06-04 13:33:22
179
原创 Week Training: 413 Arithmetic Slices
Many ways to do, using dp, we can let dp[i] be the number of slices until i in the array, so the state transformation is dp[i]=dp[i-1]+1, when the new number is able to make a arithmetic slice with be
2017-05-28 15:33:02
199
原创 Week Training: 486 Predict the Winner
An another dp problem. The key to solve is still to find the dp state, here we chose the score player 1 get more than player 2. dp[i][j] means the score exceeded between position i and j in the array.
2017-05-21 11:07:42
220
原创 Week Training: 494 Target Sum
A good trying for 0-1 package problems. Using dp[i][j] to show the number of methods with ith element in the array and j the sum. The initiative is dp[0][nums[0]] = 1. However, this problem has many t
2017-05-14 11:16:10
153
原创 Week Training: 392 Is Subsequence
An easy task, even no using of DP, just increase the t counter when different or increase both when same.class Solution {public: bool isSubsequence(string s, string t) { int i=0,j=0;
2017-05-06 22:01:06
175
原创 Week Training: 516 Longest Palindromic Subsequence
The main difficulty is we have to find the length of longest palindromic subsequence, but not a substring. Which means the sequence may not be continuous. There are many ways to do, but using dynamic
2017-04-30 10:50:46
199
原创 Week Training: 406 Queue Reconstruction by Height
Before, I traverse the vector times and times to find the one to insert into the new vector each time, with a low efficiency. However, I was impressed by another ingenious method and used it without h
2017-04-23 09:54:03
171
原创 Week Training: 452 Minimum Number of Arrows to Burst Balloons
An easy training of greedy algorithm. We just need to sort the vector of pairs to an ascending order on the basis of its second value, said the right bound of each balloon, making it the position of t
2017-04-16 11:08:34
246
原创 Week Training: 513 Find Bottom Left Tree Value
The purpose of this problem is to find the leftmost value in the last row of the tree, what first come to me is BFS. Using a queue for auxiliary, we traverse the tree row by row, finding the leftmost
2017-04-09 13:44:54
215
原创 Week Training: 542 01 Matrix
Our purpose is to find the distance to the nearest 0 of each element in the matrix. It comes to me first that we should use BFS for each element, but how is still challenging. Since the returned value
2017-04-02 13:40:13
178
原创 Week Training: 515 Find Largest Value in Each Tree Row
First we need a flag to position the number visited in the tree and help to expand the vector returned, then we put it into the recursion, while the first number in the row is pushed in and the other
2017-03-26 21:40:30
183
原创 Week Training: 215 Kth Largest Element in an Array
This is an exercise of the selection algorithm in the class this week just in time. In order to find the kth largest number, we first choose a random pivot, then put the numbers smaller and larger tha
2017-03-19 12:35:03
3611
原创 Week Training: 240 Search a 2D Matrix II
The key to solve this problem is to find a way to simplify searching process. There's an easy method: start from the top right corner of the matrix, because the matrix is ascending in both row and col
2017-03-11 11:37:03
167
原创 Week Training: 241 Different Ways to Add Parentheses
As we are learning divide and conquer, a problem like this can help us understand well. It seems hard, since we don't know how long the string is, there might be lots of conditions. However, through t
2017-03-05 12:14:36
185
原创 Week Training: 508 Most Frequent Subtree Sum
The key to this problem is using STL map. In order to store the occurrence of each sum, we have to add the sum and its occurrence to the map in every traverse, until the tree is fully gone through. Th
2017-02-25 16:15:36
238
原创 Week Training: 495 Teemo Attacking
Although the difficulty of the problem is "medium", it's a easy problem obscured by a story. The fact of the problem is just to find the total length of effective insertion of a integer to a sequence.
2017-02-24 16:00:45
178
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人