编程知识 cdmana.com

Cry! The new technical director temporarily checks the algorithm. I have been developing Java for five years (1)

Java Post development 3 year , The company's temporary spot check algorithm , I'll remember these questions all my life

At this time , A middle-aged Mediterranean appeared in my sight :“ With all of you , Pay attention to the , In order to examine everyone's programming ability ,5 There will be a test in minutes , After saving your code , Gather in the big meeting room .”

...

Colleague a :“ Isn't this the technical director from headquarters ?”

Colleague b :“WC, The first time I saw a big man .”

I :“ Didn't you hear about the test ?”

...

later , We did a test for no apparent reason , When I hand in a blank test paper , Bring out a blank brain , Listen to colleagues talking about test answers .

Colleagues tell me , This algorithm problem comes 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 , In the mall N A hat , Some hats may cost the same . Dudu bear wants to buy a hat which is the third cheapest , Ask the third cheapest hat price ?

Input description :

First enter a positive integer N(N <= 50), Next input N The number represents the price of each hat ( 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 :

/ Simple , The time complexity is also low /

#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(s

《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing

yn==3)

break;

t++;

}

syn==3?cout<<t:cout<<-1;

}

2、 On a number axis there are N A little bit , The coordinates of the first point are the current position of the degree bear , The first N-1 One point is the home of Dudu bear . Now he needs to start from 0 Coordinate No.1 goes to N-1 Coordinate No .

But in addition to 0 Coordinates and N-1 Coordinate No , He could be in the rest of the N-2 Choose a point out of the coordinates , And ignore this point directly , Ask Dudu bear how far to go home at least ?

answer :

from N-2 Choose a point out of the coordinates , And ignore this point directly . Ignore a point directly It's just Directly affect , The distance between the front and back nodes of this node . This For the time being, we will name the distance of influence optimization distance , All the nodes in order to form a set of three nodes , In this way, you only need to go through one cycle to get the result .

The larger the optimization distance is, the shorter the total distance will be if the midpoint element of the set is removed , Here's the code .

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 Total distance

  • repetition Three nodes The total distance repeatedly calculated in

  • select Optimize the maximum distance The added distance of the three nodes

  • add The three ending distances are max in The distance between the head and tail nodes

  • last Of the last three nodes The tail distance is not calculated twice Need to add

  • optimizeDistance Optimize the distance

*/

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];

// The distance between the three points

int threePointDistance = Math.abs(mid - begin) +

Math.abs(end - mid);

// The distance between two points That is, the distance to be subtracted is calculated many times

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、 Dudu bear has recently been particularly interested in full permutations , about 1 To n An arrangement of , The degree bear found that the appropriate greater than and less than symbols can be inserted in the middle according to the size relationship ( namely '>' and '<' ) Make it a legal inequality sequence .

But now Dudu bear has only k A less than symbol, i.e ('<'') and n-k-1 A greater than symbol ( namely '>'), Dudu bear wants to know about 1 to n How many permutations in any permutation can use these symbols to make it a legal inequality sequence .

Last

Learning video :

 crying ! The new technical director temporarily checks the algorithm ,Java I've been developing for five years (1)

The real interview question of Dachang :

 crying ! The new technical director temporarily checks the algorithm ,Java I've been developing for five years (1)

This article has been  CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included

版权声明
本文为[mb61ab40ce80d9c]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/12/202112122212513433.html

Scroll to Top