यह फिर से हेलोवीन है!


10

समस्या का विवरण

हम सभी एक ट्विक्स से प्यार करते हैं (क्योंकि यह सबसे अच्छी कैंडी है), लेकिन यह बच्चों की पहली हैलोवीन है --- हम उनके लिए प्रत्येक प्रकार की कैंडी में से कम से कम एक को पकड़ेंगे। प्रत्येक हेलोवीन नंबरलाइन एवेन्यू के सभी निवासी यह कहते हुए एक ईमेल भेजते हैं कि इस वर्ष वे किस प्रकार की कैंडी देंगे।

ओह! और हम 1 डी दुनिया में रहते हैं।

कुछ मायनों में असाधारण रूप से आलसी होने के कारण और दूसरों में नहीं, हमने मकानों का एक नक्शा बनाया है जो सड़क के किनारे स्थित हैं। हमने उनके पास कैंडी के प्रकार भी नोट किए। यहाँ हम इस वर्ष के लिए बनाया गया नक्शा है:

 [(-2, {"Kisses", "KitKats"}),
 (1, {"KitKats", "Peanut Butter Cups"}),
 (6, {"Kisses", "Twix"}),
 (9, {"Skittles"}),
 (10, {"Twix"})]

बच्चों के छोटे पैरों की खातिर, हमें कम से कम प्रत्येक प्रकार के कैंडी में से एक को इकट्ठा करने के लिए पड़ोस के किसी भी घर से शुरू होने वाले सबसे कम चलने की आवश्यकता है ।

उदाहरण

कुछ उपयोगकर्ताओं (शैगी सहित) के अनुरोध पर, मैं कुछ काम किए गए उदाहरणों में टॉस करूंगा। उम्मीद है कि इससे चीजें साफ होंगी। :) इनपुट:

 [(-2, {"Kisses", "KitKats"}),
 (1, {"KitKats", "Peanut Butter Cups"}),
 (6, {"Kisses", "Twix"}),
 (9, {"Skittles"}),
 (10, {"Twix"})]

आउटपुट:

[1, 2, 3]

एक और नक्शा और समाधान ...

इनपुट:

[(-3, {"KitKats", "Twix"}),
(-1, {"Hundred Grands"}),
(3, {"Kisses"}),
(12, {"Hundred Grands", "Twix", "KitKats"})]

आउटपुट :

[0, 1, 2]

हम 9 घरों में कैंडीज इकट्ठा करने वाले 9 घर का समन्वय शुरू कर सकते हैं और 1. यह 8 इकाइयों को चलाकर कैंडी कोटा भरता है, लेकिन क्या यह सबसे छोटा समाधान है?

नियम

उदाहरण के लिए प्रविष्टियों को समान रूप से संरचित एकल तर्क में ले जाना चाहिए और सबसे कम समाधान में आने के लिए घरों के सूचकांकों का उत्पादन करना चाहिए ।

विशिष्ट कोड गोल्फ नियम लागू होते हैं: बाइट्स जीत में सबसे छोटा सही समाधान!

PS यह दुनिया की सबसे बड़ी टेक कंपनियों में से एक द्वारा दिया गया एक साक्षात्कार प्रश्न था। यदि आपको गोल्फ पसंद नहीं है, तो एक ओ (k * n) समय समाधान खोजने की कोशिश करें जहां k कैंडी प्रकार की संख्या है और n घरों की संख्या है।

संपादित करें

जैसा कि जोनाथन एलन ने बताया, कुछ भ्रम इस मामले में "सूचकांकों" का अर्थ है। हम तर्क सूची में घरों की स्थिति का उत्पादन करना चाहते हैं और लेन पर उनके निर्देशांक नहीं।


6
यह काम किए गए उदाहरण और कुछ परीक्षण मामलों की जरूरत है।
शैगी

2
क्या हम दो तर्क ले सकते हैं; घर के नंबरों की सूची और कैंडी प्रकारों की एक समान सूची?
Adám

1
@KevinCruijssen न तो: सबसे कम समाधान में यात्रा करने के लिए घरों के सूचकांकों का उत्पादन करें
Adám

2
मैंने मान लिया कि "सूचकांक" और "पद" पर्यायवाची थे (अर्थात नंबरलाइन एवेन्यू पर पते वही होंगे जो हमें वापस लौटाने चाहिए) क्या वह गलत है?
जोनाथन एलन

1
@ केविनक्रूजसेन महान प्रश्न! इनपुट में क्रम में रहने के लिए संख्या की गारंटी दी जाती है। और मैं इस धारणा की अनुमति दूंगा कि तार में कोई भी अंक नहीं होता है क्योंकि मुझे पता है कि सभी कैंडीज संख्याओं के साथ उन्हें बाहर निकालती हैं (सौ ग्राम, और तीन Musketeers)। :)
Qfwfq

जवाबों:


3

जेली , 16 बाइट्स

ŒPṪ€ẎQLƲÐṀẎISƊÞḢ

एक मोनडिक लिंक जो इनपुट को स्वीकार करता है जैसा कि एक सूची में बताया गया है, जो सबसे कम संख्या वाले उच्चतम एवेन्यू हाउस (यदि हमें किसी भी ऑर्डर को स्वीकार करने की अनुमति दे सकता है ) से क्रमबद्ध किया गया है, जो सबसे कम नंबर वाले घर पर शुरू होने वाले सबसे कम पथ की पैदावार करता है और एवेन्यू की यात्रा करता है।

इसे ऑनलाइन आज़माएं!

हम सब इस तरह के कम से कम पथ अनुगामी बाइट्स की जगह खोजने के लिए चाहते हैं, तो ÞḢ, के साथ ÐṂ; यह भी 16 बाइट्स है।

कैसे?

ŒPṪ€ẎQLƲÐṀẎISƊÞḢ - Link: list of [index, candies]
ŒP               - power-set
        ÐṀ       - keep those for which this is maximal:
       Ʋ         -   last four links as a monad:
  Ṫ€             -     tail €ach -- this removes the candies lists from the current list
                 -                  and yields them for use now
    Ẏ            -     tighten (to a flat list of candies offered by these hoses)
     Q           -     de-duplicate (get the distinct candies offered)
      L          -     length (how many distinct candies are on offer)
              Þ  - sort (now just the indexes of remaining sets due to Ṫ) by:
             Ɗ   -   last three links as a monad:
          Ẏ      -     tighten (to a flat list of indexes since Ṫ leaves a list behind)
           I     -     incremental differences (distances between houses)
            S    -     sum
               Ḣ - head (get the first)

1
अच्छा लगा। आपके स्पष्टीकरण के लिए, मुझे लगता है कि आप दूसरे त्वरित के लिए अधिकतम का मतलब है।
निक केनेडी

हां जो मैंने किया।
जोनाथन एलन


2

05AB1E , 22 बाइट्स

æʒ€θ˜I€θ˜åP}€€нD€¥OWQÏ

मान लें कि इनपुट-सूची में संख्या सबसे कम से उच्चतम तक छांटी गई है।
यदि एक से अधिक समाधान पाए जाते हैं, तो यह उन सभी को आउटपुट करेगा।

इसे ऑनलाइन आज़माएं।

स्पष्टीकरण:

æ            # Get the powerset (all possible combinations) of the (implicit) input-list
 ʒ           # Filter this list of combinations by:
  €θ         #  Get the last items of each (the list of strings)
    ˜        #  Flatten the list
  I          #  Get the input-list again
   €θ˜       #  Get the last items of each (the list of strings) flattened as well
      å      #  Check for each if it is in the list of strings of this combination
       P     #  Check if all are present
 }           # Close the filter (we now have all combinations, containing all unique strings)
  €€н        # Only leave the first items of each item in the combination (the integers)
     D       # Duplicate this list
      €¥     # Get the deltas (forward differences) of each
        O    # Sum these deltas
         W   # Get the lowest sum (without popping the list)
          Q  # Check for each if it's equal to this minimum
           Ï # And only leave the list of integers at the truthy indices
             # (which are output implicitly as result)


0

हास्केल , 343 372 बाइट्स

@ ASCII- केवल सुधार के लिए धन्यवाद , टिप्पणी में प्रस्तावित 271 बाइट्स संस्करण भी है :)

import Data.List
import Data.Function
f s=subsequences(map(\a@(x,y)->(x,y,[(a`elemIndices`s)!!0]))s)
g f s=if f*s<=0 then f+abs f+abs s else f+abs(f-s)
h=foldl(\(a,b,c)(d,e,f)->(g a d,nub(b++e),c++f))(0,[],[])
i s=map h(filter(not.null)s)
l m=filter(\(_,x,_)->length x==(maximum$map(\(_,x,_)->length x)m))m
m=minimumBy(compare`on`(\(p,_,_)->p))
n s=(\(_,_,l)->l)$m$l$i$f s

इसे ऑनलाइन आज़माएं!


Ungolfed

import Data.List
import Data.Function

allPaths :: [(Integer, [String])] -> [[(Integer, [String], [Int])]]
allPaths xs = subsequences(map (\a@(x,y) -> (x,y,[(a`elemIndices`s) !! 0])) s)

pathLength :: Integer -> Integer -> Integer
pathLength f s = if f*s <= 0 then f + abs f + abs s else f + abs(f - s)

traversePath :: [(Integer, [String], [Int])] -> (Integer, [String], [Int])
traversePath = foldl (\(n1, a1, c1) (n2, a2, c2) -> (pathLength n1 n2, nub (a1 ++ a2), c1 ++ c2)) (0, [], [])

allTraversedPaths :: [[(Integer, [String], [Int])]] -> [(Integer, [String], [Int])]
allTraversedPaths xs = map traversePath (filter (not . null) xs)

getCompletePaths :: [(Integer, [String], [Int])] -> [(Integer, [String], [Int])]
getCompletePaths m = filter (\(_,x,_) -> length x == ( maximum $ map (\(_,x,_) -> length x) m)) m

getFastestPath :: [(Integer, [String], [Int])] -> (Integer, [String], [Int])
getFastestPath = minimumBy (compare `on` (\(p, _, _) -> p))

getPath :: [(Integer, [String])] -> (Integer, [String], [Int])
getPath xs = (\(_,_,l) -> l) getFastestPath $ getCompletePaths $ allTraversedPaths $ allPaths xs

पहला प्रयास


आपको केवल उस टपल के तीसरे तत्व को वापस करना चाहिए, और आपके आयात के बाद आपके पास एक एक्सट्रनल न्यूलाइन है
ASCII-only

315? (अभी भी केवल तीसरे तत्व को वापस करना है)
केवल


तो हाँ आप लंबाई को हार्डकोड नहीं कर सकते हैं
केवल


0

O (k * n) स्थान के साथ O (k * n) समय समाधान

xii0i<nxicii

i1j1i0<i1i0j0i0j0

इस प्रकार, हमारा एल्गोरिथ्म है:

// A[k] is the number of each candy we get from the first k houses
A := array of n bags
A[0] := {}
for k := 0 to n - 1
  A[k] := A[k - 1] + c[k - 1]
end
best_distance := ∞
best_i := -1
best_j := -1
// Find the range [i, j] such that we get all candy types
j := n
for i := n - 1 to 0
  while j > i and (A[j - 1] - A[i]) has all candy types
    j := j - 1
  end
  if (A[j] - A[i]) does not have all candy types then continue end
  distance = x[j - 1] - x[i]
  if distance < best_distance then
    best_distance = distance
    best_i = i
    best_j = j
  end
end
return best_i ..^ best_j

AO(k)O(nk)nnnO(n)O(nk)O(k)O(nk)

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.