Skip to content

Math Others

Table of Contents

1523. Count Odd Numbers in an Interval Range

2829. Determine the Minimum Sum of a k-avoiding Array

2829. Determine the Minimum Sum of a k-avoiding Array - Python Solution
def minimumSum(n: int, k: int) -> int:
    m = min(k // 2, n)
    return (m * (m + 1) + (k * 2 + n - m - 1) * (n - m)) // 2


if __name__ == "__main__":
    n = 5
    k = 4
    print(minimumSum(n, k))  # 18

2579. Count Total Number of Colored Cells

2834. Find the Minimum Possible Sum of a Beautiful Array

1414. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K

319. Bulb Switcher

1780. Check if Number is a Sum of Powers of Three

3091. Apply Operations to Make Sum of Array Greater Than or Equal to k

2310. Sum of Numbers With Units Digit K

2844. Minimum Operations to Make a Special Number

2541. Minimum Operations to Make Array Equal II

2195. Append K Integers With Minimal Sum

2457. Minimum Addition to Make Integer Beautiful

1017. Convert to Base -2

1954. Minimum Garden Perimeter to Collect Enough Apples

1073. Adding Two Negabinary Numbers

1823. Find the Winner of the Circular Game

166. Fraction to Recurring Decimal

166. Fraction to Recurring Decimal - Python Solution
# Math
def fractionToDecimal(numerator: int, denominator: int) -> str:
    if numerator == 0:
        return "0"

    res = []

    if (numerator < 0) ^ (denominator < 0):
        res.append("-")

    numerator, denominator = abs(numerator), abs(denominator)

    # Integer part
    res.append(str(numerator // denominator))
    remainder = numerator % denominator

    if remainder == 0:
        return "".join(res)

    res.append(".")

    # Dictionary to store remainders and their corresponding indices
    remainder_map = {}

    while remainder != 0:
        if remainder in remainder_map:
            res.insert(remainder_map[remainder], "(")
            res.append(")")
            break

        remainder_map[remainder] = len(res)
        remainder *= 10
        res.append(str(remainder // denominator))
        remainder %= denominator

    return "".join(res)


numerator = 4
denominator = 333
print(fractionToDecimal(numerator, denominator))  # 0.(012)

3012. Minimize Length of Array Using Operations

483. Smallest Good Base

972. Equal Rational Numbers

1862. Sum of Floored Pairs

1739. Building Boxes

2443. Sum of Number and Its Reverse

1806. Minimum Number of Operations to Reinitialize a Permutation

458. Poor Pigs

60. Permutation Sequence

2117. Abbreviating the Product of a Range

660. Remove 9

2979. Most Expensive Item That Can Not Be Bought

2647. Color the Triangle Red

Comments