Skip to content

Commit 218fbc8

Browse files
chore: add LeetCode daily solution
1 parent 0924a7e commit 218fbc8

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Smallest Integer Divisible by K (Medium)
2+
3+
**Problem ID:** 1015
4+
**Date:** 2025-11-25
5+
**Link:** https://leetcode.com/problems/smallest-integer-divisible-by-k/
6+
7+
## Approach
8+
9+
To solve the problem of finding the length of the smallest positive integer \( n \) that consists solely of the digit '1' and is divisible by a given integer \( k \), we can utilize a breadth-first search (BFS) approach to explore potential candidates for \( n \).
10+
11+
### Approach:
12+
13+
1. **Understanding the Problem**:
14+
- The number \( n \) can be represented as a sequence of '1's: \( 1, 11, 111, \ldots \). The goal is to find the smallest such number that is divisible by \( k \).
15+
16+
2. **Using Modulo for Divisibility**:
17+
- Instead of generating large numbers directly, we can work with their remainders when divided by \( k \). This is because if \( n \equiv 0 \mod k \), then \( n \) is divisible by \( k \).
18+
19+
3. **BFS for Exploration**:
20+
- We can treat each number formed by '1's as a node in a graph. Starting from the number '1', we can generate new numbers by appending another '1' to the current number.
21+
- For each number represented by its remainder \( r \) (where \( r = n \mod k \)), we can generate two new remainders:
22+
- \( (r \times 10 + 1) \mod k \) (this represents appending another '1').
23+
- We will keep track of the length of the number (or the number of '1's) in the BFS queue.
24+
25+
4. **Cycle Prevention**:
26+
- To avoid processing the same remainder multiple times, we maintain a set of visited remainders.
27+
28+
5. **Termination**:
29+
- The BFS continues until we either find a remainder of \( 0 \) (indicating divisibility by \( k \)) or exhaust all possibilities without finding such a remainder.
30+
31+
### Data Structures:
32+
- A queue to facilitate the BFS process.
33+
- A set to track visited remainders to prevent cycles.
34+
35+
### Time Complexity:
36+
- The time complexity is \( O(k) \) since there are at most \( k \) different remainders (from \( 0 \) to \( k-1 \)) that we can encounter.
37+
38+
### Space Complexity:
39+
- The space complexity is also \( O(k) \) due to the storage of visited remainders.
40+
41+
### Conclusion:
42+
By using BFS and focusing on remainders rather than constructing large numbers, we efficiently find the length of the smallest integer composed solely of '1's that is divisible by \( k \). If no such integer exists, we return -1.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int smallestRepunitDivByK(int k) {
3+
if (k % 2 == 0 || k % 5 == 0) {
4+
return -1;
5+
}
6+
7+
int length = 1;
8+
int remainder = 1 % k;
9+
10+
while (remainder != 0) {
11+
remainder = (remainder * 10 + 1) % k;
12+
length++;
13+
}
14+
15+
return length;
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var smallestRepunitDivByK = function(k) {
2+
if (k % 2 === 0 || k % 5 === 0) return -1;
3+
4+
let n = 1;
5+
let remainder = 1 % k;
6+
7+
while (remainder !== 0) {
8+
remainder = (remainder * 10 + 1) % k;
9+
n++;
10+
}
11+
12+
return n;
13+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def smallestRepunitDivByK(self, k: int) -> int:
3+
if k % 2 == 0 or k % 5 == 0:
4+
return -1
5+
6+
length = 1
7+
num = 1 % k
8+
9+
while num != 0:
10+
num = (num * 10 + 1) % k
11+
length += 1
12+
13+
return length

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
289289
- 2025-11-22 — [Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/) (Easy) → `Easy/2025-11-22-3190-Find-Minimum-Operations-to-Make-All-Elements-Divisible-by-Three`
290290
- 2025-11-23 — [Greatest Sum Divisible by Three](https://leetcode.com/problems/greatest-sum-divisible-by-three/) (Medium) → `Medium/2025-11-23-1262-Greatest-Sum-Divisible-by-Three`
291291
- 2025-11-24 — [Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/) (Easy) → `Easy/2025-11-24-1018-Binary-Prefix-Divisible-By-5`
292+
- 2025-11-25 — [Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/) (Medium) → `Medium/2025-11-25-1015-Smallest-Integer-Divisible-by-K`

0 commit comments

Comments
 (0)