-1

我在一次采访中被提出了这个问题,但从未真正想出一个很好的解决方案。有人有“最佳”解决方案吗?目标是效率并能够处理大量输入。

提供材料:

我得到了一长串商店及其开/关时间(比如 1000)。

问题:

在一天中的给定时间,返回有多少家商店营业

示例数据:

Sainsburys 10:00 23:00
Asda 02:00 18:00
Walmart 17:00 22:00

示例输入/输出

Input | Output
12:00 | 2
22:00 | 1 (walmart shut @ 22:00)
17:30 | 3

问题的两个部分是如何存储数据以及如何有效地获得答案,我猜你如何读取输入等并不重要。

感谢您的时间和洞察力!

4

3 回答 3

2

让我们试一试:

//First, we turn the time input into an int and then compare it to the open and
//closing time ints to determine if the shop is open. We'l use 10:00 in this example.
//get magic time int
int magicTimeInt = CInt("10:00".Replace(":",""));
int openstorecount = 0;
foreach(var shoptime in ShopTimesList)//SHopTImesList is the list of shop names and times
{
    string[] theShop = shoptime.Split(" ");
    if( CInt(theshop[1].ToString().Replace(":", "")) < magicTimeInt 
    && 
    CInt(theshop[2].ToString().Replace(":", "")) > magicTimeInt)
    {
        openstorecount++;
    }
}
Console.WriteLine("10:00 | " + openstorecount.ToString());
于 2012-11-21T11:48:46.557 回答
0

I would do it Java:

class Shop {
    String name;
    Time opening, closing;

    Shop(String name; Time opening, Time closing){
        this.name = name;
        this.opening = opening;
        this.closing = closing;
    }

    public boolean isOpen(Time time){
        return opening.before(time) && closing.after(time)
    }
}

Add code to trim date information from the time values, and just make a collection of all the stores, iterate through, and incerment your count for each open one.

于 2012-11-21T12:20:48.877 回答
0

我会使用一个数据库:

TABLE shops (
 name VARCHAR,
 open TIME,
 close TIME
)

SELECT count(*) AS number_of_shops FROM shops WHERE [input_time] BETWEEN open AND close

为了防止查询计算沃尔玛(在您的示例中),您可以添加一秒来打开并从关闭中减去一秒(或者几分钟让任何人有机会购买东西)。

于 2012-11-21T11:54:33.830 回答