- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定一个由单位正方形组成的矩形 (m,n) 的尺寸,输出与矩形对角线相交的单位正方形的数量 - 包括边界和顶点。
我的算法通过循环遍历所有单位正方形来解决这个问题(假设可以绘制我们的对角线从 (0,0) 到 (m,n)
我的算法解决了 10 个测试中的 9 个,但效率太低,无法在给定时间内解决第十个测试。
我对所有提高效率的建议都持开放态度,但以提出特定问题的名义……我似乎在自己的逻辑中脱节了关于添加 break 语句以从过程中删除一些步骤的问题。我的想法是,这应该无关紧要,但它确实会影响结果,我一直无法弄清楚为什么。
所以,有人可以帮助我了解如何插入不影响输出的中断。
或者如何消除循环。我目前正在使用嵌套循环。
所以,是的,我认为我的问题是算法而不是语法。
def countBlackCells(m, n):
counter=0
y=[0,0]
testV=0
for i in xrange(n): #loop over m/x first
y[0]=float(m)/n*i
y[1]=float(m)/n*(i+1)
#print(str(y))
for j in xrange(m): #loop over every n/y for each x
if((y[0]<=(j+1) and y[0]>=j) or (y[1]>=(j) and y[1]<=j+1)):#is min of line in range inside teh box? is max of line?
counter+=1
#testV += 1
else: pass # break# thinking that once you are beyond the line in either direction, your not coming back to it by ranging up m anymore. THAT DOESN"T SEEM TO BE THE CASE
#tried adding a flag (testV), so that inner loop would only break if line was found and then lost again, still didn't count ALL boxes. There's something I'm not understanding here.
return counter
一些样本,输入/输出
输入:人数:3米:4输出:6
输入:人数:3米:3输出:7
输入:人数:33米:44输出:86
最佳答案
求 G
- m 和 n 的最大公约数。
如果 G > 1
则对角线与 G-1
内部顶点相交,接触(不相交)2*(G-1)
单元格.
在这些内部顶点之间有 G 个边互为质数的子矩形 M x N (m/G x n/G)
现在考虑互质M和N的情况。这种矩形的对角线不与除起点和终点外的任何顶点相交。但它必须与 M 条垂直线和 N 条水平线相交,并且在每个交叉点对角线都进入新的单元格,因此它与 M + N - 1
个单元格相交(减去 1
为考虑垂直线和水平线相交的第一个角)
因此使用这些线索并推导出最终解决方案。
关于python codefights Count The Black Boxes,建模矩形的对角线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42673006/
我很难用 JavaScript 解决这个问题 给你一个字符串 s,它由英文字母、标点符号、空白字符和方括号组成。保证 s 中的括号形成一个规则的括号序列。 你的任务是反转每对匹配括号中的字符串,从最里
根据问题陈述: Write a solution with O(n) time complexity and O(1) additional space complexity. Given an ar
到目前为止,我已经做到了。我坚持递归。我不知道如何前进,加入和倒退等等。 def callrecursion(s): a=s.index('(') z=len(s) - string[
给定一个整数序列作为一个数组,确定是否可以通过从数组中删除不超过一个元素来获得一个严格递增的序列。 例子 对于序列[1, 3, 2, 1],输出应该是: almostIncreasingSequenc
我在介绍中遇到了 Codefights Arcade 问题 13 中的一个问题。以下是到目前为止的问题陈述和我的代码。我对如何解决这个问题的想法是递归地向下/在嵌套序列内工作,当我到达一个序列(没有嵌
我正在 codefights.com 练习编码。这是查看他们要求的链接:link .我很难理解这些 return 语句在下面的代码中到底做了什么。 我的代码: def avoidObstacles(i
我在使用 python3 的 CodeFights 上遇到了“areSimilar”问题。 提示指出“如果可以通过交换一个数组中的最多一对元素从另一个数组中获得一个数组,则两个数组被称为相似。 给定两
根据 Codefighters: 注意:编写一个具有 O(n) 时间复杂度和 O(1) 额外空间复杂度的解决方案,因为这是您在真实面试中被要求做的事情。 给定一个仅包含从 1 到 a.length 范
我正在尝试从 CodeFights 解决以下问题。问题结束后,我用 Java 留下了答案。该代码适用于除最后一个问题之外的所有问题。报时限异常。我该怎么做才能让它运行在 3000 毫秒以下(CodeF
我日常任务的目标是计算给定数组 (int [] a) 中表示了多少组整数(从 1 到 10000、10001 到 20000 等,直到 1000000)(例如,a[ 0] =2,所以属于第1组)。 我
我目前正在研究 Code Fights Arcade 的 arrayChange 关卡。这是目标: 给你一个整数数组。在每一步中,您都可以 恰好将其元素之一增加一。找到最小数量的 从输入中获得严格递增
我无法通过最后的隐藏测试。你能告诉我我错过了什么吗?提前致谢。 语句如下:给定一个整数序列作为数组,确定是否可以通过从数组中删除不超过一个元素来获得严格递增的序列。 boolean almostInc
给定一个由单位正方形组成的矩形 (m,n) 的尺寸,输出与矩形对角线相交的单位正方形的数量 - 包括边界和顶点。 我的算法通过循环遍历所有单位正方形来解决这个问题(假设可以绘制我们的对角线从 (0,0
我在 CodeFights.com 上看到过 C++ 函数的这种语法形式: int i,j,w,myFunction(auto s, auto v) { // here, i, j,and w a
来自 Codefighters: Note: Write a solution with O(n) time complexity and O(1) additional space complexi
我正在做 codefight 的挑战:扫雷。 描述: 我的代码如下: def minesweeper(matrix): for x in range(len(matrix)):
我正在 CodeFights.com 上尝试 sumOFTwo 挑战,但不幸的是我无法完成它以查看解决方案。我所有的测试都成功了,直到第 15 次隐藏测试,它说它超过了时间限制。 挑战是 - 你有两个
所以我必须找到包含 n 位数字的最大数字。例如,如果 n=2,那么 largestNumber = 99。这就是我的答案。 function largestNumber(n) { var nu
我是一名优秀的程序员,十分优秀!