当前位置:网站首页>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

边栏推荐
- Superficial understanding of conditional random fields
- json,xml,txt
- Application and examples of C language structure
- Introduction to arm Cortex-M learning
- Application circuit and understanding of BAT54C as power supply protection
- SQL Server 删除数据库所有表和所有存储过程
- Chapter7-11_ Deep Learning for Question Answering (2/2)
- Common web page status return code crawler
- How to learn to understand Matplotlib instead of simple code reuse
- [work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip
猜你喜欢

传感器:MQ-5燃气模块测量燃气值(底部附代码)

Sensorless / inductive manufacturing of brushless motor drive board based on stm32

Sensor: sht30 temperature and humidity sensor testing ambient temperature and humidity experiment (code attached at the bottom)

Chapter7-10_ Deep Learning for Question Answering (1/2)

Paper reading - joint beat and downbeat tracking with recurrent neural networks
![[pytorch] kaggle image classification competition arcface + bounding box code learning](/img/1e/5e921987754da1e1750acdadb36849.jpg)
[pytorch] kaggle image classification competition arcface + bounding box code learning

传感器:SHT30温湿度传感器检测环境温湿度实验(底部附代码)

Armv8-m (Cortex-M) TrustZone summary and introduction

4.11 introduction to firmware image package

Common web page status return code crawler
随机推荐
STM32 timer interrupt learning notes
Fast Color Segementation
[51nod.3210] binary Statistics (bit operation)
Functional translation
An image is word 16x16 words: transformers for image recognition at scale
Basic exercise of test questions Yanghui triangle (two-dimensional array and shallow copy)
Easydl related documents and codes
Record: how to solve the problem of "the system cannot find the specified path" in the picture message uploaded by transferto() of multipartfile class [valid through personal test]
ROS learning-6 detailed explanation of publisher programming syntax
Port mapping between two computers on different LANs (anydesk)
Sqlserver2008 denied select permission on object'***** '(database'*****', schema'dbo')
C language compressed string is saved to binary file, and the compressed string is read from binary file and decompressed.
Mac下搭建MySQL环境
Armv8-m (Cortex-M) TrustZone summary and introduction
[programming idea] communication interface of data transmission and decoupling design of communication protocol
Application and routine of C language typedef struct
STM32 steering gear controller
Parameter measurement method of brushless motor
Linear, integer, nonlinear, dynamic programming
Sensor: MQ-5 gas module measures the gas value (code attached at the bottom)