leetcode -day9 Candy & Gas Station & Binary Tree Maximum Path Sum

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

时间: 2024-10-11 11:30:34

leetcode -day9 Candy & Gas Station & Binary Tree Maximum Path Sum的相关文章

[LeetCode]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. [分析]    需要考虑以上两种情况: 1 左子树或者右子树中存有最大路径和 不能和根节点形成一个路径 2 左子树 右子树 和根节点形成最大路径 [代码] /******

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] 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. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.

[LeetCode] 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. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo

[Leetcode][Tree][Binary Tree Maximum Path Sum]

找书中权值和最大的路径,至少包含一个点. 有点类似LCA(最近公共祖先),树的问题关键是如何划分子问题,然后递归求解. 想到了可以返回两种结果,一个是单独路径的最大和,一种是子树的最大和,然后在求解的过程中不断的更新答案. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val

Binary Tree Maximum Path Sum leetcode java

题目: 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. 题解: 递归求解. 取当前点和左右边加和,当前点的值中最大的作为本层返回值.并在全局维护一个max.使用数组,因为是引用类型.所以在递归过程中可以保存结果. 代码如下: 1

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven 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 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow. 然后考虑经过这个节点的情况来更新最终答案.更新答案后返回Vnow供父节点继续更新. 代码很简单. 有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大.参考POJ3728 cla

【leetcode】Binary Tree Maximum Path Sum

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. 用递归确定每一个节点作为root时,从root出发的最长的路径 在每一次递归中计算maxPath 1 /** 2 * Def

Leetcode OJ: Binary Tree Maximum Path Sum

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. 这个问题可以转化为,以某节点作为祖先,并经过这一节点的和最大的路径. 于就其实就是要遍历每一个节点,并对此节点求出 1. 以