Java post development for 3 years, the company's temporary spot check algorithm, I remember these questions after leaving for a lifetime, from shallow to deep

A few days ago, our company did a stupid thing, a very, very stupid thing. I thought that after I came out of school, there would be nothing related to the exam except for the test of finding a job.

However, there was an unexpected situation. The technical director and personnel director of the company suddenly came to our business line, called my boss and organized a new "examination" for us.

It was a sunny afternoon. I crossed my legs, holding a cup of cappuccino in my left hand and my Logitech mouse in my right hand. I rolled the wheel axle and shuttled between the headlines.

"Pale yellow dress, fluffy hair"

"WC, witness history. This year's college entrance examination was postponed for a month. If I met this kind of thing, wouldn't I 985 minutes?"

Colleague: "is that why you can't go to college?"

Me: "Er ~"

"WC, ah Shui, is this going to taobo?"

Colleague: "what does this have to do with your failure to go to undergraduate?"

Me: "Hello, hello?"

At this time, a middle-aged Mediterranean appeared in my sight: "everyone, pay attention. In order to investigate your programming ability, there will be a test five minutes later. After saving your code, gather in the big conference room."

...

Colleague a: "isn't this the technical director from the headquarters?"

Colleague B: "WC, the first time I saw a big man."

Me: "didn't you hear the test?"

...

Later, we had an ambiguous test. When I handed in a blank test paper, I brought out a blank brain and listened to my colleagues talking about the test answers.

My colleague told me that this algorithm problem came from Baidu and gave me a document.

The specific contents of the document are as follows:

1. Dudu bear wants to go to the mall to buy a hat. There are N hats in the mall. The prices of some hats may be the same. Dudu bear wants to buy a hat with the third cheapest price. What's the price of the third cheapest hat?

Enter Description:

First, enter a positive integer N (N < = 50), and then enter N numbers to represent the price of each hat (the prices are positive integers and less than or equal to 1000)

Output Description:

If there is a third cheap hat, please output the price, otherwise output - 1

Input example 1:

10

10 10 10 10 20 20 30 30 40 40

Output example 1:

30

answer:

1.  `/*Simple and low time complexity*/  #`

2.  `include < iostream >  using  namespace std;`

3.  `int main()`

4.  `{`

5.  `int n, t =  0, syn =  0;`

6.  `int price[1000]  =  {`

7.  `0`

8.  `};`

9.  `cin >> n;`

10.  `while(n--)`

11.  `{`

12.  `cin >> t;`

13.  `price[t]  =  1;`

14.  `}`

15.  `t =  0;`

16.  `for(int i =  0; i <  1000; i++)`

17.  `{`

18.  `if(price[t]  && syn <  3) syn++;`

19.  `if(syn ==  3)  break;`

20.  `t++;`

21.  `}`

22.  `syn ==  3  ? cout << t : cout <<  -1;`

23.  `}`

</pre>

2. There are n points on a number axis. The coordinate of the first point is the current position of Dudu bear, and the N-1 point is the home of Dudu bear. Now he needs to go from coordinate 0 to coordinate n-1 in turn.

However, in addition to coordinates 0 and N-1, he can select a point in the other N-2 coordinates, directly ignore this point, and ask how far Dudu bear will go home at least?

answer:

Select a point from N-2 coordinates and ignore it directly. Directly ignoring a point will only directly affect the distance between the nodes before and after this node. This influence distance is temporarily named as the optimization distance. All nodes are formed into a set of three nodes in order. In this way, the results can be obtained only through one cycle.

The larger the optimization distance is, the shorter the total distance will be if the midpoint element of this set is removed. The code above is shown below.

1.  `import`

2.  `java.util.`

3.  `Scanner;`

4.  `publicclass`

5.  `Main`

6.  `{`

7.  `publicstaticvoid`

8.  `main(String[] args)`

9.  `{`

10.  `Scanner`

11.  `scanner =  new`

12.  `Scanner(System.in);`

13.  `int`

14.  `length = scanner.nextInt();`

15.  `int[] arrays = newint[length];`

16.  `for(int i =  0; i < length; i++)`

17.  `{`

18.  `arrays[i]  = scanner.nextInt();`

19.  `}`

20.  `/**`

22.  `* sum Total distance`

24.  `* repetition The total distance repeatedly calculated in the three nodes`

26.  `* select Optimize the distance between the three nodes with the largest distance`

28.  `* add The three end distances are the distance between the head and tail nodes in max`

30.  `* last The tail distance of the last three nodes is not calculated twice and needs to be added`

32.  `* optimizeDistance Optimized distance`

34.  `*/`

35.  `int`

36.  `sum =  0,`

37.  `int`

38.  `repetition =  0,`

39.  `intselect =  0,`

40.  `int`

41.  `add =  0,`

42.  `intlast =  0,`

43.  `int`

44.  `optimizeDistance =  0;`

45.  `for(int i =  0; i <=  (arrays.length -  3); i++)`

46.  `{`

47.  `intbegin = arrays[i];`

48.  `int`

49.  `mid = arrays[i +  1];`

50.  `intend = arrays[i +  2];`

51.  `//Distance between three points`

52.  `int`

53.  `threePointDistance =  Math.abs(mid -  begin)  +  Math.abs(end  - mid);`

54.  `//The distance between two points is the distance to be subtracted after being calculated many times`

55.  `int`

56.  `twoPointDistance =  Math.abs(end  - mid);`

57.  `int`

58.  `contrast = threePointDistance -  Math.abs(begin  -  end);`

59.  `repetition += twoPointDistance;`


### last

### **[CodeChina open source project: [analysis of Java interview questions of first-line large manufacturers + core summary learning notes + latest explanation Video]]( https://codechina.csdn.net/m0_60958482/java-p7)**

![](https://img-blog.csdnimg.cn/img_convert/dff92c27d5214a04439cbb22a8d5f0fb.png)

![](https://img-blog.csdnimg.cn/img_convert/a521671fb568290bf9f2c15321deb45e.png)

![](https://img-blog.csdnimg.cn/img_convert/f8fa0a58022988d6b8c0b737346026b2.png)

![](https://img-blog.csdnimg.cn/img_convert/bd541a704de10721c8536bd8ecab17f4.png)

![](https://img-blog.csdnimg.cn/img_convert/8fa072a8aedd74929a8b1a5821e448a4.png)

![](https://img-blog.csdnimg.cn/img_convert/e22d9be786786fb56c2135ffa14769fe.png)


Due to space reasons, I won't show more
5589)]

[External chain picture transfer...(img-f8ubtRwt-1630464155590)]

[External chain picture transfer...(img-EVZ3gpgA-1630464155592)]

[External chain picture transfer...(img-J4lnw2Yf-1630464155593)]

[External chain picture transfer...(img-v0Y2Ji7t-1630464155594)]

[External chain picture transfer...(img-Ceict68a-1630464155595)]


Due to space reasons, I won't show more

Keywords: Java Algorithm Back-end

Added by knobby2k on Sat, 18 Dec 2021 02:02:36 +0200