More than 25% of the number of elements in the array 1287. orderly appearance
1286. letter combinations iterator
1289. Minimum descending path and II
Fall and can not keep only a minimum of two original array, hacked.
More than 25% of the number of elements in the array 1287. orderly appearance
More than 25% of the number of elements in the array 1287. orderly appearance
To give you a non-decreasing order integer array, this array is known exactly an integer number of times it occurs more than 25% of the total number of array elements.
Please find and return the integer
Example:
**输入:** arr = [1,2,2,6,6,6,6,7,10]
**输出:** 6
Tip Hint
prompt:
1 <= arr.length <= 10^4
0 <= arr[i] <= 10^5
Code
class Solution {
public:
int findSpecialInteger(vector<int>& arr) {
int n = arr.size(),val = arr[0];
pair<int,int>ans({0,0});
for(int i = 0,c = 0;i < n;++i){
if(arr[i] == val) c++,ans=max(ans,make_pair(c,val));
else c = 1,val = arr[i];
}
return ans.second;
}
};
1288. deleted covered range
Give you a range list, you delete the interval list are covered by other sections.
Only if c <= a
and b <= d
when we believe that the interval [a,b)
is the interval [c,d)
covered.
After completing all the delete operation, you return to the list of the number of remaining range.
Example:
**输入:** intervals = [[1,4],[3,6],[2,8]]
**输出:** 2
**解释:** 区间 [3,6] 被区间 [2,8] 覆盖,所以它被删除了。
Tip Hint
Tips:
1 <= intervals.length <= 1000
0 <= intervals[i][0] < intervals[i][1] <= 10^5
- For all
i != j
:intervals[i] != intervals[j]
Code
class Solution {
typedef pair<int, int>PII;
public:
static bool cmp(vector<int>& a, vector<int>& b) {
return (a[0] != b[0]) ? (a[0] < b[0]) : (a[1] > b[1]);
}
int removeCoveredIntervals(vector<vector<int>>& intervals) {
const int n = intervals.size();
sort(intervals.begin(), intervals.end(), cmp);
int l = intervals[0][1], ans = 1;
for(int i = 1; i < n; ++i) {
if(intervals[i][1] > l)
ans++, l = intervals[i][1];
}
return ans;
}
};
1286. letter combinations iterator
1286. letter combinations iterator
You design an iterator class, including the following:
- A constructor function, the input parameters comprising: a unique and ordered character string
characters
(the string contains only lowercase letters) and a numbercombinationLength
. - Function Next () , according to the dictionary order to return the length of
combinationLength
the next letter combination. - Function the hasNext () , there is only the length of
combinationLength
the next letter combination, before returningTrue
; otherwise, returnFalse
.
Example:
CombinationIterator iterator = new CombinationIterator("abc", 2); // 创建迭代器 iterator
iterator.next(); // 返回 "ab"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "ac"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "bc"
iterator.hasNext(); // 返回 false
Tip Hint
prompt:
1 <= combinationLength <= characters.length <= 15
- Each test contains up to
10^4
twice the function call. - Topic ensure that each function calls
next
are when the presence of a combination of letters.
Code
class CombinationIterator {
public:
string characters;
string cur;
int combinationLength;
bool first;
CombinationIterator(string characters, int combinationLength) {
this->characters = characters;
this->combinationLength = combinationLength;
this->cur = characters.substr(0, combinationLength);
this->first = true;
}
string next() {
if(first) {
first = false;
return cur;
}
vector<int>pos;
for(int i = 0; i < combinationLength; i++) {
pos.push_back(characters.find_first_of(cur[i]));
}
for(int i = pos.size() - 1; i >= 0; --i) {
if((i == pos.size() - 1 && pos[i] < characters.length() - 1)
|| (i < pos.size() - 1 && pos[i] + 1 != pos[i + 1])) {
shiftPos(pos, i);
break;
}
}
cur = "";
for(int i = 0, sz = pos.size(); i < sz; ++i)
cur += characters[pos[i]];
return cur;
}
void shiftPos(vector<int>&pos, int i) {
pos[i]++;
for(int j = i + 1; j < combinationLength; ++j)
pos[j] = pos[j - 1] + 1;
}
bool hasNext() {
vector<int>pos;
for(int i = 0; i < combinationLength; i++) {
pos.push_back(characters.find_first_of(cur[i]));
}
//for(auto i : pos) cout << i << " "; cout << endl;
if(pos[0] == characters.length() - combinationLength)
return false;
return true;
}
};
/**
* Your CombinationIterator object will be instantiated and called as such:
* CombinationIterator* obj = new CombinationIterator(characters, combinationLength);
* string param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
1289. Minimum descending path and II
1289. Minimum descending path and II
Give you a square integer arr
, the definition of "non-zero offset decline path" as: from arr
selecting a numeric array in each row, and the order of elected figures, the neighbors are not in the same column of the original array.
Please return to a non-zero offset of the minimum number and descending path.
Sample input and output sample Sample Input and Sample Output
Example 1:
**输入:** arr = [[1,2,3],[4,5,6],[7,8,9]]
**输出:** 13
**解释:**
所有非零偏移下降路径包括:
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
下降路径中数字和最小的是 [1,5,7] ,所以答案是 13 。
Tip Hint
prompt:
1 <= arr.length == arr[i].length <= 200
-99 <= arr[i][j] <= 99
Code
class Solution {
public:
static const int inf = 0x3f3f3f3f;
int minFallingPathSum(vector<vector<int>>& arr) {
int n = arr.size(), m = arr[0].size();
int s[n][m];
for(int j = 0; j < m; ++j)
s[0][j] = arr[0][j];
for(int i = 1; i < n; ++i) {
for(int j = 0; j < m; ++j) {
s[i][j] = inf;
for(int k = 0; k < m; ++k) {
if(k == j) continue;
s[i][j] = min(s[i][j], s[i - 1][k] + arr[i][j]);
}
}
}
int ans = inf;
for(int i = 0;i < m;++i) ans = min(ans,s[n-1][i]);
return ans;
}
};