diff --git a/977.py b/977.py new file mode 100644 index 0000000..4d9fa55 --- /dev/null +++ b/977.py @@ -0,0 +1,28 @@ +# https://leetcode.com/problems/squares-of-a-sorted-array/ + +from typing import List +import unittest + + +class Solution: + def sortedSquares(self, nums: List[int]) -> List[int]: + return sorted([n**2 for n in nums]) + + +class Test(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_first(self): + self.assertEqual( + self.solution.sortedSquares(nums=[-4, -1, 0, 3, 10]), [0, 1, 9, 16, 100] + ) + + def test_second(self): + self.assertEqual( + self.solution.sortedSquares(nums=[-7, -3, 2, 3, 11]), [4, 9, 9, 49, 121] + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/README.md b/README.md index c7b3f8c..8e67dc8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ | Problem Number | Tag | URL | | -------------- | ---------------------- | -------------------------------------------------------------------------- | | 1.py | Hash table | https://leetcode.com/problems/two-sum/ | +| 977.py | Sorting | https://leetcode.com/problems/squares-of-a-sorted-array/ | | 1051.py | | | | 1207.py | | | | 1337.py | Heap | https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix |