@@ -513,8 +513,155 @@ public:
513513
514514# 134. 加油站
515515
516- https://programmercarl.com/0134.%E5%8A%A0%E6%B2%B9%E7%AB%99.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE
516+ ```cpp
517+ class Solution {
518+ public:
519+ int canCompleteCircuit(vector<int>& g, vector<int>& c) {
520+ int n = g.size();
521+ vector<int> h(n,0);
522+ // h[i] -> gas change from i-1 -> i
523+ h[0] = g[n-1] - c[n-1];
524+ for (int i = 1 ; i < n ; i ++ ) {
525+ h[i] = g[i-1]-c[i-1];
526+ }
527+ int start = 0;
528+ int pos = start;
529+ int csum = 0;
530+ while (pos < start + n && start < n) {
531+ csum += h[(1+pos)%n];
532+ if (csum < 0) {
533+ start = pos + 1;
534+ csum = 0;
535+ }
536+ pos ++ ;
537+ }
538+ return start<n?start:-1;
539+ }
540+ };
541+ ```
542+
543+ 贪心算法(方法一) 还挺巧妙的,我这个就是个最大子数组的算法
544+
545+ # 135. 分发糖果
546+
547+ ``` cpp
548+ class Solution {
549+ public:
550+ int candy(vector<int >& v) {
551+ // 1,3,4,5,2
552+ // 1,2,3,4,1
553+ //
554+ vector<int >res(v.size(),1);
555+ for (int i = 1 ; i < v.size() ; i ++ ) {
556+ if (v[ i] > v[ i-1] ) res[ i] = res[ i-1] + 1;
557+ }
558+ for (int i = v.size() - 2; i >= 0 ; i -- ) {
559+ if (v[ i] > v[ i+1] && res[ i] <= res[ i+1] ) res[ i] = res[ i+1] + 1;
560+ }
561+ return accumulate(res.begin(),res.end(),0);
562+ }
563+ };
564+ ```
565+
566+ WA了一发漏了`&& res[i] <= res[i+1]`
567+
568+ # 860.柠檬水找零
569+
570+ ```cpp
571+ class Solution {
572+ public:
573+ bool lemonadeChange(vector<int>& bills) {
574+ int c5 = 0, c10 = 0;
575+ for ( int bill : bills) {
576+ switch (bill) {
577+ case 5:
578+ c5 ++ ;
579+ break;
580+ case 10:
581+ if (c5 == 0) return false;
582+ c5 -- ;
583+ c10 ++ ;
584+ break;
585+ default:
586+ if (c10>0&&c5>0) {
587+ c10 -- ; c5 -- ; continue;
588+ }
589+ if (c5 >= 3) {
590+ c5 -= 3; continue;
591+ }
592+ return false;
593+ break;
594+ }
595+ }
596+ return true;
597+ }
598+ };
599+ ```
600+
517601
602+ 模拟题
603+
604+ # 406.根据身高重建队列
605+ 错误解答
518606``` cpp
519607
520- ```
608+ bool f (const vector<int >&a, const vector<int >&b) {
609+ if (a[ 1] ==b[ 1] )return a[ 0] >b[ 0] ;return a[ 1] <b[ 1] ;
610+ }
611+ class Solution {
612+ public:
613+ vector<vector<int >> reconstructQueue(vector<vector<int >>& p) {
614+ vector<vector<int >> res;
615+ sort(p.begin(),p.end(),f);
616+ for ( auto peo: p ) {
617+ if (peo[ 1] !=0) break;
618+ res.push_back(peo);
619+ }
620+ reverse(res.begin(),res.end());
621+ for (int i = res.size(); i < p.size(); i ++ ) {
622+ auto x = p[ i] ;
623+ int insert_pos = 0; int cnt = 0;
624+ while ( cnt < x[ 1] + 1 ) {
625+ if (p[ insert_pos] [ 0 ] >= x[ 0] ) cnt ++ ;
626+ insert_pos ++ ;
627+ }
628+ res.insert(res.begin()+insert_pos - 1,x);
629+ }
630+ return res;
631+ }
632+ };
633+ ```
634+
635+ > 这道题我没有能够做出来
636+
637+ 在135. 分发糖果 (opens new window)我就强调过一次,遇到两个维度权衡的时候,一定要先确定一个维度,再确定另一个维度。
638+
639+ 如果两个维度一起考虑一定会顾此失彼。
640+
641+ > 我就是错误的按照k来从小到大排序了
642+
643+ ```cpp
644+
645+ bool f(vector<int>& a, vector<int>& b){
646+ if(a[0]==b[0])return a[1]<b[1];return a[0]>b[0];
647+ }
648+ class Solution {
649+ public:
650+ vector<vector<int>> reconstructQueue(vector<vector<int>>& p) {
651+ vector<vector<int>> res;
652+ sort(p.begin(),p.end(),f);
653+ for (int i = 0; i < p.size(); i ++ ) {
654+ auto x = p[i];
655+ int pos = x[1];
656+ res.insert(res.begin()+pos,x);
657+ }
658+ return res;
659+ }
660+ };
661+ ```
662+
663+ 先按照身高排序,固定住规律。按照k排序没法获得额外的规律
664+
665+ # 452. 用最少数量的箭引爆气球
666+
667+ https://programmercarl.com/0452.%E7%94%A8%E6%9C%80%E5%B0%91%E6%95%B0%E9%87%8F%E7%9A%84%E7%AE%AD%E5%BC%95%E7%88%86%E6%B0%94%E7%90%83.html
0 commit comments