The Stack Overflow podcast is back! Listen to an interview with our new CEO.

New answers tagged

0

If you are given an unordered list and asked to test whether just one item is in the list then a sequential check of each item in the list is the best you can do. If you want to carry out multiple tests (but you do not know all the test items in advance) then it is worth the initial overhead of sorting the list, creating a hash table, or creating a search ...


2

Transform your input to a graph in which each vertex represents a stop on a line. If a stop is shared by multiple lines, then add one copy of the vertex per line and a new super-vertex connected to all such copies with edges of weight 2.5 (so there is a path of length 5 between any two of these stops). Add edges between consecutive stops of the same line ...


0

If there is no upper bound on the number of classes per semester and the dependencies are acyclic, this can be solved by a modified topological sort as follows: Schedule every class for which all (zero or more) prerequisites are met in the earliest semester in which they are available. Repeat this for all remaining classes (i.e. those not yet scheduled), ...


0

When measuring comparisons, we usually only charge comparisons of elements of the array (more accurately, we charge comparisons for the datatype of array elements, but not of array indices). In the example of linear search, at each iteration you are comparing one element of the array to the input element being searched. So in total, you are making exactly $n$...


2

The Shortest Levenshtein's Distance should be good enough. See if this Matching Wildcards Wikipedia page can be of help.


0

With no other given assumptions, it is a linear solution in the most optimal scenario. One can have better solution (binary search) in problems where all the values after first true are true. But if there is only one element, it is a O(n) solution.


-2

Usually an array is considered as uniformly distributed when the difference between the elements are equal or almost same. Example 1: 1,2,3,4,5,6 (Difference is 1) Example 2: 10,20,31,40,55,60,73,80(Here the difference between the two adjacent elements are almost close to 10). Interpolation search is to be used when the given array is both sorted and ...


3

Here is an informal proof, which you can try to formalize. Suppose that we’re looking for 1 in an array of length $n$ of 0s and 1s. We consider the following distribution: each element is 1 with probability $1/n$. (You can also try your luck with the uniform distribution over all arrays with a single 1.) Given the transcript of the algorithm, you can ...


1

Another possibility would be to store all the words in an array, sort it, and then do lookups on it using binary-search. Disadvantages compared to a Trie: Worse asymptotic time complexity for lookups ($O(\mathrm{log}\ n)$ average where n is the number of entries). Probably less efficient to insert additional entries, as the array will need to be resized. ...


3

The problem arises due to curse of dimensionality. If the dimensionality is $k$ , the number of points in the data, $N$ should be $N ≫ 2^k$. Otherwise, when $k-d$ trees are used with high-dimensional data, most of the points in the tree will be evaluated and the efficiency is no better than brute force search. So for high value of $k$, approximate nearest-...


Top 50 recent answers are included