问题

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

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

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

说明:

  • 给出的房屋和供暖器的数目是非负数且不会超过 25000。
  • 给出的房屋和供暖器的位置均是非负数且不会超过10^9。
  • 只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖。
  • 所有供暖器都遵循你的半径标准,加热的半径也一样。

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

输出: 1

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

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

输出: 1

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


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:

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

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.

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.


示例

public class Program {public static void Main(string[] args) {var S = "I speak Goat Latin";var res = ToGoatLatin(S);Console.WriteLine(res);Console.ReadKey();}private static string ToGoatLatin(string S) {//按空格分隔var split = S.Split(' ');//定义结果var res = string.Empty;for(var i = 0; i < split.Length; i++) {if(IsStartsWithVowel(split[i])) {res += split[i];} else {//辅音字母开头时,首字母后置res += split[i].Substring(1) + split[i][0];}//追回字符串 ma 和按索引重复的字符 ares += "ma" + RepeatString(i + 1) + " ";}return res.Trim();}private static bool IsStartsWithVowel(string word) {//判断是不是元音字母var s = word[0].ToString().ToLower();return s == "a" || s == "e" || s == "i" || s == "o" || s == "u";}private static string RepeatString(int count) {//重复字符串var c = 'a';var res = string.Empty;for(var i = 0; i < count; i++) {res += c;}return res;}}

以上给出1种算法实现,以下是这个案例的输出结果:

Imaa peaksmaaa oatGmaaaa atinLmaaaaa

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#475-供暖器(Heaters)相关推荐

  1. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  2. leetcode 工作每日一题 475. 供暖器 (二分 stl)

    题意: 冬季已经来临. 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖.在加热器的加热半径范围内的每个房屋都可以获得供暖.现在,给出位于一条水平线上的房屋 houses 和供暖器 heater ...

  3. LeetCode 刷题之路(python版)

    摘自:https://blog.csdn.net/qq_32384313/article/details/90745354 LeetCode 刷题之路(python版) 小坏wz 2019-06-02 ...

  4. 【Coding】LeetCode刷题记录

    常用数据结构 1. 集合 2. 排序 3. 二分 - 模板 [33. 搜索旋转排序数组](https://leetcode-cn.com/problems/search-in-rotated-sort ...

  5. C#LeetCode刷题-队列

    队列篇 # 题名 刷题 通过率 难度 363 矩形区域不超过 K 的最大数值和 27.2% 困难 621 任务调度器 40.9% 中等 622 设计循环队列 C#LeetCode刷题之#622-设计循 ...

  6. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C#LeetC ...

  7. C#LeetCode刷题-栈

    栈篇 # 题名 刷题 通过率 难度 20 有效的括号 C#LeetCode刷题之#20-有效的括号(Valid Parentheses) 33.0% 简单 42 接雨水 35.6% 困难 71 简化路 ...

  8. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) 15.3% 中 ...

  9. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

最新文章

  1. httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过htt......
  2. 如果知道一个控件类型的对话框句柄是编辑框控件
  3. Python学习 第2天 IDE开发工具
  4. Python实现顺序表
  5. php5.*.* iis 安装
  6. ASP.NET Core 2.0 : 图说管道,唐僧扫塔的故事
  7. 【2019年07月08日】A股最便宜的股票
  8. 动态壁纸小程序(带流量主)源码
  9. 阿里云服务器(windows2012)
  10. 史上最完整Java中将File转化为MultipartFile的方法(附阿里云腾讯云对象存储API对照)
  11. Maven学习总结(26)——maven update时,报:Preference node org.eclipse.wst.validation...
  12. 解决在TP5中无法使用快递鸟的即时查询API
  13. POJ 1947 Rebuilding Roads
  14. Android开发中验证码的生成
  15. c语言 程序段 数据段,C程序段(代码段、数据段、BSS段以及堆栈)的详解
  16. 集中化Linux日志管理系统
  17. 逻辑回归及美团逻辑回归总结
  18. access数据库窗体设计实验报告_Access2010实验报告
  19. android麦克风设置在哪,手机麦克风设置实用教程
  20. i510200h和i78750h哪个好

热门文章

  1. SQL Server2008数据库的维护 (第四部分)
  2. LeetCode 108. Convert Sorted Array to Binary Search Tree
  3. LeetCode 121. Best Time to Buy and Sell Stock
  4. Linux—JVM等性能调优监控
  5. Linux—Centos 7.x安装Tomcat8
  6. Java缓存Ehcache-核心类和方法介绍及代码实例
  7. 草稿 listview控件切换大小图标
  8. 演练 制作爱奇异视频播放列表 0929
  9. python-装饰器简介
  10. python-类的装饰器