Java developers Kong three years, the company's interim checks algorithm, after leaving these questions I remember a lifetime

A few days ago our company did something stupid, very, very stupid things. I thought from after school out there looking for a job in addition to the test, but will not have anything to do with the examination-related thing.

 

However, the weather is unpredictable, the company's technical director, personnel director, the two big brothers suddenly come into our business lines, call me boss, give us organized a special "test."

It was a sunny afternoon, I tilted his legs, his left hand holding a cup of cappuccino, right hand clutching my Logitech mouse, rolling axle, shuttling between the hot headlines.

"Yellow dress fluffy hair ~ ~"

"WC, witness history, this year's college entrance examination delayed for a month, then if I run into such a thing, I would not divide 985 minutes?"

Colleagues: "That's why you pass the test undergraduate?"

Me: "uh ..."

"WC, Ah Tao Bo Shui this is gonna do?"

Colleagues: "This test into college have anything to do with you?"

Me: "Hey?"

 

At this time, a middle-aged Mediterranean never seen in my line of sight: "Ladies and gentlemen, take note, in order to examine all of the programming ability, a test after five minutes, to save their own code after a good, to large the conference room. "

...

A colleague: "This is not the headquarters to the technical director do?"

Colleagues B: "WC, the first time I saw big brother."

Me: "You do not hear you want to test it?"

...

Later, we unknowingly carried out a test, when I went blank papers pay up, put a blank mind with them, listening to colleagues talk about the test answers.

Colleague told me this question from Baidu algorithm, and gave me a document.

File contents are as follows:

1, degree of want to go to the mall to buy a bear hat, the mall has N hat, a hat and some may be the same price. Degree of Bear wanted to buy a third cheaper price hats, cheap hats third asked how much the price?

 

Enter a description:

First inputs a positive integer N (N <= 50), N represents the number of input next price per hat (prices are positive integers, and 1,000 or less)

Output Description:

If the third cheap hat exists, the price is much output, or output -1

Input example 1:

10

10 10 10 10 20 20 30 30 40 40

Output Example 1:

30

answer:

/*简单,时间复杂度也低*/
#include<iostream>
using namespace std;int main(){
int n,t=0,syn=0;
int price[1000]={0};
cin>>n;
while(n--){
cin>>t;
price[t]=1;
}
t=0;
for(int i=0;i<1000;i++){
if(price[t]&&syn<3)
syn++;
if(syn==3)
break;
 t++;
}
syn==3?cout<<t:cout<<-1;
}

 

2, a total of N points on a number line, the coordinates of the first point of the current position of the bear, the N-1 degree is the point of the bear home. Now he needs to go in order of N-1 number from 0 coordinate coordinates.

But in addition to the coordinates 0 and N-1 number coordinates, he can elect the remaining N-2 coordinates of a point, and that point directly to ignore, and asked to go home to at least degree of bear how much distance?

 

answer:

Selecting a point from the coordinates of the N-2, and directly to the point ignored. Simply ignore a point will directly affect the distance before and after the node node. This distance affect us for the time being named to optimize distance, all the nodes in order to form a set of three nodes, will be able to get results this way only through a cycle.

If removed from the greater the optimization of this element of the set will be such that the midpoint of the total distance is shorter, the code below.

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int[] arrays = new int[length];

        for (int i = 0; i < length; i++) {
            arrays[i] = scanner.nextInt();
        }

        /**
        * sum 总距离
        * repetition 三个节点 中被重复计算的总距离
        * select 优化距离最大的 三个节点两两相加的距离
        * add 三个结尾距离为 max 中 头尾节点的距离
        * last 最后三个节点中 尾距离没有被计算两次 需要加上
        * optimizeDistance 优化距离
        */
        int sum = 0,int repetition = 0,int select = 0,int add = 0,int last = 0,int optimizeDistance = 0;

        for (int i = 0; i <= (arrays.length - 3); i++) {
            int begin = arrays[i];
            int mid = arrays[i + 1];
            int end = arrays[i + 2];

            //三个点之间的距离
            int threePointDistance = Math.abs(mid - begin) +
                Math.abs(end - mid);

            //两个点之间的距离 即被多次计算待会需要减掉的距离
            int twoPointDistance = Math.abs(end - mid);
            int contrast = threePointDistance - Math.abs(begin - end);
            repetition += twoPointDistance;
            sum += threePointDistance;
            last = twoPointDistance;

            if (contrast > optimizeDistance) {
                optimizeDistance = contrast;
                select = threePointDistance;
                add = Math.abs(end - begin);
            }
        }

        System.out.println((sum - select + last) - repetition + add);
    }
}

3, of particular interest in recent degrees bears full array, arranged for a 1 to n, the degree of insertion can bear found suitable in accordance with the magnitude relation is greater than in the middle and less-than sign (i.e., '>' and '<') so inequality has become a legitimate number of columns.

But now the degree of hand bears only k small in symbol i.e. ( '<' '), and nk-1 one greater than symbol (i.e.,'> '), the degree of bear want to know for any arrangement of 1 to n, the number of arrangement may using these symbols it is legitimate inequality columns.

 

answer:

dp[i][j] = (dp[i - 1][j - 1] * (i - j) + dp[i - 1][j] * (j + 1)) % 2017;

DP [i] [j] denotes the i-th digit has a number j and can be composed of a small number in the (course number is greater than the number i - j - 1 times, need to use later)

Was added while the first digit i + 1, divided into the following four cases:

  1. If i + 1 is inserted at the beginning of the current sequence, i.e., with a <2, 3 joined after> 1 <2, will find a greater than equal to the number of simultaneously added! (At this time, the relationship can ignore between 1 and 2, since the i + 1> i)
  2. If i + 1 is inserted into the end of the current sequence, i.e., 1 <2 becomes 1 <2 <3, will find a less than equal to the number of simultaneously added! (At this time, the relationship can ignore between 1 and 2, since the i + 1> i)
  3. If i + 1 between a less-than added, i.e., have a <2, and 3 was added to the intermediate, find into a 1 <3> 2, while adding a greater than equal number!
  4. If the intermediate i + 1 is greater than the addition of a number, i.e., with 2> 1, into a 2 <3> 1, while adding a less than equivalent number!

In summary, DP [I] [J] is equal to the sum of the above four cases:

dp [i - 1] [j  ] will be added at the beginning of i is equal to one greater than the number, which requires to have a small number when j i-1 number;

dp [i - - 1] [  1 j] have small in number j-1 i is incremented when the end of the addition of an equal number less than that required number i-1;

dp [i - 1] [j  ] * j i is applied between any number less than, equal to a greater than the number added, i.e. required to have a small number when j i-1 number, are each less than the number It may be made once inserted;

dp [i - 1] [j  - 1] * - (i- j 1) when i is greater than between the load as a number, equal to one less than the number added, the number of required i.e. i-1 j-1 have th Less than sign;

At this time, the total of (i - 1) - (j - 1) - 1 th number is greater than, greater than each of a number of such operations should be carried out combining like terms shall dp [i] [j] = (dp [i - 1] [j - 1] * (i - j) + dp [i - 1] [j] * (j + 1)).

Finally, remember to take the mold.

......

Due to space reasons, I am not one to show you, you need to file a friend can be private letter to me.

Because preparing for interviews, previously collected Java Advanced book "Java Core knowledge finishing .pdf", covering the full text of the JVM, locks, high concurrency, reflection, mybatis, Spring principle, micro-services, Zookeeper, databases, data structures, etc. content, Access: get the comments section!

So, I was called to the office the boss a cup of tea, then it is gone.

OS: You TM layoffs layoffs, assault test how is it?


During this time, my job is to be a state, we found some algorithms and Android -related courses (yes, I turn Andrews, after a problem can be explored to explore)

Help promote what specific content of this course:

JAVA container

 

tree

 

Graph Theory

 

algorithm

 

They also lower the recorded video file, have all signed up before the taped video, I first looked at in more detail and updates waiting to see me live, because it is lifelong see.

 

 

 

 

I heard that some of these videos can be shared out, I needed to be private letter reply [information] for obtaining your way.

Because preparing for interviews, previously collected Java Advanced book "Java Core knowledge finishing .pdf", covering the full text of the JVM, locks, high concurrency, reflection, mybatis, Spring principle, micro-services, Zookeeper, databases, data structures, etc. content, Access: get the comments section!

Published 158 original articles · won praise 43 · views 20000 +

Guess you like

Origin blog.csdn.net/EnjoyEDU/article/details/105252061