https://i.imgur.com/Pf46qZU.png
第一次寫出第四題 家人們 衝擊徽章可不是開玩笑的 第一題: 相鄰 奇偶相同 換一次數字之後最小的數字 思路: 照做 ```cpp class Solution { public: long long minimumCost(int m, int n, vector<int>& horizontalCut, vector<int>& verticalCut) { long long res = 0; long long horicut = 1; long long vertcut = 1; sort(horizontalCut.begin() , horizontalCut.end()); sort(verticalCut.begin() , verticalCut.end()); int hp = m-2; int vp = n-2; while(hp>=0 && vp>=0) { if(horizontalCut[hp] > verticalCut[vp]) { res += (long long)horizontalCut[hp]*vertcut; horicut ++; hp--; } else if(horizontalCut[hp] <= verticalCut[vp]) { res += (long long)verticalCut[vp]*horicut; vertcut ++; vp--; } } while(hp>=0) { res += (long long)horizontalCut[hp]*vertcut; horicut ++; hp--; } while(vp>=0) { res += (long long)verticalCut[vp]*horicut; vertcut ++; vp--; } return res; } }; ``` 第二題: 在listnode裡刪掉要刪掉的數字 思路: 先放個prehead指向一開始的head 然後對所有node那個那個就好了 ```cpp /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* modifiedList(vector<int>& nums, ListNode* head) { unordered_set<int> save; for(int k : nums) { save.insert(k); } ListNode* prehead = new ListNode(0,head); ListNode* p = prehead; while (p != NULL && p->next != NULL) { if (save.find(p->next->val) != save.end()) { p->next = p->next->next; } else { p = p->next; } } return prehead->next; } }; ``` 第三四題: 切蛋糕 給你所有切蛋糕位子需要花費的點數 然後橫的切一條之後 直的要切的蛋糕數量就會增加1 問你最少的花費點數 思路: 首先要知道 在同一方向的動作不會影響彼此 而越後切的蛋糕 受到不同方向數量影響會越大 所以同一個方向要先切大的數字 然後考慮不同方向的順序 可以寫一個證明 https://i.imgur.com/hsqxgE6.jpeg
直的花費y 橫的花費x 然後 姆咪姆咪 y>x的話 不管n是多少 先切y一定比先切x好 所以這個問題變成two pointer了 然後要記得紀錄切過幾個蛋糕 sort之後讓每個不同方向的動作比較大小 姆咪 ```cpp class Solution { public: long long minimumCost(int m, int n, vector<int>& horizontalCut, vector<int>& verticalCut) { long long res = 0; long long horicut = 1; long long vertcut = 1; sort(horizontalCut.begin() , horizontalCut.end()); sort(verticalCut.begin() , verticalCut.end()); int hp = m-2; int vp = n-2; while(hp>=0 && vp>=0) { if(horizontalCut[hp] > verticalCut[vp]) { res += (long long)horizontalCut[hp]*vertcut; horicut ++; hp--; } else if(horizontalCut[hp] <= verticalCut[vp]) { res += (long long)verticalCut[vp]*horicut; vertcut ++; vp--; } } while(hp>=0) { res += (long long)horizontalCut[hp]*vertcut; horicut ++; hp--; } while(vp>=0) { res += (long long)verticalCut[vp]*horicut; vertcut ++; vp--; } return res; } }; ``` -- ※ 發信站: 批踢踢實業坊(ptt-website.tw), 來自: 1.162.13.212 (臺灣) ※ 文章網址: https://ptt-website.tw/Marginalman/M.1720929954.A.719
SecondRun: 大師 07/14 12:07
sustainer123: 大師 07/14 12:08
wu10200512: 幹你好強 07/14 12:08
sustainer123: 我第三題直接沒想法 哇哇嗚嗚嗚 07/14 12:08
JIWP: 別捲了 07/14 12:09
Peycere: 記者收了包養網多少啦 07/14 12:09
NCKUEECS: 大師 07/14 12:10
Rushia: 你好優秀 07/14 12:10
Furina: 我好崇拜你 07/14 12:54