2012-11-21 44 views
-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 

問題的兩個部分是如何存儲數據以及如何有效地得到答案,我猜你是如何讀取輸入等並不重要。

感謝您的時間和見解!

回答

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()); 
0

我會用一個數據庫:

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

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

爲了防止(在你的例子)計數沃爾瑪(Walmart)查詢,您可以添加第二個開減去接近一秒的時間(或者幾分鐘給任何人機會購買某物)。

0

我會做的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) 
    } 
} 

添加代碼從時間值修剪日期的信息,只是讓所有的商店的集合,遍歷和incerment你計數每開一個。