代码
class Solution {
public :
static bool cmp ( vector< int > const & first, vector< int > const & second) {
if ( first[ 0 ] == second[ 0 ] ) return first[ 1 ] < second[ 1 ] ;
return first[ 0 ] < second[ 0 ] ;
}
int findMinArrowShots ( vector< vector< int >> & points) {
sort ( points. begin ( ) , points. end ( ) ) ;
int result = 1 ;
for ( int i = 1 ; i < points. size ( ) ; ++ i) {
if ( points[ i] [ 0 ] > points[ i - 1 ] [ 1 ] ) {
result++ ;
}
else {
points[ i] [ 1 ] = min ( points[ i - 1 ] [ 1 ] , points[ i] [ 1 ] ) ;
}
}
return result;
}
} ;
int main ( ) {
vector< vector< int >> points = { { 9 , 12 } , { 1 , 10 } , { 4 , 11 } , { 8 , 12 } , { 3 , 9 } , { 6 , 9 } , { 6 , 7 } } ;
Solution s;
int result = s. findMinArrowShots ( points) ;
printf ( "%d\n" , result) ;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
代码
class Solution {
public :
static bool cmp ( vector< int > const & first, vector< int > const & second) {
return first[ 1 ] < second[ 1 ] ;
}
int eraseOverlapIntervals ( vector< vector< int >> & intervals) {
if ( intervals. size ( ) == 0 ) return 0 ;
sort ( intervals. begin ( ) , intervals. end ( ) , cmp) ;
int result = 1 ;
int end = intervals[ 0 ] [ 1 ] ;
for ( int i = 1 ; i < intervals. size ( ) ; ++ i) {
if ( end <= intervals[ i] [ 0 ] ) {
end = intervals[ i] [ 1 ] ;
result++ ;
}
}
return intervals. size ( ) - result;
}
} ;
int main ( ) {
vector< vector< int >> intervals = { { 1 , 2 } , { 2 , 3 } } ;
Solution s;
int result = s. eraseOverlapIntervals ( intervals) ;
printf ( "%d\n" , result) ;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
代码
class Solution {
public :
static bool cmp ( vector< int > const & first, vector< int > const & second) {
return first[ 0 ] < second[ 0 ] ;
}
vector< int > partitionLabels ( string s) {
vector< vector< int >> nums;
vector< int > result;
unordered_map< char , map< int , int >> umap;
for ( int i = 0 ; i < s. size ( ) ; ++ i) {
if ( umap. find ( s[ i] ) == umap. end ( ) )
umap[ s[ i] ] [ 0 ] = i;
else {
umap[ s[ i] ] [ 1 ] = i;
}
}
unordered_map< char , map< int , int >> :: iterator it;
for ( it= umap. begin ( ) ; it!= umap. end ( ) ; ++ it) {
vector< int > temp;
for ( pair< const int , int > & iter : it-> second) {
temp. push_back ( iter. second) ;
}
if ( temp. size ( ) == 1 ) temp. push_back ( temp. back ( ) ) ;
nums. push_back ( temp) ;
}
sort ( nums. begin ( ) , nums. end ( ) , cmp) ;
for ( int i = 1 ; i < nums. size ( ) ; ++ i) {
if ( nums[ i] [ 0 ] < nums[ i - 1 ] [ 1 ] ) {
nums[ i] [ 1 ] = max ( nums[ i] [ 1 ] , nums[ i - 1 ] [ 1 ] ) ;
nums[ i] [ 0 ] = min ( nums[ i] [ 0 ] , nums[ i - 1 ] [ 0 ] ) ;
}
else {
result. push_back ( nums[ i- 1 ] [ 1 ] - nums[ i- 1 ] [ 0 ] + 1 ) ;
}
}
result. push_back ( nums[ nums. size ( ) - 1 ] [ 1 ] - nums[ nums. size ( ) - 1 ] [ 0 ] + 1 ) ;
return result;
}
} ;
int main ( ) {
string s = "ababcbacadefegdehijhklij" ;
Solution solution;
vector< int > result = solution. partitionLabels ( s) ;
return 0 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
代码
class Solution {
public :
static bool cmp ( vector< int > const & first, vector< int > const & second) {
return first[ 0 ] < second[ 0 ] ;
}
vector< vector< int >> merge ( vector< vector< int >> & intervals) {
vector< vector< int >> result;
sort ( intervals. begin ( ) , intervals. end ( ) , cmp) ;
for ( int i = 1 ; i < intervals. size ( ) ; ++ i) {
if ( intervals[ i] [ 0 ] <= intervals[ i - 1 ] [ 1 ] ) {
intervals[ i] [ 1 ] = max ( intervals[ i] [ 1 ] , intervals[ i - 1 ] [ 1 ] ) ;
intervals[ i] [ 0 ] = min ( intervals[ i] [ 0 ] , intervals[ i - 1 ] [ 0 ] ) ;
}
else {
result. push_back ( intervals[ i - 1 ] ) ;
}
}
result. push_back ( intervals[ intervals. size ( ) - 1 ] ) ;
return result;
}
} ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
代码
class Solution {
public :
int monotoneIncreasingDigits ( int n) {
string strNum = to_string ( n) ;
int flag = strNum. size ( ) ;
for ( int i = strNum. size ( ) - 1 ; i > 0 ; -- i) {
if ( strNum[ i - 1 ] > strNum[ i] ) {
flag = i;
strNum[ i - 1 ] -- ;
}
}
for ( int i = flag; i < strNum. size ( ) ; ++ i) {
strNum[ i] = '9' ;
}
return stoi ( strNum) ;
}
} ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
代码
class Solution {
public :
int maxProfit ( vector< int > & prices, int fee) {
int result = 0 ;
int minPrice = prices[ 0 ] ;
for ( int i = 1 ; i < prices. size ( ) ; ++ i) {
if ( prices[ i] < minPrice) minPrice = prices[ i] ;
if ( prices[ i] >= minPrice && prices[ i] <= minPrice + fee) {
continue ;
}
if ( prices[ i] > minPrice + fee) {
result += prices[ i] - minPrice - fee;
minPrice = prices[ i] - fee;
}
}
return result;
}
} ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
代码
class Solution {
public :
int result = 0 ;
int traversal ( TreeNode* cur) {
if ( cur == nullptr ) return 2 ;
int left = traversal ( cur-> left) ;
int right = traversal ( cur-> right) ;
if ( left == 2 && right == 2 ) return 0 ;
if ( left == 0 || right == 0 ) {
result++ ;
return 1 ;
}
if ( left == 1 || right == 1 ) return 2 ;
return - 1 ;
}
int minCameraCover ( TreeNode* root) {
if ( traversal ( root) == 0 ) result++ ;
return result;
}
} ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
参考资料:
代码随想录