给定两个数组price [] , type []和一个整数S ,任务是检查是否可以在不超过总价格S的情况下从两个不同的类别中选择两个项目。type []数组中的每个元素表示商品的类别。第i个元素, prices []数组中的每个元素都表示第i个元素的价格。
例子:
Input: S = 10, type[] = {0, 1, 1, 0}, prices[] = {3, 8, 6, 5}
Output: Yes
Explanation:
Elements prices[0] and prices[2] can be selected
Total prices = prices[0] + prices[2] = 3 + 6 = 9
Input: S = 15, type[] = {0, 1, 1, 0}, prices[] = {5, 7, 6, 5}
Output: No
Explanation:
There is no possible solution such that total price is less than 15.
方法:想法是使用两个嵌套循环迭代选择每个可能的对。对于每对货币对,检查其类别是否不同且总价格是否小于S;如果是,则可以选择两个项目,否则就没有这样的货币对项目。
下面是上述方法的实现:
C++
// C++ implementation to check if
// two items can be selected from
// two different categories without
// exceeding the total price
#include
using namespace std;
// Function to check if
// two items can be selected from
// two different categories without
// exceeding the total price
string check(int S, int prices[],
int type[], int n)
{
// Loop to choose two different
// pairs using two nested loops
for (int j = 0; j < n; j++) {
for (int k = j + 1; k < n; k++) {
// Condition to check if the price
// of these two elements is less than S
if ((type[j] == 0 && type[k] == 1)
|| (type[j] == 1 && type[k] == 0)) {
if (prices[j] + prices[k] <= S) {
return "Yes";
}
}
}
}
return "No";
}
int main()
{
int prices[] = { 3, 8, 6, 5 };
int type[] = { 0, 1, 1, 0 };
int S = 10;
int n = 4;
// Function Call
cout << check(S, prices, type, n);
return 0;
}
Java
// Java implementation to check if
// two items can be selected from
// two different categories without
// exceeding the total price
import java.util.*;
class GFG{
// Function to check if
// two items can be selected from
// two different categories without
// exceeding the total price
static String check(int S, int prices[],
int type[], int n)
{
// Loop to choose two different
// pairs using two nested loops
for (int j = 0; j < n; j++)
{
for (int k = j + 1; k < n; k++)
{
// Condition to check if the price
// of these two elements is less than S
if ((type[j] == 0 && type[k] == 1) ||
(type[j] == 1 && type[k] == 0))
{
if (prices[j] + prices[k] <= S)
{
return "Yes";
}
}
}
}
return "No";
}
// Driver Code
public static void main(String[] args)
{
int prices[] = { 3, 8, 6, 5 };
int type[] = { 0, 1, 1, 0 };
int S = 10;
int n = 4;
// Function Call
System.out.print(check(S, prices, type, n));
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 implementation to check if
# two items can be selected from
# two different categories without
# exceeding the total price
# Function to check if
# two items can be selected from
# two different categories without
# exceeding the total price
def check(S, prices, type1, n):
# Loop to choose two different
# pairs using two nested loops
for j in range(0, n):
for k in range(j + 1, n):
# Condition to check if the price
# of these two elements is less than S
if ((type1[j] == 0 and type1[k] == 1) or
(type1[j] == 1 and type1[k] == 0)):
if (prices[j] + prices[k] <= S):
return "Yes";
return "No";
# Driver Code
prices = [ 3, 8, 6, 5 ];
type1 = [ 0, 1, 1, 0 ];
S = 10;
n = 4;
# Function Call
print(check(S, prices, type1, n));
# This code is contributed by Code_Mech
C#
// C# implementation to check if
// two items can be selected from
// two different categories without
// exceeding the total price
using System;
class GFG{
// Function to check if two items
// can be selected from two
// different categories without
// exceeding the total price
static String check(int S, int []prices,
int []type, int n)
{
// Loop to choose two different
// pairs using two nested loops
for(int j = 0; j < n; j++)
{
for(int k = j + 1; k < n; k++)
{
// Condition to check if the price
// of these two elements is less than S
if ((type[j] == 0 && type[k] == 1) ||
(type[j] == 1 && type[k] == 0))
{
if (prices[j] + prices[k] <= S)
{
return "Yes";
}
}
}
}
return "No";
}
// Driver Code
public static void Main(String[] args)
{
int []prices = { 3, 8, 6, 5 };
int []type = { 0, 1, 1, 0 };
int S = 10;
int n = 4;
// Function call
Console.Write(check(S, prices, type, n));
}
}
// This code is contributed by sapnasingh4991
输出:
Yes