Leetcode 1375. Bulb Switcher III (python+cpp)
題目
解法:
這道題目想清楚了關鍵就非常的簡單。當一個燈被點亮時,這個燈變數的條件是,他的左邊的所有燈都是亮的。意味著從這個等開始到1的燈都必須全部點亮,換句話說,他左邊包括他自己一共被點亮的燈數必須等於他的編號。而要滿足所有的燈都是藍色,顯然由最右邊的點亮的等來決定,只要最右邊此時被點亮的燈滿足變藍條件,那麼自然所有的燈都是藍色的,所以只需要簡單操作如下:
- 儲存並更新此刻及之前所有狀態中被點亮燈的最右邊那盞
- 每一次判斷最右邊那盞是否符合變藍的條件
class Solution:
def numTimesAllBlue(self, light: List[int]) -> int:
max_bulb_ind = 0
count = 0
turnedon_bulb = 0
for bulb in light:
max_bulb_ind = max(max_bulb_ind,bulb)
turnedon_bulb += 1
if turnedon_bulb == max_bulb_ind:
count += 1
return count
C++版本
class Solution {
public:
int numTimesAllBlue(vector<int>& light) {
int count = 0;
int max_bulb_ind = 0;
int turnedon_bulbs = 0;
for(int i=0;i<light.size();i++){
max_bulb_ind = max(max_bulb_ind,light[i]);
turnedon_bulbs++;
if(max_bulb_ind == turnedon_bulbs) count++;
}
return count;
}
};
相關文章
- 319. Bulb Switcher
- Leetcode 1366. Rank Teams by Votes (python+cpp)LeetCodePython
- LeetCode-House Robber IIILeetCode
- Leetcode 329. Longest Increasing Path in a Matrix (python+cpp)LeetCodePython
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- LeetCode-Shortest Word Distance IIILeetCode
- 【LeetCode】House Robber III(337)LeetCode
- Best Time to Buy and Sell Stock III -- LeetCodeLeetCode
- leetcode260-single number iiiLeetCode
- Leetcode-Best Time to Buy and Sell Stock IIILeetCode
- Best Time to Buy and Sell Stock III leetcode javaLeetCodeJava
- Leetcode——437. 路徑總和 IIILeetCode
- leetcode best-time-to-buy-and-sell-stock-iii(Java)LeetCodeJava
- [LintCode/LeetCode] Contains Duplicate IIILeetCodeAI
- LeetCode-Two Sum III - Data structure designLeetCodeStruct
- [LeetCode] 3163. String Compression IIILeetCode
- Leetcode 之 PHP 解析 (260. Single Number III)LeetCodePHP
- LeetCode 只出現一次的數字IIILeetCode
- 最新Turbo Boost Switcher Pro補丁破解版 Turbo Boost Switcher Pro破解安裝教程
- leetcode 123 買賣股票的最佳時機 IIILeetCode
- Leetcode 960:刪列造序 III(超詳細的解法!!!)LeetCode
- [Leetcode學習-c++&java]Next Greater Element I ~ IIILeetCodeC++Java
- leetcode —— 1004. 最大連續1的個數 IIILeetCode
- Hackable: III
- LeetCode每日一題:反轉字串中的單詞 III(No.557)LeetCode每日一題字串
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解題報告LeetCode
- A - 卡牌遊戲 III遊戲
- Reflective Journal III
- leetcode力扣 1004. 最大連續1的個數 IIILeetCode力扣
- Environment Switcher 原理解析(註解、Apt、反射、混淆)APT反射
- 應用程式切換工具:rcmd–App Switcher for MacAPPMac
- Caramba Switcher for mac自動鍵盤佈局開關Mac
- Chapter III What Is Truth IIAPT
- Diablo III ZOJ - 3769
- 劍指 Offer 32 - III. 從上到下列印二叉樹 III二叉樹
- cpu頻率調節軟體:Turbo Boost Switcher Pro for macMac
- cpu溫度監測:Turbo Boost Switcher Pro 啟用版
- 「譯」MotionLayout介紹 (part III)