|
| 1 | +/* |
| 2 | + * ## 题目 |
| 3 | + * |
| 4 | + * * https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/599/week-2-may-8th-may-14th/3737/ |
| 5 | + * * https://leetcode.com/problems/construct-target-array-with-multiple-sums/ |
| 6 | + * |
| 7 | + * ## 分析 |
| 8 | + * |
| 9 | + * 这道题的核心是使用 max binary heap 而非 vector 来处理数据,避免多次轮训目标数组判断最大数。 |
| 10 | + * |
| 11 | + * 根据题目的特点其实是可以找到线索的: |
| 12 | + * |
| 13 | + * 1. 关注的是最大值(实施过程中会发现也关注第二大的值,即 max numbers 问题) |
| 14 | + * 2. 不关注目标数组的序号,即可以通过其他更好的数据结构处理,忽略目标结构 |
| 15 | + * |
| 16 | + * 另外在性能方面,如果最大值在做完减操作后还是最大的,插入和取出该值的操作没有意义,却会触发两次堆排序。所以可以判断如果处理后的最大值大于第二大的值,那么就继续做减处理。 |
| 17 | + * |
| 18 | + * 除此之外需要处理的都是一些边界问题,如: |
| 19 | + * |
| 20 | + * * 只有一个数字的时候该怎么处理 |
| 21 | + */ |
| 22 | + |
| 23 | +use std::collections::BinaryHeap; |
| 24 | + |
| 25 | +pub struct Solution {} |
| 26 | + |
| 27 | +impl<'a> Solution { |
| 28 | + pub fn is_possible(target: Vec<i32>) -> bool { |
| 29 | + if target.len() == 1 { |
| 30 | + match target.iter().nth(0) { |
| 31 | + Some(1) => return true, |
| 32 | + _ => return false, |
| 33 | + } |
| 34 | + } |
| 35 | + let mut heap = BinaryHeap::new(); |
| 36 | + let mut sum = 0 as i64; |
| 37 | + for &e in target.iter() { |
| 38 | + heap.push(e as i64); |
| 39 | + sum += e as i64; |
| 40 | + } |
| 41 | + loop { |
| 42 | + // 提取堆中最大的数字 |
| 43 | + let mut max = heap.pop().unwrap(); |
| 44 | + if max == 1 { |
| 45 | + return true; |
| 46 | + } |
| 47 | + let second_max = *heap.peek().unwrap(); |
| 48 | + loop { |
| 49 | + // 处理最大数字直到它不是最大的 |
| 50 | + let stuff = 2 * max - sum; // 计划插入对象 |
| 51 | + if stuff < 1 { |
| 52 | + return false; |
| 53 | + } else if stuff == 1 && second_max == 1 { |
| 54 | + return true; |
| 55 | + } |
| 56 | + sum -= max - stuff; |
| 57 | + if stuff < second_max { |
| 58 | + heap.push(stuff); |
| 59 | + break; |
| 60 | + } |
| 61 | + max = stuff; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(test)] |
| 68 | +mod test { |
| 69 | + use super::*; |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn example_1() { |
| 73 | + let target = vec![9, 3, 5]; |
| 74 | + assert!(Solution::is_possible(target)); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn example_2() { |
| 79 | + let target = vec![1, 1, 1, 2]; |
| 80 | + assert!(!Solution::is_possible(target)); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn example_3() { |
| 85 | + let target = vec![8, 5]; |
| 86 | + assert!(Solution::is_possible(target)); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn submission_1() { |
| 91 | + let target = vec![5, 2]; |
| 92 | + assert!(Solution::is_possible(target)); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn submission_2() { |
| 97 | + let target = vec![9, 9, 9]; |
| 98 | + assert!(!Solution::is_possible(target)); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn submission_3() { |
| 103 | + let target = vec![1, 1, 61, 9, 17]; |
| 104 | + assert!(Solution::is_possible(target)); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn submission_4() { |
| 109 | + let target = vec![2]; |
| 110 | + assert!(!Solution::is_possible(target)); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn submission_5() { |
| 115 | + let target = vec![ |
| 116 | + 835647834, 503037935, 773002076, 731298404, 903645595, 488189634, 319785391, 111546683, |
| 117 | + 609144970, 415205491, 685900245, 878508756, 236413773, 991053691, 947595808, 913014938, |
| 118 | + 533038855, 88376603, 436545178, 983496954, 122530918, 346542007, 55893465, 472984628, |
| 119 | + 347337093, 322587100, 552866643, 609759356, 288893937, 471774337, 465491640, 783022406, |
| 120 | + 699817530, 340584553, 663909719, 651419106, 846593366, 952609573, 912379694, 661318302, |
| 121 | + 538633771, 745093202, 753577352, 60741272, 800245613, 228718955, 314289253, 384902244, |
| 122 | + 834091366, 330486268, 832528567, 405339553, 667374764, 477631332, 458512135, 281436435, |
| 123 | + 301856749, 331522322, 439316110, 65782135, 313620054, 377064760, 689776101, 453352404, |
| 124 | + 739524725, 113039032, 403624252, 864855957, 816177580, 472331359, 702356269, 634580725, |
| 125 | + 79566086, 723272803, 484129094, 785382934, 519691527, 303358848, 673141033, 900376058, |
| 126 | + 640090231, 332948759, 3578533, 603932450, 300252459, 455172786, 398327644, 667611961, |
| 127 | + 579527425, 847780358, 251487657, 239105566, 949519075, 816672375, 569680210, 522034786, |
| 128 | + 433488047, 339341869, 323607606, 695508020, 671840506, 403376732, 437224135, 704526427, |
| 129 | + 733331510, 566004060, 190603334, 401309800, 135615700, 480920888, 880495868, 394755529, |
| 130 | + 754131300, 980354442, 940475050, 455976643, 26150213, 620704469, 305714399, 452126616, |
| 131 | + 331922227, 285017717, 547688077, 571029451, 580099665, 888037179, 116069830, 492086251, |
| 132 | + 460673303, 652403652, 529457074, 959765712, 884239314, 707976976, 421820667, 102910899, |
| 133 | + 850649035, 332557694, 156833539, 193014693, 712889809, 65402492, 81873875, 205826928, |
| 134 | + 906576787, 536580780, 98764481, 614216242, 724741649, 926963940, 415525579, 707467586, |
| 135 | + 670685849, 645316339, 663012408, 451540628, 190074574, 100279961, 373676876, 108645392, |
| 136 | + 439186589, 289025528, 317751557, 696247292, 252536329, 524764647, 664019135, 681731166, |
| 137 | + 697044386, 721419536, 89117662, 386734310, 933623270, 18288756, 858592177, 843786785, |
| 138 | + 267433641, 104266328, 816027769, 103604036, 874219314, 626912072, 643973051, 138646542, |
| 139 | + 930635468, 638385362, 666376936, 75446650, 725532251, 558802301, 898021675, 602462415, |
| 140 | + 287100257, 103256759, 241469306, 418203100, 359820338, 439745508, 711532895, 913171524, |
| 141 | + 828285226, 490697952, 94810193, 272869408, 26560972, 979834901, 5721504, 185048349, |
| 142 | + 42545696, 396115122, 904051513, 220906107, 738993493, 588839845, 742987869, 641939237, |
| 143 | + 20134580, 613427664, 760712842, 725934923, 233138479, 694808775, 504699750, 45306699, |
| 144 | + 928921991, 348537469, 341584856, 314823473, 323200569, 5906520, 590908812, 7058731, |
| 145 | + 612527161, 95929757, 923027538, 504340570, 108267579, 981049825, 3621484, 268758794, |
| 146 | + 155138552, 967177696, 154587439, 226488698, 435889878, 282062129, 837804113, 874696398, |
| 147 | + 704916352, 672370979, 429701338, 699214790, 311303195, 947796876, 50395096, 943382310, |
| 148 | + 264134145, 110060599, 662341190, 707099896, 778109433, 209892183, 129970328, 614555781, |
| 149 | + 65561327, 723924768, 308208692, 907464540, 511861513, 664166961, 278299804, 383189150, |
| 150 | + 305144703, 399367644, 745158781, 611505481, 788035592, 958094209, 4691422, 982804694, |
| 151 | + 182195783, 644444281, 888532003, 913888496, 781449985, 252740240, 661510236, 998487245, |
| 152 | + 284318090, 646237565, 822471400, 371153017, 988783, 234143485, 906623027, 265667900, |
| 153 | + 88762375, 654203306, 124205686, 724165887, 715879556, 778678123, 201533689, 241172717, |
| 154 | + 220243793, 300732194, 834092268, 546274723, 978426161, 544966162, 860580567, 833904274, |
| 155 | + 408002579, 683573852, 414196298, 567536223, 401758309, 449922489, 264257881, 552000887, |
| 156 | + 313883439, 121158141, 816111867, 426696045, 166578559, 869183250, 677763442, 449243505, |
| 157 | + 446613544, 618933881, 765618361, 782239559, 139564825, 17361518, 375149549, 752695711, |
| 158 | + 170774896, 24223979, 929368291, 714737788, 968883177, 418171397, 28938451, 530535587, |
| 159 | + 831491933, 388232832, 375471966, 501547932, 42057135, 105907929, 319325198, 866969758, |
| 160 | + 883623220, 636639049, 98250237, 119670840, 810521959, 994818843, 979182684, 698619283, |
| 161 | + 306787611, 79779236, 316097105, 20407838, 246365554, 405157606, 746584313, 338447287, |
| 162 | + 956653822, 157277229, 382734547, 919850823, 17698838, 400783732, 846595424, 820236688, |
| 163 | + 844188244, 592045378, 664969413, 199985888, 986952695, 924241714, 545487784, 487539172, |
| 164 | + 147186879, 945151212, 958877753, 669099388, 403471548, 499546716, 915071435, 897865485, |
| 165 | + 892380510, 328284017, 649646782, 564916899, 126507447, 341928577, 916918420, 335542645, |
| 166 | + 562151880, 341948589, 153658487, 915169176, 189949851, 956608598, 92763236, 671268832, |
| 167 | + 829439855, 198464213, 96704707, 504412819, 126161975, 935193455, 513168023, 134286447, |
| 168 | + 385405295, 615567547, 17991890, 790707341, 991003956, 917307082, 145826213, 725182412, |
| 169 | + 707571456, 193431873, 694550855, 988821536, 296476226, 166333863, 595537769, 579249117, |
| 170 | + 788071113, 445822525, 312987370, 690589050, 707167073, 734770753, 836025747, 398024391, |
| 171 | + 614034356, 19892950, 177961692, 901857208, 570179250, 880745468, 628351955, 471663624, |
| 172 | + 537067058, 620422588, 801133337, 443608528, 655426152, 630822673, 323931531, 616054005, |
| 173 | + 715419230, 344542774, 569479396, 46182177, 115731968, 615621055, 927437470, 415304055, |
| 174 | + 613134814, 593402338, 555887984, 424883671, 737555229, 580850096, 206291958, 408415618, |
| 175 | + 568495923, 383046806, 331153888, 630013482, 639299672, 932192570, 426968629, 665396997, |
| 176 | + 377560684, 425576288, 499413833, 304016029, 435936610, 890997684, 37462641, 308478314, |
| 177 | + 648743689, 968679592, 6563304, 528867706, 815369648, 699526086, 854594514, 77461257, |
| 178 | + 912978080, 437849059, 584065006, 448033159, 851418436, 275202511, 933127854, 57918210, |
| 179 | + 936121297, 728315416, 493270959, 635640085, 751603865, 815510463, 322128401, 817518864, |
| 180 | + 113319096, 719706704, 151675058, 579563270, 817496583, 331259914, 304828788, 832758432, |
| 181 | + 514693374, 628452536, 859015740, 54698207, 502378937, 925823748, 468799569, 912430738, |
| 182 | + 503849861, 155092332, 42145830, 16914543, 954818713, 944191613, 753948564, 101131208, |
| 183 | + 628620169, 514926770, 674609079, 477754503, 226746575, 399775590, 317837974, 685000591, |
| 184 | + 380154246, 862619940, 156768091, 907663822, 640534228, 509892852, 140342102, 997418526, |
| 185 | + 552535460, 430179820, 454222315, 646598713, 14409817, 280310343, 324342456, 625965172, |
| 186 | + 814280682, 974660429, 122025967, 697839974, 290110820, 878936393, 874327021, 252896959, |
| 187 | + 368474189, 596100537, 759459589, 74112139, 496075772, 660681195, 476919942, 732314798, |
| 188 | + 881551587, 600771213, 916355905, 62502174, 722694262, 846208181, 235701369, 377851030, |
| 189 | + 444635218, 905821472, 6007590, 14543647, 806494416, 995077069, 714758118, 844053358, |
| 190 | + 554781732, 772216007, 970855797, 411569324, 513182664, 137085083, 204095216, 632442670, |
| 191 | + 843815967, 289982568, 631746100, 825138049, 349162102, 5326115, 207201935, 835205999, |
| 192 | + 161469959, 312314799, 644367317, 122663749, 602404965, 763996480, 974548439, 732304994, |
| 193 | + 808596406, 180421152, 975264158, 122323736, 966123769, 50110342, 744171007, 561228509, |
| 194 | + 513803739, 631885254, 848740278, 38362310, 334317889, 407483295, 704719903, 793514069, |
| 195 | + 3850010, 47429717, 332085264, 296925009, 179969654, 285965854, 188329006, 353044452, |
| 196 | + 907132439, 173498729, 971005353, 886796300, 374137584, 167007939, 257311427, 69578518, |
| 197 | + 986207933, 820577598, 762893591, 855603877, 154159499, 81230719, 988672413, 560039653, |
| 198 | + 187206037, 124634358, 756546188, 562543979, 382863463, 791317508, 638677331, 472652962, |
| 199 | + 704857946, 755439531, 209470628, 413904724, 236402946, 980178476, 925780627, 624361832, |
| 200 | + 986373739, 865597279, 733945791, 40519093, 346073968, 531933636, 452742332, 388931131, |
| 201 | + 509971886, 203046081, 86591499, 107265603, 170834917, 222658017, 931432329, 545903464, |
| 202 | + 244840320, 750824817, 665373361, 418717528, 723513553, 572491457, 458224851, 688126547, |
| 203 | + 872604574, 477404041, 810687226, 902428227, 809180291, 181691685, 593946617, 443990866, |
| 204 | + 886931195, 671951824, 117916938, 88508493, 101987861, 998221579, 302729532, 637782794, |
| 205 | + 713816070, 909221425, 478649015, 947515758, 588376423, 102208139, 387839447, 126033549, |
| 206 | + 266213171, 286274226, 201637730, 911783168, 261943681, 622500013, 456472374, 708804967, |
| 207 | + 489562877, 836248357, 622073857, 432626926, 157337505, 416320771, 436152162, 592575425, |
| 208 | + 42633820, 518316330, 29165774, 254298082, 747662083, 309417930, 635421959, 306846172, |
| 209 | + ]; |
| 210 | + assert!(!Solution::is_possible(target)); |
| 211 | + } |
| 212 | +} |
0 commit comments