分区 主分区 和 扩展分区_等和分区

分区 主分区 和 扩展分区

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 subset problem.

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

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 <bits/stdc++.h>
using namespace std;
bool equalsubset(vector<int> arr, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
//cout<<sum<<endl;
if (sum % 2 == 1)
return false;
bool dp[n + 1][sum + 1];
memset(dp, false, sizeof(dp));
for (int i = 0; i <= sum; i++)
dp[0][i] = false;
for (int i = 0; i <= n; i++)
dp[i][0] = true;
for (int i = 1; i <= sum; i++) {
for (int j = 1; j <= n; j++) {
if (i >= 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<int> 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: 2
Enter n, number of elements: 5
Enter elements: 3 4 6 2 5
YES
Enter n, number of elements: 6
Enter elements: 1 3 4 8 5 6
NO

翻译自: https://www.includehelp.com/icp/equal-sum-partition.aspx

分区 主分区 和 扩展分区

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540601.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ORACLE 物理读 逻辑读 一致性读 当前模式读总结浅析

在ORACLE数据库中有物理读&#xff08;Physical Reads&#xff09;、逻辑读&#xff08;Logical Reads&#xff09;、一致性读&#xff08;Consistant Get&#xff09;、当前模式读&#xff08;DB Block Gets&#xff09;等诸多概念&#xff0c;如果不理解或混淆这些概念的话&a…

[转载] Java Formatter toString()方法与示例

参考链接&#xff1a; Python | 输出格式化 output format 格式化程序类toString()方法 (Formatter Class toString() method) toString() method is available in java.util package. toString()方法在java.util包中可用。 toString() method is for the string representat…

arm tbh_TBH的完整形式是什么?

arm tbhTBH&#xff1a;说实话 (TBH: To Be Honest) TBH is an abbreviation of "To Be Honest". It is internet slang which generally used as an acronym or hashtag over the internet on social media networking sites like Facebook, Instagram, Twitter, Yo…

异常:fatal: unable to access 'https://git.oschina.net/pcmpcs/library.git/': Could not resolve host...

git fork项目时出现的异常. 原因: 我以前用的是ssh地址做的远程通信地址&#xff0c;而这次是用的是https&#xff0c;因为很久没用&#xff0c;所以忘记了以前是用ssh的了。解决方案一&#xff1a;复制ssh协议的地址&#xff0c;然后再关联远程仓库。并且在VCS下的git下的Rem…

计数器数组_子数组计数

计数器数组Problem statement: 问题陈述&#xff1a; Given an array of N positive integers a1, a2, ..., an. The value of each contiguous subarray of a given array is the maximum element present in that subarray. The task is to return the number of subarrays…

[转载] 列表、元组及通用序列操作

参考链接&#xff1a; Python | 重点数据类型 (字符串&#xff0c;列表&#xff0c;元组&#xff0c;迭代)(String, List, Tuple, Iteration) 序列是Python中最基本的数据结构&#xff08;一些基本特性类似于C中的数组模板类&#xff09;&#xff0c;序列中的每一个元素都有相…

onActivityResult()后onresume()

当你调用完一个存在的activity之后&#xff0c;onActivityResult将会返回以下数据&#xff1a;你调用时发出的requestCode、被调用activity的结果标志resultCode&#xff08;如RESULT_OK&#xff09;和其他的额外数据。我们期望的都是得到RESULT_OK&#xff0c;表示调用成功&am…

java反射用法示例_Java包| 类型,用法,示例

java反射用法示例配套 (Packages) Packages in Java is simply a mechanism to encapsulate (i.e. to put in a short and concise form) a group of classes,interfaces,enumerations, sub packages, etc. In real world, application is developed in such a manner so that …

[转载] python 元组tuple - python基础入门(14)

参考链接&#xff1a; Python元组Tuple 目录 一.元组tuple定义 二.元组tuple查询 三.元组tuple不支持删除/修改数据 四.元组tuple与列表list的相互转换 五.重点总结 在上一篇文章中我们讲解了关于python列表List的相关内容&#xff0c;今天给大家解释一下列表List的…

MaxCompute 2.0—从ODPS到MaxCompute

从ODPS到MaxCompute-阿里大数据的进化之路是一个商用大数据系统发展史&#xff0c;一个商业大数据系统要解决的问题有可靠性&#xff0c;高性能&#xff0c;安全性等等六个方面。内部产品名ODPS的MaxCompute&#xff0c;是阿里巴巴内部发展的一个高效能、低成本&#xff0c;完全…

python数值类型_Python数值类型

python数值类型In programming, Data Types are an essential concept. Data of various types can be stored in variables as per the task we want the variables to perform. 在编程中&#xff0c;数据类型是必不可少的概念。 根据我们希望变量执行的任务&#xff0c;各种类…

[转载] Python高级变量(列表、元组、字典、字符串、公共方法)

参考链接&#xff1a; Python | 重点数据类型 (字符串&#xff0c;列表&#xff0c;元组&#xff0c;迭代)(String, List, Tuple, Iteration) 文章目录 高级变量类型目标知识点回顾 01. 列表1.1 列表的定义1.2 列表常用操作del 关键字&#xff08;科普&#xff09;关键字、函数…

python 操作mongodb数据库参考文档

参考文档链接&#xff1a;https://pypi.python.org/pypi/pymongo pymongo的参考文档http://api.mongodb.com/python/current/tutorial.html mongoengine的参考文档&#xff1a;https://pypi.python.org/pypi/mongoengine#downloads Flask-MongoEngine的参考文档&#xff1a;htt…

php eot eod_EOD的完整形式是什么?

php eot eodEOD&#xff1a;一天结束 (EOD: End Of Day) EOD is an abbreviation of "End Of Day". EOD是“ End Of Day”的缩写 。 It is an expression, which is commonly used in the Gmail platform. In a particular mail, if the sender wants to give the d…

[转载] python元组 tuple

参考链接&#xff1a; Python元组Tuple 类型特点&#xff1a;可以存放多个、 可以重复的&#xff0c;有顺序的数据&#xff0c;数据不可变。 如果项目中需要定义多个数据到一个变量中存放 存放的数据&#xff0c;在项目运行过程中&#xff0c;会发生数据的增加、修改、删除…

aio nio aio_AIO的完整形式是什么?

aio nio aioAIO&#xff1a;多合一 (AIO: All-in-one) AIO is an abbreviation of "all-in-one", which is also known as an MFP (multi-function product/printer/peripheral), multi-functional or multi-function device (MFD). It is a workplace machine that …

[转载] python基础入门二

参考链接&#xff1a; Python集合Set 写代码,有如下变量,请按照要求实现每个功能 &#xff08;共6分&#xff0c;每小题各0.5分&#xff09; name ” aleX” 1)移除 name 变量对应的值两边的空格,并输出处理结果 2) 判断 name 变量对应的值是否以 “al” 开头,并输出结果
…

组合数据类型练习,英文词频统计实例上

1、字典实例&#xff1a;建立学生学号成绩字典&#xff0c;做增删改查遍历操作。 建立&#xff1a; d{0001:99,0003:89,0004:98,0005:100,0006:78} 增&#xff1a;d[0002]79 删&#xff1a;d.pop(0001) 改&#xff1a;d[0004]100 查&#xff1a;print(d[0002]) 遍历操作&#x…

茱莉亚分形_茱莉亚的NaN Constant

茱莉亚分形Julia| NaN / Nan64常数 (Julia | NaN/Nan64 Constant) Nan / Nan64 is a constant of the Float64 type in Julia programming language, it represents "not-a-number" value. Nan / Nan64是Julia编程语言中Float64类型的常量&#xff0c;它表示“非数字…

[转载] Python3 数组

参考链接&#xff1a; Python中的Array | 数组1(简介和功能) 一、list和array的区别 Python的数组通过Numpy包的array实现。 Python里二者最大的区别是&#xff0c;list可以存储不同类型的数据&#xff0c;而array只能存储相同类型的数据。 import numpy #直接定义 a […