Skip to content

Commit ab61fc4

Browse files
Add files via upload
1 parent f06885f commit ab61fc4

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 1018. Binary Prefix Divisible By 5
2+
# Easy
3+
# Topics
4+
# premium lock icon
5+
# Companies
6+
# Hint
7+
# You are given a binary array nums (0-indexed).
8+
9+
# We define xi as the number whose binary representation is the subarray nums[0..i] (from most-significant-bit to least-significant-bit).
10+
11+
# For example, if nums = [1,0,1], then x0 = 1, x1 = 2, and x2 = 5.
12+
# Return an array of booleans answer where answer[i] is true if xi is divisible by 5.
13+
14+
15+
16+
# Example 1:
17+
18+
# Input: nums = [0,1,1]
19+
# Output: [true,false,false]
20+
# Explanation: The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10.
21+
# Only the first number is divisible by 5, so answer[0] is true.
22+
# Example 2:
23+
24+
# Input: nums = [1,1,1]
25+
# Output: [false,false,false]
26+
27+
28+
# Constraints:
29+
30+
# 1 <= nums.length <= 105
31+
# nums[i] is either 0 or 1.
32+
33+
from typing import List
34+
35+
class Solution:
36+
def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
37+
result = []
38+
current = 0
39+
for bit in nums:
40+
current = (current * 2 + bit) % 5
41+
result.append(current == 0)
42+
return result

0 commit comments

Comments
 (0)