第一题
代码语言:javascript复制class Solution {
public:
vector<int> luckyNumbers (vector<vector<int>>& matrix) {
vector<int> ans;
vector<int> n;
for(int i=0;i<matrix.size();i )
{
int x=INT_MAX;
for(int j=0;j<matrix[i].size();j )
{
x=min(x,matrix[i][j]);
}
n.push_back(x);
}
vector<int> m;
for(int i=0;i<matrix[0].size();i )
{
int x =-1;
for(int j=0;j<matrix.size();j )
{
x=max(x,matrix[j][i]);
}
m.push_back(x);
}
for(int i=0;i<n.size();i )
{
for(int j=0;j<m.size();j )
{
if(n[i]==m[j])
ans.push_back(n[i]);
}
}
return ans;
}
};
第二题
代码语言:javascript复制class CustomStack {
public:
int s[1005];
int m;
int top;
CustomStack(int maxSize) {
m=maxSize;
top=0;
}
void push(int x) {
if(top<m)
s[top ]=x;
}
int pop() {
if(top==0)
return -1;
int x=s[top-1];
top--;
return x;
}
void increment(int k, int val) {
for(int i=0;i<k&&i<top;i )
{
s[i] =val;
}
}
};
/**
* 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);
*/
第三题
代码语言:javascript复制/**
* 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:
vector<int> nums;
TreeNode* balanceBST(TreeNode* root) {
fun(root);
TreeNode* node;
build(node,0,nums.size()-1);
return node;
}
void build(TreeNode* &root,int l,int r)
{
int mid = (l r)/2;
int x = nums[mid];
root = new TreeNode(x);
if(l<=mid-1)
build(root->left,l,mid-1);
if(mid 1<=r)
build(root->right,mid 1,r);
}
void fun(TreeNode* root)
{
if(root!=NULL)
{
fun(root->left);
nums.push_back(root->val);
fun(root->right);
}
}
};
第四题
代码语言:javascript复制typedef long long int _int;
struct Node
{
int effic;
int speed;
}a[100005];
int cmp(Node a,Node b)
{
if(a.effic==b.effic)
return a.speed>b.speed;
return a.effic<b.effic;
}
class Solution {
public:
priority_queue<int, vector<int>, greater<int>> q;
_int value = 1000000000 7;
int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
for(int i=0;i<n;i )
{
a[i].speed = speed[i];
a[i].effic = efficiency[i];
}
sort(a,a n,cmp);
_int ans=0;
_int x;
_int y=0;
for(int i=n-1;i>=0;i--)
{
ans=max(ans,a[i].effic*(y a[i].speed));
q.push(a[i].speed);
y =a[i].speed;
if(q.size()>k-1)
{
y-=q.top();
q.pop();
}
}
return ans%value;
}
};