475.Heaters java

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 are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.

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

Example 1:

Input: [1,2,3],[2]
Output: 1
Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.

 

Example 2:

Input: [1,2,3,4],[1,4]
Output: 1
Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.

按照题目要求来看,就是在heaters上放置炉子,覆盖全部的houses,求距离最近炉子的最大半径。
思路:一个house可以选择两个炉子。左边的和右边的,哪个距离自己近,就选择被哪个炉子温暖,注意第一个heater左边的houses只能被第一个heater温暖,最后一个heater右边的
houses只能被最后一个heater温暖,其余中间的房子,均可以被左边和右边的同时温暖,在所有的最近的半径里面选择最大的半径,就是题目要求求出的半径。
java代码:
public class Solution {
    public int findRadius(int[] houses, int[] heaters) {
            Arrays.sort(houses);
       Arrays.sort(heaters);
       int max=0;
       for(int i=0;i<houses.length;i++)
       {
    	   //二分法找出第一个大于houses[i]的heater[mid]
    	   int left=0;
    	   int right=heaters.length-1;
    	   while(left<right)//for循环是用来求解距离自己最近的heater
    	   {
	    	   int mid=left+(right-left)/2;
	    	   if(houses[i]>heaters[mid])
	    	   {
	    		   left=mid+1;
	    	   }
	    	   else
	    		   right=mid; 
    	   }
    	   if(right==0)//如果是第一个heater的话 距离就是用这个炉子减去房间的距离
    		   max=Math.max(max,Math.abs(heaters[right]-houses[i]));
    	  else {//其余的话,都是和上一个炉子和这个炉子减去房间的距离,选择其中最小的
    			   int distance1=Math.abs(heaters[right]-houses[i]);
    	    	   int distance2=Math.abs(houses[i]-heaters[right-1]);
    	    	   max=Math.max(max, Math.min(distance1, distance2));//在所有的距离里面选择最大值
			}
    	   
       }
       return max;
    }
}

  

 

转载于:https://www.cnblogs.com/zhangfanxmian/p/6859672.html

这个问题比较广泛,需要更多的信息才能提供一个具体的解决方案。以下是一个供热系统的简单模拟代码,仅供参考。请注意,这里的模拟代码仅仅是一个简单的示例,实际的供热系统需要更复杂的模拟和控制。 ```python import numpy as np class HeatingSystem: def __init__(self, n_rooms, n_floors, n_heaters): self.n_rooms = n_rooms self.n_floors = n_floors self.n_heaters = n_heaters self.rooms = np.zeros((n_floors, n_rooms)) self.heaters = [Heater() for i in range(n_heaters)] def simulate(self, n_steps): for i in range(n_steps): # calculate the temperature change in each room for f in range(self.n_floors): for r in range(self.n_rooms): self.rooms[f, r] += self.temperature_change(f, r) # adjust the heaters based on the temperature in each room for h in self.heaters: h.adjust_power(self.rooms) def temperature_change(self, floor, room): pass # TODO: implement the temperature change formula class Heater: def __init__(self): self.power = 0 def adjust_power(self, rooms): pass # TODO: implement the power adjustment algorithm ``` 在这个简单的模拟中,`HeatingSystem`代表整个供热系统,包括房间和加热器。每个房间都有一个温度,加热器的功率可以调节,以控制房间的温度。`simulate`方法模拟了供热系统的运行,每一步都会更新房间的温度,并根据温度来调整加热器的功率。 要使用这个模拟,需要实现`temperature_change`和`adjust_power`方法。`temperature_change`方法计算每个房间的温度变化,可以根据房间的绝缘性、外部温度、加热器功率等因素来计算。`adjust_power`方法根据房间的温度来调整加热器的功率,可以使用一些简单的算法,例如比例控制或PID控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值