Skip to content

Commit c340390

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 1018
1 parent df2e5fb commit c340390

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
- [933 Number of Recent Cells](https://leetcode.com/problems/number-of-recent-calls/description/)
279279
- [983 Minimum Cost for Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/description/)
280280
- [1009 Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/description/)
281+
- [1018 Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/description/)
281282
- [1027 Longest Arithmetic Subsequence](https://leetcode.com/problems/longest-arithmetic-subsequence/description/)
282283
- [1035 Uncrossed Lines](https://leetcode.com/problems/uncrossed-lines/description/)
283284
- [1137 N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/description/)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
8+
"""
9+
You are given a binary array nums (0-indexed).
10+
11+
We define xi as the number whose binary representation is the subarray
12+
nums[0..i] (from most-significant-bit to least-significant-bit).
13+
14+
- For example, if nums = [1,0,1], then x0 = 1, x1 = 2, and x2 = 5.
15+
16+
Return an array of booleans answer where answer[i] is true if xi is divisible
17+
by 5.
18+
"""
19+
# Time Complexity: O(n)
20+
# Space Complexity: O(1)
21+
res = []
22+
remain = 0
23+
for bit in nums:
24+
remain = ((remain << 1) | bit) % 5
25+
res.append(remain == 0)
26+
return res
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._1018_binary_prefix_divisible_by_5 import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([0, 1, 1], [True, False, False]),
12+
([1, 1, 1], [False, False, False]),
13+
],
14+
)
15+
def test_func(nums: List[int], expected: List[bool]):
16+
"""Tests the solution of a LeetCode problem."""
17+
prefixes = Solution().prefixesDivBy5(nums)
18+
assert prefixes == expected

0 commit comments

Comments
 (0)