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. 【转载】VMware虚拟机修改硬盘容量大小

    很多人在安装虚拟机系统的时候,为了节省硬盘空间,把硬盘容量设置得较小,可是后来发现硬盘容量不够用了.在VMware中又不能直接修改虚拟机的硬盘容量大小,或者重建虚拟机系统,非常麻烦. 其实在VMwar ...

  2. C++ 之 const 随笔记

    const关键字,相信对C语言有所了解的同学都应该知道他的作用:1.修饰常量,2.修饰指针,3.修饰函数 1.修饰常量 const修饰后的变量被定义为常量 2.修饰指针 当用const修饰指针的时候, ...

  3. Windows环境下 PHP+Apache+Mysql配置

    网上关于这种搭配的配置有许多许多,但是不知道大家有否碰到这么一个问题,就是做好的PHP程序(内含访问mysql数据库的操作)发布到Apache服务器上之后, 运行程序,提示未找到数据库函数. 仔细检查 ...

  4. 第一章 ------ AutoYout介绍

    1.使用自动布局的好处: (1)让两个视图进行尺寸匹配,使两个视图始终保持相同的宽度 (2)无论父视图如何改变,视图都可以相对于父视图居中 (3)拜放一行视图时将几个视图的底部对齐 (4)将两个视图偏 ...

  5. MySQL 数据库实现远程连接

    1,刚开始我使用的是Navicat for MySQL工具连接远程的mysql的数据库. 报错了.报错信息是 Error 1130: Host '192.168.1.80' is not allowe ...

  6. sublime福音:微信小程序组件及API补全插件

    微信自带的编辑器操作起来各种不顺手,调试的时候需要用到,但是编辑的时候还是用自己熟悉的编辑器好一点. 将文件目录导入到sublime,在sublime编辑保存后,回到小程序开发工具刷新页面即可. 下面 ...

  7. C# 代码示例_结构/数组/枚举...

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 图数据库(graph database)资料收集和解析 - daily

    Motivation 图数据库中的高科技和高安全性中引用了一个关于图数据库(graph database)的应用前景的乐观估计: 预计到2017年,图数据库产业在数据库市场的份额将从2个百分点增长到2 ...

  9. bootstrap-10

    实现原理: 通过定义容器大小,平分12份(也有分为24份或32份,但12份是最常见的),在调整内外边距,最后结合媒体查询,就制作除了强大的响应式网格系统. 工作原理: 1.数据行(.row)必须包含在 ...

  10. frame和bounds区别

    学习ios开发有一段时间了,项目也做了两个了,今天看视频,突然发现view的frame和bound两个属性,发现bound怎么也想不明白,好像饶你了死胡同里,经过一番尝试和思考,终于弄明白bound的 ...