Java Huawei Real Exam Questions-Seating Arrangement for New Employees

need:

The work station consists of the sequence F1, F2...Fn, and the Fi value is 0, 1 or 2. Among them, 0 represents vacant, 1 represents occupied, and 2 represents obstacles.
  1. The friendliness of a certain vacancy is the sum of the number of consecutive old employees on the left and right.
  2. In order to facilitate new employees to learn and ask for help, priority is given to vacancies with high friendliness. Sequence, find the maximum value of friendship among all gaps

Input description:
     The first line is the station sequence: F1.F2...Fn, 1<=n<=100000, Fi value is 0, 1 or 2. Among them, 0 represents vacancy, 1 code is occupied, and 2 represents obstacles. 0 represents vacancy, 1 code is occupied, and 2 represents obstacles.
Output description:
    The maximum value of friendliness among all slots. If there is no space, return 0 

Example 1
input:
0 1 0
output:
1

Note:
The first and third positions have a friendliness of 1.

Example 2
input:
1 1 0 1 2 1 0
output:
3

Note:
The friendliness of the third location is 3. Due to the barrier, the left side gets 2 points and the right side only gets 1 point.

coding:

public class TesEmployee {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入工位序列:");
        String[] postion = sc.nextLine().split(" ");
        //字符串数组转换成整数数组
        int[] arr = Arrays.asList(postion).stream().mapToInt(Integer::parseInt).toArray();

        //使用map存放
        Map<Integer, Integer> mapLeft = new HashMap<>();
        Map<Integer, Integer> mapRight = new HashMap<>();

        //调用方法
        saveMap(arr,0,mapLeft);
        saveMap(arr,1,mapRight);

//        所有空位中友好度的最大值
        int max=getMax(mapLeft,mapRight);
        System.out.println(max);

    }

    /**
     * 计算所有空位中友好度的最大值
     * @param map1
     * @param map2
     * @return
     */
    public static int getMax(Map<Integer, Integer> map1,Map<Integer, Integer> map2){
       int max=0;
        for (Integer key:map1.keySet()
             ) {
            Integer  value=map1.get(key)+map2.get(key);
            //进行比较
            max=Math.max(max,value);
        }
        return max;
    }

    /**
     * 正向遍历一遍,反向遍历一遍,遇到0,就把连续1的值记录下来
     * @param arr
     * @param flag
     * @param map
     */
    public static void saveMap(int[] arr, int flag, Map<Integer, Integer> map) {
        //累计1的和
        int sum = 0;
        if (flag == 0) {  //如果是正向遍历, 工位是1的,sum就加1,工位是0,则sum为0
            //循环
            for (int i = 0; i < arr.length; i++) {
                //判断
                if (arr[i] == 1) {
                    sum++;  //累计和
                } else if (arr[i] == 0) {
                    map.put(i, sum);
                    sum = 0; //重置sum为0
                } else {
                    sum = 0; //重置sum为0
                }
            }
        } else {  //如果是反向遍历,同理;
            //循环
            for (int i = arr.length - 1; i >= 0; i--) {
                //判断
                if (arr[i] == 1) {
                    sum++;  //累计和
                } else if (arr[i] == 0) {
                    map.put(i, sum);
                    sum = 0; //重置sum为0
                } else {
                    sum = 0; //重置sum为0
                }
            }
        }

    }


}

Effect:

Guess you like

Origin blog.csdn.net/hlx20080808/article/details/133346490