[InterviewBit] Largest Number

Largest Number

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string Solution::largestNumber(const vector<int> &A) {
vector<string> AA;
for(auto& a : A) AA.push_back(to_string(a));
sort(rbegin(AA), rend(AA), [](auto a, auto b) {
return a + b < b + a;
});
string res = "";
for(auto& s : AA) res+=s;
reverse(begin(res), end(res));
while(res.size() > 1 and res.back() == '0') res.pop_back();
reverse(begin(res), end(res));
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/20/PS/interviewbit/largest-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.