Skip to content

Commit 33fbf09

Browse files
committed
feat: resolved
1 parent a22e922 commit 33fbf09

File tree

1 file changed

+117
-4
lines changed
  • construct_target_array_with_multiple_sums/src

1 file changed

+117
-4
lines changed

construct_target_array_with_multiple_sums/src/lib.rs

+117-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
/*
22
* https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/599/week-2-may-8th-may-14th/3737/
3+
*
4+
* 这道题的核心是使用 max binary heap 而非 vector 来处理数据,避免多次轮训目标数组判断最大数。
5+
*
6+
* 根据题目的特点其实是可以找到线索的:
7+
*
8+
* 1. 关注的是最大值(实施过程中会发现也关注第二大的值,即 max numbers 问题)
9+
* 2. 不关注目标数组的序号,即可以通过其他更好的数据结构处理,忽略目标结构
10+
*
11+
* 另外在性能方面,如果最大值在做完减操作后还是最大的,插入和取出该值的操作没有意义,却会触发两次堆排序。所以可以判断如果处理后的最大值大于第二大的值,那么就继续做减处理。
12+
*
13+
* 除此之外需要处理的都是一些边界问题,如:
14+
*
15+
* * 只有一个数字的时候该怎么处理
316
*/
417

518
use std::collections::BinaryHeap;
@@ -15,10 +28,10 @@ impl<'a> Solution {
1528
}
1629
}
1730
let mut heap = BinaryHeap::new();
18-
let mut sum = 0;
19-
for e in target.iter() {
20-
heap.push(*e);
21-
sum += e;
31+
let mut sum = 0 as i64;
32+
for &e in target.iter() {
33+
heap.push(e as i64);
34+
sum += e as i64;
2235
}
2336
loop {
2437
// 提取堆中最大的数字
@@ -91,4 +104,104 @@ mod test {
91104
let target = vec![2];
92105
assert!(!Solution::is_possible(target));
93106
}
107+
108+
#[test]
109+
fn submission_5() {
110+
let target = vec![
111+
835647834, 503037935, 773002076, 731298404, 903645595, 488189634, 319785391, 111546683,
112+
609144970, 415205491, 685900245, 878508756, 236413773, 991053691, 947595808, 913014938,
113+
533038855, 88376603, 436545178, 983496954, 122530918, 346542007, 55893465, 472984628,
114+
347337093, 322587100, 552866643, 609759356, 288893937, 471774337, 465491640, 783022406,
115+
699817530, 340584553, 663909719, 651419106, 846593366, 952609573, 912379694, 661318302,
116+
538633771, 745093202, 753577352, 60741272, 800245613, 228718955, 314289253, 384902244,
117+
834091366, 330486268, 832528567, 405339553, 667374764, 477631332, 458512135, 281436435,
118+
301856749, 331522322, 439316110, 65782135, 313620054, 377064760, 689776101, 453352404,
119+
739524725, 113039032, 403624252, 864855957, 816177580, 472331359, 702356269, 634580725,
120+
79566086, 723272803, 484129094, 785382934, 519691527, 303358848, 673141033, 900376058,
121+
640090231, 332948759, 3578533, 603932450, 300252459, 455172786, 398327644, 667611961,
122+
579527425, 847780358, 251487657, 239105566, 949519075, 816672375, 569680210, 522034786,
123+
433488047, 339341869, 323607606, 695508020, 671840506, 403376732, 437224135, 704526427,
124+
733331510, 566004060, 190603334, 401309800, 135615700, 480920888, 880495868, 394755529,
125+
754131300, 980354442, 940475050, 455976643, 26150213, 620704469, 305714399, 452126616,
126+
331922227, 285017717, 547688077, 571029451, 580099665, 888037179, 116069830, 492086251,
127+
460673303, 652403652, 529457074, 959765712, 884239314, 707976976, 421820667, 102910899,
128+
850649035, 332557694, 156833539, 193014693, 712889809, 65402492, 81873875, 205826928,
129+
906576787, 536580780, 98764481, 614216242, 724741649, 926963940, 415525579, 707467586,
130+
670685849, 645316339, 663012408, 451540628, 190074574, 100279961, 373676876, 108645392,
131+
439186589, 289025528, 317751557, 696247292, 252536329, 524764647, 664019135, 681731166,
132+
697044386, 721419536, 89117662, 386734310, 933623270, 18288756, 858592177, 843786785,
133+
267433641, 104266328, 816027769, 103604036, 874219314, 626912072, 643973051, 138646542,
134+
930635468, 638385362, 666376936, 75446650, 725532251, 558802301, 898021675, 602462415,
135+
287100257, 103256759, 241469306, 418203100, 359820338, 439745508, 711532895, 913171524,
136+
828285226, 490697952, 94810193, 272869408, 26560972, 979834901, 5721504, 185048349,
137+
42545696, 396115122, 904051513, 220906107, 738993493, 588839845, 742987869, 641939237,
138+
20134580, 613427664, 760712842, 725934923, 233138479, 694808775, 504699750, 45306699,
139+
928921991, 348537469, 341584856, 314823473, 323200569, 5906520, 590908812, 7058731,
140+
612527161, 95929757, 923027538, 504340570, 108267579, 981049825, 3621484, 268758794,
141+
155138552, 967177696, 154587439, 226488698, 435889878, 282062129, 837804113, 874696398,
142+
704916352, 672370979, 429701338, 699214790, 311303195, 947796876, 50395096, 943382310,
143+
264134145, 110060599, 662341190, 707099896, 778109433, 209892183, 129970328, 614555781,
144+
65561327, 723924768, 308208692, 907464540, 511861513, 664166961, 278299804, 383189150,
145+
305144703, 399367644, 745158781, 611505481, 788035592, 958094209, 4691422, 982804694,
146+
182195783, 644444281, 888532003, 913888496, 781449985, 252740240, 661510236, 998487245,
147+
284318090, 646237565, 822471400, 371153017, 988783, 234143485, 906623027, 265667900,
148+
88762375, 654203306, 124205686, 724165887, 715879556, 778678123, 201533689, 241172717,
149+
220243793, 300732194, 834092268, 546274723, 978426161, 544966162, 860580567, 833904274,
150+
408002579, 683573852, 414196298, 567536223, 401758309, 449922489, 264257881, 552000887,
151+
313883439, 121158141, 816111867, 426696045, 166578559, 869183250, 677763442, 449243505,
152+
446613544, 618933881, 765618361, 782239559, 139564825, 17361518, 375149549, 752695711,
153+
170774896, 24223979, 929368291, 714737788, 968883177, 418171397, 28938451, 530535587,
154+
831491933, 388232832, 375471966, 501547932, 42057135, 105907929, 319325198, 866969758,
155+
883623220, 636639049, 98250237, 119670840, 810521959, 994818843, 979182684, 698619283,
156+
306787611, 79779236, 316097105, 20407838, 246365554, 405157606, 746584313, 338447287,
157+
956653822, 157277229, 382734547, 919850823, 17698838, 400783732, 846595424, 820236688,
158+
844188244, 592045378, 664969413, 199985888, 986952695, 924241714, 545487784, 487539172,
159+
147186879, 945151212, 958877753, 669099388, 403471548, 499546716, 915071435, 897865485,
160+
892380510, 328284017, 649646782, 564916899, 126507447, 341928577, 916918420, 335542645,
161+
562151880, 341948589, 153658487, 915169176, 189949851, 956608598, 92763236, 671268832,
162+
829439855, 198464213, 96704707, 504412819, 126161975, 935193455, 513168023, 134286447,
163+
385405295, 615567547, 17991890, 790707341, 991003956, 917307082, 145826213, 725182412,
164+
707571456, 193431873, 694550855, 988821536, 296476226, 166333863, 595537769, 579249117,
165+
788071113, 445822525, 312987370, 690589050, 707167073, 734770753, 836025747, 398024391,
166+
614034356, 19892950, 177961692, 901857208, 570179250, 880745468, 628351955, 471663624,
167+
537067058, 620422588, 801133337, 443608528, 655426152, 630822673, 323931531, 616054005,
168+
715419230, 344542774, 569479396, 46182177, 115731968, 615621055, 927437470, 415304055,
169+
613134814, 593402338, 555887984, 424883671, 737555229, 580850096, 206291958, 408415618,
170+
568495923, 383046806, 331153888, 630013482, 639299672, 932192570, 426968629, 665396997,
171+
377560684, 425576288, 499413833, 304016029, 435936610, 890997684, 37462641, 308478314,
172+
648743689, 968679592, 6563304, 528867706, 815369648, 699526086, 854594514, 77461257,
173+
912978080, 437849059, 584065006, 448033159, 851418436, 275202511, 933127854, 57918210,
174+
936121297, 728315416, 493270959, 635640085, 751603865, 815510463, 322128401, 817518864,
175+
113319096, 719706704, 151675058, 579563270, 817496583, 331259914, 304828788, 832758432,
176+
514693374, 628452536, 859015740, 54698207, 502378937, 925823748, 468799569, 912430738,
177+
503849861, 155092332, 42145830, 16914543, 954818713, 944191613, 753948564, 101131208,
178+
628620169, 514926770, 674609079, 477754503, 226746575, 399775590, 317837974, 685000591,
179+
380154246, 862619940, 156768091, 907663822, 640534228, 509892852, 140342102, 997418526,
180+
552535460, 430179820, 454222315, 646598713, 14409817, 280310343, 324342456, 625965172,
181+
814280682, 974660429, 122025967, 697839974, 290110820, 878936393, 874327021, 252896959,
182+
368474189, 596100537, 759459589, 74112139, 496075772, 660681195, 476919942, 732314798,
183+
881551587, 600771213, 916355905, 62502174, 722694262, 846208181, 235701369, 377851030,
184+
444635218, 905821472, 6007590, 14543647, 806494416, 995077069, 714758118, 844053358,
185+
554781732, 772216007, 970855797, 411569324, 513182664, 137085083, 204095216, 632442670,
186+
843815967, 289982568, 631746100, 825138049, 349162102, 5326115, 207201935, 835205999,
187+
161469959, 312314799, 644367317, 122663749, 602404965, 763996480, 974548439, 732304994,
188+
808596406, 180421152, 975264158, 122323736, 966123769, 50110342, 744171007, 561228509,
189+
513803739, 631885254, 848740278, 38362310, 334317889, 407483295, 704719903, 793514069,
190+
3850010, 47429717, 332085264, 296925009, 179969654, 285965854, 188329006, 353044452,
191+
907132439, 173498729, 971005353, 886796300, 374137584, 167007939, 257311427, 69578518,
192+
986207933, 820577598, 762893591, 855603877, 154159499, 81230719, 988672413, 560039653,
193+
187206037, 124634358, 756546188, 562543979, 382863463, 791317508, 638677331, 472652962,
194+
704857946, 755439531, 209470628, 413904724, 236402946, 980178476, 925780627, 624361832,
195+
986373739, 865597279, 733945791, 40519093, 346073968, 531933636, 452742332, 388931131,
196+
509971886, 203046081, 86591499, 107265603, 170834917, 222658017, 931432329, 545903464,
197+
244840320, 750824817, 665373361, 418717528, 723513553, 572491457, 458224851, 688126547,
198+
872604574, 477404041, 810687226, 902428227, 809180291, 181691685, 593946617, 443990866,
199+
886931195, 671951824, 117916938, 88508493, 101987861, 998221579, 302729532, 637782794,
200+
713816070, 909221425, 478649015, 947515758, 588376423, 102208139, 387839447, 126033549,
201+
266213171, 286274226, 201637730, 911783168, 261943681, 622500013, 456472374, 708804967,
202+
489562877, 836248357, 622073857, 432626926, 157337505, 416320771, 436152162, 592575425,
203+
42633820, 518316330, 29165774, 254298082, 747662083, 309417930, 635421959, 306846172,
204+
];
205+
assert!(!Solution::is_possible(target));
206+
}
94207
}

0 commit comments

Comments
 (0)