📌  相关文章
📜  通过将某些数字替换为B的数字来最大化A的值

📅  最后修改于: 2021-04-23 05:49:34             🧑  作者: Mango

给定两个字符串AB表示两个整数,任务是与B的任何位替换A的0位或更多位后打印A的最大化的值。

:B A的数字只能用一次。

例子:

方法:由于必须将A的值最大化,因此任何数字都将仅被较大值的数字替换。左边的数字在贡献该值方面具有更大的意义,因此应将其替换为尽可能大的值。对B进行排序,并在A中从左到右进行迭代,并尝试尽可能使用可用选项的最大值替换当前数字。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximized value of A
string maxValue(string a, string b)
{
    // Sort digits in ascending order
    sort(b.begin(), b.end());
    int n = a.length();
    int m = b.length();
  
    // j points to largest digit in B
    int j = m - 1;
    for (int i = 0; i < n; i++) {
  
        // If all the digits of b have been used
        if (j < 0)
            break;
  
        if (b[j] > a[i]) {
            a[i] = b[j];
  
            // Current digit has been used
            j--;
        }
    }
  
    // Return the maximized value
    return a;
}
  
// Driver code
int main()
{
    string a = "1234";
    string b = "4321";
  
    cout << maxValue(a, b);
  
    return 0;
}


Java
// Java implementation of the approach 
   
import java.util.*;
  
class GFG{ 
  
// Function to return the maximized value of A 
static String maxValue(char []a, char []b) 
{ 
    // Sort digits in ascending order 
    Arrays.sort(b); 
    int n = a.length; 
    int m = b.length; 
  
    // j points to largest digit in B 
    int j = m - 1; 
    for (int i = 0; i < n; i++) { 
  
        // If all the digits of b have been used 
        if (j < 0) 
            break; 
  
        if (b[j] > a[i]) { 
            a[i] = b[j]; 
  
            // Current digit has been used 
            j--; 
        } 
    } 
  
    // Return the maximized value 
    return String.valueOf(a); 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    String a = "1234"; 
    String b = "4321"; 
  
    System.out.print(maxValue(a.toCharArray(), b.toCharArray())); 
}
} 
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
  
# Function to return the maximized
# value of A
def maxValue(a, b):
      
    # Sort digits in ascending order
    b = sorted(b)
    bi = [i for i in b]
    ai = [i for i in a]
      
    n = len(a)
    m = len(b)
  
    # j points to largest digit in B
    j = m - 1
    for i in range(n):
  
        # If all the digits of b 
        # have been used
        if (j < 0):
            break
  
        if (bi[j] > ai[i]):
            ai[i] = bi[j]
  
            # Current digit has been used
            j -= 1
          
    # Return the maximized value
    x = "" . join(ai)
    return x
  
# Driver code
a = "1234"
b = "4321"
  
print(maxValue(a, b))
  
# This code is contributed 
# by mohit kumar


C#
// C# implementation of the approach 
using System;
  
class GFG
{ 
  
// Function to return the maximized value of A 
static String maxValue(char []a, char []b) 
{ 
    // Sort digits in ascending order 
    Array.Sort(b); 
    int n = a.Length; 
    int m = b.Length; 
  
    // j points to largest digit in B 
    int j = m - 1; 
    for (int i = 0; i < n; i++)
    { 
  
        // If all the digits of b have been used 
        if (j < 0) 
            break; 
  
        if (b[j] > a[i]) 
        { 
            a[i] = b[j]; 
  
            // Current digit has been used 
            j--; 
        } 
    } 
  
    // Return the maximized value 
    return String.Join("",a); 
} 
  
// Driver code 
public static void Main(String[] args) 
{ 
    String a = "1234"; 
    String b = "4321"; 
  
    Console.Write(maxValue(a.ToCharArray(), b.ToCharArray())); 
}
} 
  
// This code is contributed by PrinciRaj1992


PHP
 $a[$i])
        { 
            $a[$i] = $b[$j]; 
  
            // Current digit has been used 
            $j--; 
        } 
    } 
  
    // Convert array into string
    $a = implode("",$a);
      
    // Return the maximized value 
    return $a ;
} 
  
    // Driver code 
    # convert string into array
    $a = str_split("1234"); 
    $b = str_split("4321"); 
  
    echo maxValue($a, $b); 
      
// This code is contributed by Ryuga
?>


输出:
4334