奇安信笔试题代码。
第一题,10X10的正方形内有两个矩形被涂黑,求可裁剪出的最大矩形面积。
class Solution: def judge(self, x0, y0, xn, yn, x1, y1, x2, y2): # 判断两个矩形是否重叠 if xn <= x1 or x0 >= x2 or yn <= y1 or y0 >= y2: return True # 不重叠返回True return False # 重叠返回False即不符合要求 def getMaxArea(self, x1, y1, x2, y2, x3, y3, x4, y4): # write code here x1, x2, y1, y2 = min(x1, x2), max(x1, x2), min(y1, y2), max(y1, y2) x3, x4, y3, y4 = min(x3, x4), max(x3, x4), min(y3, y4), max(y3, y4) ans = 0 for x0 in range(10): for y0 in range(10): for xn in range(x0 + 1, 11): for yn in range(y0 + 1, 11): if self.judge(x0, y0, xn, yn, x1, y1, x2, y2) and self.judge(x0, y0, xn, yn, x3, y3, x4, y4): ans = max(ans, (xn - x0) * (yn - y0)) return ans第二题,candies数组和coins数组,coins[i]为0时的candies[i]加起来,给一个数字n,可修改coins内的连续n个数字为0.
class Solution: def maxCandies(self, candies, coin, n): # write code here if n >= len(candies): return sum(candies) ans, count = 0, 0 for index in range(len(candies)): if coin[index] == 0: count += candies[index] ans = count for i in range(len(candies) - n + 1): tmp = count for j in range(i, i + n): if coin[j] == 1: tmp += candies[j] ans = max(ans, tmp) return ans
全部评论
(1) 回帖