题解 | #剪绳子#
剪绳子
https://www.nowcoder.com/practice/57d85990ba5b440ab888fc72b0751bf8
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param n int整型 # @return int整型 # class Solution: def build(self, n, best_n): best = -1 for i in range(2, int(n / 2) + 1): tmp = best_n[i] * best_n[n - i] if tmp > best: best = tmp return best def cutRope(self, n: int): # write code here if n < 4: return n - 1 best_n = {1:1, 2:2, 3:3} for i in range(2, n - 1): if i not in best_n: best_n[i] = self.build(i, best_n) print(best_n) best = -1 for i in range(2, int(n / 2) + 1): val_i = best_n[i] * best_n[n - i] if val_i > best: best = val_i return best