Skip to content

Commit ad8ba7d

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 204
1 parent ff2b230 commit ad8ba7d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
- [200 Number of Islands](https://leetcode.com/problems/number-of-islands/description/)
191191
- [201 Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/description/)
192192
- [202 Happy Number](https://leetcode.com/problems/happy-number/description/)
193+
- [204 Count Primes](https://leetcode.com/problems/count-primes/description/)
193194
- [205 Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/description/)
194195
- [207 Course Schedule](https://leetcode.com/problems/course-schedule/description/)
195196
- [208 Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/description/)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def countPrimes(self, n: int) -> int:
5+
"""
6+
Given an integer n, return the number of prime numbers that are strictly less
7+
than n.
8+
"""
9+
# Time Complexity: O(n*sqrt(n))
10+
# Space Complexity: O(n)
11+
if n == 0 or n == 1:
12+
return 0
13+
primes = [True for i in range(n)]
14+
primes[0] = primes[1] = False
15+
p = 2
16+
while p * p < n:
17+
if primes[p]:
18+
for i in range(p * p, n, p):
19+
primes[i] = False
20+
p += 1
21+
return sum(primes)

tests/test_204_count_primes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._204_count_primes import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["n", "expected"],
8+
argvalues=[
9+
(10, 4),
10+
(0, 0),
11+
(1, 0),
12+
],
13+
)
14+
def test_func(n: int, expected: int):
15+
"""Tests the solution of a LeetCode problem."""
16+
primes = Solution().countPrimes(n)
17+
assert primes == expected

0 commit comments

Comments
 (0)