-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
''' | ||
Given a positive integer K, you need to find the length of the smallest positive integer N such that N is divisible by K, and N only contains the digit 1. | ||
Return the length of N. If there is no such N, return -1. | ||
Note: N may not fit in a 64-bit signed integer. | ||
Example 1: | ||
Input: K = 1 | ||
Output: 1 | ||
Explanation: The smallest answer is N = 1, which has length 1. | ||
Example 2: | ||
Input: K = 2 | ||
Output: -1 | ||
Explanation: There is no such positive integer N divisible by 2. | ||
Example 3: | ||
Input: K = 3 | ||
Output: 3 | ||
Explanation: The smallest answer is N = 111, which has length 3. | ||
Constraints: | ||
1 <= K <= 105 | ||
''' | ||
class Solution: | ||
def smallestRepunitDivByK(self, K: int) -> int: | ||
current = 1 | ||
current %= K | ||
seen = set() | ||
seen.add(current) | ||
length = 1 | ||
|
||
|
||
|
||
while True: | ||
if current == 0: | ||
return length | ||
current *= 10 | ||
current += 1 | ||
current %= K | ||
if current in seen: | ||
return -1 | ||
seen.add(current) | ||
length += 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
''' | ||
Given an array target and an integer n. In each iteration, you will read a number from list = {1,2,3..., n}. | ||
Build the target array using the following operations: | ||
Push: Read a new element from the beginning list, and push it in the array. | ||
Pop: delete the last element of the array. | ||
If the target array is already built, stop reading more elements. | ||
You are guaranteed that the target array is strictly increasing, only containing numbers between 1 to n inclusive. | ||
Return the operations to build the target array. | ||
You are guaranteed that the answer is unique. | ||
Example 1: | ||
Input: target = [1,3], n = 3 | ||
Output: ["Push","Push","Pop","Push"] | ||
Explanation: | ||
Read number 1 and automatically push in the array -> [1] | ||
Read number 2 and automatically push in the array then Pop it -> [1] | ||
Read number 3 and automatically push in the array -> [1,3] | ||
Example 2: | ||
Input: target = [1,2,3], n = 3 | ||
Output: ["Push","Push","Push"] | ||
Example 3: | ||
Input: target = [1,2], n = 4 | ||
Output: ["Push","Push"] | ||
Explanation: You only need to read the first 2 numbers and stop. | ||
Example 4: | ||
Input: target = [2,3,4], n = 4 | ||
Output: ["Push","Pop","Push","Push","Push"] | ||
''' | ||
class Solution: | ||
def buildArray(self, target: List[int], n: int) -> List[str]: | ||
ans = [] | ||
best = 0 | ||
|
||
for x in range(1, n+1): | ||
if x in target: | ||
ans.append("Push") | ||
best = max(len(ans), best) | ||
else: | ||
ans.append("Push") | ||
ans.append("Pop") | ||
return ans[:best] | ||
|