Skip to content
LC-1447 Medium LeetCode

1447. Simplified Fractions

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 68% Topics: Math, String, Number Theory
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n^2 * logn)
# Space: O(n^2)

import fractions

class Solution(object):
    def simplifiedFractions(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        lookup = set()
        for b in xrange(1, n+1):
            for a in xrange(1, b):
                g = fractions.gcd(a, b)
                lookup.add((a//g, b//g))
        return map(lambda x: "{}/{}".format(*x), lookup)

Solution from kamyu104/LeetCode-Solutions · MIT