Skip to content

Latest commit

 

History

History
60 lines (43 loc) · 1.57 KB

_2698. Find the Punishment Number of an Integer.md

File metadata and controls

60 lines (43 loc) · 1.57 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : February 15, 2025

Last updated : February 15, 2025


Related Topics : Math, Backtracking

Acceptance Rate : 82.46 %


Solutions

Python

class Solution:
    def pot_sums(self, sqr: str, target_val: int) -> bool :
        if not sqr :
            return target_val == 0

        return any(self.pot_sums(sqr[i + 1:], target_val - int(sqr[:i + 1]))
                   for i in range(0, len(sqr)))

    def punishmentNumber(self, n: int) -> int:
        return sum(x ** 2 for x in range(1, n + 1) if self.pot_sums(str(x ** 2), x))
class Solution:
    def pot_sums(self, sqr: str, target_val: int) -> bool :
        if not sqr :
            return target_val == 0

        return any(self.pot_sums(sqr[i + 1:], target_val - int(sqr[:i + 1]))
                   for i in range(0, len(sqr)))


    def punishmentNumber(self, n: int) -> int:
        output = 0
        for i in range(1, n + 1) :
            sqr = i ** 2
            if self.pot_sums(str(sqr), i) :
                output += sqr

        return output