Skip to content

Commit

Permalink
Update 动态规划设计:最长递增子序列.md
Browse files Browse the repository at this point in the history
create problem-300 c++
  • Loading branch information
csguojin authored Nov 11, 2020
1 parent 8b8f413 commit 84569b8
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion 动态规划系列/动态规划设计:最长递增子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,45 @@ public int lengthOfLIS(int[] nums) {
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
======其他语言代码======

[Kian](https://github.com/KianKw/) 提供 C++ 代码

```c++
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
/* len 为牌的数量 */
int len = nums.size();
vector<int> top(len, 0);
/* 牌堆数初始化为0 */
int piles = 0;
for (int i = 0; i < len; i++) {
/* nums[i] 为要处理的扑克牌 */
int poker = nums[i];

/***** 搜索左侧边界的二分查找 *****/
int left = 0, right = piles;
while (left < right) {
int mid = left + (right - left) / 2;
if (top[mid] > poker) {
right = mid;
} else if (top[mid] < poker) {
left = mid + 1;
} else if (top[mid] == poker) {
right = mid;
}
}
/*********************************/

/* 没找到合适的牌堆,新建一堆 */
if (left == piles)
piles++;
/* 把这张牌放到牌堆顶 */
top[left] = poker;
}
/* 牌堆数就是 LIS 长度 */
return piles;
}
};
```

0 comments on commit 84569b8

Please sign in to comment.