AVOID THIS CODING INTERVIEW MISTAKE!

preview_player
Показать описание
AVOID THIS CODING INTERVIEW MISTAKE!
Рекомендации по теме
Комментарии
Автор

Master data structures and algorithms for FREE at algomap.io :)

GregHogg
Автор

This is one of like 200 leetcode string-analysis questions where because the inputs are constrained to ASCII characters, you can use a fixed size integer as a bitmapped set indexed by the ASCII character code of each of the jewels. That approach gives time complexity that is linear in N and constant space complexity. If you want to really impress your interviewer on most of these "easy" questions you can learn this intermediate-level technique for creating very efficient implementations of sets.

Another good implementation that is better than this one for is to create a fixed size array and store the count of each letter in `stones` in that array, then simply return the sum of the counts of all the letters in `jewels`. This is worse than the bitset approach on the constant factors in memory consumption and probably also cache performance but has the same asymptotic complexity.

reallyreason
Автор

This is way better. But you also use space for the solution

seventeeenn
Автор

Hash map laughing in corner after butchering every problem's soul

error-myut
Автор

it did indeed help lowering my self esteem

pablo-piqz
Автор

depends on the maximum length of jewels. if it is not that long, your set algorithm is actually worse. might be slower and requires extra space.

timelimitexceeded
Автор

You could argue that the time complexity is O(n) for both, because m will never increase past 52 (if the strings consist of only letters) or at most 255.

ericb
Автор

Sets are O(1), but really slow. Use should not use them in small examples, they are only fast on large scale. And in the example of characters a array with 256 length can do it faster than a set (use the unsigned character as an index to the array)

rodrigoqteixeira
Автор

Or you can do Counter(stones) and then sum the counters for each char in jewels

DavideCanton
Автор

That’s really cool observation bro, gotta pay attention to time complexity

EverAfterBreak
Автор

Although this is slower for small amount data
It is very efficient on large amounts of data

wn
Автор

But makinv a set, won't that cut out duplicates?

That_EvilGuy
Автор

This is a great video bro! Thanks for sharing!

ZackWhitbord
Автор

i didn't get how the complexity is n+m instead of N*(numbers of elements in set)

RakshitGoel-yh
Автор

The real gems and jewels are these videos

hamadrehman
Автор

Explain the problem first before you jump into solving it

theguardianarchives
Автор

i don't understand why would you use jewels = set([j for j in jewels]) instead of jewels = set(jewels)

techniq
Автор

why not jewels = set(jewels) instead of jewels = set(j for j in jewels)?

boy_with_thorn
Автор

Maybe I'm just not familiar with this language. But why a set instead of a hash table? Or do sets in this language use hash tables?

FrozenKnight
Автор

Please make a full playlist of leetcode using java

iabhisripathi