简体   繁体   English

这个O(1)空间怎么样而不是O(n)空间。 firstNotRepeatingCharacter挑战解决方案

[英]How is this O(1) space and not O(n) space. firstNotRepeatingCharacter Challenge solution

I am having trouble understanding how the following solution is O(1) space and not O(n) space. 我无法理解以下解决方案是O(1)空间而不是O(n)空间。 The coding challenge is as follows: 编码挑战如下:

Write a solution that only iterates over the string once and uses O(1) additional memory, since this is what you would be asked to do during a real interview. 编写一个只迭代字符串一次并使用O(1)额外内存的解决方案,因为这是您在真实访谈期间要求做的事情。

Given a string s, find and return the first instance of a non-repeating character in it. 给定一个字符串s,找到并返回其中非重复字符的第一个实例。 If there is no such character then return '_'. 如果没有这样的字符,则返回'_'。

The following is a solution that is O(1) space. 以下是O(1)空间的解决方案。

 function firstNotRepeatingCharacters(s: string) : string { const chars: string[] = s.split(''); let duplicates = {}; let answer = '_'; let indexAnswer = Number.MAX_SAFE_INTEGER; chars.forEach((element, index) => { if(!duplicates.hasOwnProperty(element)) { duplicates[element] = { count: 1, index } } else { duplicates[element].count++; duplicates[element].index = index; } }); for(const key in duplicates) { if(duplicates[key].count === 1 && duplicates[key].index < indexAnswer) { answer = key; indexAnswer = duplicates[key].index; } } return answer; } console.log(firstNotRepeatingCharacter('abacabad')); console.log(firstNotRepeatingCharacter('abacabaabacaba')); 

I do not understand how the above solution is O(1) space. 我不明白上述解决方案是如何O(1)空间。 Since we are iterating through our array we are mapping each element to an object (duplicate). 由于我们遍历数组,因此我们将每个元素映射到一个对象(重复)。 I would think this would be considered O(n), could somebody clarify how this is O(1) for me. 我认为这将被视为O(n),有人可以澄清这对我来说是O(1)。 Thanks. 谢谢。

The memory usage is proportion to the number of distinct characters in the string. 内存使用量与字符串中不同字符的数量成比例。 The number of distinct characters has an upper limit of 52 (or some other finite value) and the potential memory usage does not increase as n increases once each of the distinct characters has been seen. 不同字符的数量具有52的上限(或一些其他有限值),并且一旦看到每个不同的字符,当n增加时,潜在的存储器使用不会增加。

Thus, there exists an upper limit on the memory usage that is constant (does not depend on n ), so the memory usage is O(1). 因此,存在使用的上限是恒定的(不依赖于n ),因此存储器使用是O(1)。

The algorithm has O(min(a,n)) space complexity (where a is number of letters used for text cooding eg for UTF8 a>1M ). 该算法具有O(min(a,n))空间复杂度(其中a是用于文本编码的字母数,例如对于UTF8 a> 1M )。 For worst case: string with uniqe characters (in this case n<=a ) eg abcdefgh the duplicates object has the same number of keys as number letters of input string - and what is clear on this case, the size of used memory depends on n . 对于最坏的情况:具有uniqe字符的字符串(在这种情况下n <= a )例如, abcdefgh duplicates对象具有与输入字符串的数字字母相同的键数 - 并且在这种情况下清楚的是,所用内存的大小取决于n

The O(1) is only for case when string contains one repeated letter eg aaaaaaa . O(1)仅适用于字符串包含一个重复字母的情况,例如aaaaaaa

Bonus : Your code can be "compressed" in this way :) 额外奖励 :您的代码可以通过这种方式“压缩”:)

 function firstNotRepeatingCharacters(s, d={}, r="_") { for(let i=0; i<s.length; i++) d[s[i]]=++d[s[i]]|0; for(let i=s.length-1; i>=0; i--) if(!d[s[i]]) r=s[i]; return r; } console.log(firstNotRepeatingCharacters('abacabad')); console.log(firstNotRepeatingCharacters('abacabaabacaba')); 

Indeed this is an 0(1) complexity, but only on space constraints. 实际上,这是0(1)复杂度,但仅限于空间限制。 Since we have an upper limit. 因为我们有一个上限。 This limit could be UTF-16, it could be the amount of English letters. 此限制可以是UTF-16,也可以是英文字母的数量。

This is a constraint given by the Developer. 这是开发人员给出的约束。 Saying that, it's only a 0(1) in space constraints if the code above ran with a finite set of combinations. 如果上面的代码用一组有限的组合运行,那么它在空间约束中只是一个0(1)。

A String it's limited by implementation to a 64 bit character "array". 一个字符串,它被实现限制为64位字符“数组”。 So the store capacity generally of a "String" type it's 2147483647 (2ˆ31 - 1) characters. 所以商店容量一般为“String”类型,它的字符为2147483647(231-1)。 That's not really what 0(1) represents. 这不是0(1)所代表的。 So virtually that's an 0(N) in space constraints. 所以实际上这是空间限制的0(N)。

Now the situation here it's totally different for time complexity constraints. 现在这里的情况与时间复杂性约束完全不同。 It should be in the optimal scenario a 0(N) + 0(N - E) + 0(N). 它应该在最佳场景中为0(N)+ 0(N-E)+ 0(N)。

Explaining: 1. First 0(N) the first loop goes through all the elements 2. Second 0(N) is about the deletion. 解释:1。首先0(N)第一个循环遍历所有元素2.第二个0(N)是关于删除。 The code delete's element's from the array. 代码删除元素来自数组。 3. 0(N - E) the second forEach loops the final popped array, so we have a constant E. 3. 0(N - E)第二个forEach循环最后一个弹出的数组,所以我们有一个常数E.

And that's supposing that the data structure is an Array. 这假设数据结构是一个数组。

There's a lot to Digg here. Digg在这里有很多东西。

TL;DR TL; DR

It's not a 0(1). 它不是0(1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 空间复杂度中的 O(1) 与 O(n) - O(1) vs O(n) in Space Complexity 这个算法的时间和空间复杂度是 O(n) 还是 O(1)? - Does this algorithm have time and space complexity O(n) or O(1)? 以下代码片段的空间复杂度是 O(1) 还是 O(N)? - Is the space complexity O(1) or O(N) for the following code snippet? 如何将此解决方案从 O(n^2) 变为 O(n)? - How to turn this solution from O(n^2) to O(n)? 我对最小差分算法问题的解决方案是否具有最佳时空复杂度 (O(nLog(n) + mLog(m)))? - Does my solution to the Smallest Difference algorithm problem have optimal space time complexity (O(nLog(n) + mLog(m)))? 如何将这个 O(n^2) 算法转换为 O(n)? - How to convert this O(n^2) algorithm to O(n)? Javascript:将解决方案更改为O(n log n) - Javascript: Change the solution to O(n log n) 此解决方案O(N)或O(LogN)的时间复杂度是多少? - What is time complexity of this solution O(N) or O(LogN)? Javascript - 在时间复杂度为 O(n) 且空间复杂度为 O(1) 的给定字符串中删除交替重复字符的最佳方法是什么? - Javascript - What is the best way to remove alternate repeating character in a given string with time complexity O(n) and Space Complexity was O(1)? 反转空间复杂度为 o(1) 的句子中的单词 - Reverse words in a sentence with space complexity o(1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM