麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

greedy: 55. Jump Game / 455. Assign Cookies

2019-11-11 03:10:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Jump Game題目描述代碼實(shí)現(xiàn)Assign Cookies題目描述代碼實(shí)現(xiàn)

55. Jump Game

題目描述

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array rePResents your maximum jump length at that position. Determine if you are able to reach the last index. For example: A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false.

代碼實(shí)現(xiàn)

法一:

// 復(fù)雜度O(n^2)的做法,導(dǎo)致超時(shí)了。class Solution {public: bool canJump(vector<int>& nums) { int nums_len = nums.size() - 1; vector<bool> flag(nums_len, false); int stt = 0; int jmp = 0; flag[0] = true; cout << nums_len << endl; for(stt = 0; stt <= nums_len; stt++) { if(flag[stt]) { int tmp = nums[stt] + stt; for(int in_range = tmp; in_range >= stt; in_range--) { if(in_range >= nums_len) return true; flag[in_range] = true; // cout << in_range << " " << flag[in_range] << " " << stt << endl; } } } return false; }};

法二: 修改第二個(gè)進(jìn)入循環(huán)的條件,沒(méi)有必要重復(fù)設(shè)置可以跳轉(zhuǎn)的標(biāo)志位。這樣做了以后可以擊敗95%的c++代碼。

class Solution {public: bool canJump(vector<int>& nums) { int nums_len = nums.size() - 1; vector<bool> flag(nums_len, false); int stt = 0; int jmp = 0; int out_range = -1; flag[0] = true; cout << nums_len << endl; for(stt = 0; stt <= nums_len; stt++) { if(flag[stt]) { int tmp = nums[stt] + stt; if(tmp > out_range) { for(int in_range = tmp; in_range > out_range; in_range--) { if(in_range >= nums_len) return true; flag[in_range] = true; // cout << in_range << " " << flag[in_range] << " " << stt << endl; } out_range = tmp; } } } return false; }};

把上面的代碼做些簡(jiǎn)化設(shè)計(jì),可以得到:

class Solution {public: bool canJump(vector<int>& nums) { int nums_len = nums.size(); int i = 0, maxreach = 0; for (; i < nums_len && i <= maxreach && maxreach < nums_len - 1; ++i) maxreach = max(maxreach,i+nums[i]); return maxreach>=nums_len-1; }};

這種做法比較簡(jiǎn)約,但是效率比較低。因?yàn)樗枰獙?duì)所有的情況調(diào)用max函數(shù),遇到較大的數(shù)組的時(shí)候就會(huì)比較耗時(shí)。這種做法不如我在法二中做的剪枝那么有效。

455. Assign Cookies

題目描述

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. Note: You may assume the greed factor is always positive. You cannot assign more than one cookie to one child. Example 1: Input: [1,2,3], [1,1] Output: 1 Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content. You need to output 1. Example 2: Input: [1,2], [1,2,3] Output: 2 Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. You have 3 cookies and their sizes are big enough to gratify all of the children, You need to output 2.

代碼實(shí)現(xiàn)

這種做法擊敗了99.24%的做法,思路比較簡(jiǎn)單。就是先排序在比較。比較的時(shí)候,如果比較到s的索引為s_stt,s_stt之前的都沒(méi)有必要在下一次比較。

class Solution {public: int findContentChildren(vector<int>& g, vector<int>& s) { int content_num = 0; int g_len = g.size(); int s_len = s.size(); sort(g.begin(), g.end()); sort(s.begin(), s.end()); int s_stt = 0; for(int i = 0; i < g_len; i++) { for(int j = s_stt; j < s_len; j++) { if(g[i] <= s[j]) { content_num++; s_stt = j+1; break; } } } return content_num; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久精品综合视频 | 成人福利视频在 | 中日韩乱码一二新区 | 中文字幕专区高清在线观看 | 国产精品午夜小视频观看 | 蝌蚪久久窝 | 久久亚洲精品久久国产一区二区 | 日本娇小videos高潮 | 亚洲网站在线观看 | 久久久久久久久久亚洲 | 亚洲福利在线视频 | 婷婷久久综合九色综合色多多蜜臀 | 高清国产午夜精品久久久久久 | 国产在线导航 | 黄色大片网 | 精品一区二区在线播放 | 欧美日韩成人一区二区 | 羞羞视频在线免费 | 九九热精品视频在线免费观看 | 国产一区精品视频 | 国产精品7区 | 欧美韩国一区 | gogo全球大胆高清人露出91 | 久久亚洲精选 | 91真视频| 亚洲免费视频大全 | 国产精品一区免费在线观看 | 久久激情国产 | 成人午夜视频在线观看 | 亚洲3p激情在线观看 | 欧美日韩国产一区二区三区在线观看 | 高清国产免费 | 久久久噜噜噜久久熟有声小说 | 精品亚洲国产视频 | 精品亚洲福利一区二区 | 国产亚洲精品综合一区91555 | 十级毛片 | av性色全交蜜桃成熟时 | 亚洲国产精品二区 | 久久一区国产 | 免费淫视频 |