leetcode 六月每日一题 leetcode1431

哎呀,一转眼到六月了。仔细反思了一下,以后要加强leetcode的专题训练,每日一题是要的,但是也要有自己的节奏,按照类型去刷题~

在这里插入图片描述
在这里插入图片描述
拿到这道题,第一个思路是暴力的,找出拥有糖果最多的数目,然后在循环一遍数组,判断其拥有糖果后是否会达到最多糖果数目。这中算法时间复杂度也是很低的O(n),很轻松就过了~

class Solution {
public:
    vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {

    	int max_candies = 0;
    	for(auto candy : candies){
    		if(candy > max_candies)
    			max_candies = candy;
    	}

    	vector<bool> result;

    	for(auto candy : candies){
    		if( (candy + extraCandies ) >= max_candies )
    			result.push_back(true);
    		else
    			result.push_back(false);
    	}

    	return result;

    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章