LeetCode第192场周赛

重新排列数组

思路

代码实现

每次push进入答案i和i+n个然后直接返回

class Solution {
public:
    vector<int> shuffle(vector<int>& nums, int n) {
        vector<int>res;
        for(int i=0;i<n;i++){
            res.push_back(nums[i]);
            res.push_back(nums[i+n]);
        }
        return res;
    }
};

数组中的 k 个最强值

思路

这题我写的比较复杂,我直接定义了结构体,然后重载了小于号,剩下的交给sort快排去做了,当然快排上界是n^2的,很幸运STL很给力

代码实现

class Solution {
public:
    struct node{
        int a,m;
        node(){}
        node(int a,int m):a(a),m(m){}
        bool operator <(const node &b)const{
            if(abs(a-m)==abs(b.a-b.m))return a>b.a;
            return abs(a-m)>abs(b.a-b.m);
        }
    };
    vector<int> getStrongest(vector<int>& arr, int k) {
        sort(arr.begin(),arr.end());
        vector<node>ans;
        int m=arr[(arr.size()-1)/2];
        for(int i=0;i<arr.size();i++)ans.push_back(node(arr[i],m));
        sort(ans.begin(),ans.end());
        vector<int>res;
        for(int i=0;i<k;i++)res.push_back(ans[i].a);
        return res;
    }
};

设计浏览器记录

思路

这个题意外的简单,用个vector来维护一下就好了

代码实现

class BrowserHistory {
public:
    vector<string>str;
    int Maxnow = 0;
    int current = 0;
    BrowserHistory(string homepage) {
        str.push_back(homepage);
        Maxnow=0;
        current=0;
    }
    void visit(string url) {
        Maxnow = current+1;
        current = current+1;
        while(str.size()!=Maxnow)str.pop_back();
        str.push_back(url);
    }
    
    string back(int steps) {
        current = max(current-steps,0);
        return str[current];
    }
    
    string forward(int steps) {
        current = min(current+steps,Maxnow);
        return str[current];
    }
};

/**
 * Your BrowserHistory object will be instantiated and called as such:
 * BrowserHistory* obj = new BrowserHistory(homepage);
 * obj->visit(url);
 * string param_2 = obj->back(steps);
 * string param_3 = obj->forward(steps);
 */

给房子涂色 III

思路

dp[i][j][k]表示从1~i分成j组,最后一个颜色为k的最少话费
然后得到转移方程
h [ i ] = = 0 时 d p [ i ] [ j ] [ k ] = m i n ( d p [ i ] [ j ] [ k ] , d p [ i − 1 ] [ k − 1 ] [ o ] + g r p [ i ] [ k ] ) o ! = k h [ i ] = = 0 时 d p [ i ] [ j ] [ k ] = m i n ( d p [ i ] [ j ] [ k ] , d p [ i − 1 ] [ k ] [ o ] + g r p [ i ] [ k ] ) o = = k h[i]==0 时 \quad dp[i][j][k]=min(dp[i][j][k],dp[i-1][k-1][o]+grp[i][k])\quad o!=k\\ h[i]==0 时 \quad dp[i][j][k]=min(dp[i][j][k],dp[i-1][k][o]+grp[i][k])\quad o ==k h[i]==0dp[i][j][k]=min(dp[i][j][k],dp[i1][k1][o]+grp[i][k])o!=kh[i]==0dp[i][j][k]=min(dp[i][j][k],dp[i1][k][o]+grp[i][k])o==k
h [ i ] ! = 0 时 d p [ i ] [ j ] [ h [ i ] ] = m i n ( d p [ i ] [ j ] [ h [ i ] ] , d p [ i − 1 ] [ j − 1 ] [ k ] k ! = h [ i ] h [ i ] ! = 0 时 d p [ i ] [ j ] [ h [ i ] ] = m i n ( d p [ i ] [ j ] [ h [ i ] ] , d p [ i − 1 ] [ 1 ] [ k ] k = = h [ i ] h[i]!=0时 \quad dp[i][j][h[i]]=min(dp[i][j][h[i]],dp[i-1][j-1][k] \quad k!=h[i] \\ h[i]!=0时 \quad dp[i][j][h[i]]=min(dp[i][j][h[i]],dp[i-1][1][k] \quad k==h[i] h[i]!=0dp[i][j][h[i]]=min(dp[i][j][h[i]],dp[i1][j1][k]k!=h[i]h[i]!=0dp[i][j][h[i]]=min(dp[i][j][h[i]],dp[i1][1][k]k==h[i]
然后注意一下初始化dp[1][1][k]的情况即可
#### 代码实现

class Solution {
public:
   int dp[105][105][30];
   int h[105];
   int grp[105][105];
   int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {
       for(int i=1;i<=m;i++)h[i]=houses[i-1];
       for(int i=1;i<=m;i++){
           for(int j=1;j<=n;j++){
               grp[i][j]=cost[i-1][j-1];
           }
       }
       for(int i=0;i<=m;i++){
           for(int j=0;j<=m;j++){
               for(int k=0;k<=n;k++){
                   dp[i][j][k]=1e9;
               }
           }
       }
       //初始化
       if(h[1]==0){
           for(int i=1;i<=n;i++){
               dp[1][1][i]=min(dp[1][1][i],grp[1][i]);
           }
       }else dp[1][1][h[1]]=0;
       for(int i=2;i<=m;i++){
           if(h[i]==0){
               for(int j=1;j<=target;j++){
                   for(int k=1;k<=n;k++){
                       for(int o=1;o<=n;o++){
                           if(o==k){
                               dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k]+grp[i][k]);
                           }else{
                               dp[i][j][k]=min(dp[i][j][k],dp[i-1][j-1][o]+grp[i][k]);
                           }
                       }
                   }
               }
           }else{
               for(int j=1;j<=target;j++){
                   for(int k=1;k<=n;k++){
                       if(h[i]==k){
                           dp[i][j][h[i]]=min(dp[i][j][h[i]],dp[i-1][j][k]);
                       }else{
                           dp[i][j][h[i]]=min(dp[i][j][h[i]],dp[i-1][j-1][k]);
                       }
                   }
               }
           }
       }
       int res = 1e9;
       if(h[m]==0){
           for(int i=1;i<=n;i++)res=min(res,dp[m][target][i]);
       }else res = dp[m][target][h[m]];
       if(res==1e9)res=-1;
       return res;
   }
};

你可能感兴趣的:(Leetcode)