当前位置:网站首页>Leetcode 698 partition to K equal sum subsets (DFS pruning)
Leetcode 698 partition to K equal sum subsets (DFS pruning)
2022-06-11 01:43:00 【_ TCgogogo_】
Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4,3,2,3,5,2,1], k = 4 Output: true Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Example 2:
Input: nums = [1,2,3,4], k = 3 Output: false
Constraints:
1 <= k <= nums.length <= 161 <= nums[i] <= 104- The frequency of each element is in the range
[1, 4].
Topic link :https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
The main idea of the topic :n A number asks if it can be divided into k And equal portions
Topic analysis : Classic search plus pruning problem , Two situations that definitely don't work :1. Sum is not k Multiple ,2. The maximum value is larger than the mean value . Before searching, you need to sort the array from large to small , Because numbers with large values are less flexible , It is faster to encounter a situation without a solution than a small number , Because the subject restriction clearly indicates that the frequency of each number is 1 To 4, When it comes to i When making a decision with a number , If i-1 No choice , And nums[i] and nums[i - 1] equal , The first i A position must not be chosen
4ms, Time beats 75.5%
class Solution {
public boolean dfs(int pos, int curSum, int each, int curSz, int k, int[] nums, boolean[] vis) {
if (curSz == k) {
return true;
}
for (int i = pos; i < nums.length; i++) {
if (i > 0 && !vis[i - 1] && nums[i] == nums[i - 1]) {
continue;
}
if (!vis[i] && curSum + nums[i] <= each) {
vis[i] = true;
if (curSum + nums[i] == each) {
if (dfs(0, 0, each, curSz + 1, k, nums, vis)) {
return true;
}
} else {
if (dfs(i + 1, curSum + nums[i], each, curSz, k, nums, vis)) {
return true;
}
}
vis[i] = false;
}
}
return false;
}
public boolean canPartitionKSubsets(int[] nums, int k) {
Arrays.sort(nums);
int sum = 0, n = nums.length;
for (int i = 0; i < n / 2; i++) {
int tmp = nums[i];
nums[i] = nums[n - i - 1];
nums[n - i - 1] = tmp;
}
for (int num : nums) {
sum += num;
}
if (sum % k != 0) {
return false;
}
int each = sum / k;
if (nums[0] > each) {
return false;
}
boolean[] vis = new boolean[n];
return dfs(0, 0, each, 0, k, nums, vis);
}
}边栏推荐
- 【ROS】ROSmsg cakin_ Make compilation error
- Introduction to the application process of Shenzhen China Patent Award, with a subsidy of 1million yuan
- 中间件_Redis_05_Redis的持久化
- 1.4PX4程序下载
- OCR文字识别经典论文详解
- Linux安装mysql数据库详解
- A tutorial on building a website from scratch with complete steps (7000 words and 102 screenshots for everyone to understand, with source code attached)
- SAS principal component analysis (finding correlation matrix, eigenvalue, unit eigenvector, principal component expression, contribution rate and cumulative contribution rate, and data interpretation)
- How to download web photos
- Web3生态去中心化金融平台——Sealem Finance
猜你喜欢

threejs:流光效果封装

Projet Visualisation et analyse des données sur les épidémies basées sur le Web crawler

Threejs: pit encountered in drawing Bessel curve with two-point coordinates

How to download web photos

Web3 ecological decentralized financial platform sealem Finance

项目_基于网络爬虫的疫情数据可视化分析
![[path planning] week 1: hodgepodge](/img/80/074b847c6826b306318aeb9866a829.jpg)
[path planning] week 1: hodgepodge

threejs:如何获得几何体的boundingBox?

SAS主成分分析(求相关阵,特征值,单位特征向量,主成分表达式,贡献率和累计贡献率以及进行数据解释)

条码固定资产管理系统的作用,固定资产条码化管理
随机推荐
Conda安装Pytorch后numpy出现问题
Inventory management and strategy mode
"It looks like robbing tickets but actually robbing money". Don't be fooled by fancy ticket robbing products again and again
[VBA Script] extract the information and pending status of all annotations in the word document
2021-3-1MATLAB写cnn的mnist数据库训练
Using completabilefuture
项目_基于网络爬虫的疫情数据可视化分析
关于mobx
Multipartfile and file interoperation tool classes
Be careful, these hidden "bugs" of "array" to "collection"“
SAS factor analysis (proc factor process, factor rotation and regression method for factor score function)
Threejs: streamer effect encapsulation
中间件_Redis_06_Redis的事务
[chess life] 01 life is like chess
[path planning] week 1: Path Planning open source code summary (ROS) version
Bad RequestThis combination of host and port requires TLS.
I was so excited about the college entrance examination in 2022
Project_ Visual analysis of epidemic data based on Web Crawler
1.7、PX4遥控器校准
How to download web photos