Problem solution · Python

Minimum Stability Factor of Array

Minimum Stability Factor of Array: a Python solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Binary search
Source
Kamyu LeetCode Solutions
Length
62 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Minimum Stability Factor of Array, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 62 lines of Python from the credited upstream file minimum-stability-factor-of-array.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMinimum Stability Factor of Array · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(nlogn * logr)# Space: O(nlogn) # number theory, binary search, rmq, sparse table, greedyclass Solution(object):    def minStable(self, nums, maxC):        """        :type nums: List[int]        :type maxC: int        :rtype: int        """        def gcd(a, b):            while b:                a, b = b, a%b            return a         def binary_search_right(left, right, check):            while left <= right:                mid = left + (right-left)//2                if not check(mid):                    right = mid-1                else:                    left = mid+1            return right         # RMQ - Sparse Table        # Template: https://github.com/kamyu104/GoogleCodeJam-Farewell-Rounds/blob/main/Round%20D/genetic_sequences2.py3        # Time:  ctor:  O(NlogN) * O(fn)        #        query: O(fn)        # Space: O(NlogN)        class SparseTable(object):            def __init__(self, arr, fn):                self.fn = fn                self.bit_length = [0]                n = len(arr)                k = n.bit_length()-1  # log2_floor(n)                for i in xrange(k+1):                    self.bit_length.extend(i+1 for _ in xrange(min(1<<i, (n+1)-len(self.bit_length))))                self.st = [[0]*n for _ in xrange(k+1)]                self.st[0] = arr[:]                for i in xrange(1, k+1):  # Time: O(NlogN) * O(fn)                    for j in xrange((n-(1<<i))+1):                        self.st[i][j] = fn(self.st[i-1][j], self.st[i-1][j+(1<<(i-1))])                    def query(self, L, R):  # Time: O(fn)                i = self.bit_length[R-L+1]-1  # log2_floor(R-L+1)                return self.fn(self.st[i][L], self.st[i][R-(1<<i)+1])         def check(l):            cnt = 0            i = 0            while i+l-1 < len(nums):                if rmq.query(i, i+l-1) >= 2:                    cnt += 1                    i += l                else:                    i += 1            return cnt > maxC         rmq = SparseTable(nums, gcd)        return binary_search_right(1, len(nums), check) 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗