leetcode Array problems 561,1351,1010,1018

561. Array Partition I

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Example 1:

Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

Note:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].

Solutions:

class Solution {
public:
    int arrayPairSum(vector<int>& nums) {
        int sum = 0;
        sort(nums.begin(), nums.end());
        for(int i = 0; i < nums.size(); i+=2)
            sum += nums[i];
        return sum;
    }
};

Performance:

1351. Count Negative Numbers in a Sorted Matrix

Given a m * n matrix grid which is sorted in non-increasing order both row-wise and column-wise. 

Return the number of negative numbers in grid.

Example 1:

Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output: 8
Explanation: There are 8 negatives number in the matrix.

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 100
  • -100 <= grid[i][j] <= 100

Solutions:

class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        int ans(0);
        for(int i=0; i<grid.size();i++)
            for(int j=0;j<grid[i].size();j++)
            {
                if(grid[i][j] < 0)
                {
                    ans += grid[i].size()-j; break;
                }
            }
        return ans;
    }
};

Performance:

online collection of Great solutions: from [C++] Three Methods

class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        int ans(0);
        
        // Brute Force: O(mn) - check if negative
        for (const vector<int>& row: grid)
            for (const int& i: row)
                if (i < 0) ans++;
        return ans;

    }
};
class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        int ans(0);

        // Binary Search: O(m lg(n)) or equivalently O(n lg(m)) - look for break point of each row / column
        for (const vector<int>& row: grid)
            ans += upper_bound(row.rbegin(), row.rend(), -1) - row.rbegin();
        return ans;
    }
};
       
class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        int ans(0);

        // Search Break Points: O(m + n) - traverse from upper right to lower left
        int m(grid.size()), n(grid[0].size()), r(0), c(n - 1);
        while (r < m) {
            while (c >= 0 && grid[r][c] < 0) c--;
            ans += n - c - 1;
            r++;
        }
        return ans;
    }
};

Useful Knowledge:

std::binary_search

std::vector::vector

C++ 创建一维二维vector方法合集

1010. Pairs of Songs With Total Durations Divisible by 60

In a list of songs, the i-th song has a duration of time[i] seconds. 

Return the number of pairs of songs for which their total duration in seconds is divisible by 60.  Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.

Example 1:

Input: [30,20,150,100,40]
Output: 3
Explanation: Three pairs have a total duration divisible by 60:
(time[0] = 30, time[2] = 150): total duration 180
(time[1] = 20, time[3] = 100): total duration 120
(time[1] = 20, time[4] = 40): total duration 60

Note:

  1. 1 <= time.length <= 60000
  2. 1 <= time[i] <= 500
class Solution {
public:
    int numPairsDivisibleBy60(vector<int>& time) {
        vector<int> m(60,0);
        int r = 0; int count = 0;
        for(const auto i:time)
        {
            r = i % 60;
            count += m[(60-r) % 60];
            m[r]++;
        }
        return count;
    }
};

1018. Binary Prefix Divisible By 5

Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-bit to least-significant-bit.)

Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.

Example 1:

Input: [0,1,1]
Output: [true,false,false]
Explanation: 
The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10.  Only the first number is divisible by 5, so answer[0] is true.

Note:

  1. 1 <= A.length <= 30000
  2. A[i] is 0 or 1

Solutions:

class Solution {
public:
    vector<bool> prefixesDivBy5(vector<int>& A) {
        vector<bool> res(A.size(), false);
        int index = 0;
        int num = 0; 
        for(const auto i:A)
        {
            num = (num << 1) + i;
            if(num % 5 == 0)
                res[index] = true;
            index++;
            num = num % 5;   //to avoid integer overflow
        }
        return res;
    }
};

Performance:

Mind-stretching solution:

class Solution {
public:
    vector<bool> prefixesDivBy5(vector<int>& A) {
        vector <vector <int> > v;         //five different reminder condition 
        v.push_back(vector<int>{0,1});
        v.push_back(vector<int>{2,3});
        v.push_back(vector<int>{4,0});
        v.push_back(vector<int>{1,2});
        v.push_back(vector<int>{3,4});
        
        int act_mod = 0;
        vector <bool> res;
        
        for(auto w : A)
        {
            act_mod = v[act_mod][w];
            res.push_back(not act_mod);
        }
        return res;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值