Weekly Contest 63

版权声明:转载著名出处 https://blog.csdn.net/gcola007/article/details/78824545

746. Min Cost Climbing Stairs

难度:esay

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).

Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

Example 1:

Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].

Note:
cost will have a length in the range [2, 1000].
Every cost[i] will be an integer in the range [0, 999].

本题是基础的动态规划题,一遍AC

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        if(cost.size()<3) return 0;
        vector<int> dp(cost.size()+1,0);
        for(int i=2;i<=cost.size();i++){
            dp[i]=min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
        }
        return dp[cost.size()];
    }
};

748. Shortest Completing Word

难度:easy

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate

Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word.

It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.

The license plate might have the same letter occurring multiple times. For example, given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does.

Example 1:

Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
Output: "steps"
Explanation: The smallest length word that contains the letters "S", "P", "S", and "T".

Note that the answer is not “step”, because the letter “s” must occur in the word twice.
Also note that we ignored case for the purposes of comparing whether a letter exists in the word.

Example 2:
Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
Output: "pest"
Explanation: There are 3 smallest length words that contains the letters "s".

We return the one that occurred first.
Note:
licensePlate will be a string with length in range [1, 7].
licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
words will have a length in the range [10, 1000].
Every words[i] will consist of lowercase letters, and have length in range [1, 15].

这个题目难就难在题目太长了,以我的英文水平看了半天才看懂.

思路是:用一个数组记录licensePlate中出现过的字母,对于输入的words字符串数组通过逆序遍历保证当最小长度相等时输出的是最先出现的字符串,通过判断word中是否包含数组中记录的字母,求最小长度

class Solution {
public:
    string shortestCompletingWord(string licensePlate, vector<string>& words) {
        vector<int> letter(26,0);
        for(int i=0;i<licensePlate.size();i++){
            if(licensePlate[i]>='A'&&licensePlate[i]<='Z')
                letter[licensePlate[i]-'A']++;
            else if(licensePlate[i]>='a'&&licensePlate[i]<='z')
                letter[licensePlate[i]-'a']++;
            else continue;
        }
        int minlen=INT_MAX;
        unordered_map<int,string> m;
        for(int i=words.size()-1;i>=0;i--){
            if(valid(letter,words[i])){
                m[words[i].size()]=words[i];
                int size=words[i].size();
                minlen=min(minlen,size);
            }
        }
        return m[minlen];
    }
    bool valid(vector<int> letter,string s){
        for(int i=0;i<s.size();i++){
            if(letter[s[i]-'a'])
                letter[s[i]-'a']--;
        }
        for(int i=0;i<letter.size();i++)
            if(letter[i])
                return false;
        return true;
    }
};

750. Number Of Corner Rectangles

难度:medium

Given a grid where each entry is only 0 or 1, find the number of corner rectangles.

A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle. Note that only the corners need to have the value 1. Also, all four 1s used must be distinct.

Example 1:

Input: grid = 
[[1, 0, 0, 1, 0],
 [0, 0, 1, 0, 1],
 [0, 0, 0, 1, 0],
 [1, 0, 1, 0, 1]]
Output: 1
Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4].

Example 2:

Input: grid = 
[[1, 1, 1],
 [1, 1, 1],
 [1, 1, 1]]
Output: 9
Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.

Example 3:

Input: grid = 
[[1, 1, 1, 1]]
Output: 0
Explanation: Rectangles must have four distinct corners.

Note:
The number of rows and columns of grid will each be in the range [1, 200].
Each grid[i][j] will be either 0 or 1.
The number of 1s in the grid will be at most 6000.

暴力遍历法理所应当的TLE了。那么如何减小时间复杂度呢

这是大神的代码,O(n²)遍历矩阵,得出所有的值为1的点的i,j保存在
x,y数组中,当x,y数组中的下一点在前一点的右下方时,矩形+1.

public:
    static int X[6005], Y[6005];
    static bool a[205][205];

    int countCornerRectangles(vector<vector<int>>& grid) {
        int n = (int)grid.size();
        int m = (int)grid[0].size();
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j]) {
                    X[cnt] = i, Y[cnt] = j;
                    ++cnt;
                    a[i][j] = 1;
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < cnt; i++) {
            for (int j = i + 1; j < cnt; j++) {
                if (X[j] > X[i] && Y[j] > Y[i]) {
                    ans += a[X[i]][Y[j]] & a[X[j]][Y[i]];
                }
            }
        }
        for (int i = 0; i < cnt; i++) {
            a[X[i]][Y[i]] = 0;
        }
        return ans;
    }
};

int Solution::X[6005], Solution::Y[6005];
bool Solution::a[205][205];

749. Contain Virus

难度:hard

A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls.

The world is modeled as a 2-D array of cells, where 0 represents uninfected cells, and 1 represents cells contaminated with the virus. A wall (and only one wall) can be installed between any two 4-directionally adjacent cells, on the shared boundary.

Every night, the virus spreads to all neighboring cells in all four directions unless blocked by a wall. Resources are limited. Each day, you can install walls around only one region – the affected area (continuous block of infected cells) that threatens the most uninfected cells the following night. There will never be a tie.

Can you save the day? If so, what is the number of walls required? If not, and the world becomes fully infected, return the number of walls used.

Example 1:

Input: grid = 
[[0,1,0,0,0,0,0,1],
 [0,1,0,0,0,0,0,1],
 [0,0,0,0,0,0,0,1],
 [0,0,0,0,0,0,0,0]]
Output: 10
Explanation:
There are 2 contaminated regions.
On the first day, add 5 walls to quarantine the viral region on the left. The board after the virus spreads is:
[[0,1,0,0,0,0,1,1],
 [0,1,0,0,0,0,1,1],
 [0,0,0,0,0,0,1,1],
 [0,0,0,0,0,0,0,1]]

On the second day, add 5 walls to quarantine the viral region on the right. The virus is fully contained.

Example 2:

Input: grid = 
[[1,1,1],
 [1,0,1],
 [1,1,1]]
Output: 4
Explanation: Even though there is only one cell saved, there are 4 walls built.
Notice that walls are only built on the shared boundary of two different cells.

Example 3:

Input: grid = 
[[1,1,1,0,0,0,0,0,0],
 [1,0,1,0,1,1,1,1,1],
 [1,1,1,0,0,0,0,0,0]]
Output: 13
Explanation: The region on the left only builds two new walls.

Note:
The number of rows and columns of grid will each be in the range [1, 50].
Each grid[i][j] will be either 0 or 1.
Throughout the described process, there is always a contiguous viral region that will infect strictly more uncontaminated squares in the next round.

这题看了半天才看懂,先放着…

猜你喜欢

转载自blog.csdn.net/gcola007/article/details/78824545
今日推荐