1365.有多少小于当前数字的数字
题目链接
static const auto io_speed_up = [](){
ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}();
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
int cnt[102]={
0};
for(int p : nums)cnt[p+1]++;
for(int i = 1;i <= 100;i++)cnt[i]+=cnt[i-1];
for(auto &p : nums)p = cnt[p];
return nums;
}
};
1366.通过投票对团队排名
题目链接
static const auto io_speed_up = [](){
ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}();
class Solution {
public:
static bool cmp(pair<int, int> a, pair<int, int> b){
if(a.first&&b.first)return a.first > b.first;
return a.first;
}
string rankTeams(vector<string>& votes) {
vector<pair<int, int>>cnt(26);
int re[26];
int l = votes[0].size();
for(int i = 0;i<26;i++){
cnt[i].second = i;re[i]=i;}
for(int j = l-1; j >= 0 ;j--){
for(int i = 0;i < 26 ;i++){
cnt[i].first = 0;re[cnt[i].second]=i;}
for(int i = 0;i<votes.size();i++)cnt[re[votes[i][j]-'A']].first++;
stable_sort(cnt.begin(), cnt.end(), cmp);
}
string ans="";
for(int i = 0; i < l ;i++){
ans+= (cnt[i].second + 'A');
}
return ans;
}
};
1367.二叉树中的列表
题目链接
static const auto io_speed_up = [](){
ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}();
class Solution {
public:
bool count(ListNode*head, TreeNode* root){
int ans = 0;
if(root==NULL){
if(head==NULL)return 1;
else return 0;
}
if(head==NULL)return 1;
if(head->val == root->val){
ans|=count(head->next,root->left);
ans|=count(head->next,root->right);
}
else return 0;
return ans;
}
bool isSubPath(ListNode* head, TreeNode* root) {
if(root == NULL)return 0;
if(count(head,root))return 1;
else return isSubPath(head, root->left)||isSubPath(head, root->right);
}
};
1368.使网格图至少有一条有效路径的最小代价
题目链接
最短路,到原方向边权0,其他方向边权1,场外不连边。
static const auto io_speed_up =[](){
ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}();
class Solution {
public:
struct edge{
int v,cost;
edge(){
}
edge(int a,int b){
v = a;cost = b;}
};
int xx[5]={
0, 0, 0, 1, -1};
int yy[5]={
0, 1, -1, 0, -0};
const int INF = 0x3f3f3f3f;
int minCost(vector<vector<int>>& grid) {
int n = grid.size(),m = grid[0].size();
vector<vector<edge> >e(n*m);
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
for(int k = 1;k<=4;k++){
int x = i + xx[k];
int y = j + yy[k];
if(x<0||x>=n||y<0||y>=m)continue;
if(grid[i][j]==k)e[i*m+j].emplace_back(edge(x*m+y,0));
else e[i*m+j].emplace_back(edge(x*m+y,1));
}
}
}
vector<int>dist(n*m,INF);
vector<bool>vis(n*m);
dist[0]=0;
deque<int>mq;
mq.push_back(0);
while(!mq.empty()){
int u = mq.front();
mq.pop_front();vis[u]=0;
for(auto p : e[u]){
if(dist[u]+p.cost < dist[p.v]){
dist[p.v]=dist[u]+p.cost;
if(!vis[p.v]){
if((!mq.empty())&&dist[p.v]<dist[mq.front()])mq.push_front(p.v);
mq.push_back(p.v);
vis[p.v]=1;
}
}
}
}
return dist[m*n-1];
}
};