首页 > 其他分享 >343. Integer Break刷题笔记

343. Integer Break刷题笔记

时间:2023-05-26 22:08:10浏览次数:52  
标签:return int max self Break range Integer dp 刷题


题目描述 难点主要是考虑整数拆成两个数之后不继续拆分的情况

class Solution:
    def integerBreak(self, n: int) -> int:
        dp = [0]*(n+1)
        dp[2] = 1
        for i in range(3,n+1):
            for j in range(1,i-1):
                dp[i] = max(dp[i],max(j*(i-j),j*dp[i-j]))
        return dp[n]

运行结果:

343. Integer Break刷题笔记_python


标签:return,int,max,self,Break,range,Integer,dp,刷题
From: https://blog.51cto.com/u_16131692/6359341

相关文章

  • 63. Unique Paths II刷题笔记
    问题描述主要是稀奇古怪的边界条件,例如左上角是1,最左边和最上边是1,有多个1,输入为行,或者列classSolution:defuniquePathsWithObstacles(self,obstacleGrid:List[List[int]])->int:m=len(obstacleGrid)n=len(obstacleGrid[0])dp=[0]*m......
  • 62. Unique Paths刷题笔记
    问题描述用动态规划做的,注意最左边和最上边的情况设置从0到n-1的列表可以用list(range(n))classSolution:defuniquePaths(self,m:int,n:int)->int:dp=[1]*mforiinrange(1,n):forjinrange(1,m):dp[j]+=dp[j......
  • 74. Search a 2D Matrix刷题笔记
    题目描述用了两个二分查找法。当然也可以把matrix转为数组来索引classSolution:defsearchMatrix(self,matrix:List[List[int]],target:int)->bool:low=0high=len(matrix)-1mid=0whilelow<=high:mid=(high......
  • 746. Min Cost Climbing Stairs刷题笔记
    题目描述出bug的时候输出打印dp就行classSolution:defminCostClimbingStairs(self,cost:List[int])->int:n=len(cost)+1ifn<=3:returnmin(cost[0],cost[1])dp=[0]*ncost.append(0)foriinrange(2......
  • 70. Climbing Stairs刷题笔记
    问题描述该题考察的是动态规划classSolution:defclimbStairs(self,n:int)->int:dp=[0]*nifn==1:return1dp[0]=1dp[1]=2foriinrange(2,n):dp[i]=dp[i-1]+dp[i-2]ret......
  • 107. Binary Tree Level Order Traversal II刷题笔记
    问题描述自底向上层序搜索python代码#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=rightclassSolution:deflevelOrd......
  • 102. Binary Tree Level Order Traversal刷题笔记
    考察二叉树的层序遍历问题描述leetcode代码:#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=rightclassSolution:deflev......
  • 94. Binary Tree Inorder Traversal刷题笔记
    问题描述中序遍历,用的是递归法,当然也可以用迭代法(栈)代码#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=rightclassSolution......
  • 145. Binary Tree Postorder Traversal刷题笔记
    问题描述后序遍历代码:#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=rightclassSolution:defpostorderTraversal(sel......
  • 144. Binary Tree Preorder Traversal刷题笔记
    问题描述前序遍历。注意嵌套函数里的list应该用append而不是+=来添加元素#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=right......