首页 > 其他分享 >LeetCode 1387. Sort Integers by The Power Value

LeetCode 1387. Sort Integers by The Power Value

时间:2024-08-04 12:16:33浏览次数:7  
标签:Sort Integers Power power -- lo value int hi

原题链接在这里:https://leetcode.com/problems/sort-integers-by-the-power-value/description/

题目:

The power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps:

  • if x is even then x = x / 2
  • if x is odd then x = 3 * x + 1

For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1).

Given three integers lohi and k. The task is to sort all integers in the interval [lo, hi] by the power value in ascending order, if two or more integers have the same power value sort them by ascending order.

Return the kth integer in the range [lo, hi] sorted by the power value.

Notice that for any integer x (lo <= x <= hi) it is guaranteed that x will transform into 1 using these steps and that the power of x is will fit in a 32-bit signed integer.

Example 1:

Input: lo = 12, hi = 15, k = 2
Output: 13
Explanation: The power of 12 is 9 (12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)
The power of 13 is 9
The power of 14 is 17
The power of 15 is 17
The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13.
Notice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15.

Example 2:

Input: lo = 7, hi = 11, k = 4
Output: 7
Explanation: The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14].
The interval sorted by power is [8, 10, 11, 7, 9].
The fourth number in the sorted array is 7.

Constraints:

  • 1 <= lo <= hi <= 1000
  • 1 <= k <= hi - lo + 1

题解:

We can sort the based on array, array[0] is the original value, array[1] is the power. First sort based on power, then on value.

To get power, we can use DFS + memo. 

Base case is 1, return 0. 

If memo contains it, return the corresponding value.

Otherwise, get the next value using DFS, update the memo and return next + 1.

Time Complexity: O(n(logM + logk)). n = hi - lo. M = maximum value encountered when calculating power. The worst case scenario is when the sequence goes through many odd numbers before reaching 1. Memoization is used. The amortized time for calStep is O(logM).

Space: O(hi + k). memo takes O(hi). minHeap takes O(k).

AC Java:

 1 class Solution {
 2     public int getKth(int lo, int hi, int k) {
 3         k = hi - lo + 1 - k + 1;
 4         PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]);
 5         for(int i = lo; i <= hi; i++){
 6             int[] can = new int[]{i, calStep(i)};
 7             minHeap.add(can);
 8             if(minHeap.size() > k){
 9                 minHeap.poll();
10             }
11         }
12 
13         return minHeap.peek()[0];
14     }
15 
16     Map<Integer, Integer> memo = new HashMap<>();
17     private int calStep(int i){
18         if(i == 1){
19             return 0;
20         }
21 
22         if(memo.containsKey(i)){
23             return memo.get(i);
24         }
25 
26         int next = 0;
27         if(i % 2 == 1){
28             next = calStep(3 * i + 1);
29         }else{
30             next = calStep(i / 2);
31         }   
32 
33         memo.put(i, next + 1);
34         return next + 1;
35     }
36 }

 

标签:Sort,Integers,Power,power,--,lo,value,int,hi
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18341618

相关文章

  • 计算机基础(Windows 10+Office 2016)教程 —— 第7章 演示文稿软件PowerPoint 2016
    第7章演示文稿软件PowerPoint20167.1PowerPoint2016入门7.1.1PowerPoint2016简介7.1.2PowerPoint2016的操作界面组成7.1.3PowerPoint2016的窗口视图方式7.1.4PowerPoint2016的演示文稿及其操作7.1.5PowerPoint2016的幻灯片及其操作7.2演示文稿的编......
  • python用List的内建函数list.sort进行排序
    对List进行排序,Python提供了两个方法方法1用List的内建函数listsort进行排序listsort(func=None,key=None,reverse=False)Python实对List进行排序,Python提供了两个方法方法1.用List的内建函数list.sort进行排序list.sort(func=None,key=None,reverse=False)>>>list=......
  • 使用Python自动将照片文件夹转换为PowerPoint幻灯片
    在这个数字时代,我们经常需要快速创建照片幻灯片来展示我们的回忆或工作成果。今天,我们将探讨如何使用Python来自动化这个过程,将一个文件夹中的所有照片转换为一个精美的PowerPoint演示文稿,每张照片占据一页,并以文件名作为标题。C:\pythoncode\new\jpeginsertppt.py全......
  • 功能齐全,深度适配 Home Assistant 的 CMPOWER W1 智能插排固件(附源码)
    固件特点:足够傻瓜,配网即用,无需添加/修改任何yaml文件,配网后HA中的mqttbroker会自动发现设备以及所有实体(包括计量)。支持计量功能,无需额外校准(电压,电流,功率,电量,频率,温度),基本满足日常使用。设备离线HA中自动更新状态显示设备不可用,当设备重新上线后HA中自动更新......
  • 如何使用Python代码获取Power Bi Visual Level数据
    我有一个Powerbi报告,托管在本地报告服务器上。现在我想使用python代码检索视觉级别数据。例如,我有一个卡片视觉效果,显示为“100”,这个“100”是根据度量计算的,对于某些视觉效果,该值直接来自数据集中的列值。现在我想检索测量值为“100”,而且我还需要直接来自python代......
  • 在cmd/powershell中使用java/javac -cp/--class-path命令链接多个jar包
    ​ 之前使用ide,习惯了傻瓜式一键运行java文件,对于java虚拟机以及java指令了解的很少,最近重温java,在使用windows中的cmd来运行java项目时,遇到了一点问题,相同的指令在cmd中能够运行,在powershell中不能正确运行,在国内网站上搜索无果后,果断去国外,在stackoverflow上找到解决办法。​ ......
  • 回调函数和qsort,strcmp函数
    有任何不懂的问题可以评论区留言,能力范围内都会一一回答1.回调函数是什么?回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,被调用的函数就是回调函数。回调函数不是由该函数的实现方直接调用,......
  • 【PowerDesigner数据建模使用笔记】
    PowerDesigner使用技巧背景思考尝试如何显示表备注、表字段备注从数据库更新到模型注意事项背景使用PowerDesigner来进行数据建模的时候,表属性字段一个个输入有点太过繁琐、痛苦。思考有没其他的更好方式来快速进行数据建模,省去逐个数据表属性的键盘敲打出来呢尝......
  • 如何通过PowerShell批量修改O365用户的office phone属性值
    我的博客园:https://www.cnblogs.com/CQman/如何通过PowerShell批量修改O365用户的officephone属性值?需求信息: 组织中的O365用户在创建时,已手动录入了办公电话(Officephone),现在需要在办公电话前面加上统一的数字,如“0571-0985”,以批量的方式统一修改。备注:O365用户的Offic......
  • where /? 在 Windows 中,where 命令是用于在命令提示符或 PowerShell 中查找指定命令的
     在Windows中,where命令是用于在命令提示符或PowerShell中查找指定命令的位置的工具。它可以帮助用户确定系统中某个可执行文件的路径。使用方法:基本用法:shellCopyCodewherecommand其中command是你要查找的命令或可执行文件的名称。例如,如果你想查找notepad的......