LeetCode Weekly Contest 111 (C++)
Welcome to the 111th LeetCode Weekly Contest
941. Valid Mountain Array - Easy
944. Delete Columns to Make Sorted - Medium
943. Find the Shortest Superstring - Hard (沒做出來,也沒看懂目前= =)
941. Valid Mountain Array
Given an array A
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
with0 < 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"
, thenA[i] < A[i+1]
- If
S[i] == "D"
, thenA[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,直接看的解答。
目前還沒看懂,有時間再看看。