414. Third Maximum Number -- Python

本文介绍了一个算法问题,即如何在给定的整数数组中找到第三大的数字,并提供了高效的解决方案。利用Python中的set和sorted函数,文章实现了O(n)的时间复杂度要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).

Example 1:

Input: [3, 2, 1]

Output: 1

Explanation: The third maximum is 1.

Example 2:

Input: [1, 2]

Output: 2

Explanation: The third maximum does not exist, so the maximum (2) is returned instead.

Example 3:

Input: [2, 2, 3, 1]

Output: 1

Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.

思路:
就是一个排序,用 set 可以直接消除重复的数字,如过长度小于 3 则肯定没有第三个大的值,返回最大值即可,如果长度达大于 3,则返回第三个大的值就行了。

Tips:
之前使用 set() 处理只包含数字的列表,默认是消除重复的元素然后按照数字大小顺序排列,但是当处理的列表中同时存在正数和负数时,set则是正数按大小排在前,负数按大小顺序排在后,第一次提交就卡在了同时正负数的这种情况。

例如:

L=[4,2,3,3,-1,-13,-4]

set(L) = {2, 3, 4, -13, -4, -1}

所以 set(nums) 外要额外加一个 sorted()函数来重新排序。

代码:

class Solution:
    def thirdMax(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        if  len(set(nums))<3:
            return nums[-1]
        else:
            return sorted(set(nums))[-3]
Requirements The code is tested on clean Ubuntu 20.04 with ROS noetic installation. Install the required package toppra: sudo pip3 install toppra catkin_pkg PyYAML empy matplotlib pyrfc3339 Run the code 1. Download and compile the code git clone https://github.com/ZJU-FAST-Lab/Primitive-Planner.git cd Primitive-Planner catkin_make -DCMAKE_BUILD_TYPE=Release 2. Generate the motion primitive library cd src/scripts python3 swarm_path.py The generated motion primitive library is stored in "src/planner/plan_manage/primitive_library/". 3. Run the planner In the "Primitive-Planner/" directory: source devel/setup.bash # or setup.zsh cd src/scripts python3 gen_position_swap.py 20 # It will generate the swarm.launch with 20 drones roslaunch primitive_planner swarm.launch Wait until all the nodes have their launch process finished and keep printing "[FSM]: state: WAIT_TARGET. Waiting for trigger". Open another terminal, publish the trigger cd src/scripts bash pub_trigger.sh Then the drones (drone number is 40) will start to fly like this Change the drone number when executing "python3 gen_position_swap.py <drone_number>". Before starting the "roslaunch" command, please open another terminal and run "htop" to monitor the Memory usage and CPU usage. Each drone requires about 200 MB memory. Keep the htop opened for entire flight. The computation time is printed on the terminal in blue text, named as "plan_time". To get the accurate computation time, please fix the CPU frequency to its maximum by sudo apt install cpufrequtils sudo cpufreq-set -g performance Otherwise the CPU will run in powersave mode with low frequency. (根据上述代码帮我生成复现最适合在乌班图20.04复现的详细的实现过程和每一步的预期输出,需要安装的每个东西的乌班图20.04兼容版本,还有每一步是否需要创建虚拟环境,每一步是在创建的虚拟环境进行还是不进入虚拟环境的终端进行)
最新发布
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值