566. Reshape the Matrix/624. Maximum Distance in Arrays/594. Longest Harmonious Subsequence

566. Reshape the Matrix

description

In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.

You’re given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.

The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the ‘reshape’ operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

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

Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
Example 2:

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

Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.

Note:
The height and width of the given matrix is in range [1, 100].
The given r and c are all positive.

implementation

class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int row = nums.size();

        if(!row) 
            return nums;

        int col = nums[0].size();

        if(row*col != r*c) 
            return nums;

        vector<vector<int>> res(r, vector<int>(c, 0));

        for(int idx_r = 0; idx_r < r; idx_r++)  {
            int num_row = idx_r*c;
            for(int idx_c = 0; idx_c < c; idx_c++) {
                int num_ttl = num_row+idx_c;
                res[idx_r][idx_c] = nums[num_ttl/col][num_ttl%col];
            }
        }

        return res;
    }
};

624. Maximum Distance in Arrays

description

Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a-b|. Your task is to find the maximum distance.

Example 1:
Input: 
[[1,2,3],
 [4,5],
 [1,2,3]]
Output: 4

Explanation:
One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
Note:

Each given array will have at least 1 number. There will be at least two non-empty arrays.
The total number of the integers in all the m arrays will be in the range of [2, 10000].
The integers in the m arrays will be in the range of [-10000, 10000].

implementation

This solution is simple: compare the abs(tail1 - head2) and abs(tail2 - head1), as the biggest difference is in it. So go through the arrays with O(n^2) complexity.

class Solution {
public:
    int maxDistance(vector<vector<int>>& arrays) {
        int res = 0;
        int row = arrays.size();
        if(row <= 1) 
            return res;
        int col = arrays[0].size();

        for(int idx1 = 0; idx1 < row - 1; idx1++) {
            int head1 = arrays[idx1][0];
            int tail1 = arrays[idx1][arrays[idx1].size()-1];
            for(int idx2 = idx1 + 1; idx2 < row; idx2++) {
                int head2 = arrays[idx2][0];
                int tail2 = arrays[idx2][arrays[idx2].size()-1];
                if(abs(tail1 - head2) > res) 
                    res = abs(tail1 - head2);
                if(abs(tail2 - head1) > res) 
                    res = abs(tail2 - head1);                
            }
        }

        return res;
    }
};

594. Longest Harmonious Subsequence

description

We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.

Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.

Example 1:
Input: [1,3,2,2,5,2,3,7]
Output: 5
Explanation: The longest harmonious subsequence is [3,2,2,2,3].

Note: The length of the input array will not exceed 20,000.

implementation

Use the map to store the key-value pair, then find the difference between two keys, if diff is 1, then compare the sum of number with previous best result.

class Solution {
public:
    int findLHS(vector<int>& nums) {
        int nums_size = nums.size();
        int res = 0;
        if(nums_size <= 1)
            return res;
        map<int, int> value_mp;
        sort(nums.begin(), nums.end());
        for(int idx = 0; idx < nums_size; idx++) {
            if(value_mp[nums[idx]] > 0)
                value_mp[nums[idx]]++;
            else
                value_mp[nums[idx]] = 1;
        }
        int pre_k = 0;
        int pre_v = 0;
        for(map<int, int>::iterator it = value_mp.begin(); it != value_mp.end(); it++)  {
            if(!pre_v) {
                pre_k = it->first;
                pre_v = it->second;
            }    
            else {
                if(it->first - pre_k == 1) {
                    if(it->second + pre_v > res)
                        res = it->second + pre_v;
                }
                pre_k = it->first;
                pre_v = it->second;
            }    
        }
        return res;        
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值