Skip to content

Commit 0a07d2f

Browse files
authored
Create apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k.py
1 parent 3d4ffc9 commit 0a07d2f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
# math
5+
class Solution(object):
6+
def minOperations(self, k):
7+
"""
8+
:type k: int
9+
:rtype: int
10+
"""
11+
# reference: https://stackoverflow.com/questions/15390807/integer-square-root-in-python
12+
def isqrt(n):
13+
a, b = n, (n+1)//2
14+
while b < a:
15+
a, b = b, (b+n//b)//2
16+
return a
17+
18+
def ceil_divide(a, b):
19+
return (a+b-1)//b
20+
21+
x = isqrt(k)
22+
return (x-1)+(ceil_divide(k, x)-1)

0 commit comments

Comments
 (0)