分区 主分区 和 扩展分区

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

分区 主分区 和 扩展分区

分区 主分区 和 扩展分区_等和分区相关推荐

  1. linux mapper 分区,device-mapper – 如何正确“扩展”linux设备映射器分区?

    我缺少 linux磁盘管理的一些概念,我在单个物理卷上有可用空间,我想扩展ext4文件系统,这是一个逻辑卷. 我看一下fdisk -l – 这是一个GPT(警告)@H_301_3@Disk /dev/ ...

  2. d盘不能扩展卷_一篇看懂!Linux磁盘的管理(分区、格式化、挂载),LVM逻辑卷,RAID磁盘阵列...

    Linux中磁盘的管理(分区.格式化.挂载),LVM逻辑卷,RAID磁盘阵列 一.认识磁盘 1.什么是磁盘: 磁盘是一种计算机的外部存储器设备,由一个或多个覆盖有磁性材料的铝制或玻璃制的碟片组成,用来 ...

  3. Linux挂载磁盘和磁盘分区及设置开机自动挂载_亲测成功

    Linux挂载磁盘和磁盘分区及设置开机自动挂载_亲测成功 Linux下磁盘分区命令主要由两个: fdisk :最大支持不超过2T,MBR分区: parted :支持GPT,适用于大容量分区: 如果挂载 ...

  4. Linux系统扩展oracle数据库所在的分区

    实验环境 系统:Centos7 装机采用lvm卷方式分区,将oracle数据库部署在了/home/oracle分区,后期发现磁盘空间不够用,添加新硬盘将分区/home/oracle扩容. 查看系统空间 ...

  5. 如何共享计算机磁盘,扩展群集共享磁盘的分区 - Windows Server | Microsoft Docs

    如何扩展群集共享磁盘的分区 10/19/2020 本文内容 本文介绍如何在基础硬件 RAID 支持容量扩展技术时向群集添加额外的存储容量. 适用于:  Windows Server 2012R2 原始 ...

  6. 分区助手扩大c盘后自动修复_分区助手怎么扩大c盘调整c盘的。

    很多朋友在购买电脑装机的时候,磁盘分区的大小一般都是由装机人员设定的,很多时候买家并没有在意这一点,但我们使用的时候就会发现大小不一,存储的时候很麻烦,甚至都有重新装机分盘的念头,别担心,小凡为您介绍 ...

  7. SQLServer 表分区 根据时间自动扩展分区

    SQLServer 表分区 根据时间自动扩展分区 前提条件 创建文件组 创建文件并且将文件加入文件组 修改分区方案 修改分区函数 创建存储过程 利用各种定时任务执行以上存储过程 前提条件 已经创建好分 ...

  8. 4T移动硬盘 分区_移动硬盘怎么分区 移动硬盘分区三种可靠方法汇总

    移动硬盘刚买到的时候是没有分区的,也就是说整个硬盘就一个区,所以我们要进行分区,有人会问为什么要分区呢,其实移动硬盘会随着增删文件而产生文件碎片,分区可以防止硬盘错误和文件碎片等原因产生的资料丢失,另 ...

  9. 3t硬盘分区 Linux win,大师为你解说3t硬盘分区【搞定步骤】_

    win7系统有很多人都喜欢使用,我们操作的过程中常常会碰到win7系统3t硬盘分区的问题.如果遇到win7系统3t硬盘分区的问题该怎么办呢?很多电脑水平薄弱的网友不知道win7系统3t硬盘分区究竟该怎 ...

最新文章

  1. JAVA CP936编码转utf8_对一个目录的文件从cp936转换成utf-8
  2. IntelliJ IDEA 12 创建Web项目 教程 超详细版
  3. svn没有右键菜单的解决方案
  4. 小白袍 -- Chapter 1 Java中的Encode与Decode
  5. ARM裸机篇--按键中断
  6. Deltix Round, Spring 2021 D. Love-Hate 随机化 + sos dp(高维前缀和)
  7. 飞畅科技-工业以太网交换机的差异性
  8. 字符设备驱动高级篇1——新接口介绍
  9. linux内核head.S文件分析
  10. 演练 base调用父类的方法 c# 1613713591
  11. Common Techniques to Improve Shadow Depth Maps
  12. 对课程第二次作业的补充与反馈
  13. Unity中的Time
  14. ME525+ Defy+ 刷机指南[zz]
  15. 什么是游戏盾,如何使用
  16. 看_那人好像一个产品狗_对_这就是产品狗
  17. 分享40个主机域名PHP源码,总有一款适合你
  18. 二层交换配置完ping失败_在三层交换机和二层交换机做怎么配置使这两台主机ping通...
  19. 数据库连接超时的处理
  20. 广度搜索与深度搜索的区别

热门文章

  1. springmvc中使用MockMvc测试controller
  2. poj 1724ROADS(bfs和dfs做法)
  3. (2021) 22 [持久化] 1-Bit的存储
  4. php源码安装配置,php源码安装时configure配置参数 | 学步园
  5. python 判断数据类型,是否与已知相同
  6. 百度MIP移动页面加速——不只是CDN
  7. 分块编码(Transfer-Encoding: chunked)VS Content-length
  8. Linux查看系统cpu个数、核心书、线程数
  9. Salesforce宣布5.82亿美元收购文件编辑公司Quip
  10. Oracle Linux 6.5 RPM安装Mysql 5.7.11