LeetCode 485. Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:
Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
Note:

The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000

分析:设立cnt数组,表示在nums[i]处当前连续的1的值,maxn取其最大的值,在遇到nums[i] == 0的时候更新maxn的值。最后还要更新一下防止最后一个是1.

 

LeetCode 257. Binary Tree Paths

257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

1
/ \
2 3
\
5
All root-to-leaf paths are:

[“1->2->5”, “1->3”]
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

 

LeetCode 475. Heaters

Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.

Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.

So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.

Note:
Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
As long as a house is in the heaters‘ warm radius range, it can be warmed.
All the heaters follow your radius standard and the warm radius will the same.
Example 1:
Input: [1,2,3],[2]
Output: 1
Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
Example 2:
Input: [1,2,3,4],[1,4]
Output: 1
Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.

题目大意:给出房子的坐标和供暖器的坐标,求供暖器的最小供热半径是多少才能满足让所有房子都暖和。

分析:先将houses和heaters排序,计算每一个house左右的供暖器的距离最小的那个值,然后将所有的这些最小值中取最大的值。
因为houses和heaters都是排序好的,所以heater[j]与houses[i]的距离应该越来越小,如果突然间变大了,说明不是最小值了,就break掉。这样就能得知最小值。

 

1026. Table Tennis (30)-PAT甲级真题(模拟)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours.

Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day.

One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (<=10000) – the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS – the arriving time, P – the playing time in minutes of a pair of players, and tag – which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players’ info, there are 2 positive integers: K (<=100) – the number of tables, and M (< K) – the number of VIP tables. The last line contains M table numbers.

Output Specification:

For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed.

Sample Input:
9
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:00 5 0
08:12:00 10 1
20:50:00 10 0
08:01:30 15 1
20:53:00 10 1
3 1
2
Sample Output:
08:00:00 08:00:00 0
08:01:30 08:01:30 0
08:02:00 08:02:00 0
08:12:00 08:16:30 5
08:10:00 08:20:00 10
20:50:00 20:50:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
3 3 2

题目大意:k张桌子,球员到达后总是选择编号最小的桌子。如果训练时间超过2h会被压缩成2h,如果到达时候没有球桌空闲就变成队列等待。
k张桌子中m张是vip桌,如果vip桌子有空闲,而且队列里面有vip成员,那么等待队列中的第一个vip球员会到最小的vip球桌训练。如果vip桌子空闲但是没有vip来,那么就分配给普通的人。如果没有vip球桌空闲,那么vip球员就当作普通人处理。
给出每个球员的到达时间、要玩多久、是不是vip(是为1不是为0)。给出球桌数和所有vip球桌的编号,QQ所有在关门前得到训练的球员的到达时间、训练开始时间、等待时长(取整数,四舍五入),营业时间为8点到21点。如果再21点后还没有开始玩的人,就不再玩,不需要输出~

分析:在变量t中将输入时间转化为秒的形式,使用T数组存储某个时刻到达的客户玩耍的时间,使用V数组记录某个时刻到达的客户是否是VIP,在vip数组中存储某张桌子是不是vip专属,Table记录每个桌子上的客户剩余使用时间,队列Wait,vWait记录还在排队的普通、VIP客户,now记录当前要去玩耍的客户的到达时刻,nowt记录当前要去玩耍的桌子,num数组记录每张桌子使用的人数,AnsI数组和AnsO数组分别记录客户的到达时刻和开始玩耍时刻,table与vtalbe分别记录当前可使用的最小的普通、VIP桌子。思路:将客户到达时间的时分秒统一转换为秒,同时注意每对客户最多让玩2个小时。以秒为单位,遍历从8点到21点的每个时刻,判断当前是否有新到达的普通或VIP客户,将其加入对应的队列中。同时遍历每张桌子,将有人的桌子的客户剩余游玩时刻-1,同时判断空闲的最小编号的普通、VIP桌子编号。当VIP排队队列有人时,判断是否有空的VIP桌子,有的话直接进行记录;如果没有,判断是否有空缺的普通桌子,并是否存在更早到的普通客户;没有的话,就记录当前VIP客户使用了普通桌子。同理,判断普通客户的桌子使用情况。在输出时,将秒转化为时、分、秒的格式即可~

1095. Cars on Campus (30)-PAT甲级真题(map,排序)

Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, the number of cars parking on campus, and at the end of the day find the cars that have parked for the longest time period.

Input Specification:

Each input file contains one test case. Each case starts with two positive integers N (<= 10000), the number of records, and K (<= 80000) the number of queries. Then N lines follow, each gives a record in the format

plate_number hh:mm:ss status

where plate_number is a string of 7 English capital letters or 1-digit numbers; hh:mm:ss represents the time point in a day by hour:minute:second, with the earliest time being 00:00:00 and the latest 23:59:59; and status is either in or out.

Note that all times will be within a single day. Each “in” record is paired with the chronologically next record for the same car provided it is an “out” record. Any “in” records that are not paired with an “out” record are ignored, as are “out” records not paired with an “in” record. It is guaranteed that at least one car is well paired in the input, and no car is both “in” and “out” at the same moment. Times are recorded using a 24-hour clock.

Then K lines of queries follow, each gives a time point in the format hh:mm:ss. Note: the queries are given in ascending order of the times.

Output Specification:

For each query, output in a line the total number of cars parking on campus. The last line of output is supposed to give the plate number of the car that has parked for the longest time period, and the corresponding time length. If such a car is not unique, then output all of their plate numbers in a line in alphabetical order, separated by a space.

Sample Input:
16 7
JH007BD 18:00:01 in
ZD00001 11:30:08 out
DB8888A 13:00:00 out
ZA3Q625 23:59:50 out
ZA133CH 10:23:00 in
ZD00001 04:09:59 in
JH007BD 05:09:59 in
ZA3Q625 11:42:01 out
JH007BD 05:10:33 in
ZA3Q625 06:30:50 in
JH007BD 12:23:42 out
ZA3Q625 23:55:00 in
JH007BD 12:24:23 out
ZA133CH 17:11:22 out
JH007BD 18:07:01 out
DB8888A 06:30:50 in
05:10:00
06:30:50
11:00:00
12:23:42
14:00:00
18:00:00
23:59:00
Sample Output:
1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09
题目大意:给出n个车牌号、时间点、进出状态的记录,然后查询k个时间点这时校园内的车辆个数。最后还要输出在校园里面呆的时间最长的车的车牌号,以及呆了多久的时间。如果有多辆车就按照它的字母从小到大输出车牌。
配对要求是,如果一个车多次进入未出,取最后一个值;如果一个车多次out未进入,取第一个值。
注意:一个车可能出入校园好多次,停车的时间应该取之和。
分析:为了简便,应该把小时和分钟都化简成秒数计算比较方便。
一开始所有车辆的id、时间和是进还是出(进的flag是1,出的flag是-1),对他们排序,先按照车牌号排序,再按照来的时间先后排序。
此后就能根据这样的排序后的顺序将所有满足条件(合法)的车辆进出记录保存到另一个数组里面。这个数组再按照时间先后排序。
因为多次询问值,为了避免超时,可以把他们的车辆数cnt数组先算出来。到时候直接取值就会比较快速。cnt[i]表示在i下标的记录的时间点的时候车辆的数量。数量可以由前一个数量+当前车辆的flag得到。
因为问询的时候是多个时间点按照从小到大的顺序,利用好这点能避免超时。如果上一个查询的index已经被记住,那么下一次就只需要从这个index开始找就可以了,避免重复寻找,浪费时间。