给定一个由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
Javascript
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
Javascript
输出:
17 11
时间复杂度: O(N*M)
辅助空间: O(1)
Efficient Approach:优化上述方式,思路是使用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
蟒蛇3
# 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
Javascript
输出:
17 11
时间复杂度: O(N + M)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。