[100] 38th (weighing balances, large input streams, and removing duplicate URLs)

Source: Internet
Author: User

I. Question

1. Use a balance (comparison only, weighing not allowed) to find the only light weight from a pile of balls. Using X balances, you can find the lighter one from the y balls and find the relationship between Y and X.

2. There is a very large input stream that can be stored without memory and can be input only once. How to randomly retrieve M records from the input stream
3. How to remove duplicate URLs from a large number of URL strings and optimize the time space complexity


Ii. Analysis


1. Y = 3 ^ x
2. Each time a record is input, a random number ranging from 0 to 1 is randomly generated, and these random numbers are used to maintain a heap with a size of M.
3. Use the modulo hash function to find a hash function and map it to the previous one. Use the link method to avoid collision. If the value after a ing is! = B, c, d..., add a to the linked list. If it is the same, delete a and continue traversing.


Iii. Key Points

Unit test: weigh three balls a B c, weigh a B, A = B --> C is light, A> B ---> B is light, a <B ---> A is light

So y takes the maximum value, that is, it can be divided into three portions at a time.

First time y/3

Second (y/3)/3

*

*

X y/(3 ^ X) = 1

So y = 3 ^ x


Answers provided by netizens are as follows:


Every time you divide the ball into three heaps, try to make the three heaps as much as possible or make one or more of them as much as possible.
The relationship between the number of balls y and the number of heavy times X is:
Y = 1 => x = 0; (apparently)
Y = 2 => x = 1; (apparently)
Y = 3 => X = 2; (apparently)
If y> 3, the ball is also divided into three heaps, marked as heap a, heap B, and heap C.
If y = 3 K (k> 1)
Weigh the ball once to determine the heap of the defective ball and reduce the number of balls to K;
If y = 3 k + 1 (k> = 1) assume that the C Stack has one more ball, and heap a and heap B are weighed.
You can weigh the ball once to determine where the ball is located,
If a = B, the defective ball is in the C heap, thus reducing the number of balls to k + 1;
The else defective balls are in the light heap, reducing the number of balls to K;
If y = 3k-1 (k> = 2) assume that the C Heap has less than one ball, and heap a and heap B are weighed.
You can weigh the ball once to determine where the ball is located,
If a = B, the defective ball is in the C heap, thus reducing the number of balls to K;
Else defective balls in the light heap, thus reducing the number of balls to K-1;

The above process is used repeatedly to obtain results.
The last number of times X = log3 (Y), where Y may need to be corrected. But the complexity is log.
Y


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.