您的位置:首页 > 其它

LeetCode Weekly Contest 51

2017-09-28 11:00 204 查看

LeetCode Weekly Contest 51

LeetCode Weekly Contest 51
Baseball Game
题目描述

分析

Next Closest Time
题目描述

分析

Redundant Connection
问题描述

分析

K Empty Slots
题目描述

分析

682. Baseball Game

题目描述

You’re now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

Integer (one round’s score): Directly represents the number of points you get in this round.

“+” (one round’s score): Represents that the points you get in this round are the sum of the last two valid round’s points.

“D” (one round’s score): Represents that the points you get in this round are the doubled data of the last valid round’s points.

“C” (an operation, which isn’t a round’s score): Represents the last valid round’s points you get were invalid and should be removed.

Each round’s operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

Example 1:

Input: [“5”,”2”,”C”,”D”,”+”]

**Output: **30

Explanation:

Round 1: You could get 5 points. The sum is: 5.

Round 2: You could get 2 points. The sum is: 7.

Operation 1: The round 2’s data was invalid. The sum is: 5.

Round 3: You could get 10 points (the round 2’s data has been removed). The sum is: 15.

Round 4: You could get 5 + 10 = 15 points. The sum is: 30.

分析

根据题意可知这道题简单模拟积分过程即可。其中:

- +表示次轮得分为前两轮得分的和;

- D表示次轮得分为上轮得分的两倍;

- C表示删除最后一个有效的得分;

- 普通数字为本轮得分。

需要注意得是,删除有效得分时,对总得分应该减去删除的分值。

给出python代码如下:

class Solution(object):
def calPoints(self, ops):
"""
:type ops: List[str]
:rtype: int
"""
score=0;
round=[]
for op in ops:
if op=='C':
score-=round[-1]
round.pop() #删除上一个有效得分
continue
if op=='+':
round.append(round[-1]+round[-2])
score+=round[-1]
continue
if op=='D':
round.append(round[-1]*2)
score+=round[-1]
continue
intger=int(op);
round.append(intger)
score+=round[-1]
return score


681. Next Closest Time

题目描述

Given a time represented in the format “HH:MM”, form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.

You may assume the given input string is always valid. For example, “01:34”, “12:09” are all valid. “1:34”, “12:9” are all invalid.

Example 1:

Input: “19:34”

Output: “19:39”

Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later. It is not 19:33, because this occurs 23 hours and 59 minutes later.

Example 2:

Input: “23:59”

Output: “22:22”

Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day’s time since it is smaller than the input time numerically.

分析

本题大意就是给定一个时间,格式为小时:分钟,将数字重组以后找出最近的时间,题目所有的数据都是合法时间格式。由于总共四位数字,因此搜索空间很小,直接穷举即可。需要注意的是,穷举过程中,如果比原先时间提前,认为这个时间是第二天。

在进行比较时,以第一天零点为基准计算过去了多少分钟,如00:20对应20min。用新时间减旧时间,如果小于零则应该加上1440,表示第二天。另外需要注意在有解的情况下,避免比较自身,另外还需要判断时间是否为有效时间。

class Solution(object):
def nextClosestTime(self, time):
"""
:type time: str
:rtype: str
"""
time_tem=time
time=time.replace(':','')

hour=int(time[0])*10+int(time[1])
min=int(time[2])*10+int(time[3])
base=hour*60+min
max=999999
result=[0,0,':',0,0]
for i in time:
for j in time:
for k in time:
for l in time:
hour_tem=int(i)*10+int(j)
min_tem=int(k)*10+int(l)
if hour_tem>23 or min_tem>59:
continue
base_temp=hour_tem*60+min_tem
inv=base_temp-base
if inv==0:
continue
if inv<0:
inv+=1440
if inv<max:
max=inv
result[0]=i
result[1]=j
result[3]=k
result[4]=l
if max==999999:
return time_tem
return ''.join(result)


684. Redundant Connection

问题描述

In this problem, a tree is an undirected graph that is connected and has no cycles.

The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.

The resulting graph is given as a 2D-array of
edges
. Each element of edges is a pair
[u, v]
with
u < v
, that represents an undirected edge connecting nodes
u
and
v
.

Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge
[u, v]
should be in the same format, with
u < v
.

Example 1:

Input: [[1,2], [1,3], [2,3]]

Output: [2,3]

Explanation: The given undirected graph will be like this:

1
/ \
2 - 3


Example 2:

Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]

Output: [1,4]

Explanation: The given undirected graph will be like this:

5 - 1 -  2
|    |
4 -  3


分析

给出一组依赖关系
u
v
,构成一组有向图,现在要求删除一条边让这个图在形态上成为一颗树。有多组解,给出最后出现的解。在理解题以后,这道题可以使用Union Find/并查集的策略来进行。对每一个节点
i
初始化一个组号,其中每一组表示已经联通的点集合,对于每一个依赖关系,如果两个点的组号不同,则进行组的合并,否则返回当前组(首次两个点在同一组的关系就是所需要的解)。

class Solution {
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
int a[3000];
for (int i=0;i<3000;i++)
a[i]=i;
for (int i=0;i<edges.size();i++)

{
int x=edges[i][0];
int y=edges[i][1];
if (a[x]==a[y]) return edges[i];
int temp=a[x];
for (int i=0;i<3000;i++)
{
if (a[i]==temp)
a[i]=a[y];
}
}
}
};


683. K Empty Slots

题目描述

There is a garden with
N
slots. In each slot, there is a flower. The
N
flowers will bloom one by one in N days. In each day, there will be
exactly
one flower blooming and it will be in the status of blooming since then.

Given an array
flowers
consists of number from
1
to
N
. Each number in the array represents the place where the flower will open in that day.

For example,
flowers[i] = x
means that the unique flower that blooms at day
i
will be at position
x
, where
i
and
x
will be in the range from
1
to
N
.

Also given an integer
k
, you need to output in which day there exists two flowers in the status of blooming, and also the number of flowers between them is
k
and these flowers are not blooming.

If there isn’t such day, output
-1
.

Example 1:

Input:

flowers: [1,3,2]

k: 1

Output: 2

Explanation: In the second day, the first and the third flower have become blooming.

Example 2:

Input:

flowers: [1,2,3]

k: 1

Output: -1

分析

给出了一组列表,分别表示
1..N
天开花位置的顺序。求是否存在有一天两个开花的位置中间恰好有
k
朵花没有开,并且开花中间的间隔为
k


由于限定了两朵花的开放间隔必须为k,因此穷举就行。维护一个有序数组,用于存放开花位置,每当新开一朵花,我们在数组中找到这朵花的左右邻居,判断是否满足条件。有序数组可以用一颗排序二叉树维护,直接采用现成的数据类型,比如Java中的TreeSet。

class Solution {
public int kEmptySlots(int[] flowers, int k) {
TreeSet<Integer> tree=new TreeSet<Integer>();
Integer day=0;
for (Integer flower:flowers)
{
day++;
tree.add(flower);
Integer lower=tree.lower(flower);
Integer higher=tree.higher(flower);
if (lower!=null && flower-lower-1==k ||
higher!=null && higher-flower-1==k)
return day;
}
return -1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode