Leetcode 164 Race Week

Access to all points of the minimum time

Thinking

Because each mobile grid has a horizontal direction, a vertical direction moving grid and a lattice diagonal direction, and a horizontal grid \ (+ \) in the vertical direction a grid \ (= \) diagonal direction a grid , so the final answer is cut adjacent points of Schiff distance and ratio.

Code

class Solution {
public:
    int minTimeToVisitAllPoints(vector<vector<int>>& points) {
        int n = points.size(), ans = 0;
        for(int i = 1; i < n; ++i) {
            int disx = abs(points[i][0]-points[i-1][0]), disy = abs(points[i][1]-points[i-1][1]);
            ans += max(disx, disy);
        }
        return ans;
    }
};

Statistical participate server communication

Thinking

Statistics for each row of each column a few servers, and then look at his peers or the same number of servers for each column is greater than a server \ (1 \) .

Code

class Solution {
public:
    int countServers(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        int row[255], col[255];
        for(int i = 0; i < n; ++i) row[i] = 0;
        for(int i = 0; i < m; ++i) col[i] = 0;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < m; ++j) {
                if(grid[i][j]) ++row[i], ++col[j];
            }
        }
        int ans = 0;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < m; ++j) {
                if((row[i] > 1 || col[j] > 1) && grid[i][j]) ++ans;
            }
        }
        return ans;
    }
};

Search recommendation system

Thinking

We first sort strings in lexicographic order, then move with two pairs of pointers, when the length of the string is less than one endpoint has traversed character on the current position and the length or \ (SearchWord \) in this position in the character does not move the pointer while.

Code

class Solution {
public:
    vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
        int n = products.size(), l = 0, r = n - 1;
        sort(products.begin(), products.end());
        int len = searchWord.length();
        vector<vector<string>> ans(len);
        for(int i = 0; i < len; ++i) {
            while(l <= r && (products[l].length() <= i || products[l][i] != searchWord[i])) ++l;
            while(r >= l && (products[r].length() <= i || products[r][i] != searchWord[i])) --r;
            if(l > r) continue;
            for(int j = l; j <= min(l + 2, r); ++j) {
                ans[i].push_back(products[j]);
            }
        }
        return ans;
    }
};

Stop in place a number of programs

Thinking

We define \ (dp [i] [j] \) represents the first \ (i \) step in \ (j \) the number of programs at this location, it is easy to find transfer equation \ (dp [i] [j ] DP = [. 1-I] [J-. 1] + DP [. 1-I] [J] + DP [. 1-I] [J +. 1] \) , we note that each state and only one state related and therefore can be optimized space with rolling array.

But this complexity is \ (O (Steps * arrLen) \) , we can see when we can move up to the \ (min (arrLen, steps) \) in this position, and therefore places greater than this value would not have considered, therefore complexity becomes \ (O (min * Steps (Steps, arrLen)) \) .

 Code

class Solution {
public:
    const int mod = 1e9 + 7;
    int dp[2][1000005];

    int add(int x, int y) {
        x += y;
        if(x >= mod) x -= mod;
        return x;
    }

    int numWays(int steps, int arrLen) {
        arrLen = min(arrLen, steps);
        for(int i = 0; i < arrLen; ++i) dp[0][i] = dp[1][i] = 0;
        dp[0][0] = 1;
        for(int i = 1; i <= steps; ++i) {
            int nw = i % 2, pre = (i - 1) % 2;
            for(int j = 0; j < arrLen; ++j) {
                dp[nw][j] = dp[pre][j];
                if(j > 0) dp[nw][j] = add(dp[nw][j], dp[pre][j-1]);
                if(j + 1 < arrLen) dp[nw][j] = add(dp[nw][j], dp[pre][j+1]);
            }
        }
        return dp[steps%2][0];
    }
};

Guess you like

Origin www.cnblogs.com/Dillonh/p/11950507.html