NetEase 2016 research and development engineer programming Questions--complete analysis

Source: Internet
Author: User
Objective

Before doing the company's real problem, encounter dynamic planning, there are some problems of mathematical nature more. NetEase 2016 research and development engineers programming problems with the previous topic is very different, not only involved in the two-tree coding, but also related to the breadth of the graph traversal, finally there is a fast row. It can be said that the three topics of gold is very high, so do a summary and analysis. 1. Compare weight

Topic Description: Xiao Ming accompanied Xiao Hong to see the diamond, they from a pile of diamonds randomly extracted two and compare their weight. The weight of these diamonds varies. After they had compared them for some time, they took a fancy to two diamond G1 and G2. Now, please judge which of these two diamonds is heavier, based on the information you have previously compared.

Given the number g1,g2 of the two diamonds, numbering starts at 1, and given the vector of the relational array, where the element is a binary group, the first element is the number of the heavier diamond in the comparison, and the second element is the lighter diamond number. Last given before the number of comparisons N. Please return the relationship between the two diamonds, if the G1 return 1,g2 more heavy return-1, can not be judged to return 0. The input data is guaranteed to be legal and there is no contradiction.
Test Sample:
2, 3,[[1, 2], [2, 4], [1, 3], [4, 3]], 4
Return: 1

Topic analysis

In fact, this topic is a problem with the graph, if the G1 than the G2 large then there is a path from G1 to G2, if the G2 than the G1 large then there must be a path to G2 G1 to the map. The direction diagram in the sample is as follows:


2 to 3 of the above figure has a forward edge, so number 2 is heavier than number 3. A two-dimensional array of topics gives a graph, and if you want to compare the size of a B, you can walk through its child nodes from a, and if you can find a path from A to B, prove A is bigger than B. Here you can use Breadth first traversal to find out if there is a path between the two nodes in the diagram.

 public static int cmp (int g1, int G2, int[][] records, int n) {hashmap<integer, Arraylist<integer>&gt ;

         Map=new Hashmap<integer, arraylist<integer>> (); The key in HashMap corresponds to the node value, in which all the neighboring connected nodes of the node are stored.

         This facilitates breadth traversal.
             for (int i=0;i<n;i++) {//joins all contiguous connected child nodes for each header node if (Map.containskey (records[i][0)))

             {Map.get (records[i][0]). Add (Records[i][1]);
                else {arraylist<integer> list=new arraylist<integer> ();
                List.add (records[i][1]);                
            Map.put (records[i][0], list);
             } if (judge (map, G1, G2)) {return 1;
         The Judge function is used to determine whether a connected path exists.
         else if (judge (map, G2, G1)) {return-1;
     return 0; public static Boolean judge (Hashmap<integer, Arraylist<integer>> map,iNT Head,int next) {queue<integer> queue=new linkedlist<integer> ();
         Arraylist<integer> list=new arraylist<integer> ();//list for heavy, traversed nodes no longer traverse.
             Queue.add (head);//breadth traversal while (!queue.isempty ()) {int val=queue.poll ();
             if (Val==next) return true;//If Next is the head connected path, then Next2. Two fork Tree

Topic Description: There is a binary tree, each point on the tree has the right value, the weight is different, please design an algorithm to calculate the maximum weight of the leaf node to the right value of the smallest leaf node distance. The distance between each edge of a binary tree is 1, and the distance between the two nodes is reached by the number of nodes one node passes through. Given the root node of the binary tree, return the distance you are seeking.

Topic analysis

To see this topic, the most intuitive feeling is to first find the largest leaf nodes, and the most lobular nodes, and then find their common ancestor node, but the nearest ancestor node is not good, because the node's pointer is one-way. So this method doesn't work. A more ingenious approach is to encode the two-fork tree node and then process the code, as shown in the following figure:

The code for the max leaf node above is: 1 1 1 1. The min leaf node is encoded at 1 1 0 0. The distance between the max node and the Min node is: The length of the max encoded length -2+min 2, where 2 is the min encoding and the length of the previous overlapping portion of the max encoding. Understand the idea of the code is relatively good to write.

public class Main_2 {static int max=integer.min_value,min=integer.max_value;
    Static String Maxcode;
    Static String Mincode; public static void Main (string[] args) {} public int Getdis (TreeNode root) {if (root==null) re
        Turn 0;
        Preorder (Root, "", ' 0 ');
        int i=0; 

        while (I<maxcode.length () &&i<mincode.length () &&maxcode.charat (i) ==mincode.charat (i)) ++i;
    Return Maxcode.length () +mincode.length () -2*i;
        public static void Preorder (TreeNode root,string Codes,char code) {Codes=codes+code;
        if (root==null) return; if (root.left==null&&root.right==null) {if (max<root.val) {max=
                Root.val;
            Maxcode=codes;
                } if (min>root.val) {min=root.val;
            Mincode=codes;
        } return;
       } Preorder (root.left, codes, ' 0 ');
    Preorder (root.right, codes, ' 1 ');
    Class TreeNode {int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    Public TreeNode (int val) {this.val = val; }
}
3 Looking for the number of K-Large

Topic Description: There is an integer array, please find out the number of K in the array according to the idea of fast sorting.
Given an array of integers a, given its size n and K (k between 1 and N), return the number k large to ensure that the answer exists.
Test Sample:

[1, 3, 5, 2 2], 5, 3

Return: 2

Topic analysis

This topic is the idea of the fast line, a quick sort is based on a number, a small number on its left, and a larger number on its right, after a sequence of sorting is completed, if the number is exactly placed in the K position, then the left of the K number is the smallest number of K. If you put a larger number on its left, than its small number on its right, a trip after sorting, if the number is exactly placed in the K position, then the left of the K number is the largest number of K.
About the quick row can read this blog: http://blog.csdn.net/u013309870/article/details/68921848

Package NetEase 2016 research and development engineer programming questions;

Import Java.util.Scanner;
        public class Main_3 {public static void Main (string[] args) {Scanner sc=new Scanner (system.in);
            while (Sc.hasnext ()) {int n=sc.nextint ();
            int K=sc.nextint ();
            int []a=new int[n];
            for (int i=0;i<n;i++) a[i]=sc.nextint ();
        Findkth (A, n, K);

    } sc.close ();       
     public static void Findkth (int[] A, int n, int K) {System.out.println (findkth (A, 0, N, k));
             public static int findkth (int[] A, int left,int right, int K) {if (left>=right)
         return 0;
         int Val=a[left];
         int i=left,j=right-1;
             while (I&LT;J) {while (i<j&&a[j]<val) j--;
             A[I]=A[J];
             while (I<j&&a[i]>=val) i++;          
         A[j]=a[i]; } a[i]=val;
         if (i==k-1) {return a[i];

     Return Findkth (A, left,i, K) +findkth (A, i+1,right, K);
 }
}
Summarize

NetEase 2016 research and development engineers programming problems in the three topics of the gold content is relatively high, especially the previous two topics of that thinking is not before the brush problem, for this solution is more novel topic, it is necessary to fully understand, and make records.

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.