给定一个由N个不同的数组组成的数组arr [] 整数和阵列Q []表示的查询,为每个查询Q中的任务[i]是通过除去从任一端数组元素,直到Q [i]的获得以找到最小和可能的。
例子:
Input: arr[] = {2, 3, 6, 7, 4, 5, 1}, Q[] = {7, 6}
Output: 17 11
Explanation:
Query 1: By popping elements from the end, sum = 1 + 5 + 4 + 7 = 17.
Query 2: Popping elements from the front, sum = 2 + 3 + 6 = 11.
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, Q[] = {4, 6, 3}
Output: 10 21 6
天真的方法:解决给定问题的最简单方法是从每个查询Q [i]的两端遍历给定数组,并打印从这两个遍历获得的最小和,直到获得值Q [i]的元素。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum sum for
// each query after removing elements
// from either ends
void minSum(int arr[], int N, int Q[],
int M)
{
// Traverse the query array
for (int i = 0; i < M; i++) {
int val = Q[i];
int front = 0, rear = 0;
// Traverse the array from
// the front
for (int j = 0; j < N; j++) {
front += arr[j];
// If element equals val,
// then break out of loop
if (arr[j] == val) {
break;
}
}
// Traverse the array from rear
for (int j = N - 1; j >= 0; j--) {
rear += arr[j];
// If element equals val, break
if (arr[j] == val) {
break;
}
}
// Print the minimum of the
// two as the answer
cout << min(front, rear) << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 6, 7, 4, 5, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
int Q[] = { 7, 6 };
int M = sizeof(Q) / sizeof(Q[0]);
// Function Call
minSum(arr, N, Q, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the minimum sum for
// each query after removing elements
// from either ends
static void minSum(int arr[], int N, int Q[],
int M)
{
// Traverse the query array
for (int i = 0; i < M; i++)
{
int val = Q[i];
int front = 0, rear = 0;
// Traverse the array from
// the front
for (int j = 0; j < N; j++)
{
front += arr[j];
// If element equals val,
// then break out of loop
if (arr[j] == val)
{
break;
}
}
// Traverse the array from rear
for (int j = N - 1; j >= 0; j--)
{
rear += arr[j];
// If element equals val, break
if (arr[j] == val)
{
break;
}
}
// Print the minimum of the
// two as the answer
System.out.print(Math.min(front, rear) + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 3, 6, 7, 4, 5, 1 };
int N = arr.length;
int Q[] = { 7, 6 };
int M = Q.length;
// Function Call
minSum(arr, N, Q, M);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Function to find the minimum sum for
# each query after removing elements
# from either ends
def minSum(arr, N, Q, M):
# Traverse the query array
for i in range(M):
val = Q[i]
front, rear = 0, 0
# Traverse the array from
# the front
for j in range(N):
front += arr[j]
# If element equals val,
# then break out of loop
if (arr[j] == val):
break
# Traverse the array from rear
for j in range(N - 1, -1, -1):
rear += arr[j]
# If element equals val, break
if (arr[j] == val):
break
# Prthe minimum of the
# two as the answer
print(min(front, rear), end = " ")
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 6, 7, 4, 5, 1]
N = len(arr)
Q = [7, 6]
M = len(Q)
# Function Call
minSum(arr, N, Q, M)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum sum for
// each query after removing elements
// from either ends
static void minSum(int[] arr, int N, int[] Q,
int M)
{
// Traverse the query array
for(int i = 0; i < M; i++)
{
int val = Q[i];
int front = 0, rear = 0;
// Traverse the array from
// the front
for(int j = 0; j < N; j++)
{
front += arr[j];
// If element equals val,
// then break out of loop
if (arr[j] == val)
{
break;
}
}
// Traverse the array from rear
for(int j = N - 1; j >= 0; j--)
{
rear += arr[j];
// If element equals val, break
if (arr[j] == val)
{
break;
}
}
// Print the minimum of the
// two as the answer
Console.Write(Math.Min(front, rear) + " ");
}
}
// Driver Code
static public void Main()
{
int[] arr = { 2, 3, 6, 7, 4, 5, 1 };
int N = arr.Length;
int[] Q = { 7, 6 };
int M = Q.Length;
// Function Call
minSum(arr, N, Q, M);
}
}
// This code is contributed by rag2127
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum sum
// for each query after removing
// element from either ends till each
// value Q[i]
void minOperations(int arr[], int N,
int Q[], int M)
{
// Stores the prefix sum from
// both the ends of the array
map m1, m2;
int front = 0, rear = 0;
// Traverse the array from front
for (int i = 0; i < N; i++) {
front += arr[i];
// Insert it into the map m1
m1.insert({ arr[i], front });
}
// Traverse the array in reverse
for (int i = N - 1; i >= 0; i--) {
rear += arr[i];
// Insert it into the map m2
m2.insert({ arr[i], rear });
}
// Traverse the query array
for (int i = 0; i < M; i++) {
// Print the minimum of the
// two values as the answer
cout << min(m1[Q[i]], m2[Q[i]])
<< " ";
}
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 6, 7, 4, 5, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
int Q[] = { 7, 6 };
int M = sizeof(Q) / sizeof(Q[0]);
// Function Call
minOperations(arr, N, Q, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum sum
// for each query after removing
// element from either ends till each
// value Q[i]
static void minOperations(int[] arr, int N,
int[] Q, int M)
{
// Stores the prefix sum from
// both the ends of the array
Map m1 = new HashMap();
Map m2 = new HashMap();
int front = 0, rear = 0;
// Traverse the array from front
for(int i = 0; i < N; i++)
{
front += arr[i];
// Insert it into the map m1
m1.put(arr[i], front);
}
// Traverse the array in reverse
for(int i = N - 1; i >= 0; i--)
{
rear += arr[i];
// Insert it into the map m2
m2.put(arr[i], rear);
}
// Traverse the query array
for(int i = 0; i < M; i++)
{
// Print the minimum of the
// two values as the answer
System.out.print(Math.min(m1.get(Q[i]),
m2.get(Q[i])) + " ");
}
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 2, 3, 6, 7, 4, 5, 1 };
int N = arr.length;
int[] Q = { 7, 6 };
int M = Q.length;
// Function Call
minOperations(arr, N, Q, M);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to find the minimum sum
# for each query after removing
# element from either ends till each
# value Q[i]
def minOperations(arr, N, Q, M):
# Stores the prefix sum from
# both the ends of the array
m1 = {}
m2 = {}
front = 0
rear = 0
# Traverse the array from front
for i in range(N):
front += arr[i]
# Insert it into the map m1
m1[arr[i]] = front
# Traverse the array in reverse
for i in range(N - 1, -1, -1):
rear += arr[i]
# Insert it into the map m2
m2[arr[i]] = rear
# Traverse the query array
for i in range(M):
# Print the minimum of the
# two values as the answer
print(min(m1[Q[i]], m2[Q[i]]),end=" ")
# Driver Code
arr = [2, 3, 6, 7, 4, 5, 1 ]
N = len(arr)
Q = [7,6]
M = len(Q)
# Function Call
minOperations(arr, N, Q, M)
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the minimum sum
// for each query after removing
// element from either ends till each
// value Q[i]
static void minOperations(int[] arr, int N,
int[] Q, int M)
{
// Stores the prefix sum from
// both the ends of the array
Dictionary m1 = new Dictionary();
Dictionary m2 = new Dictionary();
int front = 0, rear = 0;
// Traverse the array from front
for(int i = 0; i < N; i++)
{
front += arr[i];
// Insert it into the map m1
m1[arr[i]] = front;
}
// Traverse the array in reverse
for(int i = N - 1; i >= 0; i--)
{
rear += arr[i];
// Insert it into the map m2
m2[arr[i]] = rear;
}
// Traverse the query array
for(int i = 0; i < M; i++)
{
// Print the minimum of the
// two values as the answer
Console.Write(Math.Min(m1[Q[i]],
m2[Q[i]]) + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 3, 6, 7, 4, 5, 1 };
int N = arr.Length;
int[] Q = { 7, 6 };
int M = Q.Length;
// Function Call
minOperations(arr, N, Q, M);
}
}
// This code is contributed by ukasp
输出:
17 11
时间复杂度: O(N * M)
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是使用Prefix Sum技术解决此问题。请按照以下步骤解决问题:
- 创建两个辅助映射,例如M1和M2 。
- 从最前面遍历数组,并将计算出的当前总和与元素一起插入到Map M1中的每个索引。
- 同样,从背面遍历数组,并将计算出的当前总和与元素一起插入映射M2中的每个索引。
- 遍历数组Q [] ,对于每个元素Q [i] ,将M1 [Q [i]]和M2 [Q [i]]的最小值打印为最小可能的总和。
下面是上述方法的实现:
C++ 14
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum sum
// for each query after removing
// element from either ends till each
// value Q[i]
void minOperations(int arr[], int N,
int Q[], int M)
{
// Stores the prefix sum from
// both the ends of the array
map m1, m2;
int front = 0, rear = 0;
// Traverse the array from front
for (int i = 0; i < N; i++) {
front += arr[i];
// Insert it into the map m1
m1.insert({ arr[i], front });
}
// Traverse the array in reverse
for (int i = N - 1; i >= 0; i--) {
rear += arr[i];
// Insert it into the map m2
m2.insert({ arr[i], rear });
}
// Traverse the query array
for (int i = 0; i < M; i++) {
// Print the minimum of the
// two values as the answer
cout << min(m1[Q[i]], m2[Q[i]])
<< " ";
}
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 6, 7, 4, 5, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
int Q[] = { 7, 6 };
int M = sizeof(Q) / sizeof(Q[0]);
// Function Call
minOperations(arr, N, Q, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum sum
// for each query after removing
// element from either ends till each
// value Q[i]
static void minOperations(int[] arr, int N,
int[] Q, int M)
{
// Stores the prefix sum from
// both the ends of the array
Map m1 = new HashMap();
Map m2 = new HashMap();
int front = 0, rear = 0;
// Traverse the array from front
for(int i = 0; i < N; i++)
{
front += arr[i];
// Insert it into the map m1
m1.put(arr[i], front);
}
// Traverse the array in reverse
for(int i = N - 1; i >= 0; i--)
{
rear += arr[i];
// Insert it into the map m2
m2.put(arr[i], rear);
}
// Traverse the query array
for(int i = 0; i < M; i++)
{
// Print the minimum of the
// two values as the answer
System.out.print(Math.min(m1.get(Q[i]),
m2.get(Q[i])) + " ");
}
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 2, 3, 6, 7, 4, 5, 1 };
int N = arr.length;
int[] Q = { 7, 6 };
int M = Q.length;
// Function Call
minOperations(arr, N, Q, M);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to find the minimum sum
# for each query after removing
# element from either ends till each
# value Q[i]
def minOperations(arr, N, Q, M):
# Stores the prefix sum from
# both the ends of the array
m1 = {}
m2 = {}
front = 0
rear = 0
# Traverse the array from front
for i in range(N):
front += arr[i]
# Insert it into the map m1
m1[arr[i]] = front
# Traverse the array in reverse
for i in range(N - 1, -1, -1):
rear += arr[i]
# Insert it into the map m2
m2[arr[i]] = rear
# Traverse the query array
for i in range(M):
# Print the minimum of the
# two values as the answer
print(min(m1[Q[i]], m2[Q[i]]),end=" ")
# Driver Code
arr = [2, 3, 6, 7, 4, 5, 1 ]
N = len(arr)
Q = [7,6]
M = len(Q)
# Function Call
minOperations(arr, N, Q, M)
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the minimum sum
// for each query after removing
// element from either ends till each
// value Q[i]
static void minOperations(int[] arr, int N,
int[] Q, int M)
{
// Stores the prefix sum from
// both the ends of the array
Dictionary m1 = new Dictionary();
Dictionary m2 = new Dictionary();
int front = 0, rear = 0;
// Traverse the array from front
for(int i = 0; i < N; i++)
{
front += arr[i];
// Insert it into the map m1
m1[arr[i]] = front;
}
// Traverse the array in reverse
for(int i = N - 1; i >= 0; i--)
{
rear += arr[i];
// Insert it into the map m2
m2[arr[i]] = rear;
}
// Traverse the query array
for(int i = 0; i < M; i++)
{
// Print the minimum of the
// two values as the answer
Console.Write(Math.Min(m1[Q[i]],
m2[Q[i]]) + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 3, 6, 7, 4, 5, 1 };
int N = arr.Length;
int[] Q = { 7, 6 };
int M = Q.Length;
// Function Call
minOperations(arr, N, Q, M);
}
}
// This code is contributed by ukasp
输出:
17 11
时间复杂度: O(N + M)
辅助空间: O(N)