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?

分析: 主要问题是可以从左升序或者从右升序,如何取大值。

方法一:从左从右分别计算一次,对值校正。并计算出最大值。要保存中间初步的值,空间复杂度:O(n)

class Solution {
public:
int candy(vector<int> &ratings) {
int n = ratings.size();
if(n == 0) return 0;
int totalCandy = 0;
int *getCandy = new int[n];
getCandy[0] = 1;
for(int i = 1; i < n; ++i)
if(ratings[i] > ratings[i-1]) getCandy[i] = getCandy[i-1] + 1;
else getCandy[i] = 1;
totalCandy += getCandy[n-1];
for(int i = n-1; i > 0; --i) {
if(ratings[i-1] > ratings[i]) getCandy[i-1] = max(getCandy[i-1], getCandy[i]+1);
totalCandy += getCandy[i-1];
}
delete[] getCandy;
return totalCandy;
}
};

方法二:从一个方向(此题从左),设置两个变量,通过计算升序长度,降序长度确定精确值。空间复杂度:O(1)

class Solution {
public:
int candy(vector<int> &ratings) {
int LA = 0, LD = 0; // 利用降序长度和升序长度,来求结果。
bool decend = false;
int totalCandy = ratings.size();
for(int i = 1; i < ratings.size(); ++i) {
if(ratings[i-1] < ratings[i]) {
if(LA == 0 || decend == true) LA = 2;//考虑情况:之前为降序,重新设置升序长度
else ++LA;
LD = 0; decend = false; //升序时不需要知道之前降序长度。
totalCandy += (LA - 1);
}else if(ratings[i-1] > ratings[i]) {
decend = true;
if(LD == 0) LD = 2;
else ++LD;
if(LD <= LA) totalCandy += (LD - 2);
else totalCandy += (LD - 1);
}else {
LA = LD = 0; // 出现相同字符,则从第二个重复字符重新开始
}
}
return totalCandy;
}
};

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.

方法一: 直接计算。(752ms)

class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int n = gas.size();
if(n == 1) return gas[0] >= cost[0] ? 0 : -1;
int tank = 0;
for(int i = 0; i < n; ++i) {
int j = i;
for(; j < n; ++j) {
tank += gas[j];
if(tank < cost[j]) break;
tank = tank - cost[j];
}
if(j == n) {
for(j = 0; j < i; ++j) {
tank += gas[j];
if(tank < cost[j]) break;
tank = tank - cost[j];
}
if(j == i) return i;
}
tank = 0;
}
return -1;
}
};

方法二:计算出每个站的油余量:(即从该站开始到下一站,还能剩余的油),将负数或者之前为非负数的值剔除。(20ms)

bool judge(int s, int e, int& cnt, vector<int> &gas) {
for(int j = s; j < e; ++j) {
cnt += gas[j];
if(cnt < 0) return false;
}
return true;
}
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int count = 0;
for(int i = 0; i < gas.size(); ++i) gas[i] -= cost[i];
for(int i = 0; i < gas.size(); ++i) {
if(gas[i] < 0) continue;
else if(i > 0 && gas[i-1] >= 0) continue;
if(judge(i, gas.size(), count, gas) && judge(0, i, count, gas)) return i;
count = 0;
}
return -1;
}
};

20. Candy && Gas Station的更多相关文章

  1. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  2. 1072. Gas Station (30)

    先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...

  3. PAT_1072 Gas Station

    1072. Gas Station (30) 时间限制  200 ms 内存限制  32000 kB 代码长度限制  16000 B 判题程序    Standard 作者    CHEN, Yue ...

  4. A1072. Gas Station

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  5. PAT 1072 Gas Station[图论][难]

    1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...

  6. [LeetCode 题解]:Gas Station

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 There are ...

  7. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  8. 1072 Gas Station (30)(30 分)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  9. 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

    题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...

随机推荐

  1. sprintf函数

    sprintf函数用法举例 #include<stdio.h> int main() { //1.连接字符串 char a1[] = {'A', 'B', 'C', 'D', 'E', ' ...

  2. Java中类型的长度

    介绍: Java中有8种基本类型,分别是boolean, char, byte, short, int, long, float, double.他们的长度固定,不是对象.对于有必要将基本类型作为对象 ...

  3. tomcat 一个项目在本机和办公室以外电脑服务器上搭建出现乱码问题

    插入数据库都是???乱码 页面浏览之前在本机插入的数据都是正常的 修改conf目录下的server.xml文件 转义字符集 为 GBK  或 UTF-8 utf-8 即 添加 URIEncoding= ...

  4. 常用的工具cmd命令

    1.stikynot 2.psr 3.cmd 4.calc 5.mspaint 6.ping

  5. [编辑器]sublime使用入门

    0.索引 1.新建工程 2.控制台 3.快捷键汇总 4.安装插件 1.新建工程: 没有找到直接新建工程的方法,目前看来只能先file -> open folder然后Save Project a ...

  6. UISegmentedControl的详细使用

    当用户输入不仅仅是布尔值时,可使用分段控件(UISegmentedControl).分段控件提供一栏按钮(有时称为按钮栏),但只能激活其中一个按钮.分段控件会导致用户在屏幕上看到的内容发生变化.它们常 ...

  7. idea中如何配置tomcat

    这几天想通过JDBC驱动使用MySQL数据库,但老是运行不成功,但是写成java就没有问题,于是想到是不是服务器没配置好 idea中配置tomcat的步骤如下 1:File->Settings. ...

  8. HANS123

    //策略:HANS123//周期:日内//类别:趋势突破 作为外汇市场上广为流行的一种突破交易策略,HANS123以其简洁的开盘后N根K线的高低点突破,作为交易信号触发的评判标准.这也是一种入场较早的 ...

  9. RedHad中yum安装与使用

    yum的安装对于linux来说,是一个福音,至少安装软件来说,非常非常方便,以前使用rpm安装,那个各种依赖,哎,说多了都是泪,现在有这个yum就方便多了. 此处记录redhad的安装.其实我也是借鉴 ...

  10. IIS 发布网站 ashx无法访问

    IIS6 问题 1.是否安装相应的.net版本 2.查看.net版本是否一致 3.查看web 服务扩展中.net版本是否允许. 4.添加相应的MIME类型文件 在IIS中右键网站→属性→主目录→配置→ ...