分區 主分區 和 擴展分區_等和分區

分區 主分區 和 擴展分區

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.pswp.cn/news/540601.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540601.shtml
英文地址,請注明出處:http://en.pswp.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 […