Leetcode 1276 Solution

This article provides solution to leetcode question 1276 (closest-divisors)

https://leetcode.com/problems/closest-divisors

Solution

class Solution:
    def closestDivisors(self, num: int) -> List[int]:
        def find(target):
            l = int(sqrt(target))
            while True:
                if target % l == 0:
                    return (l, target // l)
                l -= 1

        l1, r1 = find(num + 1)
        l2, r2 = find(num + 2)

        if r1 - l1 < r2 - l2:
            return [l1, r1]
        else:
            return [l2, r2]