Greedy Algorithm of Niuke Online Programming

topic one

topic description

A group of children are playing games, now please distribute candy according to the game score, the requirements are as follows:

1. Every child gets at least one candy no matter how much they score.

2. Between any two adjacent children, the child with more points must take more candies. (if the same, there is no such restriction)

Given an array arr representing the score array, please return the minimum number of candies needed.

problem solving ideas

This problem uses the greedy algorithm

What we mainly need to do is to ensure that if there is a data difference between the two data, we need to change the number of candies

First assign a value of 1 to the candy value in each place

We scan from the front to the back, if the next one is greater than the previous one, we need to add one to the back one on the basis of the previous one; If the number of candies distributed in the middle is smaller, it is updated to the number of candies on the right + 1, otherwise it remains unchanged.

code display

import java.util.*;
public class Solution {
    public int candy (int[] arr) {
        int n = arr.length;
        if (n <= 1)
            return n;
        int[] nums = new int[n];
        //初始化
        for (int i = 0; i < n; i++)
            nums[i] = 1;
        //从左到右遍历
        for (int i = 1; i < arr.length; i++) {
            //如果右边在递增,每次增加一个
            if (arr[i] > arr[i - 1])
                nums[i] = nums[i - 1] + 1;
        }
        //记录总糖果数
        int res = nums[arr.length - 1];
        //从右到左遍历
        for (int i = arr.length - 2; i >= 0; i--) {
            //如果左边更大但是糖果数更小
            if (arr[i] > arr[i + 1] && nums[i] <= nums[i + 1])
                nums[i] = nums[i + 1] + 1;
            //累加和
            res += nums[i];
        }
        return res;
    }
}

Topic 2 Moderator Scheduling

topic description

There are n events about to be held, and each event has a start time and an end time. The start time of the i-th event is starti, and the end time of the i-th event is endi. To hold an event, you need to prepare for the event An event host.

An event host can only participate in one event at a time. And the activity host needs to participate in the whole activity. In other words, if a host participates in the i-th activity, then the host cannot participate in any other activities during the time period (starti, endi). Find the minimum number of hosts needed to successfully hold these n events.

problem solving ideas

// write code here

First of all, we need to be clear that the start time of our activity must not be greater than the end time, so even if we use sorting to sort the start and end of the activity, it will not affect it because what we are looking for is that the start time is greater than a certain end time Time, so we only need to find the start time greater than the end time

When you find one, you can move the end time back, and it will not affect

Finally we can find out how many moderators we need

code display

import java.util.*;
public class Solution {
    public int minmumNumberOfHost (int n, int[][] startEnd) {
        int[] start = new int[n];
        int[] end = new int[n];
        //分别得到活动起始时间
        for (int i = 0; i < n; i++) {
            start[i] = startEnd[i][0];
            end[i] = startEnd[i][1];
        }
        //单独排序
        Arrays.sort(start, 0, start.length);
        Arrays.sort(end, 0, end.length);
        int res = 0;
        int j = 0;
        for (int i = 0; i < n; i++) {
            //新开始的节目大于上一轮结束的时间,主持人不变
            if (start[i] >= end[j])
                j++;
            else
                //主持人增加
                res++;
        }
        return res;
    }
}

おすすめ

転載: blog.csdn.net/young_man2/article/details/127150203