当前位置:网站首页>Buckle exercise - 32 divided into k equal subsets
Buckle exercise - 32 divided into k equal subsets
2022-07-24 12:12:00 【qq_ forty-three million four hundred and three thousand six hun】
32 Divided into k Equal subsets
1. Problem description
Given an array of integers nums And a positive integer k, Find out if it is possible to divide this array into k A non empty subset , The sum is equal to .
Example 1:
Input : nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output : True
explain : It is possible to divide it into 4 A subset of (5),(1,4),(2,3),(2,3) Equal to the sum .
2. Enter description
First type nums Length of array n,
Then input n It's an integer , Space off .
Last input k.
1 <= k <= n <= 16
0 < nums[i] < 10000
3. The output shows that
Output true or false
4. Example
Input
7
4 3 2 3 5 2 1
4
Output
true
5. Code
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<stack>
using namespace std;
bool backtracking(vector<int>nums, vector<int>edges, int len, int index)
{
if (index == nums.size())
return true;
for (int i = 0; i < edges.size(); i++)
{
if (nums[index] + edges[i] > len)
continue;
if (i > 0 && edges[i] == edges[i - 1])
continue;
edges[i] += nums[index];
if (backtracking(nums, edges, len, index + 1))
return true;
edges[i] -= nums[index];// to flash back
}
return false;
}
bool canPartitionKSubsets(vector<int>& nums, int k)
{
//1. The total length is less than k
if (nums.size() < k)
return false;
//2. Calculate the total length len ,len Must be k Integer multiple
int len = 0;
for (int num : nums)
len += num;
if (len%k != 0)
return false;
//3.
int single_length = len / k;// Calculate the length of a single interval
vector<int>edges(k);
sort(nums.begin(), nums.end(), greater<int>());// Sort from large to small
//4. to flash back
return backtracking(nums, edges, single_length, 0);
}
int main()
{
vector<int>nums;
int n, k,temp;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> temp;
nums.push_back(temp);
}
cin >> k;
bool res = canPartitionKSubsets(nums, k);
cout << (res ? "true" : "false") << endl;
return 0;
}
边栏推荐
- QT notes - EventFilter event filter
- C Advanced - data storage
- 字符串匹配的KMP
- Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]
- Record a garbage collection and analysis of gceasy
- Acwing 92. recursive implementation of exponential enumeration
- One week's wonderful content sharing (issue 13)
- Threat hunting collection
- Install MariaDB columnstore (version 10.3)
- Collision, removal and cleaning
猜你喜欢

Source code analysis sentry user behavior record implementation process

Qt5.12 + vs2019 cannot locate the program input point in the dynamic link library

08.01 adjacency matrix

Use of multithreading in QT

MySQL advanced (XVII) cannot connect to database server problem analysis

Design of digital oscilloscope based on arm and FPGA -- QMJ

JMeter while controller

How to use a third party without obtaining root permission topic: MIUI chapter

三、MFC消息映射机制实现原理

Zhihuihuayun | cluster log dynamic collection scheme
随机推荐
CCF 201803_ 1 jump jump
Skillfully using command line parameters in Delphi to realize the trigger function of dragging files onto program icons
Dynamic memory management
One of his birds sold for 60million -- the collection of eight mountain people in the Ming and Qing Dynasties
L1-043 reading room
Mysql database
【C和指针第14章】预处理器
[mathematical basis of Cyberspace Security Chapter 3] congruence
TypeNameExtractor could not be found
MySQL advanced (XVII) cannot connect to database server problem analysis
容错、熔断的使用与扩展
JMeter while controller
Examples of map search
Browser logic vulnerability collection
Summary of MySQL database combined with actual SQL optimization of the project
Convergence rules for 4 * 4 image weights
Differences between JS map and foreach
An analysis of the CPU surge of an RFID tag management system in.Net
计算两个坐标经纬度之间的距离(5种方式)
基于ARM和FPGA的数字示波器设计——QMJ