Skip to content

Commit b04d0d5

Browse files
committed
add 300
1 parent 98c08c5 commit b04d0d5

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ LeetCode solution (C++ and Python)
2424
064 [Minimum Path Sum](https://leetcode-cn.com/problems/minimum-path-sum/) | [C++](https://github.com/codename1995/leetcodehub/blob/master/cpp/64_Minimum_Path_Sum/64_Minimum_Path_Sum.cpp) | 1. DP|
2525
070 [Climbing Stairs](https://leetcode-cn.com/problems/climbing-stairs/ ) | [C++](https://github.com/codename1995/leetcodehub/blob/master/cpp/70_Climbing_Stairs/70_Climbing_Stairs.cpp) | 1. DP|
2626
121 [Best Time To Buy And Sell Stock](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/submissions/ ) | [C++](https://github.com/codename1995/leetcodehub/blob/master/cpp/121_Best_Time_To_Buy_And_Sell_Stock/121_Best_Time_To_Buy_And_Sell_Stock.cpp) | 1. DP|
27-
322 [Coin Change](https://leetcode-cn.com/problems/coin-change/ ) | [C++](https://github.com/codename1995/leetcodehub/blob/master/cpp/322_Coin_Change/322_Coin_Change.cpp) | 1. DP (Top-down)|
27+
300 [Longest Increasing Subsequence](https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/zui-chang-shang-sheng-zi-xu-lie-dong-tai-gui-hua-2/ ) | [C++](https://github.com/codename1995/leetcodehub/blob/master/cpp/300_Longest_Increasing_Subsequence/300_Longest_Increasing_Subsequence.cpp) | 1. DP O(n^2) <br> 2. DP + Binary Searcy O(nlogn)|
28+
322 [Coin Change](https://leetcode-cn.com/problems/coin-change/ ) | [C++](https://github.com/codename1995/leetcodehub/blob/master/cpp/322_Coin_Change/322_Coin_Change.cpp) | 1. DP (Bottom-Up)|
2829
| 337 [House Robber III](https://leetcode-cn.com/problems/house-robber-iii/ ) | [C++](https://github.com/codename1995/leetcodehub/blob/master/cpp/337_House_Robber_III/337_House_Robber_III.cpp) | 1. 树型DP,子函数返回{MaxWithCurrNode, MaxWithoutCurrNode} |
2930

3031
# Sort by coding-interview (剑指offer)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <string>
2+
#include <iostream>
3+
#include <vector>
4+
#include <iterator>
5+
#include <algorithm>
6+
#include <stack>
7+
#include <random>
8+
#include <numeric>
9+
#include <iomanip>
10+
#include <map>
11+
#include <cmath>
12+
#include <cstring>
13+
14+
using namespace std;
15+
16+
class Solution {
17+
public:
18+
int lengthOfLIS(vector<int>& nums) {
19+
// 2. DP + BinarySearch
20+
// O(n logn)
21+
// It works, but hard to recall it in an interview
22+
// Specifically, update an array ('tails') which store a increasing subarray
23+
// Visit all numbers, and
24+
// if one number larger than all numbers in 'tails', then append it at last
25+
// otherwise, insert it into 'tails' by binary search.
26+
if (nums.size() == 0) return 0;
27+
vector<int> tails(1, nums[0]);
28+
for (int i = 1; i < nums.size(); ++i)
29+
{
30+
if (nums[i] > tails.back()) tails.push_back(nums[i]);
31+
else {
32+
int l = 0, r = tails.size() - 1;
33+
while (l < r)
34+
{
35+
int m = (l + r) >> 1;
36+
if (tails[m] < nums[i]) l = m + 1;
37+
else r = m;
38+
}
39+
tails[l] = nums[i];
40+
}
41+
}
42+
return tails.size();
43+
44+
// 1. DP
45+
// O(n^2)
46+
if (nums.size() == 0) return 0;
47+
vector<int> dp(nums.size(), 1);
48+
for (int i = 1; i < nums.size(); ++i) {
49+
for (int j = 0; j < i; ++j) {
50+
if (nums[j] < nums[i])
51+
dp[i] = max(dp[i], dp[j] + 1);
52+
}
53+
}
54+
int maxx = dp[0];
55+
for (auto n : dp)
56+
maxx = max(maxx, n);
57+
return maxx;
58+
}
59+
};

0 commit comments

Comments
 (0)