首页 \ 问答 \ 为什么这个简单算法T(n / 2)+1的最坏情况时间复杂度与n ^ 2 + T(n-1)相反?(Why is the worst case time complexity of this simple algorithm T(n/2) +1 as opposed to n^2+T(n-1)?)

为什么这个简单算法T(n / 2)+1的最坏情况时间复杂度与n ^ 2 + T(n-1)相反?(Why is the worst case time complexity of this simple algorithm T(n/2) +1 as opposed to n^2+T(n-1)?)

在此处输入图像描述

以下问题是关于大学最近的任务。 我原以为答案是n ^ 2 + T(n-1)因为我认为n ^ 2会使它的渐近时间复杂度为O(n ^ 2)。 与T(n / 2)+1一样,其渐近时间复杂度为O(log2(n))。

答案被返回,结果是正确的答案是T(n / 2)+1然而我无法理解为什么会这样。

有人可能会向我解释为什么这个算法的最坏情况时间复杂度? 我对时间复杂性的理解可能是错误的。


enter image description here

The following question was on a recent assignment in University. I would have thought the answer would be n^2+T(n-1) as I thought the n^2 would make it's asymptotic time complexity O(n^2). Where as with T(n/2)+1 its asymptotic time complexity would be O(log2(n)).

The answers were returned and it turns out the correct answer is T(n/2)+1 however I can't get my head around why this is the case.

Could someone possibly explain to me why that's the worst case time complexity of this algorithm? It's possible my understanding of time complexity is just wrong.


原文:https://stackoverflow.com/questions/36627715
更新时间:2024-04-24 12:04

最满意答案

我也使用glmatrix和require.js,最新的版本有require.js支持,对我来说似乎工作正常。

但是我对require.js的使用略有不同,我的模块通常是这样开始的:

define(['lib/glmatrix/gl-matrix'],
    function(glmatrix) {
        var myModule = function() {};
        // use glmatrix.vec3, etc here
        return myModule;
});

这对你有用吗?


I use glmatrix too with require.js, the most recent version has require.js support and for me it seems to work fine.

I have however I slightly different usage with require.js, my modules usually start like this:

define(['lib/glmatrix/gl-matrix'],
    function(glmatrix) {
        var myModule = function() {};
        // use glmatrix.vec3, etc here
        return myModule;
});

Does this work for you?

相关问答

更多
  • 这也是我的确切问题! 我也必须使用一个较旧的jQuery,而且还有更多的“传统”JavaScript库。 最好的技术是什么? (我可以编辑你的问题,使它更广泛,如果你不介意。)这是我学到的。 RequireJS作者James Burke解释了RequireJS + jQuery文件组合的优点 。 你得到两件事 一个模块jquery是可用的,它是jQuery对象。 这是安全的: // My module depends on jQuery but what if $ was overwritten? defi ...
  • 尝试使用requirejs.config的paths属性:如果你将when.js放在js/libs/when/when.js ,你可以尝试: requirejs.config({ paths: { 'when': 'libs/when/when' } }); define(['when'], function(When) {... 如果您的JavaScript代码包含baseUrl ,则甚至不需要baseUrl ,但如果需要,您可以指 ...
  • 在问题解决后发布我的评论作为答案。 在指定路径时,require.js不需要文件扩展名.JQuery的当前路径 jquery : 'vendor/jquery-2.2.0.min.js', 将被取代 jquery : 'vendor/jquery-2.2.0.min', 有关文档的更多信息 RequireJS默认还假定所有依赖项都是脚本,因此它不希望在模块ID上看到尾随的“.js”后缀。 在将模块ID转换为路径时,RequireJS会自动添加它。 Posting my comment as an answe ...
  • 只需检查API,您就会遇到一个相当基本的问题,例如x = f(x); vec3.normalize()返回输入内核的修改版本(见下文) 来自http://glmatrix.net/docs/2.2.0/symbols/src/gl-matrix_src_gl-matrix_vec3.js.%3Chtml 333 /** 334 * Normalize a vec3 335 * 336 * @param {vec3} out the receiving vector 337 * @param {vec ...
  • r.js优化器无法理解包含多个匿名define调用的文件。 您必须找到不包含多个匿名define调用的oraclemaps版本。 (这意味着除了oraclemaps之外你必须得到jQuery和Kendo oraclemaps 。)如果这样的构建不可用,你可以处理oraclemaps来删除额外的模块。 或者您可以处理它以便为define调用添加名称。 There's nothing the r.js optimizer can do to understand files that contain multi ...
  • 使用RequireJS服务器端存在原因,但它们很少。 除非你能说明理由,例如: 我必须使用RequireJS因为X. “X”是证明使用RequireJS的理由,那么你就不应该这样做。 请注意,仅仅想要以AMD格式编写模块是不够的,因为存在允许在Node中加载AMD模块的加载器 (如node-amdl-loader )。 当我想测试不依赖于浏览器的代码时,我实际上使用了这个。 我以AMD格式编写模块,并指定如果在Node中使用,则应使用类似node-amd-loader的AMD加载程序。 这样,库既可以在No ...
  • 根据: https : //code.google.com/p/android/issues/detail?id=221529 问题已经解决(确切地说:小时前)。 只是为了重建你的项目。 According to: https://code.google.com/p/android/issues/detail?id=221529 The problem is already fixed (exactly: hour ago). Just for sure rebuild your project.
  • 您很可能已经下载了Cordova src并解压缩了cordova-android.zip文件。 相反,只需下载Phonegap下载并将其解压缩并cd进入phonegap / lib / android / bin文件夹即可执行create。 More than likely you have downloaded the Cordova src and extracted the cordova-android.zip file. Instead, just download the Phonegap do ...
  • 您应该在需求调用中使用“Smooch”而不是“smooch”,如下所示: