当前位置:网站首页>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;
}
边栏推荐
- [rust] what software should I use to develop rust? Recommended editors commonly used to support rust
- MySQL creates partition tables and automatically partitions them by day
- QT notes - qtablewidget table spanning tree, qtreewidget tree node generates table content
- Difference between GCC -l parameter and -l parameter
- 如何在IM系统中实现抢红包功能?
- Zhihuihuayun | cluster log dynamic collection scheme
- 源码分析Sentry用户行为记录实现过程
- Chapter 1 Introduction
- CCF 1-2 question answering record (1)
- 08.01 adjacency matrix
猜你喜欢

动态内存管理

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

三、MFC消息映射机制实现原理
![[rust] rust language foundation | you should quickly get an impression when learning a language](/img/70/b420d62759f0c52bf4cc8d2a500813.png)
[rust] rust language foundation | you should quickly get an impression when learning a language

Day3: branch structure

C进阶——数据的存储

Conference publishing function of conference OA project
![[rust] Why do I suggest you learn rust | a preliminary study of rust](/img/33/a5e7d22e87502fa8582920cb34de9f.png)
[rust] Why do I suggest you learn rust | a preliminary study of rust

Jmeter-While控制器

6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!
随机推荐
AcWing 92. 递归实现指数型枚举
Quick check list of various XSS payloads
Experience of redis deepwater area -- Interview reference
利用huggingface模型翻译英文
基于ARM和FPGA的数字示波器设计——QMJ
Dynamic memory management
VMware virtual machine and vSphere migrate to each other
makefile快速使用
CCF 1-2 question answering record (1)
【网络空间安全数学基础第3章】同余
L1-043 阅览室
With the strong development of cloud native, how should enterprises seize business opportunities
Day3: branch structure
08.01 adjacency matrix
Import the data in MariaDB into columnstore
C进阶——数据的存储
SQL multi condition query cannot be implemented
[Commons beanautils topic] 005- convertutils topic
Common formulas and application scenarios of discrete distribution
Do you regret learning it?