力扣2815.數組中的最大數對和
-
遍歷每個元素 并求其中最大的數字
- 將每個數字對應的最大元素存在數組中
- 每遍歷到一個新的元素 ,將其存在對應的數組位置中
-
class Solution {public:int maxSum(vector<int>& nums) {vector<int> cnt(10,INT_MIN);int res = -1;for(int v : nums){int max_d = 0;for(int x = v;x;x/=10)max_d = max(max_d,x%10);res = max(res,v + cnt[max_d]);cnt[max_d] = max(cnt[max_d] , v);}return res;}};