按身高排序
class Solution {
map< int , string, greater< int >> person;
vector< string> ret;
public :
vector< string> sortPeople ( vector< string> & names, vector< int > & heights) {
int n = names. size ( ) ;
for ( int i = 0 ; i < n; ++ i) {
person[ heights[ i] ] = names[ i] ;
}
for ( auto & e : person) {
ret. push_back ( e. second) ;
}
return ret;
}
} ;
class Solution {
public :
vector< string> sortPeople ( vector< string> & names, vector< int > & heights) {
int n = names. size ( ) ;
vector< int > index ( n) ;
for ( int i = 0 ; i < n; ++ i) index[ i] = i;
ranges:: sort ( index, [ & heights] ( int left, int right) {
return heights[ left] > heights[ right] ;
} ) ;
vector< string> ret;
for ( int i : index) {
ret. push_back ( names[ i] ) ;
}
return ret;
}
} ;
优势洗牌 先排序 如果比不过,就去拖累最强的哪一个 如果能比过,那就直接比
class Solution {
public :
vector< int > advantageCount ( vector< int > & nums1, vector< int > & nums2) {
vector< int > index ( nums2. size ( ) ) ;
for ( int i = 0 ; i < index. size ( ) ; ++ i) index[ i] = i;
ranges:: sort ( index, [ & nums2] ( int left, int right) {
return nums2[ left] < nums2[ right] ;
} ) ;
ranges:: sort ( nums1) ;
ranges:: sort ( nums2) ;
vector< int > ret ( nums1. size ( ) ) ;
int start = 0 , end = nums1. size ( ) - 1 ;
for ( int e : nums1) {
if ( e > nums2[ start] ) {
ret[ index[ start++ ] ] = e;
} else {
ret[ index[ end-- ] ] = e;
}
}
return ret;
}
} ;
最长回文串
class Solution {
public :
int longestPalindrome ( string s) {
vector< int > v ( 256 ) ;
for ( char c : s) {
if ( v[ c] == 0 ) v[ c] = 1 ;
else v[ c] = 0 ;
}
int count = 0 ;
for ( int e : v) count += e;
if ( count > 0 ) return s. size ( ) - count + 1 ;
else return s. size ( ) ;
}
} ;
增减字符串匹配
class Solution {
public :
vector< int > diStringMatch ( string s) {
vector< int > ret;
int start = 0 , end = s. size ( ) ; ;
for ( char c : s) {
if ( c == 'I' ) {
ret. push_back ( start++ ) ;
} else {
ret. push_back ( end-- ) ;
}
}
ret. push_back ( start) ;
return ret;
}
} ;
分发饼干
class Solution {
public :
int findContentChildren ( vector< int > & g, vector< int > & s) {
ranges:: sort ( g) ;
ranges:: sort ( s) ;
int count = 0 , i = 0 ;
for ( int e : g) {
while ( i < s. size ( ) && s[ i] < e) ++ i;
if ( i < s. size ( ) ) {
++ count;
++ i;
} else break ;
}
return count;
}
} ;
最优除法
class Solution {
public :
string optimalDivision ( vector< int > & nums) {
if ( nums. size ( ) == 1 ) return to_string ( nums[ 0 ] ) ;
else if ( nums. size ( ) == 2 ) return to_string ( nums[ 0 ] ) + "/" + to_string ( nums[ 1 ] ) ;
string ret;
ret += to_string ( nums[ 0 ] ) ;
ret += "/(" ;
for ( int i = 1 ; i < nums. size ( ) - 1 ; ++ i) {
ret += to_string ( nums[ i] ) ;
ret += "/" ;
}
ret += to_string ( nums. back ( ) ) ;
ret += ")" ;
return ret;
}
} ;
跳跃游戏 II
int jump ( vector< int > & nums) {
if ( 0 >= nums. size ( ) - 1 ) return 0 ;
if ( 0 + nums[ 0 ] >= nums. size ( ) - 1 ) return 1 ;
int i = 0 , count = 0 ;
while ( i < nums. size ( ) ) {
int start = i, len = nums[ i] ;
for ( int j = 1 ; j <= nums[ i] ; ++ j) {
if ( i + j < nums. size ( ) && i + j + nums[ i + j] > start + len) {
start = i + j;
len = nums[ i + j] ;
}
}
++ count;
i = start;
if ( start + len >= nums. size ( ) - 1 ) {
break ;
}
}
return count + 1 ;
}
int jump ( vector< int > & nums) {
int count = 0 ;
int left = 0 , right = 0 ;
while ( right < nums. size ( ) - 1 ) {
int max_right = right + 1 ;
while ( left <= right) {
max_right = max ( max_right, left + nums[ left] ) ;
++ left;
}
left = right + 1 ;
right = max_right;
++ count;
}
return count;
}
跳跃游戏
class Solution {
public :
bool canJump ( vector< int > & nums) {
int left = 0 , int right = 0 ;
int max_right = 0 ;
while ( left <= right) {
if ( max_right >= nums. size ( ) - 1 ) return true ;
for ( int i = left; i <= right; ++ i) {
max_right = max ( max_right, i + nums[ i] ) ;
}
left = right + 1 ;
right = max_right;
}
return false ;
}
} ;
加油站
int canCompleteCircuit ( vector< int > & gas, vector< int > & cost) {
int n = gas. size ( ) ;
vector< int > diff ( n) ;
for ( int i = 0 ; i < n; ++ i) {
diff[ i] = gas[ i] - cost[ i] ;
}
for ( int i = 0 ; i < n; ) {
if ( diff[ i] >= 0 ) {
int cur = diff[ i] ;
for ( int j = i + 1 ; j < n; ++ j) {
cur += diff[ j] ;
if ( cur < 0 ) {
i = j + 1 ;
break ;
}
}
if ( cur < 0 ) {
continue ;
}
for ( int j = 0 ; j < i; ++ j) {
cur += diff[ j] ;
if ( cur < 0 ) {
return - 1 ;
}
}
return i;
} else {
++ i;
}
}
return - 1 ;
}
单调递增的数字 如果高位递增,就不去修改 从左向右,找到第一个递减的位置 从这个位置向前推,找到相同区域的最左端,使其减小 1,后面的数修改成 9
int monotoneIncreasingDigits ( int n) {
string s = to_string ( n) ;
int i = 1 ;
for ( ; i < s. size ( ) ; ++ i) {
if ( s[ i] < s[ i- 1 ] ) break ;
}
if ( i == s. size ( ) ) return stoi ( s) ;
int j = i - 1 ;
for ( ; j > 0 ; -- j) {
if ( s[ j] != s[ j- 1 ] ) {
s[ j] -= 1 ;
break ;
}
}
if ( j == 0 ) {
if ( s[ j] != '1' ) {
s[ j] -= 1 ;
} else {
s. erase ( s. begin ( ) ) ;
j -= 1 ;
}
}
for ( j += 1 ; j < s. size ( ) ; ++ j) {
s[ j] = '9' ;
}
return stoi ( s) ;
}