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 withmaxSize
which is the maximum number of elements in the stack or do nothing if the stack reached themaxSize
.void push(int x)
Addsx
to the top of the stack if the stack hasn't reached themaxSize
.int pop()
Pops and returns the top of stack or -1 if the stack is empty.void inc(int k, int val)
Increments the bottomk
elements of the stack byval
. If there are less thank
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 ofincrement
,push
andpop
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
and10^4
. - The tree nodes will have distinct values between
1
and10^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;
}
};