博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分区 主分区 和 扩展分区_等和分区
阅读量:2533 次
发布时间:2019-05-11

本文共 5531 字,大约阅读时间需要 18 分钟。

分区 主分区 和 扩展分区

Description:

描述:

This is a popular interview coding problem which has been featured in interview rounds of Amazon, Oyo rooms, Adobe.

这是一个受欢迎的采访编码问题,已在亚马逊,Oyo房间,Adobe的采访回合中出现。

Problem statement:

问题陈述:

Given a set of numbers, check whether it can be partitioned into two subsets or not such that the sum of elements in both subsets is same.

给定一组数字,请检查是否可以将其划分为两个子集,以使两个子集中的元素之和相同。

Input:

输入:

First line contains N, the number of elements in the set and the second line contains the elements of the set.

第一行包含N ,集合中元素的数量,第二行包含该集合的元素。

Output:

输出:

Print YES if the given set can be partitioned into two subsets such that the sum of elements in both subsets is equal, else print NO.

YES打印如果给定的集可被划分成两个子集,使得元件在两个子集之和是相等的,否则打印NO。

Example with explanation:

带有说明的示例:

N=5    Elements are:    3, 4, 6, 2, 5    Output: Yes    The set can be partitioned into two subsets with equal sum,    Which is,     subset1: {3, 2, 5} with sum 10    subset2: {4, 6} with sum 10    Another example can be,    N=6    Elements are;    1, 3, 4, 8, 5, 6    Output would be NO since there is no way to do so.

Solution Approach:

解决方法:

We would first check the recursive solution.

我们将首先检查递归解决方案。

Let's create two subset initially where the first subset contains all the elements and the second one is an empty one.

首先创建两个子集,其中第一个子集包含所有元素,第二个子集为空元素。

We calculate the total sum and our function is:

我们计算总和,我们的函数是:

bool EqualPartition(index,subset1Sum,subset2Sum)

So, initially the function call will be

因此,最初的函数调用将是

bool EqualPartition(n-1,total_sum,0)

Where n-1= index of last element, which is index

其中n-1 =最后一个元素的 索引 ,即索引

total_sum= total sum of all elements, which is subset1Sum

total_sum =所有元素的总和 ,即subset1Sum

0= subset2Sum initially

0 =子集2初始总和

Now, our idea is to check by either including the indexed element in subset2 or by not including. And we will continue doing this recursively until we reach our base case.

现在,我们的想法是通过将索引元素包括在subset2中或不包括进行检查。 我们将继续递归执行此操作,直到达到基本情况为止。

Let's see the function definition:

让我们看一下函数定义:

Function    EqualPartition(index,subset1Sum,subset2Sum):        if subset1Sum==subset2Sum //our objective to find            return true        if index<0            return false        return  EqualPartition(index-1,subset1Sum-arr[index],subset2Sum+arr[index]) ||                 EqualPartition(index-1,subset1Sum,subset2Sum)End Function

So the recursive definition consists of the case what we discussed above.

因此,递归定义由我们上面讨论的情况组成。

EqualPartition(index-1,subset1Sum-arr[index],subset2Sum+arr[index]) = add the current element(arr[index]) to subset2     EqualPartition(index-1,subset1Sum,subset2Sum) = ignore the current element and recur for other elements

So this recursive definition will generate a recursion tree where we can find many overlapping sub problems, hence we would solve by dynamic programing. The solution approach is similar to .

因此,此递归定义将生成一个递归树,在其中可以找到许多重叠的子问题,因此可以通过动态编程来解决。 解决方法类似于 。

So we have to create the DP table and fill up the table as per the solution approach in this article.

因此,我们必须根据本文中的解决方案方法创建DP表并填写该表。

So, we have dp[n+1][sum+1] filled up now.

因此,我们现在已经填充了dp [n + 1] [sum + 1]

sum = total sum of elements

总和 =元素总和

How can we utilize this piece of information as our solution?

我们如何利用这些信息作为我们的解决方案?

Not too tough. If dp[i][sum/2] is true for i= 1 to n, it ensures that we have a subset which sums (sum/2) . Thus the remaining subset will have to be also of sum (sum/2).

不太难。 如果dp [i] [sum / 2]对于i = 1到ntrue ,则确保我们有一个总和(sum / 2)的子集。 因此,剩余的子集也将必须是和(sum / 2)

This means we can have two equal sum subset.

这意味着我们可以有两个相等的和子集。

Now, the point is what if (sum) is odd.

现在,关键是如果( sum )是奇数

Check our second example.

检查我们的第二个例子。

Elements are: 1, 3, 4, 8, 5, 6

Sum=27 which is odd.
(sum/2)=13 with integer division.
dp[6][13] = true as 8,5 sums to 13.

元素是: 1、3、4、8、5、6

总和= 27 ,这很奇怪。
(sum / 2)= 13 (整数除法)。
dp [6] [13] = true,因为8,5等于13。

So we would get output YES but is it the solution?

因此,我们将输出为YES,但这是解决方案吗?

What's the catch then?

那有什么收获呢?

The catch is if sum is odd, the answer will be always NO. You can't partition in two equal subsets.

问题是,如果总和奇数 ,答案将始终为 。 您不能分为两个相等的子集。

So before doing anything, just check whether the total sum is odd or not. If the sum is odd simply return false else proceed with the further DP. This would optimize time too.

因此,在执行任何操作之前,只需检查总和是否为奇数 。 如果总和是奇数,则简单地返回false,否则继续下一个DP。 这也会优化时间。

C++ Implementation:

C ++实现:

#include 
using namespace std;bool equalsubset(vector
arr, int n){
int sum = 0; for (int i = 0; i < n; i++) sum += arr[i]; //cout<
<
= arr[j - 1]) dp[j][i] = dp[j - 1][i] | dp[j - 1][i - arr[j - 1]]; else dp[j][i] = dp[j - 1][i]; } } for (int i = 1; i <= n; i++) if (dp[i][sum / 2]) return true; return false;}int main(){
int t, n, item; cout << "Enter number of test cases: "; cin >> t; for (int i = 0; i < t; i++) {
cout << "Enter n, number of elements: "; cin >> n; vector
a; cout << "Enter elements: "; for (int j = 0; j < n; j++) {
cin >> item; a.push_back(item); } if (equalsubset(a, n)) cout << "YES\n"; else cout << "NO\n"; } return 0;}

Output

输出量

Enter number of test cases: 2Enter n, number of elements: 5Enter elements: 3 4 6 2 5YESEnter n, number of elements: 6Enter elements: 1 3 4 8 5 6NO

翻译自:

分区 主分区 和 扩展分区

转载地址:http://bitzd.baihongyu.com/

你可能感兴趣的文章
安装php扩展
查看>>
mysql统计某一个数据库中有几张表
查看>>
百度移动搜索主要有如下几类结果构成
查看>>
梦断代码最后4章读后感
查看>>
python学习---字符串
查看>>
Python爬虫面试题170道:2019版【1】
查看>>
JavaBean规范
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
简单的栈和队列
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
点语法
查看>>
IO之Socket网络编程
查看>>
PCRE demo【转】
查看>>
矩阵的SVD分解
查看>>
gitlab的配置
查看>>