1、
Candy
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
- Each child must have at least one candy.
- Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
分析:乍看这一题,真是无从下手,评分高的小孩比左右邻居高,那可以分为比左邻居高,比右邻居高,那可以分成两边遍历,用一个数组保存小孩分得的糖果数,第一遍从左到右,如果右边小孩比左边高则是左边小孩的糖果数加1,否则则为1,第二遍从右到左,如果左边小孩比右边小孩高并且保存的糖果数左边比右边低,则更新左边小孩的糖果数为右边小孩的糖果数加1,最后总和为所有的糖果数。这样用空间O(n),来换得了时间复杂度O(n)。
代码如下:
class Solution { public: int candy(vector<int> &ratings) { if(ratings.empty()){ return 0; } int *candyNum = new int[ratings.size()]; candyNum[0] = 1; for(int i=1; i<ratings.size();++i){ if(ratings[i]>ratings[i-1]){ candyNum[i] = candyNum[i-1]+1; }else{ candyNum[i] = 1; } } int Total = candyNum[ratings.size()-1]; for(int i=ratings.size()-2; i>=0; --i){ if(ratings[i]>ratings[i+1] && candyNum[i]<=candyNum[i+1]){ candyNum[i] = candyNum[i+1]+1; } Total += candyNum[i]; } delete[] candyNum; return Total; } };
改进,考虑到本题目只关心总数,不需要知道每个小孩所分得的糖果数,可以减少空间复杂度。请参考http://www.cnblogs.com/felixfang/p/3620086.html
2、Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of
gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station‘s index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
分析:首先想到的方法是从汽油站0开始遍历,依次判断是否是满足条件的起点(即每过一个站汽油是否够),但是此种方法时间复杂度为O(n^2),显然太没有意思。想其他的方法,如何降低时间复杂度,当经过一个站汽油不够时,不是改变起点从头遍历,而是采取一种方法避免此种遍历,当汽油不够时,此时要想到此战的汽油够,就得将起点前移,再判断汽油够不够,这样减少了计算步骤。动态规划的判断还是挺有意思的。同时注意如何使之成为一个环形公路,即当起始点退到头时,再从尾开始。
代码如下:
class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { if(gas.empty()||cost.empty()||gas.size()!=cost.size()){ return -1; } int stationAllNum = gas.size(); //总站数 int gasInCar = 0; //车里剩余数量 int startIndex = 0; //开始站索引 int endIndex = 0; //结束站索引 int goStationNum = 0; int index = 0; while(goStationNum < stationAllNum){ gasInCar += gas[index] - cost[index]; if(gasInCar >= 0){ ++endIndex; index = endIndex; }else{ --startIndex; if(startIndex < 0){ startIndex = stationAllNum - 1; } index = startIndex; } ++ goStationNum; } if(gasInCar >= 0){ return startIndex; }else{ return -1; } } };
3、Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1 / 2 3
Return 6
.
分析:看到二叉树,想到的方法肯定是递归,但是这个递归,感觉不太好想,纠结半天才弄出来。
递归的思路,求出左子树和右子树的最大路径和,如果左右子树的路径小于0,则最大和为当前根结点的值,如果大于0,则为左右子树中最大路径+当前根结点的值.
注意的点:(1)每次递归的值是从子节点到当前节点的路径的最大值,必须要包括当前节点。(2)递归当左右子树中最大路径的值都大于0时,此时返回值不是两者的和+根结点的值,而是其中大者的值+根结点的值,这点需要理解。(3)考虑到2,则需要设置一个全局变量,来更新最大路径值,尤其是左右子树的最大路径都大于0时,需要检测两者和加根结点的值是否大于全局变量。
代码如下:
class Solution { public: int maxPathSumInt; int maxPathSum(TreeNode *root) { maxPathSumInt = INT_MIN; maxPathSumCore(root); return maxPathSumInt; } int maxPathSumCore(TreeNode *root){ if(root->left==NULL && root->right==NULL){ if(root->val > maxPathSumInt){ maxPathSumInt = root->val; } return root->val; } int lSum = INT_MIN; int rSum = INT_MIN; if(root->left!=NULL){ lSum = maxPathSumCore(root->left); } if(root->right!=NULL){ rSum = maxPathSumCore(root->right); } int sum = INT_MIN; if(lSum<=0 && rSum<=0){ sum = root->val; }else if( lSum<=0 ){ sum = root->val+rSum;//root->val>0? }else if( rSum<=0 ){ sum = root->val+lSum; }else{ sum = max(lSum,rSum)+root->val; if(lSum+rSum+root->val > maxPathSumInt){ maxPathSumInt = lSum+rSum+root->val; } } if(sum > maxPathSumInt){ maxPathSumInt = sum; } return sum; } };
leetcode -day9 Candy & Gas Station & Binary Tree Maximum Path Sum,布布扣,bubuko.com