LeetCode 178周赛题解

1365. 有多少小于当前数字的数字

const int maxn=500+10;
//int hash[maxn]={0};
vector<int>ans;
class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
        int n=nums.size();
        ans.clear();
        for(int i=0;i<n;i++){
            int cnt=0;
            for(int j=0;j<n;j++){
                if(nums[i]>nums[j])cnt++;
            }
            ans.push_back(cnt);
        }
        //ans.resize(n);
        return ans;
    }
};

  

1366. 通过投票对团队排名

const int maxn=1e4+10;
map<char,vector<int>>mp;
int m;
inline bool cmp(char &a,char &b){
    for(int i=0;i<min(m,26);i++){
        if(mp[a][i]>mp[b][i])return true;
        if(mp[a][i]<mp[b][i])return false;//这一步不能少!!!
    }
    return a<b;
    //下面这种比较的方法也是可以的,这是map的一种特性,数组vector内部从第一个元素开始往后自动一个一个比较。。。
    //return mp[a]==mp[b]?a<b:mp[a]>mp[b];
}
class Solution {
public:
    string rankTeams(vector<string>& votes) {
        int n=votes.size();
        m=votes[0].size();
        for(auto v:votes[0]){
            mp[v]=vector<int>(m,0);//这一步是进行初始化的步骤。。。
        }
        for(auto v:votes){
            for(int i=0;i<m;i++){
                mp[v[i]][i]++;//这个表示的是,mp的二维map表示的是v[i]的字符排名在第i位有多少个。。。
                //cout<<mp[v[i]][i]<<endl;
            }
        }
        string res=votes[0];
        sort(res.begin(),res.end(),cmp);
        return res;
    }
};

  

1367. 二叉树中的列表

class Solution {
public:
    bool check(ListNode* head,TreeNode* root){
        if(head==nullptr)return true;
        if(root==nullptr)return false;
        if(root->val!=head->val)return false;
        bool ret=false;
        ret=ret||check(head->next,root->left);
        ret=ret||check(head->next,root->right);
        return ret;
    }
    bool dfs(ListNode* head,TreeNode* root){
        bool ret=false;//初始设置成false,这是因为之后只要有true就可以啦,是或条件
        if(root==nullptr)return false;
        
        ret=ret||check(head,root);//只要有一条路径符合条件就可以,所以为或条件。。
        ret=ret||dfs(head,root->left);
        ret=ret||dfs(head,root->right);
        return ret;
    }
    bool isSubPath(ListNode* head, TreeNode* root) {
        return dfs(head,root);
    }
};

  

1368. 使网格图至少有一条有效路径的最小代价

class Solution {
public:
    //0-1BFS,需要使用deque:双端队列
    //可以将箭头方向的cost看成0,其他方向的cost看成1,一直计算下去。。。
    
    int minCost(vector<vector<int>>& grid) {
        int m=grid.size();
        int n=grid[0].size();
        deque<pair<int,int>>dq;//算是初始化,一开始点所在的位置。。。这是一个双端队列,第一个数据是一维的位置,第二个数据是cost
        dq.push_front(make_pair(0,0));
        //代价小的cost放在双端队列前面优先出队,代价大的放在优先队列后面,因为我们需要尽量选择代价小的路径
        vector<vector<int>>dir{{0,1},{0,-1},{1,0},{-1,0}};//依次表示的是从0:{0,1}右,1:{0,-1}左,2:{1,0}下,3:{-1,0}上
        vector<int>vis(m*n,0);//表示已经扩展过了。。。
        while(!dq.empty()){
            //auto [p,cost]=dq.front();
            pair<int,int>q=dq.front();
            int p=q.first,cost=q.second;
            dq.pop_front();
            int px=p/n,py=p%n;
            if(px==m-1&&py==n-1)return cost;
            if(vis[p]++)continue;//如果已经扩展过了,那么就不用再扩展了。。。
            for(int i=0;i<4;i++){
                int tx=px+dir[i][0],ty=py+dir[i][1];
                int tp=tx*n+ty;
                if(tx<0||tx>=m||ty<0||ty>=n||vis[tp])continue;
                if(grid[px][py]==i+1){//表示的是不需要更改方向
                    dq.push_front(make_pair(tp,cost));
                }else{
                    dq.push_back({tp,cost+1});//表示的是需要更改方向这时加入队尾,因为有可能有比这个更好的选择。。。
                }
                
            }
        }
        return -1;
    }
};

  

猜你喜欢

转载自www.cnblogs.com/zb121/p/12399428.html
今日推荐