The beauty of programming 1.5 quickly find faulty machines

Source: Internet
Author: User

Topic:

There are many servers that store data, assuming that a machine only stores a record labeled ID, assuming that the total number of machines is below 1 billion and that the ID is an integer less than 1 billion, assuming that each piece of data holds two backups, so that two machines store the same data.

The question is: 1. Suppose you get a list of data file IDs at a certain time, can you quickly find the IDs that appear only once in the table? The data ID of the failed machine store is quickly identified.

2. What happens if two machines fail? (assuming that two machines that store the same data do not fail at the same time, the list is missing two different IDs)

4 Ways of solving the method are given.

Solution One:

The most traditional comparison list, you need to traverse the entire list, record the number of occurrences of each ID, the last output only once the ID, time complexity o (n), spatial complexity is also O (n), if the amount of data is too large, the actual operation efficiency will be very low.

Solution Two:

Optimized storage space, most of the data, most occurrences are 2, the machine is a small number of failures, through the hash Table,key value for the machine Id,value value is the number of occurrences. Iterate through the list, and if you encounter an ID, add 1 to the value of the ID. If the value is 2, delete the key. The rest of the last hash table is the faulty machine ID.

In the best case, the space complexity is O (1) and the worst is O (n).

Solution Three:

The first two methods have reduced the space complexity to O (n), and if you want to drop to the constant level, you need to switch to a different way of thinking without traversing the list.

If you can record the result of traversing a list with only one variable, then the spatial complexity can be O (1).

X (N) =id_lost. Considering the list, only one ID appears 1 times, the other is 2 times, you can use the computer language in the XOR (XOR, ⊙).

A⊕a=0

A⊕0=a

Final x (n) =list[0]⊕list[1]⊕list[2]⊕ ... The result of ⊕list[n] is an ID number that appears only once, with a spatial complexity of 1.

However, if there are 2 IDs that occur only once, assuming that the ID of a,b,x (n) is a⊕b, the value of AB is still not determined.

The binary value of XOR or after, one is 1, it can be inferred that the number of AB2, this position, one is 0, the other is 1. we divide all the IDs into 2 categories, one for the 1 and one for the 0. These 2 classes contain one of the AB, using 2 variables, traversing the list to make the difference or processing, you can find the values of a and B.

Solution Four:

The problem is extended, now the ID of the fault machine is the same, and the 2 machines that store the same data are at the same time fart. Using some mathematical knowledge, there are invariants in mathematics, where in the pre-booked integer ID collection, all the IDs are added to get a invariants . Add the current ID collection, subtract the sum of the current ID collection with the invariant, and you can get the lost data ID. Time complexity is O (n), Spatial complexity O (1).

If these 2 machine IDs are different, we just get the sum of their IDs, X+y=a, and don't know exactly how much they are. It is possible to construct an equation, form an equation, solve the equations, and derive the values of x and Y. Also use the concept of invariants, such as the product of all IDs, and finally come to xy=b to solve the value of x, Y.

Summarize:

In the algorithm design is a slowly developing process, the beginning may just come up with the most common one solution, inefficient, but can solve the problem. After doing this, you need to do some optimization in terms of time complexity or space complexity to go a step further. can also according to some of the conditions in the problem, think of some of the methods can be solved by computer, such as XOR, and, regular expression. You can also use the idea of mathematics to see whether there is a solution to the problem.

A small procedure with a solution

#include"stdio.h"intMain () {intlist[]={1,2,3,5,6,7,8,9,0,1,2,3,4,5,6,8,9};//? 0,7,4    intT; intLength=sizeof(List)/sizeof(list[0]); printf ("%d\n", length);  for(intI=0; i<length;i++) {T=0;  for(intj=0; j<length;j++)            if(list[i]==list[j]) T=t+1; if(t!=2) printf ("%d\n", List[i]); }} 

The beauty of programming 1.5 quickly find faulty machines

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.