릿코드(LEETCODE)
biweekly Contest 25
cepiloth
2020. 5. 5. 22:34
반응형
https://leetcode.com/contest/biweekly-contest-25/
1번 문제 - 1431. Kids With the Greatest Number of Candies
class Solution {
public:
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
vector<int> A = candies;
sort(A.begin(), A.end());
int cMax = A[candies.size()-1];
vector<bool> vb;
for(auto a:candies) {
int cand = a;
if(cand >= cMax|| cand + extraCandies >= cMax)
vb.push_back(true);
else
vb.push_back(false);
}
return vb;
}
};
2번 문제 - 1432. Max Difference You Can Get From Changing an Integer
class Solution {
public:
int find(string S) {
for (int i = 0; i < S.size(); i++) {
if (S[i] != '9') {
return i;
}
}
return 0;
}
int find2(string S) {
for (int i = 0; i < S.size(); i++) {
if (S[i] != '1') {
if (S[i] == '0')
continue;
return i;
}
}
return 0;
}
void S_replace(string& s, char code) {
for (int i = 0; i<s.size(); i++) {
if (s[i] == code) {
s[i] = '9';
}
}
}
void S_replace2(string& s, char code) {
for (int i = 0; i < s.size(); i++) {
if (s[i] == code) {
s[i] = '1';
}
}
}
void S_replace3(string& s, char code) {
for (int i = 0; i < s.size(); i++) {
if (s[i] == code) {
s[i] = '0';
}
}
}
int maxDiff(int num) {
string S = to_string(num);
int position = find(S);
char code = S[position];
string DS = S;
S_replace(DS, code);
position = find2(S);
code = S[position];
if (position == 0) {
S_replace2(S, code);
}
else {
S_replace3(S, code);
}
int sol = abs(atoi(S.c_str()) - atoi(DS.c_str()));
return sol;
}
};
반응형