-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from bisect import bisect_left | ||
from typing import List | ||
import unittest | ||
|
||
|
||
class SolutionShort: | ||
def maxEnvelopes(self, envelopes: List[List[int]]) -> int: | ||
m = [float("inf")] * len(envelopes) | ||
for _, h in sorted(envelopes, key=lambda x: (x[0], -x[1])): | ||
m[bisect_left(m, h)] = h | ||
return sum(v < float("inf") for v in m) | ||
|
||
|
||
def set_val(a, v, i): | ||
a[i:i + 1] = [v] | ||
|
||
|
||
class SolutionReadable: | ||
def maxEnvelopes(self, envelopes: List[List[int]]) -> int: | ||
m = [] | ||
for _, h in sorted(envelopes, key=lambda x: (x[0], -x[1])): | ||
set_val(m, h, bisect_left(m, h)) | ||
return len(m) | ||
|
||
|
||
# noinspection PyMethodMayBeStatic | ||
class MyTestCase(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.solutions = [SolutionShort(), SolutionReadable()] | ||
|
||
def test_examples(self): | ||
for solution in self.solutions: | ||
assert solution.maxEnvelopes([[5,4],[6,4],[6,7],[2,3]]) == 3 | ||
assert solution.maxEnvelopes([[1,1],[1,1],[1,1]]) == 1 | ||
|
||
def test_simple(self): | ||
for solution in self.solutions: | ||
assert solution.maxEnvelopes([[3,5]]) == 1 | ||
|
||
def test_complex(self): | ||
for solution in self.solutions: | ||
assert solution.maxEnvelopes([[100,50],[80, 60], [30, 30], [50,100]]) == 2 | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |