1. 程式人生 > >[日常刷題]leetcode D37

[日常刷題]leetcode D37

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.

Solution in C++:

關鍵點:

  • 遍歷目標選擇

思路:

  • 這個題做了一個小時,由於遍歷物件錯誤而做不下去,去做下一題了,然後後來回來看了人家的思路,發現自己的應該也是對了,就是沒有簡介,分類討論的情況搞的太多了,然後遍歷物件也選錯了,選擇了heaters。
  • 這邊主要的思路就是計算所有房子和離自己最近的heater的距離,然後再取最大值
int findRadius(vector<int>& houses, vector<int>& heaters) {
        size_t hosize = houses.size();
        size_t hesize = heaters.size();
        if (hosize == 0)
            return 0;
        
        sort(houses.begin(), houses.end());
        sort(heaters.begin(), heaters.end());
        
        int i = 0, r = 0;
        for(auto house : houses){
            while(i+1 < hesize && house >= ceil((heaters[i+1] + heaters[i])/2.0))
                ++i;
            r = max(r, abs(heaters[i] - house));
        }
        
        return r;
    }

476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

Example 1:

Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Example 2:

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

Solution in C++:

關鍵點:

  • 位運算

思路:

  • 我的方法主要是暴力,先把num轉換為對應的二進位制,然後遍歷字串求值,只不過將其中的1當作0,0當作1.
  • 這邊主要介紹一下我在discuss裡面看到的一個比較簡介的解決方法。這裡主要是獲取一個和num一樣位數的全1數,因為 0 ^ 1 = 1; 1^1 = 0剛好符合題意要求,所以當t > num時停止,則t-1是為與num相同位數的全1序列。

方法一:暴力

string tobinary(int num){
        string result;
        
        while(num){
            result = to_string(num % 2) + result;
            num /= 2;
        }
        return result;
    }
    
    int findComplement(int num) {
        int re = 0;
        if (num == 0)
            return 1;
        else{
            string result = tobinary(num);
            size_t size = result.size();
            for(int i = 0; i < size; ++i){
                int tmp = (result[i] - '0') ^ 1;
                re += tmp * pow(2, size - 1 - i);
            }
        }
        
        return re;
    }

方法二:位運算

int findComplement(int num) {
        long long t=1;
        while(t<=num)
            t=t<<1;
        return (t-1)^num;
    }

小結

哎,今天也是挫敗感不低啊,不知道是不是最近事情變多了,然後也有點懷疑這樣不復習資料結構和演算法的刷題策略是不是有錯誤,但是又覺得刷了這麼久了不想輕易就放棄。狀態太不好了。。。

知識點

  • 分情況
  • 位運算