Drop the eggs and drop the table

Source: Internet
Author: User

Drop the eggs: give you k eggs and let you test the hardness of the egg. The measurement method is to drop down the floor at different heights, if the eggs are broken in the I layer but not in the I-1 layer, then we know the hardness of the egg, that is, the layers represent the hardness of the egg. In addition, it is assumed that the eggs can be expressed by layers of a certain layer. Now the question is, how many times does it take to test the hardness of egg shells with K eggs in the worst case?

For example, if there is only one egg, the egg can only be tested one layer before the egg hardness is measured. If the building height has N layers, N experiments are required.

What if there are two eggs? Assume that the worst case requires at least m experiments, then the first egg can only be tested at the m layer. If the m layer is not broken, the next step is to conduct an experiment on the m + m-1 layer. If the m-1 layer breaks down, the second egg must be tested layer by layer from layer 1st to the m-1 layer, this ensures that up to M experiments are performed in the worst case. And so on, then we obtain the floor interval sequence of an experiment, M, m-2 ,...... 1, this can ensure the minimum M, that is to say, M takes 1 + 2 + ...... + M> = the minimum value of N.

There is no direct formula for calculating the minimum number of experiments required in the worst case for any number of eggs and floors of any height. In fact, this is a dynamic planning problem.

So what is a jump table? What is the relationship between this and table jumping?

If we want to store N strings with different lengths, how can we efficiently store these strings? If we allocate memory of the same size to each string, the overhead of each string is the same as that of the longest string, which will inevitably waste space. However, the advantage of such storage is that you can quickly perform string SEARCH, for example, you can use binary search.

What should we do now: we need to store the data without wasting space, and the search efficiency should be good? Dictionary tree, balanced binary tree? These are all possible, but it is a waste of space to store such a tree structure, and the implementation is complicated. The table will be used here.

To make storage efficient, the simplest way is to allocate the string as much space as possible, without the need to fix the size and consistency, in addition, each string is stored in several fixed bytes before it, so that each string can be easily identified from the beginning of the storage. Then the problem arises, so we cannot get a good search efficiency, because the binary method is useless here. The idea of table jumping is to insert a certain number of pointers between the string sequence so that a certain number of strings are skipped instead of simply finding the adjacent strings.

Searching F in the first table requires 6 queries, but only 5 queries in the second table. Now the question is, if the number of strings is N, how many jump pointers do we need? What is the interval between each pointer?

Suppose that you do not care about the overhead of the extra storage pointer space, you can use N pointers, each pointing to a string, and you can use binary search, this method is quite good among a lot of problems with small scale.

However, if you think that storing pointers is a waste of space, you need to skip the table. This is the same as the problem of dropping eggs. No formula can tell you how to do it. In fact, the most common strategy is to insert pointers at intervals, and the effect is good.

Assume that the table length is N and K pointers need to be inserted at intervals. In the worst case, K + N/K queries are required to find an element, that is, y = K + N/K, evaluate the derivation of K, y '= 1-N/K2, And let y' = 0 obtain the value of K when Y is the minimum value, 1-N/k2 = 0 => k2 = n => K = SQRT (N), that is, SQRT (n) Pointers are required in optimal conditions, the interval between each pointer is also SQRT (n ).

For example, if the table length is 1 million, when K is 1000, the worst case is 1000 + 1000 = 2000 queries to find an element, and the sequential search is expected to be a string of 0.5 million queries. Although there are many ways to solve this problem that requires both high storage efficiency and high search speed, there is no doubt that the idea of table jumping is the simplest, in addition, many practical problems are solved in this way. In a word, simplicity is beauty.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.