Skip to content

Commit

Permalink
1015.py
Browse files Browse the repository at this point in the history
  • Loading branch information
224apps committed Nov 25, 2020
1 parent 40f1b39 commit a6aa142
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
51 changes: 51 additions & 0 deletions 1001-1100/1015.py
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

52 changes: 52 additions & 0 deletions 1400-1500/1441.py
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]

0 comments on commit a6aa142

Please sign in to comment.