当前位置:网站首页>LeetCode 2342. Digital and equal number of one of the biggest and
LeetCode 2342. Digital and equal number of one of the biggest and
2022-07-30 01:28:00 【Michael amin】
1. 题目
给你一个下标从 0 开始的数组 nums ,数组中的元素都是 正 整数.请你选出两个下标 i 和 j(i != j),且 nums[i] 的数位和 与 nums[j] 的数位和相等.
请你找出所有满足条件的下标 i 和 j ,找出并返回 nums[i] + nums[j] 可以得到的 最大值 .
示例 1:
输入:nums = [18,43,36,13,7]
输出:54
解释:满足条件的数对 (i, j) 为:
- (0, 2) ,两个数字的数位和都是 9 ,相加得到 18 + 36 = 54 .
- (1, 4) ,两个数字的数位和都是 7 ,相加得到 43 + 7 = 50 .
所以可以获得的最大和是 54 .
示例 2:
输入:nums = [10,12,19,14]
输出:-1
解释:不存在满足条件的数对,返回 -1 .
提示:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/max-sum-of-a-pair-with-equal-sum-of-digits
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
2. 解题
- 和作为 key,值为数字的 list,之后对 list 排序,取最大的2个
class Solution:
def maximumSum(self, nums: List[int]) -> int:
def bitsum(x):
s = 0
while x:
s += x%10
x //= 10
return s
d = defaultdict(list)
for x in nums:
d[bitsum(x)].append(x)
ans = -1
for k, v in d.items():
if len(v) > 1:
v.sort()
ans = max(ans, sum(v[-2:]))
return ans
384 ms 27.4 MB Python3
- 排序,can be changed to keep only the largest two
class Solution:
def maximumSum(self, nums: List[int]) -> int:
def bitsum(x):
s = 0
while x:
s += x%10
x //= 10
return s
d = defaultdict(list)
for x in nums:
s = bitsum(x)
if len(d[s]) <= 1:
d[s].append(x)
else:
arr = d[s]
if arr[0] > arr[1]:
arr[0], arr[1] = arr[1], arr[0]
if x > arr[0]:
d[s] = [x, arr[1]]
ans = -1
for k, v in d.items():
if len(v) > 1:
ans = max(ans, sum(v))
return ans
372 ms 26.5 MB Python3
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
边栏推荐
- 实习经历梳理
- 自学HarmonyOS应用开发(56)- 用Service保证应用在后台持续运行
- 【LeetCode每日一题】——404.左叶子之和
- STM32 - OLED display experiment
- 性能测试理论1 | 性能测试难点问题梳理
- 【LeetCode每日一题】——230.二叉搜索树中第K小的元素
- How to set up hybrid login in SQL server in AWS
- sublime 背景透明度以及列编辑
- [Flutter] Flutter preloading of mixed development solves the problem of slow page loading for the first time
- [Microservice~Nacos] Nacos service provider and service consumer
猜你喜欢
![2022-07-29:一共有n个人,从左到右排列,依次编号0~n-1, h[i]是第i个人的身高, v[i]是第i个人的分数, 要求从左到右选出一个子序列,在这个子序列中的人,从左到右身高是不下降的。](/img/a0/998fb7edca5ebe5d9b1d1e8b705faa.png)
2022-07-29:一共有n个人,从左到右排列,依次编号0~n-1, h[i]是第i个人的身高, v[i]是第i个人的分数, 要求从左到右选出一个子序列,在这个子序列中的人,从左到右身高是不下降的。

网络原理 基础知识

Detailed introduction of @RequestParam annotation

验证框架-01

【Flutter】Flutter inspector 工具使用详解,查看Flutter布局,widget树,调试界面等

STM32——OLED显示实验

vscode 工作区配置插件 配置不同工作环境

CMake Tutorial 巡礼(1)_基础的起点

【微服务~Nacos】Nacos之配置中心

小白必看|不用编程的labview,ATECLOUD完全满足你的需求
随机推荐
API 接口批量测试
图解LeetCode——593. 有效的正方形(难度:中等)
在服务器上运行node流程
The range of motion of the robot
sublime 背景透明度以及列编辑
经济衰退时期的对比:如今更像历史上的哪段时期?
气路旋转连接器怎么用
Unity笔记——FairyGUI
Detailed introduction to the usage of Nacos configuration center
Navicat error: 1045-Access denied for user [email protected](using passwordYES)
@RequestParam注解的详细介绍
[VMWARE--Shared files]
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
【MySQL必知必会】 范式 | ER模型
[Flutter] Detailed explanation of the use of the Flutter inspector tool, viewing the Flutter layout, widget tree, debugging interface, etc.
my creative day
go语言解决自定义header的跨域问题
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
小白必看|不用编程的labview,ATECLOUD完全满足你的需求
帽式滑环的工作原理