From 29b60e7f52be3814f1b6b9547ee5dad9a30dc92f Mon Sep 17 00:00:00 2001 From: Eduardo Santos Date: Wed, 10 Jul 2024 01:50:36 -0300 Subject: [PATCH] Improve README.md and 217 in O(n^2) --- README.md | 11 ++++++++--- python3/217/217-contains-duplicate-a.py | 12 ++++++++---- python3/217/217-contains-duplicate-b.py | 2 +- python3/217/217-contains-duplicate-c.py | 2 +- python3/217/217-contains-duplicate-d.py | 2 +- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 51322c3..aa39a28 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,17 @@
-

Solutions to LeetCode problems written in Python 3.

+

Solutions to LeetCode problems written in Python 3

-The algorithm or technique used, time and space complexities, and optimality are given as comments at the top of each file. Optimality always favors time. +The algorithm or technique used, time complexity, auxiliary space, and +optimality are given as comments at the top of each file. Optimality always +favors time. > [!NOTE] -> [Binary logarithms](https://en.wikipedia.org/wiki/Binary_logarithm) are notated $\mathop{{}^{}\mathrm{lb}} x$ (e.g., `n lb n`) in compliance with [ISO 80000-2:2019](https://www.iso.org/standard/64973.html). +> [Binary logarithms](https://en.wikipedia.org/wiki/Binary_logarithm), as in +> [logarithmic time](https://en.wikipedia.org/wiki/Time_complexity#Logarithmic_time), +> are notated $\mathop{{}^{}\mathrm{lb}} x$ (e.g., `lb n`) in compliance with +> [ISO 80000-2:2019](https://www.iso.org/standard/64973.html). ## 🧩 Solved diff --git a/python3/217/217-contains-duplicate-a.py b/python3/217/217-contains-duplicate-a.py index de09d10..2cf40aa 100644 --- a/python3/217/217-contains-duplicate-a.py +++ b/python3/217/217-contains-duplicate-a.py @@ -1,15 +1,19 @@ # Programming language: Python 3 # Algorithm/technique: nested iteration # Time complexity: O(n^2) -# Space complexity: O(1) +# Auxiliary space: O(1) # Optimal: no # Notes: class Solution: def containsDuplicate(self, nums: list[int]) -> bool: - for i, num in enumerate(nums): - for j in range(i + 1, len(nums)): - if num == nums[j]: + n = len(nums) + + # enumerate does not allow the last item to be skipped without slicing, + # which would require O(n) auxiliary space. + for i in range(n - 1): + for j in range(i + 1, n): + if nums[i] == nums[j]: return True return False diff --git a/python3/217/217-contains-duplicate-b.py b/python3/217/217-contains-duplicate-b.py index f0a1ae1..05b1af6 100644 --- a/python3/217/217-contains-duplicate-b.py +++ b/python3/217/217-contains-duplicate-b.py @@ -1,7 +1,7 @@ # Programming language: Python 3 # Algorithm/technique: sorting # Time complexity: O(n lb n) -# Space complexity: O(1) +# Auxiliary space: O(1) # Optimal: no # Notes: diff --git a/python3/217/217-contains-duplicate-c.py b/python3/217/217-contains-duplicate-c.py index d02b7aa..ec10908 100644 --- a/python3/217/217-contains-duplicate-c.py +++ b/python3/217/217-contains-duplicate-c.py @@ -1,7 +1,7 @@ # Programming language: Python 3 # Algorithm/technique: set # Time complexity: O(n) -# Space complexity: O(n) +# Auxiliary space: O(n) # Optimal: yes # Notes: diff --git a/python3/217/217-contains-duplicate-d.py b/python3/217/217-contains-duplicate-d.py index 94ea39f..750d8a7 100644 --- a/python3/217/217-contains-duplicate-d.py +++ b/python3/217/217-contains-duplicate-d.py @@ -1,7 +1,7 @@ # Programming language: Python 3 # Algorithm/technique: set # Time complexity: O(n) -# Space complexity: O(n) +# Auxiliary space: O(n) # Optimal: yes # Notes: