ๆ•ดๆ•ฐๆ‹†ๅˆ† ๐ŸŒŸ๐ŸŒŸ๐ŸŒŸไธญ็ญ‰

่ฏพๅŽไฝœไธš

้—ฎ้ข˜ๆ่ฟฐ

ๅŽŸๆ–‡้“พๆŽฅ๏ผš343. ๆ•ดๆ•ฐๆ‹†ๅˆ†

็ป™ๅฎšไธ€ไธชๆญฃๆ•ดๆ•ฐ n ๏ผŒๅฐ†ๅ…ถๆ‹†ๅˆ†ไธบ k ไธช ๆญฃๆ•ดๆ•ฐ ็š„ๅ’Œ๏ผˆ k >= 2 ๏ผ‰๏ผŒๅนถไฝฟ่ฟ™ไบ›ๆ•ดๆ•ฐ็š„ไน˜็งฏๆœ€ๅคงๅŒ–ใ€‚

่ฟ”ๅ›ž ไฝ ๅฏไปฅ่Žทๅพ—็š„ๆœ€ๅคงไน˜็งฏ ใ€‚

็คบไพ‹ 1:

่พ“ๅ…ฅ: n = 2
่พ“ๅ‡บ: 1
่งฃ้‡Š: 2 = 1 + 1, 1 ร— 1 = 1ใ€‚
็คบไพ‹ 2:

่พ“ๅ…ฅ: n = 10
่พ“ๅ‡บ: 36
่งฃ้‡Š: 10 = 3 + 3 + 4, 3 ร— 3 ร— 4 = 36ใ€‚

ๆ็คบ:

2 <= n <= 58

ไปฃ็ ๅฎž็Žฐ

Java

class Solution {
    public int integerBreak(int n) {
        int res = n / 3;
        int mod = n % 3;

        if(n <= 2){
            return 1;
        }

        if(n <= 3){
            return 2;
        }

        if(mod == 0){
            return (int)Math.pow(3, res);
        }else if(mod == 1){
            return (int)Math.pow(3, res - 1) * 4;
        }else{
            return (int)Math.pow(3, res) * 2;
        }
    }
}

Python

class Solution(object):
    def integerBreak(self, n):
        """
        :type n: int
        :rtype: int
        """
        res = n // 3
        mod = n % 3

        if n <= 2:
            return 1
        elif n == 3:
            return 2

        if mod == 0:
            return 3 ** res
        elif mod == 1:
            return 3 ** (res - 1) * 4
        else:
            return 3 ** res * 2

C++

class Solution {
public:
    int integerBreak(int n) {
        int res = n / 3;
        int mod = n % 3;

        if (n <= 2) {
            return 1;
        } else if (n == 3) {
            return 2;
        }

        if (mod == 0) {
            return pow(3, res);
        } else if (mod == 1) {
            return pow(3, res - 1) * 4;
        } else {
            return pow(3, res) * 2;
        }
    }
};

Go

func integerBreak(n int) int {
    res := n / 3
    mod := n % 3

    if n <= 2 {
        return 1
    } else if n == 3 {
        return 2
    }

    if mod == 0 {
        return int(math.Pow(3, float64(res)))
    } else if mod == 1 {
        return int(math.Pow(3, float64(res-1))) * 4
    } else {
        return int(math.Pow(3, float64(res))) * 2
    }
}

ๅ‘่กจ่ฏ„่ฎบ

ๅŽๆ‰่ƒฝ่ฏ„่ฎบ