232ms cpp easy solution


  • 1
    M
        int findRadius(vector<int>& houses, vector<int>& heaters) {
            set<int> heater_set;
            for (const int& heater : heaters) {
                heater_set.insert(heater);
            }
            int result = 0, dis;
    
            for (const int& house : houses) {
                auto iter = heater_set.lower_bound(house);
                if (iter == heater_set.begin()) {
                    dis = abs(*iter - house);
                } else if (iter == heater_set.end()) {
                    // left iterator
                    auto l_iter = next(iter, -1);
                    dis = abs(*l_iter - house);
                } else {
                    auto l_iter = next(iter, -1);
                    dis = min(abs(*iter - house), abs(*l_iter - house));
                }
                result = max(dis, result);
            }
            return result;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.