leetcode解题之475. Heaters Java版(加热器)

475. Heaters


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.

Note:

  1. Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
  2. Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
  3. As long as a house is in the heaters' warm radius range, it can be warmed.
  4. All the heaters follow your radius standard and the warm radius will the same.

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.
使用2个数组,house的左边和右边找到最近的取暖器,然后在左边和右边的取暖器中找出个最小半径。最后再在所有house的最小半径中,找出一个最大值,就可以保证所有house都能取暖了。
坐标数组里保存的都是真实坐标+1,这样,0就可以表示house的左边或者右边没有取暖器了。
import java.util.Arrays;

public class Solution {
	public int findRadius(int[] houses, int[] heaters) {
		Arrays.sort(houses);
		Arrays.sort(heaters);

		int left = 0, right = heaters.length - 1;
		int[] lefts = new int[houses.length];
		int[] rights = new int[houses.length];
		for (int i = 0; i < houses.length; i++) {
			// 找到最近的左侧取暖器
			while (left < heaters.length && heaters[left] <= houses[i]) {
				// 这里可以储存为left+1,如果是0,就代表没有取暖器
				lefts[i] = left + 1;
				left++;
			}
			// 下次遍历的left值,是这次的left值
			left = lefts[i] != 0 ? (lefts[i] - 1) : 0;
			while (right >= left && heaters[right] >= houses[i]) {
				rights[i] = right + 1;
				right--;
			}
			right = heaters.length - 1;
		}

		int ret = 0;
		for (int i = 0; i < houses.length; i++) {
			int min = Integer.MAX_VALUE;
			// 找到houses[i] 的最近取暖器
			if (lefts[i] != 0) {
				min = Math.min(houses[i] - heaters[lefts[i] - 1], min);
			}
			if (rights[i] != 0) {
				min = Math.min(heaters[rights[i] - 1] - houses[i], min);
			}
			ret = Math.max(min, ret);
		}

		System.out.println(ret);
		return ret;
	}
}

使用二分查找,top discuss中的答案
这是Discuss中的top解法。先将取暖器数组排序,在遍历所有house,对每个house,在取暖器数组中进行binary search,如果命中,则说明取暖器位置和house位置重合,这个house的最小半径为0;如果没有命中,则使用返回的index,将index左边和右边的取暖器坐标与house坐标求差值,找出这个house最小半径。说白了,也是在查找house的最近左右取暖器
public int findRadius(int[] houses, int[] heaters) {
		Arrays.sort(heaters);
		int result = 0;
		for (int house : houses) {
			// 找不到返回-(low+1)
			int index = Arrays.binarySearch(heaters, house);
			if (index < 0) {
				index = ~index;
				// ~index是大于target 第一个的下标,有可能超出数组范围
				int dist1 = index - 1 >= 0 ? 
						house - heaters[index - 1] : Integer.MAX_VALUE;
				int dist2 = index < heaters.length ? 
						heaters[index] - house : Integer.MAX_VALUE;
				// 找到离house的最近的取暖炉
				int min = Math.min(dist1, dist2);
				result = Math.max(result, min);
			}
		}
		return result;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Java中的排序功能来实现。可以使用Arrays.sort()函数,将列表中的元素按照字母顺序排序,或者使用Collections.sort()函数,将列表中的元素按用户指定的排序规则排序。 ### 回答2: 为了实现LeetCode 2561题(Rearranging Fruits)的要求,需要使用Java编程语言。主要思路是遍历给定的水果数组,同时用一个哈希表来记录每个水果出现的次数。然后根据题目要求,重新排列水果使得相同类型的水果尽可能接近,并且按照出现次数的非递增顺序排序。 具体实现步骤如下: 1. 创建一个HashMap来存储每个水果的出现次数。遍历给定的水果数组,如果该水果已经存在于HashMap中,则将其出现次数加1;否则,将该水果添加到HashMap,并将其出现次数初始化为1。 2. 创建一个ArrayList来存储已经排列好的水果。通过HashMap的entrySet方法获取到每种水果和它的出现次数,然后将这些entry按照出现次数的非递增顺序进行排序。 3. 遍历排序好的entry集合,根据每个水果的出现次数,在ArrayList中连续添加相应数量的水果。 4. 返回排列好的水果数组。 以下是Java代码的示例实现: ```java import java.util.*; class Solution { public String[] rearrange(String[] fruits) { HashMap<String, Integer> fruitCountMap = new HashMap<>(); // 统计每个水果的出现次数 for (String fruit : fruits) { if (fruitCountMap.containsKey(fruit)) { fruitCountMap.put(fruit, fruitCountMap.get(fruit) + 1); } else { fruitCountMap.put(fruit, 1); } } ArrayList<Map.Entry<String, Integer>> sortedEntries = new ArrayList<>(fruitCountMap.entrySet()); // 根据出现次数进行非递增排序 Collections.sort(sortedEntries, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) { return entry2.getValue().compareTo(entry1.getValue()); } }); ArrayList<String> rearrangedFruits = new ArrayList<>(); // 根据出现次数连续添加水果 for (Map.Entry<String, Integer> entry : sortedEntries) { String fruit = entry.getKey(); int count = entry.getValue(); for (int i = 0; i < count; i++) { rearrangedFruits.add(fruit); } } return rearrangedFruits.toArray(new String[0]); } } ``` 使用以上代码,可以对给定的水果数组进行重新排列,使得相同类型的水果尽可能接近,并且按照出现次数的非递增顺序进行排序。返回的结果就是排列好的水果数组。 ### 回答3: 题目要求将一个字符串中的水果按照特定规则重新排列。我们可以使用Java来实现这个问题。 首先,我们需要定义一个函数来解决这个问题。 ```java public static String rearrangeFruits(String fruits) { // 将字符串转换为字符数组方便处理 char[] fruitArray = fruits.toCharArray(); // 统计每种水果的数量 int[] fruitCount = new int[26]; for (char fruit : fruitArray) { fruitCount[fruit - 'a']++; } // 创建一个新的字符数组来存储重新排列后的结果 char[] rearrangedFruitArray = new char[fruitArray.length]; // 逐个将水果按照规则放入新数组中 int index = 0; for (int i = 0; i < 26; i++) { while (fruitCount[i] > 0) { rearrangedFruitArray[index++] = (char) ('a' + i); fruitCount[i]--; } } // 将字符数组转换为字符串并返回 return new String(rearrangedFruitArray); } ``` 上述代码中,我们首先将字符串转换为字符数组,并使用一个长度为26的数组来统计每一种水果的数量。然后,我们创建一个新的字符数组来存储重新排列后的结果。 接下来,我们利用双重循环将每一种水果按照规则放入新数组中。最后,我们将字符数组转换为字符串并返回。 例如,如果输入字符串为`"acbba"`,则经过重新排列后,输出结果为`"aabbc"`。 这样,我们就用Java实现了题目要求的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值