精華區beta Marginalman 關於我們 聯絡資訊
今天每日 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
JIWP: 臭甲 03/04 12:12
DJYOSHITAKA: 大濕 03/04 12:17
zoeredbird: 甲甲甲甲甲甲 03/04 12:18
abcd991276: 大師 03/04 18:08