LeetCode Weekly Contest 111 (C++)

Welcome to the 111th LeetCode Weekly Contest

941. Valid Mountain Array                                - Easy

944. Delete Columns to Make Sorted              - Medium

942. DI String Match                                        - Easy

943. Find the Shortest Superstring                  - Hard (没做出来,也没看懂目前= =)

941. Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array.

Recall that A is a mountain array if and only if:

  • A.length >= 3
  • There exists some i with 0 < i < A.length - 1 such that:
    • A[0] < A[1] < ... A[i-1] < A[i]
    • A[i] > A[i+1] > ... > A[B.length - 1]

解:

    就是说,判断一个数组是不是想“山峰”的形状,值得注意的是 [ 1, 2, 3, 4, 5 ] 这样右边没有低的部分的也不算是 valid mountain array我就是直接找到最大值,然后向左向右遍历,如果有一个数不符合就false。

bool validMountainArray(vector<int>& A) {
    if(A.size() < 3) return false;
    int maxIdx = max_element(A.begin(), A.end()) - A.begin();
    if(maxIdx == 0 || maxIdx == A.size() - 1) return false;
    for(int i = maxIdx - 1; i >= 0; i--)
        if(A[i] >= A[i + 1])
            return false;
    for(int i = maxIdx + 1; i < A.size(); i++)
        if(A[i] >= A[i - 1])
            return false;
    return true;
}

944. Delete Columns to Make Sorted

We are given an array A of N lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have a string "abcdef" and deletion indices {0, 2, 3}, then the final string after deletion is "bef".

Suppose we chose a set of deletion indices D such that after deletions, each remaining column in A is in non-decreasing sorted order.

Formally, the c-th column is [A[0][c], A[1][c], ..., A[A.length-1][c]]

Return the minimum possible value of D.length.

Input: ["cba","daf","ghi"]   Output: 1

Input: ["a","b"]                  Output: 0

Input: ["zyx","wvu","tsr"]   Output: 3

解:

    就是要删除一些位置,让删除后的所有string,对应位置是非降序的,那就对于每个位置,从第一个string往后遍历,只要后边比前边大,那这个位置就要删除,跳过,判断下一个位置即可。

int minDeletionSize(vector<string>& A) {
    int toDel = 0, len = A[0].length(), sizeA = A.size();
    for(int idx = 0; idx < len; idx++)
        for(int i = 0; i < sizeA - 1; i++)
            if(A[i][idx] > A[i + 1][idx])
            {
                ++toDel;
                break;
            }
    return toDel;
}

942. DI String Match

Given a string S that only contains "I" (increase) or "D" (decrease), let N = S.length.

Return any permutation A of [0, 1, ..., N] such that for all i = 0, ..., N-1:

  • If S[i] == "I", then A[i] < A[i+1]
  • If S[i] == "D", then A[i] > A[i+1]

Input: "IDID"         Output: [0,4,1,3,2]

Input: "III"             Output: [0,1,2,3]

Input: "DDI"          Output: [3,2,0,1]

解:

    这道题就是道简单数学题,显然,第一个D对应的就是最大的数,之后每个D递减,第一个I对应最小的数,之后每个I递减,最后数会比 DI String 多一位,是最后剩下的一位,也就是 max 和 min 相等的时候。

vector<int> diStringMatch(string S) {
    int N = S.length();
    vector<int> res;
    int max = N, min = 0;
    for(auto s : S)
    {
        if(s == 'I') res.push_back(min++);
        else         res.push_back(max--);
    }
    res.push_back(max); // min也可以因为 max 一定等于 min
    return res;
}

943. Find the Shortest Superstring

Given an array A of strings, find any smallest string that contains each string in A as a substring.

We may assume that no string in A is substring of another string in A.

Example 1:

Input: ["alex","loves","leetcode"]
Output: "alexlovesleetcode"
Explanation: All permutations of "alex","loves","leetcode" would also be accepted.

Example 2:

Input: ["catg","ctaagt","gcta","ttca","atgcatc"]
Output: "gctaagttcatgcatc"

解:

    这道题做的时候并没有什么头绪,感觉能想到的方法都一定会TLE,直接看的解答。

    目前还没看懂,有时间再看看。

猜你喜欢

转载自blog.csdn.net/Bob__yuan/article/details/84202455