当前位置:网站首页>Leetcode 473. Match to square [violence + pruning]
Leetcode 473. Match to square [violence + pruning]
2022-06-13 02:22:00 【JackDawn!?】
subject

Main idea and ideas
The meaning of the title is to give an array , It is required to judge whether the numbers in the array can be divided into 4 Share . First initialize the four edges with the length of 0, Try adding a match to one of the edges in turn . The method of pruning is written in the notes .
Code
class Solution {
int sum;
bool check(int l1, int l2, int l3, int l4, int cur, vector<int>& vec, int left) {
if(l1 > sum || l2 > sum || l3 > sum || l4 > sum) return false; // One edge is greater than the total number of 1/4, Return early false
if(l1+left < sum || l2+left < sum || l3+left<sum || l4+left<sum) return false; // One side plus all the remaining matches is still less than the total 1/4, Return early false
if(cur == vec.size()) {
// Reach the root of the permutation tree
if( l1 == l2 && l2 == l3 && l3 == l4) {
return true; }
else return false;
}
int l = left-vec[cur];
return (
check(l1+vec[cur], l2, l3, l4, cur+1, vec, l) ||
check(l1, l2+vec[cur], l3, l4, cur+1, vec, l) ||
check(l1, l2, l3, l4+vec[cur], cur+1, vec, l) ||
check(l1, l2, l3+vec[cur], l4, cur+1, vec, l)
);
}
static bool cmp(const int& a, const int& b) {
return a > b;
}
public:
// Judge whether it can be quartered
bool makesquare(vector<int>& matchsticks) {
sum = 0;
for(auto i : matchsticks) sum += i;
sum /= 4;
sort(matchsticks.begin(), matchsticks.end(), this->cmp); // First assign a large match , Good for pruning
return check(0, 0, 0, 0, 0, matchsticks, sum*4);
}
};
result
边栏推荐
- [reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP
- [work with notes] MFC solves the problem that pressing ESC and enter will automatically exit
- regular expression
- STM32 external interrupt Usage Summary
- ROS learning-8 pit for custom action programming
- Paipai loan parent company Xinye quarterly report diagram: revenue of RMB 2.4 billion, net profit of RMB 530million, a year-on-year decrease of 10%
- Basic exercises of test questions letter graphics ※
- Share three stories about CMDB
- STM32 steering gear controller
- Basic exercise of test questions Yanghui triangle (two-dimensional array and shallow copy)
猜你喜欢
SQLserver2008 拒绝了对对象 '****' (数据库 '****',架构 'dbo')的 SELECT 权限
[learning notes] xr872 GUI littlevgl 8.0 migration (display part)
Area of basic exercise circle ※
Port mapping between two computers on different LANs (anydesk)
Chapter7-13_ Dialogue State Tracking (as Question Answering)
Share three stories about CMDB
[learning notes] xr872 audio driver framework analysis
[unity] problems encountered in packaging webgl project and their solutions
Chapter7-12_ Controllable Chatbot
【Unity】打包WebGL項目遇到的問題及解决記錄
随机推荐
拍拍贷母公司信也季报图解:营收24亿 净利5.3亿同比降10%
[keras] data of 3D u-net source code analysis py
Application and routine of C language typedef struct
记录:如何解决MultipartFile类的transferTo()上传图片报“系统找不到指定的路径“问题【亲测有效】
[unity] problems encountered in packaging webgl project and their solutions
[pytorch]fixmatch code explanation (super detailed)
Open source video recolor code
Easydl related documents and codes
Think about the possibility of attacking secure memory through mmu/tlb/cache
Anti crawling strategy (IP proxy, setting random sleep time, bilbili video information crawling, obtaining real URLs, processing special characters, processing timestamp, and multithreading)
Understanding and thinking about multi-core consistency
Stm32+ze-08 formaldehyde sensor tutorial
SQL Server 删除数据库所有表和所有存储过程
Swiper horizontal rotation grid
Jump model between mirrors
[reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP
Sensor: MQ-5 gas module measures the gas value (code attached at the bottom)
redis
ROS learning-6 detailed explanation of publisher programming syntax
Linear, integer, nonlinear, dynamic programming