📌  相关文章
📜  通过对数组元素应用“+”和“*”操作可以获得的最小数字

📅  最后修改于: 2021-09-06 06:23:26             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[]和一个长度为(N – 1)的字符串S ,包含字符‘+’‘*’ ,任务是找到应用算术运算后可以获得的最小数字字符串S 中提到的数组元素以任意顺序排列。

例子:

方法:可以使用位掩码解决给定的问题。请按照以下步骤解决问题:

  • 将字符串S中的乘法和加法运算的计数存储在变量中,分别为muladd
  • 现在,应用于数组元素的操作可以在掩码(二进制字符串)中进行编码,这样如果掩码的i 位被设置,则它等于‘1’ ,并且必须执行乘法运算。否则,执行加法。
  • 初始化一个变量,表示如ANS INT_MAX该存储结果的最小值。
  • 因此,创建[0, 2 (N – 1) ]范围内的所有掩码并找到掩码中设置的位数并执行以下步骤:
    • 如果掩码中设置的位数等于mul ,则将此掩码应用于数组A[] ,即,如果掩码的i 位“1”,则对第i元素执行乘法运算和(i + 1)元素。
    • 否则,执行加法。
    • ans的值更新为ans和上述步骤中计算出的值中的最小值。
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the smallest number
// that can  be obtained after applying
// the arithmetic operations mentioned
// in the string S
int minimumSum(int A[], int N, string S)
{
    // Stores the count of multiplication
    // operator in the string
    int mul = 0;
    for (int i = 0;
         i < (int)S.size(); i++) {
        if (S[i] == '*')
            mul += 1;
    }
 
    // Store the required result
    int ans = 1000000;
 
    // Iterate in the range to
    // create the mask
    for (int i = 0;
         i < (1 << (N - 1)); i++) {
        int cnt = 0;
        vector v;
 
        // Checking the number of bits
        // that are set in the mask
        for (int j = 0; j < N - 1; j++) {
 
            if ((1 << j) & (i)) {
                cnt += 1;
                v.push_back('*');
            }
            else {
                v.push_back('+');
            }
        }
 
        // Check if the number of bits
        // that are set in the mask is
        // multiplication operation
        if (cnt == mul) {
 
            // Storing the elements
            // that is to be added
            deque d;
            d.push_back(A[0]);
 
            // Apply the multiplications
            // operation first
            for (int j = 0; j < N - 1; j++) {
 
                // If sign is '*', then
                // multiply last element
                // of deque with arr[i]
                if (v[j] == '*') {
 
                    int x = d.back();
                    d.pop_back();
                    x = x * A[j + 1];
 
                    // Push last multiplied
                    // element in the deque
                    d.push_back(x);
                }
                else {
 
                    // If the element is to
                    // be added, then add
                    // it to the deque
                    d.push_back(A[j + 1]);
                }
            }
 
            int sum = 0;
 
            // Add all the element of
            // the deque
            while (d.size() > 0) {
                int x = d.front();
                sum += x;
                d.pop_front();
            }
 
            // Minimize the answer with
            // the given sum
            ans = min(ans, sum);
        }
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 2, 2, 2 };
    string S = "**+";
    int N = sizeof(A) / sizeof(A[0]);
    cout << minimumSum(A, N, S);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the smallest number
// that can  be obtained after applying
// the arithmetic operations mentioned
// in the String S
static int minimumSum(int A[], int N, String S)
{
     
    // Stores the count of multiplication
    // operator in the String
    int mul = 0;
    for(int i = 0;
            i < (int)S.length(); i++)
    {
        if (S.charAt(i) == '*')
            mul += 1;
    }
 
    // Store the required result
    int ans = 1000000;
 
    // Iterate in the range to
    // create the mask
    for(int i = 0;
            i < (1 << (N - 1)); i++)
    {
        int cnt = 0;
        Vector v = new Vector();
 
        // Checking the number of bits
        // that are set in the mask
        for(int j = 0; j < N - 1; j++)
        {
            if (((1 << j) & (i)) > 0)
            {
                cnt += 1;
                v.add('*');
            }
            else
            {
                v.add('+');
            }
        }
 
        // Check if the number of bits
        // that are set in the mask is
        // multiplication operation
        if (cnt == mul)
        {
             
            // Storing the elements
            // that is to be added
            LinkedList d = new LinkedList();
            d.add(A[0]);
 
            // Apply the multiplications
            // operation first
            for(int j = 0; j < N - 1; j++)
            {
                 
                // If sign is '*', then
                // multiply last element
                // of deque with arr[i]
                if (v.get(j) == '*')
                {
                    int x = d.getLast();
                    d.removeLast();
                    x = x * A[j + 1];
 
                    // Push last multiplied
                    // element in the deque
                    d.add(x);
                }
                else
                {
                     
                    // If the element is to
                    // be added, then add
                    // it to the deque
                    d.add(A[j + 1]);
                }
            }
            int sum = 0;
 
            // Add all the element of
            // the deque
            while (d.size() > 0)
            {
                int x = d.peek();
                sum += x;
                d.removeFirst();
            }
 
            // Minimize the answer with
            // the given sum
            ans = Math.min(ans, sum);
        }
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 2, 2, 2, 2 };
    String S = "**+";
    int N = A.length;
     
    System.out.print(minimumSum(A, N, S));
}
}
 
// This code is contributed by shikhasingrajput


输出:
8

时间复杂度: O(2 (N – 1) *N)
辅助空间: O(N)