【leetcode】第 180 场周赛

比赛链接:https://leetcode-cn.com/contest/weekly-contest-180/

赛后总结

这场状态还不错,可惜遇到了知识盲点,以及hard虽然有思路但没能快速实现。差一点就能完成flag了。

第1题:弄清思路后就很快,出了一个低级错误,耽误了很久。

第2题:程序设计类的题,没什么难度。

第3题:平衡树是弱项,需要补一下。关键是当时脑子没转过弯来。。

第4题:dp题,想出来了解法,但在实现遇到了困难,还不会在稍复杂的问题中熟练组合使用容器,编程速度太慢。

优点

1.状态稳定,该做的做完了。

缺点&改进

1.低级错误!

2.平衡树知识需补充。

3.dp多刷一些难题。

 

题目

1.【easy】5356. 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.

题目链接:https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/

思路

没什么技巧的正常解法。

在遍历时更新每行每列的最值记录,完成后再遍历这个记录,取最值出现一致的位置的值。

class Solution {
public:
    vector luckyNumbers (vector>& matrix) {
        vector res;
        int row = matrix.size();
        if(row==0) return res;
        int col = matrix[0].size();
        vector row_min(row, -1);
        vector col_max(col, -1);
        for(int i=0; imatrix[col_max[j]][j]){
                    col_max[j] = i;
                }
            }
        }
        for(int i=0; i

 

2.【medium】5357. 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.

题目链接:https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/

思路

程序设计题都不难,只需根据题意逐条实现即可。

class CustomStack {
public:
    int num = 0;
    stack rec;
    int max_num;
    CustomStack(int maxSize) {
        max_num = maxSize;
    }
    
    void push(int x) {
        if(num0){
            int res = rec.top();
            rec.pop();
            --num;
            return res;
        }
        return -1;
    }
    
    void increment(int k, int val) {
        if(k<=0) return;
        stack tmps;
        for(int i=num; i>0;--i){
            int tmp = rec.top();
            rec.pop();
            if(i<=k){
                tmp += val;
            }
            tmps.push(tmp);
        }
        while(!tmps.empty()){
            int tmp = tmps.top();
            tmps.pop();
            rec.push(tmp);
        }
    }
};
/**
 * 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);
 */

 

3.【medium】5179. 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:

【leetcode】第 180 场周赛_第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.

题目链接:https://leetcode-cn.com/problems/balance-a-binary-search-tree/

思路

题目说树已生成,需要对其进行修改,乍一看比无序数组生成平衡BST还难,但实际上有办法摆脱无序。

因为已经是BST了,进行一次中序遍历即可获得有序数组,再重新生成树时对数组进行二分法取值,生成的树就是平衡的了。

当时一直纠结于如何修改树结构。。万万没想到。。

/**
 * 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) return root;
        auto nums = trace(root);
        int len = nums.size();
        return build(nums, 0, len-1);
    }
    vector trace(TreeNode* root){
        vector res;
        if(!root) return res;
        auto l = trace(root->left);
        res.insert(res.end(),l.begin(),l.end());
        res.push_back(root->val);
        auto r = trace(root->right);
        res.insert(res.end(),r.begin(),r.end());
        return res;
    }
    TreeNode* build(vector& nums, int l, int r){
        if(l>r) return nullptr;
        int mid = l + (r-l)/2;
        TreeNode* root = new TreeNode(nums[mid]);
        root->left = build(nums, l, mid-1);
        root->right = build(nums, mid+1, r);
        return root;
    }
};

 

4.【hard】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

题目链接:https://leetcode-cn.com/problems/maximum-performance-of-a-team/

思路

看到这个数据大小,应该能想到暴力解法不行,需要用dp或贪心来对付。

于是可以想到,在取员工的时候,希望它们的组合能够 最低效率高+速度和大,但是二者并不能兼得,所以我们先看一个条件——效率,因为更容易获得。

于是按效率的大小对<效率,速度>数据对排序,从效率最大的员工算起:

1)若员工数不满k,则直接将速度加上,更新工作总量max(原工作总量,加和后总量);

2)若员工数量已满,那么看当前员工的速度是否比组合里的最小速度大,小则忽略当前员工,大则替换员工,并更新工作总量max(原工作总量,替换后总量)。(这里的考虑是:最低效率一直在下降,如果速度还不能有提升的话,工作总量肯定没法提升)

需要用一些辅助的容器:priority_queue用户存储数据对,multipleset用于存储k个最大速度。

其实思路和编程都不算太难,只不过在有限的时间里思路会变得紧张。

typedef unsigned long long ull;
class Solution {
    struct cmp{
        inline bool operator()(pair &a, pair &b){
            if(a.first != b.first) return a.first < b.first;
            else return a.second < b.second;
        }
    };
public:
    int maxPerformance(int n, vector& speed, vector& efficiency, int k) {
        if(k<=0 || n<=0 )return 0;
        priority_queue, vector>, cmp> emp;
        for(int i=0; i ss;
        for(int i=0; i*oldsp){
                    ssum = ssum - *oldsp + human.second;
                    ss.erase(oldsp);
                    ss.insert(human.second);
                    sum = max(sum, ssum * human.first);
                }    
            }
        }
        return sum % (1000000007);
    }
};

 

你可能感兴趣的:(code,c++,比赛)