- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在研究下面的例子
Given: 2 sorted arrays of integers(e.g. A = [1,2,3,4,5], B = [6, 7, 8, 9, 10]) and answer(e.g 13)
Find: What I have to do is to find pair of indices(1 in each array) of those two elements in both arrays so when I add them then it should be equal to the given answer.
我使用了以下两种解决方案。但是他们两个的问题是我正在循环遍历两个数组。首先我循环遍历第一个数组,然后在这个数组中循环通过第二个数组。并在这两个索引上添加元素以查看其添加是否等于答案。它工作正常并输出正确答案。问题是表现。如果我们在两个数组中都有 10,000 个整数,那么这些循环将占用大量资源,例如时间、CPU 和内存来执行并获得答案。
如何更有效地解决上述特定问题?
function find (A1, A2, ans) {
var a = [];
for (var i = 0, len1 = A1.length; i < len1; i++) {
for (var j = 0, len2 = A2.length; j < len2; j++) {
if (ans === A1[i] + A2[j]) {
a.push(i + ', ' + j);
}
}
}
return a;
}
第二个
function search (A, B, ans) {
var arr = [];
A.map(function (v, i) {
B.filter(function (val, ind) {
if (ans === v + val) arr.push(i + ', ' +ind);
});
});
return arr;
}
最佳答案
解决方案1
您可以使用较少的元素遍历数组的所有元素,直到 answer 和在第二个数组中的二进制搜索 (answer - array[index])
,此算法的复杂度为 O(N log M )
。
解决方案2
或者您可以在线性时间内合并两个数组并应用以下算法在线性时间内找到对。合并时,保留大小为 N+M
的反向映射数组 mapA 和 mapB,其中 mapA[i] 指向数组 A 中的索引,从合并数组的第 ith 个数组来否则为-1。对 mapB 也做类似的事情。
/* Merge the arrays */
mapA, mapB, MA all are arrays of size M+N, initialized with all -1
i = 0, j = 0
while(i < M && j < N)
if(A[i] < B[j])
MA[i+j] = A[i];
mapA[i+j] = i++;
else
MA[i+j] = B[j];
mapB[i+j] = j++;
while(i < M)
MA[i+j] = A[i];
mapA[i+j] = i++;
while(j < N)
MA[i+j] = B[j];
mapB[i+j] = j++;
/* Search the pair */
i = 0
j = N + M - 1
while(i < j){
if(mapA[i] == -1) i++;
else if(mapB[j] == -1) j--;
else if (MA[i] + MA[j] == answer) return pair(mapA[i], mapB[j]);
else if (MA[i] + MA[j] < answer) i++;
else if (MA[i] + MA[j] > answer) j--;
}
return null_pair; // no answer found
解决方案3
有一个更好的算法(受 3 和算法的启发)在线性时间内工作,即常量空间中的 O(N + M)。
i = 0
j = M - 1
while(i < N && j >= 0){
if (A[i] + B[j] == answer) return pair(i, j);
else if (A[i] + B[j] < answer) i++;
else if (A[i] + B[j] > answer) j--;
}
return null_pair; // no answer found
证明
让我们假设 A[x] + B[y] = answer
。然后 x
将首先到达 i
或 j
将首先到达 y
或者我们将找到一些其他对A[i] + B[j] = 答案
。不失一般性,我们假设 x
首先变成 i
。现在对于所有 j > y
,A[i] + B[j] > answer
所以 j 最终会得出答案。如果没有答案,我们将退出循环。
关于javascript - 使用 javascript 使用算法解决问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26098797/
一、前言: Python如何使用OS模块调用cmd 在os模块中提供了两种调用 cmd 的方法,os.popen() 和 os.system() os.system(cmd) 是在执行command命
我对双链表进行了简化。我的双链表是一个以头和尾为节点的结构。 有一个函数可以创建列表并返回它。在同一函数中,我执行尾节点和头节点之间的链接。问题是,当我返回列表(因此转到函数之外)时,所有链接都消失了
我有这个信息。 let params2: [String: AnyObject] = [ "app_token": myapptoken, "member_access_token":
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
我正在尝试发出解析为特定 IP 的 cURL 请求。从我读过的所有内容来看,这在我看来在语法上是正确的,但我仍然看到“无法解决主机错误”。有人能指出我正确的方向吗?我看到了各种错误: curl —-r
我正在尝试使用 curl 在 jira 服务器中获取数据。我试过这个命令 curl -u username:password -X GET -H "Content-Type: applicat
因此,下面的代码有时会起作用,有时它会添加&符号(到复制缓冲区),我试图从文本字符串中删除它。 代码的要点是将字符串从正确位置复制到与号之前。但是,在随机情况下,它仍然会添加 & 符号。 Privat
p = Int('p') q = Int('q') s = Solver() s.add(1<=p<=9, 1<=q<=19, 5<(3*p-4*q)<10) s.check() print s.mo
我在这里阅读了分配问题的解决方案:http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=hungarianAlgorithm 我了解 O(n
控制台中显示警告: the id param was not provided. 文档提到将使用当前路由: current is the current Route by default (most
我正在尝试使用hector-core作为grails Maven构建中的依赖项。 me.prettyprint hector-core 1.0-3 bundle 我收到一个错误 [
我正在尝试使用 JavaScript 变得更好,并且我在破坏页面方面做得非常好:))))) 我正在尝试使用 Firebug 进行调试,但一开始有点困惑。它的哪个选项卡用于调试 JavaScript?我
我正在使用最新的 Angular + Firebase 并尝试设置登录授权系统。我有包含登录+注册链接的 home.html,转到 login.html 并添加凭据工作正常(提交时记录正确的 UID)
我有一个 iPad 应用,现在需要将其转换为通用应用。我已将目标设置为 Universal,现在它也可以部署到 iPhone,但是,我有一个主要问题:即使我已经创建了我的 main分别查看两种设备类型
我在 CSS 中使用媒体查询来根据 IE11 和 Chrome 的屏幕分辨率缩放我的网页。当我运行 this webpage在我的 2 个不同屏幕上的 chrome(顺便说一句,我用它来确定我的最小宽
我正在解决nodeschool练习“Juggling Async”,我是这样解决的 var http=require("http"); var urls=process.argv.slice(2,pr
我试图相对神秘地要求一个文件,发生了以下情况 这很好,它指向 /Users/marcos/Desktop/Taper/lib/utils.js myPath = "/Users/marcos/Desk
我正在尝试解决一个项目,但遇到了问题。 • 您的程序应该显示一个菜单,允许用户执行以下操作以下操作(注:使用GUI): 添加新客户 删除客户 修改客户信息//此选项必须显示子菜单: --------1
我需要 x 图标来删除输入字段值1. 当用户键入任何内容时,将显示“x”图标 如果输入框中没有可用的值,x 将被隐藏 当输入框中的值可用并且焦点移出输入框时,我们需要隐藏 x 图标并聚焦,我们需要再次
我正在使用 ajs(1.4.7) 和 angular-ui-router(0.2.15) 开发一个简单的 AJS 应用程序。 经历了this文章并选择了路由解析技术。 这是我遇到的错误 错误:[$in
我是一名优秀的程序员,十分优秀!