Problem solving report of the 180th week of LeetCode

5356. Lucky numbers in matrices

Topic type: violence enumeration
Enumerate each element, and then check for luck numbers.

class Solution { 
        vector<int> luckyNumbers (vector<vector<int>>& matrix) {
            vector<int> res; 
            for(int i = 0, n = matrix.size(); i < n; i++) { 
                for(int j = 0, m = matrix[i].size(); j < m; j++) {
                    bool flag = true;
                    for(int k = 0; flag && k < m; k++) {
                        if(k == j) { continue; }
                        if(matrix[i][j] > matrix[i][k]) { flag = false;}
                    for(int k = 0; flag && k < n; k++) {
                        if(k == i) { continue; }
                        if(matrix[i][j] < matrix[k][j]) { flag = false;}
                    if (flag) {
            return res;

5357. Design a stack to support incremental operations

Topic type: simulation, stack
Use the size variable to record the capacity of the stack, and use the top variable to record the top position of the stack.
Pit: when the increment operation is performed, there may be less than k elements in the stack.

const int MAXN = 1000;

class CustomStack {
	int data[MAXN];
	int size;
	int top;
		CustomStack(int maxSize): size(maxSize), top(0) {}

		void push(int x) {
			if (top < size) {
				data[top++] = x;

		int pop() {
			if(top > 0) {
				return data[--top];
			return -1;

		void increment(int k, int val) {
			for(int i = 0, n = min(k, top); i < n; i++) {
				data[i] += val;

5179. Balancing binary search trees

Topic type: tree traversal, recursion, construction
First, the binary search tree is traversed, and the elements in the tree are saved according to the order of traversal.

Construct balanced binary search tree according to ascending array:

  1. If the array is empty, the corresponding tree is also empty.
  2. If the array is not empty and the length is set to N, then the element at position n2\frac{n}{2}2n should be the root node of the tree. Subarray [1,n2 − 1] [1, frac {n} {2} - 1] [1, 2n − 1] and [N2 + 1,n] [frac {n} {2} + 1,n] [2n + 1,n] respectively correspond to left and right subtrees. Because the length difference between the two subarrays is not more than 1, the height difference between the left and right subtrees is not more than 1.
 * 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 {
    void dfs(TreeNode *root, vector<int> &vec) {
        if(root == nullptr) { return; }
        dfs(root->left, vec);
        dfs(root->right, vec);
    TreeNode* construct(const vector<int> &vec, int L, int R) {
        if(L > R) {
            return nullptr;
        int mid = (L+R)>>1;
        auto ptr = new TreeNode(vec[mid]);
        ptr->right = construct(vec, mid+1, R);
        ptr->left = construct(vec, L, mid-1);
        return ptr;
    TreeNode* balanceBST(TreeNode* root) {
        if(root == nullptr) {
            return nullptr;
        vector<int> data;
        dfs(root, data);
        return construct(data, 0, data.size()-1);

5359. Maximum team performance

Topic type: quick sort, heap sort, enumeration
C + + knowledge points: std::priority_queue, std::greater, operator overload
Pit: note that the data range may exceed int32 during operation.

std::priority_queue You can see
std::greater You can see

Thinking roadmap:
When the efficiency of the two schemes is equal, the scheme with larger speed sum is obviously better.
The input of questions determines that there are at most n kinds of efficiency.
For every efficiency, there must be an optimal solution.
The final answer must be the best of the n schemes.
The problem is transformed into how to find out the optimal scheme of each efficiency quickly:

  • Enumerate efficiency from large to small, and use an array to maintain optional employees. Because the efficiency keeps decreasing, the employee will only be added to the array and will never be deleted.
  • In the array type, select k maximum speeds (which can be maintained by heap sorting), and use the current enumeration to multiply the sum of efficiency and K maximum speeds as the optimal solution of the current efficiency.

Note: the efficiency of the selected k maximum speed s may be greater than that of the current enumeration, but this does not affect the correctness of the final answer. Because if this option is indeed the final answer, its value must be recorded in the optimal solution of other efficiency.
Programming skills: use STD:: priority queue instead of heap sorting code to improve coding speed.

class Solution {
	struct Engineer {
		int64_t s;
		int64_t e;
		Engineer(int64_t _s, int64_t _e) : s(_s), e(_e) {}
		bool operator < (const Engineer &r) const {
			return this->e > r.e;
	vector<Engineer> data;
	priority_queue<int, vector<int>, greater<int>> pq;
	int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
		for(int i = 0; i < n; i++) {
			data.push_back(Engineer(speed[i], efficiency[i]));
		sort(data.begin(), data.end());
		int64_t sum = 0, anw = 0;
		for(int i = 0; i < n; i++) {
            sum += data[i].s;
			if(pq.size() > size_t(k)) {
				int64_t tmp =;
				sum -= tmp;
			anw = max(anw, sum * data[i].e);
		return anw%(1000000007);

Scan and follow HelloNebu for more computer related articles~

Published 5 original articles, praised 0, visited 63
Private letter follow

Tags: less Programming

Posted on Sun, 15 Mar 2020 00:45:40 -0400 by icm