From 7b6cab8386033457eaabc41a35e160ef0035fdf7 Mon Sep 17 00:00:00 2001 From: SherlockGuo <38394051+SherlockGuo@users.noreply.github.com> Date: Tue, 17 Nov 2020 17:49:13 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90300.=E6=9C=80=E9=95=BF=E4=B8=8A?= =?UTF-8?q?=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97=E3=80=91=E3=80=90Python?= =?UTF-8?q?=E3=80=91=20(#507)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add python * add python * add python Co-authored-by: LockyGuo Co-authored-by: labuladong --- ...36\345\255\220\345\272\217\345\210\227.md" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\256\276\350\256\241\357\274\232\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\256\276\350\256\241\357\274\232\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index a2a0460..edef473 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\256\276\350\256\241\357\274\232\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\350\256\276\350\256\241\357\274\232\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -217,6 +217,51 @@ public int lengthOfLIS(int[] nums) { ======其他语言代码====== +```python 动态规划 +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + n = len(nums) + f = [1] * (n) + + for i in range(n): + for j in range(i): + if nums[j] < nums[i]: + f[i] = max(f[i], f[j] + 1) + + res = 0 + for i in range(n): + res = max(res, f[i]) + return res +``` + +```python 二分查找 +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + stack = [] + + def find_index(num): + l, r = 0, len(stack) + while l < r: + mid = l + r >> 1 + if stack[mid] >= num: + r = mid + else: + l = mid + 1 + + return r + + + for num in nums: + if not stack or num > stack[-1]: + stack.append(num) + else: + position = find_index(num) + stack[position] = num + + return len(stack) +``` + + [Kian](https://github.com/KianKw/) 提供 C++ 代码 ```c++ @@ -257,3 +302,4 @@ public: } }; ``` + -- GitLab