【LeetCode刷题】供暖器:妙解

冬季已经来临。?你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。

现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径。

所以,你的输入将会是房屋和供暖器的位置。你将输出供暖器的最小加热半径。

说明:

?
?

给出的房屋和供暖器的数目是非负数且不会超过 25000。

给出的房屋和供暖器的位置均是非负数且不会超过10^9。

只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖。

所有供暖器都遵循你的半径标准,加热的半径也一样。

示例 1:

输入: [1,2,3],[2]

输出: 1

解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。

示例 2:

输入: [1,2,3,4],[1,4]

输出: 1

解释: 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。

house





heater

?
?

我的错误:思路是以供暖器为标,计算房子的距离,正确的是以房子为标,计算每个房子和最近的供暖器的距离,取每个房子距离中最大的一个。就差一点,但是错了;双指针法更加简单,只需要O(M+N),思路差不多,总之都是基于排序的,所以都有NlogN不可避免。

原文地址:https://www.cnblogs.com/xukaiae86/p/12267439.html

时间: 2024-10-10 12:12:09

【LeetCode刷题】供暖器:妙解的相关文章

leetcode 刷题之路 94 N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of

leetcode 刷题之路 95 N-Queens I

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. N皇后问题的变种,要求直接输出N皇后的解法数目.这道题可以在N-Queens I的基础上增加计数功能,在每求得一个成功的解时(行数为N时)使计数变量递增即可.题目不要求输出具体的解法,因此可以做一点优化,使用position数组用来表示皇后的位置,p

【leetcode刷题笔记】Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 所谓的anagrams,只若干个词,它们包含的字母的个数和种类完全一样,只是字符的顺序不一样.比如说bus,usb,sub就是一组angrams.同一组angrams具有排序后相同的特点,比如对上述三个单词按字典序排序分别得到bsu,bsu,bsu.我们用这一点

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t