zoukankan      html  css  js  c++  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.

    Approach #1:

    class Solution {
    public:
        int findRadius(vector<int>& houses, vector<int>& heaters) {
            sort(houses.begin(), houses.end());
            sort(heaters.begin(), heaters.end());
            vector<int> res(houses.size(), INT_MAX);
            for (int i = 0, h = 0; i < houses.size() && h < heaters.size(); ) {
                if (houses[i] <= heaters[h]) {
                    res[i] = heaters[h] - houses[i];
                    i++;
                } else 
                    h++;
            }
            for (int i = houses.size()-1, h = heaters.size()-1; i >= 0 && h >= 0; ) {
                if (houses[i] >= heaters[h]) {
                    res[i] = min(res[i], houses[i] - heaters[h]);
                    i--;
                } else
                    h--;
            }
            return *max_element(res.begin(), res.end());
        }
    };
    

    Runtime: 92 ms, faster than 13.76% of C++ online submissions for Heaters.

    Analysis:

    std::max_element

    default (1)
    template <class ForwardIterator>
      ForwardIterator max_element (ForwardIterator first, ForwardIterator last);
    
    custom (2)
    template <class ForwardIterator, class Compare>
      ForwardIterator max_element (ForwardIterator first, ForwardIterator last,
                                   Compare comp);
    Return largest element in range

    Returns an iterator pointing to the element with the largest value in the range [first,last).

    The comparisons are performed using either operator< for the first version, or comp for the second; An element is the largest if no other element does not compare less than it. If more than one element fulfills this condition, the iterator returned points to the first of such elements.

    The behavior of this function template is equivalent to:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    template <class ForwardIterator>
      ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
    {
      if (first==last) return last;
      ForwardIterator largest = first;
    
      while (++first!=last)
        if (*largest<*first)    // or: if (comp(*largest,*first)) for version (2)
          largest=first;
      return largest;
    }
     



    Parameters

    first, last
    Input iterators to the initial and final positions of the sequence to compare. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
    comp
    Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered less than the second.
    The function shall not modify any of its arguments.
    This can either be a function pointer or a function object.

    Return value

    An iterator to largest value in the range, or last if the range is empty.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    【leetcode❤python】 374. Guess Number Higher or Lower
    【leetcode❤python】 8. String to Integer (atoi)
    【leetcode❤python】 438. Find All Anagrams in a String
    【leetcode❤python】 88. Merge Sorted Array
    【leetcode❤python】 225. Implement Stack using Queues
    【leetcode❤python】 58. Length of Last Word
    463:归档和传输文件
    438:管理网络
    365:查看系统日志条目
    350:描述系统日志架构
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9925524.html
Copyright © 2011-2022 走看看