diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_136_longest_increasing_subsequence.py b/src/my_project/interviews/top_150_questions_round_22/ex_136_longest_increasing_subsequence.py new file mode 100644 index 00000000..7e083be7 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_136_longest_increasing_subsequence.py @@ -0,0 +1,14 @@ +from typing import List +import bisect + + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + tails = [] + for num in nums: + pos = bisect.bisect_left(tails, num) + if pos == len(tails): + tails.append(num) + else: + tails[pos] = num + return len(tails) diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_136_longest_increasing_subsequence.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_136_longest_increasing_subsequence.ts new file mode 100644 index 00000000..3a9f5a71 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_136_longest_increasing_subsequence.ts @@ -0,0 +1,13 @@ +function lengthOfLIS(nums: number[]): number { + const tails: number[] = []; + for (const num of nums) { + let lo = 0, hi = tails.length; + while (lo < hi) { + const mid = (lo + hi) >> 1; + if (tails[mid] < num) lo = mid + 1; + else hi = mid; + } + tails[lo] = num; + } + return tails.length; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_136_longest_increasing_subsequence_round_22.py b/tests/test_150_questions_round_22/test_136_longest_increasing_subsequence_round_22.py new file mode 100644 index 00000000..a9a3c008 --- /dev/null +++ b/tests/test_150_questions_round_22/test_136_longest_increasing_subsequence_round_22.py @@ -0,0 +1,24 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_136_longest_increasing_subsequence import Solution + + +class IncreasingLongestSubquenceTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example1(self): + self.assertEqual(self.solution.lengthOfLIS([10, 9, 2, 5, 3, 7, 101, 18]), 4) + + def test_example2(self): + self.assertEqual(self.solution.lengthOfLIS([0, 1, 0, 3, 2, 3]), 4) + + def test_example3(self): + self.assertEqual(self.solution.lengthOfLIS([7, 7, 7, 7, 7, 7, 7]), 1) + + def test_single_element(self): + self.assertEqual(self.solution.lengthOfLIS([5]), 1) + + +if __name__ == '__main__': + unittest.main()