Leetcode周赛180

 

1380. Lucky Numbers in a Matrix

Given a m * n matrix of distinct numbers, return all lucky numbers in the matrix in any order.

A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.

 

Example 1:

Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column

Example 2:

Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: [12]
Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.

Example 3:

Input: matrix = [[7,8],[1,2]]
Output: [7]

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= n, m <= 50
  • 1 <= matrix[i][j] <= 10^5.
  • All elements in the matrix are distinct.
class Solution {
public:
    vector<int> luckyNumbers (vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
        vector<int> res;
        for (int i = 0; i < m; i++) {
            int rowMin = 100000;
            int col = -1;
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] < rowMin) {
                    rowMin = matrix[i][j];
                    col = j;
                }
            }
            int colMax = rowMin;
            bool flag = true;
            for (int k = 0; k < m; k++) {
                if (matrix[k][col] > colMax) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                res.push_back(colMax);
            }
        }
        return res;
    }
};

1381. Design a Stack With Increment Operation

Design a stack which supports the following operations.

Implement the CustomStack class:

  • CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number of elements in the stack or do nothing if the stack reached the maxSize.
  • void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize.
  • int pop() Pops and returns the top of stack or -1 if the stack is empty.
  • void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.

 

Example 1:

Input
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
Output
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
Explanation
CustomStack customStack = new CustomStack(3); // Stack is Empty []
customStack.push(1);                          // stack becomes [1]
customStack.push(2);                          // stack becomes [1, 2]
customStack.pop();                            // return 2 --> Return top of the stack 2, stack becomes [1]
customStack.push(2);                          // stack becomes [1, 2]
customStack.push(3);                          // stack becomes [1, 2, 3]
customStack.push(4);                          // stack still [1, 2, 3], Don't add another elements as size is 4
customStack.increment(5, 100);                // stack becomes [101, 102, 103]
customStack.increment(2, 100);                // stack becomes [201, 202, 103]
customStack.pop();                            // return 103 --> Return top of the stack 103, stack becomes [201, 202]
customStack.pop();                            // return 202 --> Return top of the stack 102, stack becomes [201]
customStack.pop();                            // return 201 --> Return top of the stack 101, stack becomes []
customStack.pop();                            // return -1 --> Stack is empty return -1.

 

Constraints:

  • 1 <= maxSize <= 1000
  • 1 <= x <= 1000
  • 1 <= k <= 1000
  • 0 <= val <= 100
  • At most 1000 calls will be made to each method of incrementpush and pop each separately.
    // class CustomStack {
    // public:
    //     CustomStack(int maxSize) {
    //         maxlen = maxSize;
    //     }
        
    //     void push(int x) {
    //         if (top < maxlen -1) {
    //             v[++top] = x;
    //         }
    //     }
        
    //     int pop() {
    //         if (top == -1) {
    //             return -1;
    //         } else {
    //             int res = v[top];
    //             top--;
    //             return res;
    //         }
    //     }
        
    //     void increment(int k, int val) {
    //         for (int i = 0; i <= min(k-1, top); i++) {
    //             v[i] += val;
    //         }
    //     }
    // private:
    //     int v[1001];
    //     int top = -1;
    //     int maxlen = 0;
    // };
    class CustomStack {
    public:
        CustomStack(int maxSize) {
            maxlen = maxSize;
        }
        
        void push(int x) {
            if (v.size() < maxlen) {
                v.push_back(x);
            }
        }
        
        int pop() {
            if (v.size() == 0) {
                return -1;
            } else {
                int res = v[v.size()-1];
                v.pop_back();
                return res;
            }
        }
        
        void increment(int k, int val) {
            int size = v.size();
            for (int i = 0; i < min(k, size); i++) {
                v[i] += val;
            }
        }
    private:
        vector<int> v;
        int maxlen = 0;
    };
    
    /**
     * Your CustomStack object will be instantiated and called as such:
     * CustomStack* obj = new CustomStack(maxSize);
     * obj->push(x);
     * int param_2 = obj->pop();
     * obj->increment(k,val);
     */

    1382. Balance a Binary Search Tree

    Given a binary search tree, return a balanced binary search tree with the same node values.

    A binary search tree is balanced if and only if the depth of the two subtrees of every node never differ by more than 1.

    If there is more than one answer, return any of them.

     

    Example 1:

  •  

    Input: root = [1,null,2,null,3,null,4,null,null]
    Output: [2,1,3,null,null,null,4]
    Explanation: This is not the only correct answer, [3,1,4,null,2,null,null] is also correct.
    

     

    Constraints:

  • The number of nodes in the tree is between 1 and 10^4.
  • The tree nodes will have distinct values between 1 and 10^5.

思路:先获取有序序列 再建树。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* balanceBST(TreeNode* root) {
        if (root == NULL) {
            return NULL;
        } else {
            inorder(root);
            return build(v, 0 ,v.size()-1);
        }
    }
private:
   vector<int> v;
    void inorder(TreeNode* root) {
        if (root) {
            inorder(root->left);
            v.push_back(root->val);
            inorder(root->right);
        }
    }
    TreeNode* build(vector<int>& v, int l, int r) {
        if (l > r) return NULL;
        int mid = (l + r) / 2;
        TreeNode* root = new TreeNode(v[mid]);
        root->left = build(v, l, mid - 1);
        root->right = build(v, mid + 1, r);
        return root;
    }
};

 

 

1383. Maximum Performance of a Team

There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7.

The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers. 

 

Example 1:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2
Output: 60
Explanation: 
We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60.

Example 2:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3
Output: 68
Explanation:
This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68.

Example 3:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4
Output: 72

 

Constraints:

  • 1 <= n <= 10^5
  • speed.length == n
  • efficiency.length == n
  • 1 <= speed[i] <= 10^5
  • 1 <= efficiency[i] <= 10^8
  • 1 <= k <= n

思路:贪心,按效率排序,限定了k个人,所以借助最小堆,超过k个人时,换出速度最小的,进行比较。

class Solution {
public:
    int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
        vector<pair<int,int>> se;
        long long int res = 0, sum = 0;
        for (int i = 0; i < n; i++) {
            se.push_back(make_pair(efficiency[i], speed[i]));
        }
        sort(se.begin(), se.end());
        priority_queue<int,vector<int>, greater<int>> pq;
        for (int i = n - 1; i >= 0; i--) {
            if (pq.size() >= k) {
                sum -= pq.top();
                pq.pop();
            }
            sum += se[i].second;
            pq.push(se[i].second);
            res = max(res, sum * se[i].first);
        }
        return res % 1000000007;
    }
};

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值