475.Heaters Java

Source: Internet
Author: User

Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.

Now, you is given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so it all HO Uses could is covered by those heaters.

So, your input would be the positions of houses and heaters seperately, and your expected output would be the minimum radius Standard of heaters.

Example 1:

Input: [1,2,3],[2]output:1explanation:the only heater is placed in the position 2, and if we use the radius 1, Then all the houses can warmed.

Example 2:

 Input: [1,2,3,4],[1,4]output:1explanation:the heater is placed in the position 1 and 4. We need to use RADIUS 1 standard and then all the houses can is warmed. 

According to the requirements of the topic, is to place the stove on the heaters, covering all the houses, to find the maximum radius of the nearest stove.
idea: A house can choose two stoves. To the left and to the right, which distance oneself near, choose to be which stove warm, notice the first heater left houses can only be the first heater warm, the last heater right of the
houses can only be the last heater warm, the rest of the Middle House, Can be left and right at the same time warm, in all the nearest radius to choose the largest radius, is the problem of the radius of the request.
Java code:
public class Solution {public    int Findradius (int[] houses, int[] heaters) {            arrays.sort (houses);       Arrays.sort (heaters);       int max=0;       for (int i=0;i

  

475.Heaters Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.