Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4ee9fb3

Browse files
python: add problem 300 and unittest
1 parent fb88aa4 commit 4ee9fb3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
from typing import List
3+
4+
5+
class Solution_291_300(object):
6+
def lengthOfLIS(self, nums: List[int]) -> int:
7+
"""
8+
300
9+
:param nums:
10+
:return:
11+
"""
12+
if len(nums) <= 1:
13+
return len(nums)
14+
max_len_arr = [1] * len(nums)
15+
arr_len = len(nums)
16+
for i in range(0, arr_len):
17+
max_len_i = max_len_arr[i]
18+
for j in range(0, i):
19+
if nums[j] < nums[i]:
20+
max_len_j = max_len_arr[j] + 1
21+
if max_len_j > max_len_i:
22+
max_len_i = max_len_j
23+
max_len_arr[i] = max_len_i
24+
max_len = max(max_len_arr)
25+
return max_len
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest import TestCase
3+
from sln_201_300.solution_291_300 import Solution_291_300
4+
5+
6+
class TestSolution_291_300(TestCase):
7+
def setUp(self):
8+
self.sln = Solution_291_300()
9+
10+
def test_lengthOfLIS(self):
11+
arr = [10, 9, 2, 5, 3, 7, 101, 18]
12+
self.assertEqual(self.sln.lengthOfLIS(arr), 4)
13+
arr1 = [4, 10, 4, 3, 8, 9]
14+
self.assertEqual(self.sln.lengthOfLIS(arr1), 3)

0 commit comments

Comments
 (0)