📌  相关文章
📜  给定范围内的数字,需要Kth最小步数才能减少到1

📅  最后修改于: 2021-04-17 17:57:52             🧑  作者: Mango

给定三个正整数LRK ,任务是从范围[L,R]中查找数量,该范围要求通过执行以下操作将K最小步数减少为1:

  • 如果X为偶数,则将X减小为X / 2
  • 否则,将X设置为(3 * X +1)

例子:

方法:想法是制作单独的函数,以从范围计算每个数字的步数,并打印获得的第K最小的值。请按照以下步骤解决问题:

  • 定义一个函数power_value()来计算数字所需的步数:
    • 基本条件:如果数字1 ,则返回0
    • 如果数字为偶数,则使用值number / 2调用函数power_value()
    • 否则,使用值数字* 3 +1调用函数power_value()
  • 现在,要查找第K最小的步骤,请执行以下步骤:
    • 初始化一个成对向量,例如ans
    • [L,R]范围内遍历数组并计算当前数i的幂值,然后在ans对向量中插入一对步数和i
    • 按照该步数的升序对向量进行排序。
  • 完成上述步骤后,从开始打印第K最小步数,即结果为ans [K – 1] .second

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
#define ll long long int
 
vector v(1000000, -1);
 
// Function to count the number of
// steps required to reduce val to
// 1 by the given operations
ll power_value(ll val)
{
    // Base Case
    if (val == 1)
        return 0;
 
    // If val is even, divide by 2
    if (val % 2 == 0) {
        v[val] = power_value(val / 2) + 1;
        return v[val];
    }
 
    // Otherwise, multiply it
    // by 3 and increment by 1
    else {
        ll temp = val * 3;
        temp++;
        v[val] = power_value(temp) + 1;
        return v[val];
    }
}
 
// Function to find Kth smallest
// count of steps required for
// numbers from the range [L, R]
ll getKthNumber(int l, int r, int k)
{
    // Stores numbers and their
    // respective count of steps
    vector > ans;
 
    // Count the number of steps for
    // all numbers from the range [L, R]
    for (ll i = l; i <= r; i++) {
        if (v[i] == -1)
            power_value(i);
    }
 
    ll j = 0;
 
    // Insert in the vector
    for (ll i = l; i <= r; i++) {
        ans.push_back(make_pair(v[i], i));
        j++;
    }
 
    // Sort the vector in ascending
    // order w.r.t. to  power value
    sort(ans.begin(), ans.end());
 
    // Print the K-th smallest number
    cout << ans[k - 1].second;
}
 
// Driver Code
int main()
{
    int L = 7, R = 10, K = 4;
    getKthNumber(L, R, K);
 
    return 0;
}


Python3
# Python3 program for the above approach
 
v = [-1]*(1000000)
 
# Function to count the number of
# steps required to reduce val to
# 1 by the given operations
def power_value(val):
   
    # Base Case
    if (val == 1):
        return 0
 
    # If val is even, divide by 2
    if (val % 2 == 0):
        v[val] = power_value(val // 2) + 1
        return v[val]
 
    # Otherwise, multiply it
    # by 3 and increment by 1
    else:
        temp = val * 3
        temp+=1
        v[val] = power_value(temp) + 1
        return v[val]
 
# Function to find Kth smallest
# count of steps required for
# numbers from the range [L, R]
def getKthNumber(l, r, k):
   
    # Stores numbers and their
    # respective count of steps
    ans = []
 
    # Count the number of steps for
    # anumbers from the range [L, R]
    for i in range(l, r + 1):
        if (v[i] == -1):
            power_value(i)
 
    j = 0
 
    # Insert in the vector
    for i in range(l, r + 1):
        ans.append([v[i], i])
        j += 1
 
    # Sort the vector in ascending
    # order w.r.t. to  power value
    ans = sorted(ans)
 
    # Prthe K-th smallest number
    print (ans[k - 1][1])
 
# Driver Code
if __name__ == '__main__':
    L,R,K = 7, 10, 4
    getKthNumber(L, R, K)
 
    # This code is contributed by mohit kumar 29.


输出:
9

时间复杂度: O(N * log N)
辅助空间: O(N)