645. Set Mismatch/598. Range Addition II/319. Bulb Switcher/374. Guess Number Higher or Lower

645. Set Mismatch

Problem Description

The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.

Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

Implementation

use the vector index to sign to indicate the result we have met in traverse the whole array. When met with a number, set the element which has the index value equals to the value of the number mod num_len. Be careful about the number n, which get 0 when modding. In this travesion, we can know the duplicate number.

Second going through, we will find the number which is not negative and this is the missing number.

Example 1:
Input: nums = [1,2,2,4]
Output: [2,3]

Note:

The given array size will in the range [2, 10000].
The given array’s numbers won’t have any order.

class Solution {
public:
    vector<int> findErrorNums(vector<int>& nums) {
        vector<int> res;
        int nums_len = nums.size();

        if(!nums_len) {
            return res;
        }

        for(int idx = 0; idx < nums_len; idx++) {
            int rem = abs(nums[idx])%nums_len;
            int rem_idx = rem > 0?rem:0;
            if(nums[rem_idx] < 0) {
                if(!rem_idx) {
                    rem_idx = nums_len;   
                }
                res.push_back(rem_idx);
            }
            else {
                nums[rem_idx] *= -1;
            }
        }

        for(int idx = 0; idx < nums_len; idx++) {
            if(nums[idx] >= 0) {
                if(!idx) {
                    res.push_back(nums_len);
                    return res;
                }
                res.push_back(idx);
                return res;
            }
        }    

        return res;
    }
};

598. Range Addition II

Problem Description

Given an m * n matrix M initialized with all 0's and several update operations.

Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for all 0 <= i < a and 0 <= j < b.

You need to count and return the number of maximum integers in the matrix after performing all the operations.

Example 1:
Input: 
m = 3, n = 3
operations = [[2,2],[3,3]]
Output: 4
Explanation: 
Initially, M = 
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]

After performing [2,2], M = 
[[1, 1, 0],
 [1, 1, 0],
 [0, 0, 0]]

After performing [3,3], M = 
[[2, 2, 1],
 [2, 2, 1],
 [1, 1, 1]]

So the maximum integer in M is 2, and there are four of it in M. So return 4.
Note:
The range of m and n is [1,40000].
The range of a is [1,m], and the range of b is [1,n].
The range of operations size won't exceed 10,000.

Implementation

If you just use brute force in this algorithm, you will get time exceeding.

For better space and computational efficiency, you need use two vectors to record the adding steps. As you can see, the max number of this matrix must be the first element. And the result must be one matrix, so we need just go through the row and column vector to get the max matrix to calculate the area which is the result of what we need.

There is one optimal solution to find the minimum element and multiply.

class Solution {
public:
    int maxCount(int m, int n, vector<vector<int>>& ops) {
        int res_cnt = 0;
        if(!m || !n) {
            return res_cnt;
        }
        vector<int> row_rec(m, 0);
        vector<int> col_rec(n, 0);

        int ops_len = ops.size();
        if(!ops_len) {
            return m*n;
        }
        for(int idx = 0; idx < ops_len; idx++) {
            int op1 = ops[idx][0];
            int op2 = ops[idx][1];
            for(int idx1 = 0; idx1 < op1; idx1++) {
                row_rec[idx1]++;
            }
            for(int idx2 = 0; idx2 < op2; idx2++) {
                col_rec[idx2]++;
            }
        }

        int col_max = 0;

        while(col_max < n) {
            if(col_rec[col_max] != col_rec[0]) {
                break;
            } 
            col_max++;
        }
        int row_max = 0;
        while(row_max < m) {
            if(row_rec[row_max] != row_rec[0]) {
                break;
            }
            row_max++;
        }
        res_cnt = row_max*col_max;

        return res_cnt;
    }
};

319. Bulb Switcher

Problem Description

There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Example:

Given n = 3. 

At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off]. 

So you should return 1, because there is only one bulb is on.

Implementation

From the above problem, you write the some examples and find that the number which is multiply by odd number is on after toggling.

class Solution {
public:
    int bulbSwitch(int n) { 
        return floor(sqrt(n));
    }
};
class Solution {
public:
    int bulbSwitch(int n) {
        int res = 0;
        for(int idx = 1; idx*idx <= n; idx++) {
            res++;
        }

        return res;
    }
};

374. Guess Number Higher or Lower

Problem Description

We are playing the Guess Game. The game is as follows:

I pick a number from 1 to n. You have to guess which number I picked.

Every time you guess wrong, I'll tell you whether the number is higher or lower.

You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):

-1 : My number is lower
 1 : My number is higher
 0 : Congrats! You got it!
Example:
n = 10, I pick 6.

Return 6.

Be afraid of overflow~ Binary Search would be very helpful.

Implementation

// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);

class Solution {
public:
    int guessNumber(int n) {
        int stt = 1;
        int lst = n;
        while(stt <= lst) {
            int mid = stt + ((lst - stt) >> 1);
            int res = guess(mid);
            if(res == 0) {
                return mid;
            }    
            else if(res == -1) {
                lst = mid - 1;
            }    
            else {
                stt = mid + 1;
            }    
        }    

        return stt < n?1:n;    
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值