leetcode周赛192

leetcode周赛192_第1张图片


class Solution {
public:
    vector shuffle(vector& nums, int n) {
        vector ans(2*n);
        for(int i=0; i

leetcode周赛192_第2张图片

排序,貌似被我写复杂了


class Solution {
public:
    vector getStrongest(vector& arr, int k) {
        sort(arr.begin(), arr.end());
        int n = arr.size();
        vector ans(k);
        vector> temp;
        int mid = arr[(n-1)>>1];
        for(int i=0; i& a, pair& b){
            return a.first==b.first? a.second

leetcode周赛192_第3张图片

没啥好说的,数组加上两个记录长度和位置的值就行


class BrowserHistory {
public:
    int cur;
    int len;
    vector history;
    BrowserHistory(string homepage) {
        cur=0;
        len=1;
        history.resize(5005);
        history[0]=homepage;
    }
    
    void visit(string url) {
        cur+=1;
        history[cur]=url;
        len = cur+1; 
    }
    
    string back(int steps) {
        cur = max(0, cur-steps);
        return history[cur];
    }
    
    string forward(int steps) {
        cur = min(cur+steps, len-1);
        return history[cur];
    }
};

leetcode周赛192_第4张图片

dp, 设dp[i][j][k] 为第i间房子涂第j种颜色,且组成K块的最小代价
注意边界值就好,复杂度O(m^2 n^2). 代码写的很丑

#define MAX  0x3f3f3f3f
class Solution {
public:
    int minCost(vector& house, vector>& cost, int M, int n, int target) {
        // dp[i][j][k]   代表 i间房子刷成了k种连续的颜色,而且第i间房子是第j种颜色
        int dp[105][105][100];
        memset(dp, 0x3f, sizeof dp);
        for(int i=0; i

你可能感兴趣的:(leetcode周赛192)