通过递增它们或对它们进行按位或来最小化使两个整数相等的步骤
给定两个正整数A和B 。任务是使用最小操作使它们相等,例如:
- A = A + 1(将 a 增加 1)。
- B = B + 1(将 b 增加 1)。
- A = A | B(将 A 替换为 A 和 B 的按位或)。
例子:
Input: A = 5, B = 9
Output: 4
Explanation: It is better to use first operation i.e increase A by 1 four times,
Input: A = 2, B = 5
Output: 2
Explanation: It is better to apply second operation then third operation,
贪心方法:问题可以在位操作的帮助下使用贪心技术来解决。
直觉:
- Try to increase A or B by 1, the steps will be the maximum steps possible.
- Now to reduce these steps,
- We need to find an intermediate number X such that X OR B = B, because only then we can jump more than 1 number in a single step.
- Once we have found possible values for X, we can check that which value among them is reachable for A in least steps.
- Those least steps + 1 step (for doing bitwise OR of X with B) will be one of the lesser number of steps for A to reach B.
- Another way to reduce these steps:
- Consider the case when instead of making X OR B = B, we find possible values of Y such that A OR Y = Y, as B can also be moved as per given problem.
- So we can find least step needed to move B to Y and then add 1 more step to do bitwise OR of A with B.
- Now try to find the minimum among the both possible lesser steps as the required number of steps to change A to B.
插图:
Suppose A = 2, B = 5
Case 1: Possible value of X such that (X OR B = B) => [0, 1, 4, 5]
Now the steps required to convert A to B if we convert A to each possible value of X first, are:
Convert A to 0 => not possible as we cannot decrement A
Convert A to 1 => not possible as we cannot decrement A
Convert A to 4 => 2 increment operation, and then 1 operation for 4 OR 5 to make A as 5. Hence total operation = 3
Convert A to 5 => 3 increment operation to make A as 5. Hence total operation = 3
Case 2: Possible value of Y such that (A OR Y = Y) => [2, 6, 7, …]
Now the steps required to convert A to B if we convert B to each possible value of Y first, are:
Convert B to 2 => not possible as we cannot decrement B
Convert B to 6 => 1 increment operation, and then 1 operation for 2 OR 6 to make A as 6. Hence total operation = 2
Convert B to 7 => 2 increment operation, and then 1 operation for 2 OR 7 to make A as 7. Hence total operation = 3
Similarly for any conversion of B to value greater than 7 will take more steps.
Therefore the least steps required to convert A to B using given operations = min(3, 2) = 2 steps.
按照下面提到的步骤来实施该方法:
- 从i = A 迭代到 B并检查 ( i | B ) 是否与B相同以及所需的步骤。
- 找到以这种方式使 A 和 B 相等所需的最小步骤(例如x )。
- 现在迭代j = B 到 B+x :
- 检查j是否满足上面提到的情况 2。
- 更新使 A 和 B 相等所需的最小步骤。
- 返回最小步数。
下面是上述方法的实现:
Java
// Java code to implement the approach
import java.io.*;
import java.lang.*;
class GFG {
// Function to find min steps
// to convert A to B
public static int AToB(int a, int b)
{
int ans = Integer.MAX_VALUE;
// If a is greater than b, swap them
if (a > b) {
int temp = b;
b = a;
a = temp;
}
// Check for every i from 0 to b
for (int i = a; i <= b; i++) {
// If i or b equals to b then
// update the answer
if ((i | b) == b) {
int j = Math.abs(a - i);
if (i != b)
j++;
ans = Math.min(ans, j);
}
}
for (int i = b + 1; i <= b + ans; i++) {
if ((i | a) == i) {
ans = Math.min(ans, i - b + 1);
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int A = 2;
int B = 5;
System.out.println(AToB(A, B));
}
}
2
时间复杂度: O(B * log B)
辅助空间: O(1)