Re: [閒聊] 每日leetcode

看板Marginalman作者 (是oin的說)時間1年前 (2024/03/04 12:12), 編輯推噓1(211)
留言4則, 4人參與, 1年前最新討論串17/1548 (看更多)
今天每日 948. Bags of tokens 給你一串陣列跟power 可以對陣列中的數字做兩種操作 1. 用power去減那個數字 換一分 2. 減一分 獲得那個數字的power 解法: 所以最好是要用power 減最少的數字 得到一分 然後缺power的時候再去找最大的數字 用一分換到最多power 所以sort是一定要的 然後用two pointers確定目前進度 解出來ㄌ ya class Solution { public: int bagOfTokensScore(vector<int>& tokens, int power) { sort(tokens.begin(),tokens.end(),less()); int maxscore = 0; int scon = 0; int len = tokens.size(); int r = len-1; int l = 0; while(l<=r) { while((l<=r)&&(tokens[l] <= power)) { power -= tokens[l]; l++; scon++; maxscore = max(scon , maxscore); } power += tokens[r]; scon --; r --; if(scon < 0) { break; } } return maxscore; } }; -- 邊版的小母雞 — fuckchicken https://i.imgur.com/wglAuYR.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 134.208.57.64 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1709525551.A.FF5.html

03/04 12:12, 1年前 , 1F
臭甲
03/04 12:12, 1F

03/04 12:17, 1年前 , 2F
大濕
03/04 12:17, 2F

03/04 12:18, 1年前 , 3F
甲甲甲甲甲甲
03/04 12:18, 3F

03/04 18:08, 1年前 , 4F
大師
03/04 18:08, 4F
文章代碼(AID): #1bvKel_r (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bvKel_r (Marginalman)