Skip to content

Commit

Permalink
Add LC 354
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyannn committed May 25, 2022
1 parent cfd9a65 commit 59f81a8
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lc_0354_envelopes.py
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()

0 comments on commit 59f81a8

Please sign in to comment.