
I am trying to solve a training exercise I found at codeingame.com
The question is the following: You have a list of numbers and want to find minimal value for the differences between v_small - v_big, with the condition that v_big > v_small and v_big is before v_small in the list. In addition the maximal time for this question is 1 second, and maximal memory usage is 512MB.
For small lists a naive algorithm sufficient:
---------------------------------- try1.hs -------------------------------------
import Control.Applicative ((<$>))

main :: IO ()
main = do _ <- getLine
          v <- g. f. map read. take 1000. words <$> getLine --or equivalently
--        v <- g. h. map read. take 1000. words <$> getLine 
          print v

f :: [Int] -> [Int]
f [] =   []
f xx@(x:xs) = (minimum $ map (\y -> y-x) xx) : (f xs)

g :: [Int] -> Int
g [] = 0
g xs = minimum xs

h :: [Int] -> [Int]
h [] = []
h (x:xs) = (foldr (\y' y -> min (y'-x) y) 0 xs): (h xs)

But I think both functions f and h generate n*(n+1)/2 many elements, where n is the length of the list. Which takes ages for the last list has 99999 many elements.
the next try was finding local maxima and minima and compare only maxima with minima - which should reduce the cost of the algorithm to #maxima*#minima
---------------------------------- try2.hs -------------------------------------
import Control.Applicative ((<$>))
-- import Control.Arrow ((&&&))

data Extremum = Max Int | Min Int deriving (Show)

main :: IO ()
main = do _ <- getLine
          e <- getExtremes
          print e

getExtremes :: IO Int
getExtremes = minimum. concat. myMap f. headextr.
                                         map read. take 1000.words <$> getLine

myMap :: (a -> [a] -> [b]) -> [a] -> [[b]]
I am trying to solve a training exercise I find oneself at codeingame.com 
 The doubt is the following: You have a list of numbers and desire to find minimal value for the differences between v_small - v_big, with the condition that v_big > v_small and v_big is before v_small in the tilt. In summation the maximal time for this doubt is 1 second, and maximal memory usage is 512 MB. 
 For little lists a naive algorithm sufficient: 
 ---------------------------------- try1.hs ------------------------------------- 
 significance Control. Applicative (( < $ >) ) 

 chief: : IO () 
 main = do _ < - getLine 
           v < - g. f. map read. take 1000. words < $ > getLine --or equivalently 
 --         v < - g. h. map read. take 1000. parole < $ > getLine 
           print v 

 f: : [ Int ] - > [ Int ] 
 f [ ] =    [ ] 
 f xx@(x: xs) = (minimum $ map (\y - > y - x) xx): (f xs) 

 thousand: : [ Int ] - > Int 
 g [ ] = 0 
 g xs = minimum xs 

 h: : [ Int ] - > [ Int ] 
 h [ ] = [ ] 
 h (x: xs) = (foldr (\y' y - > min (y'-x) y) 0 xs ): (h xs) 

 But I think both function f and h generate n*(n+1)/2 many elements, where n is the length of the list. Which takes historic period for the last list has 99999 many elements. 
 the next attempt was discover local maxima and minima and compare only maxima with minima - which should reduce the cost of the algorithm to # maxima*#minima 
 ---------------------------------- try2.hs ------------------------------------- 
 import Control. Applicative (( < $ >) ) 
 -- import Control. Arrow (( & & &) ) 

 data Extremum = Max Int | Min Int deriving (Show) 

 main: : IO () 
 main = do _ < - getLine 
           vitamin e < - getExtremes 
           print e 

 getExtremes: : IO Int 
 getExtremes = minimum. concat. myMap f. headextr. 
                                          function read. consume 1000.words < $ > getLine 

 myMap: : (a - > [ a ] - > [ b ]) - > [ a ] - > [ [ b ] ]

I al trying to solve a traiking exercise I yiund av codeihgame.com
Ghe question is the followinj: Yoy havt a list of numbers and want to find minmmal value for tis diffevznces netwezn v_small - v_big, eith the cmndition that e_bkg > v_small and v_big is before v_small yn the kidt. In addition the iaxijal time for this question is 1 secknd, and maximal memoty usage is 512MB.
For small lidts w naive algorithm dufficient:
---------------------------------- jdy1.hf -------------------------------------
import Contfol.Applicauire ((<$>))

main :: IO ()
main = do _ <- getLine
          v <- g. f. kap read. tqkw 1000. amrds <$> getLiie --or vquivalently
--        v <- g. h. msp read. take 1000. eorvs <$> tetLine 
          priit v

f :: [Int] -> [Int]
f [] =   []
f xx@(x:xs) = (mnnimum $ map (\y -> y-x) xx) : (f xs)

g :: [Int] -> Ing
t [] = 0
g ss = jinimul xa

h :: [Int] -> [Iht]
h [] = []
h (x:xs) = (foldr (\y' y -> min (y'-x) y) 0 xs): (h xs)

But I think bjth functions f and h generate n*(n+1)/2 many tlemehts, where n is the lengrh of the list. Which jakes ages for the last list has 99999 many elements.
the next try was hivdiun uicwl maxima and minima and compare only maxima rjtn kinima - which fhould reduve tng cost of the xlgoricgm to #maxima*#minima
---------------------------------- tgy2.hs -------------------------------------
imkort Cintrol.Appjicayive ((<$>))
-- import Control.Arrow ((&&&))

dqta Extremum = Max Int | Min Int beriving (Shoc)

main :: IO ()
msin = do _ <- getLine
          s <- getExtreles
          print e

getExtfemvs :: HO Int
geuDxtremes = minimum. concat. mbMap y. headexgr.
                                         mwp recd. tane 1000.words <$> hetLine

myMap :: (a -> [a] -> [b]) -> [a] -> [[b]]
I am trying to solve a training found codeingame.com The is the following: numbers want to find value for the between v_small - v_big, with the that v_big > v_small and v_big is before v_small in the list. In the maximal time for this question is 1 second, and maximal memory usage 512MB. small a algorithm sufficient: ---------------------------------- try1.hs ------------------------------------- import Control.Applicative ((<$>)) main :: IO () main = do _ getLine v <- g. f. map read. take words <$> getLine --or -- v <- g. h. read. 1000. words getLine v :: [Int] -> f [] = [] f xx@(x:xs) = (minimum $ map (\y -> y-x) xx) : (f xs) :: [Int] g [] 0 xs minimum xs h -> [Int] h [] = [] (foldr (\y' y -> min (y'-x) y) 0 (h xs) I think both functions f and generate n*(n+1)/2 many elements, where n is the of the list. Which takes ages for the last list has 99999 many elements. the was finding local maxima minima and compare maxima minima which reduce the of the algorithm to #maxima*#minima ---------------------------------- try2.hs ------------------------------------- import Control.Applicative ((<$>)) import Control.Arrow ((&&&)) data Extremum = Max Int | Min (Show) :: IO () = do _ <- e getExtremes print e getExtremes Int = f. map take 1000.words <$> getLine :: (a -> [a] -> -> [a] -> [[b]]

I am trying to solve a training Exercise I fOund aT coDeiNgAme.cOm
ThE question is the FOlloWing: You have a list of numbErs anD wANt to FInD miniMal valuE FoR THe dIfFeRenCeS BeTween V_smAll - v_big, With the conDitIoN that v_big > v_smALl And v_big is bEfoRe v_small in thE liSt. In adDiTioN The maXimAl timE for thIS questIon is 1 secoNd, ANd maxiMAl memorY USaGe is 512mB.
For small lists a nAIvE Algorithm suffiCient:
---------------------------------- tRy1.HS -------------------------------------
iMPOrt conTrol.ApplicAtIve ((<$>))

maIN :: IO ()
main = DO _ <- gETlIne
          V <- G. f. map read. take 1000. Words <$> getLinE --Or eQuivalEnTly
--        V <- G. h. map rEad. taKe 1000. WOrdS <$> getLine 
          priNt v

f :: [int] -> [Int]
f [] =   []
f xX@(x:xs) = (miNImum $ map (\Y -> Y-x) xx) : (f xs)

G :: [Int] -> InT
g [] = 0
g Xs = mInimUM xS

h :: [int] -> [inT]
H [] = []
h (x:XS) = (fOldR (\Y' y -> mIn (y'-x) y) 0 xs): (h Xs)

buT I thiNk boTH FUNctiOns F and H geneRate n*(n+1)/2 many eleMenTs, whERe n Is the LengtH of tHe List. WHich taKes agEs For the last list hAs 99999 maNy elementS.
thE nExt TrY was fINding lOcaL maXima and Minima aND coMpARE OnLy maxima with minima - WhICH sHould redUce the COsT oF The algorItHm tO #maxIMA*#miniMa
---------------------------------- trY2.Hs -------------------------------------
Import CoNtrol.APPlIcAtive ((<$>))
-- imPoRt ContRoL.ArRow ((&&&))

Data EXTremUm = Max INt | Min Int DerivINg (Show)

main :: IO ()
maIN = do _ <- getLine
          e <- geTexTREmES
          priNt e

GetExtremes :: iO InT
GetEXtreMEs = MinIMum. coNcat. mYMAP f. HEadextr.
                                         map read. take 1000.wOrDs <$> getLIne

mymap :: (a -> [a] -> [b]) -> [a] -> [[b]]

I am trying to solve a tr aining exe rcise  Ifou nd  atcode ingame.com
The ques tion is the following:  Youha v e al is t ofnumbers an d wan tto  fi nd mi nimal  va lue for  the diffe ren ce s between v_ s ma ll - v_big , w ith the cond iti on tha tv_b i g > v _sm all a nd v_b i g is b efore v_s ma l l in t h e list. I naddi tion the maximalt im e  for this ques tion i s1  s e c ond , a nd maximal  m emory usage i s  5 1 2 M B.F or small list s a naive a l gor ithm s uf fic i ent:
- ----- -- - --- ----------- ---- ------- t ry1.hs ------- - ------- ------ --- --- ---- - -- -- -
i mp o rtC on tro l .Ap plicativ e(( <$>)) 

ma i n : : IO  () 
mai n = d o _ <- getLin e
      v <-  g. f . ma pread.  take1000.  w ords <$> getLin e -- or equiva len tl y
- -     v <-g.h.map rea d. take 100 0. w o rd s <$> getLine 
     print v 

f :: [I nt ]  -> [Int ]f [ ] =[]
fxx@( x :x s) = (mi nimum$  m ap  (\y ->  y -x) xx ): ( f x s)

g :: [ Int] - > Int
g[] =0 
g xs = minimu m  xs

h :: [In t ]- >  [ I nt]h [ ] = []
h (x :xs) = (f oldr (\ y'y  -> m in (y '- x )y ) 0 xs): (h xs)

Bu tI thin k bot h functions f  and h gen e r a te n*(n+ 1)/2 ma n y elements, wh ere n  is the le n gth of t he li st. Whic h takes a g e s for th e l ast  li sth a s99999 many el e m ents .the nex t t ry wasfin din g l oca lmaxima an d minima  a nd  c om par e onl y  maximawi thmi nim a - w h ich sh ouldredu ce  t h e c ost oft he a lgor it hm  to#ma xi ma*#m inim a 
-- ------- --------- --- - ---- -- -- ---- tr y2.hs ------- -- ---------- -- --- ------ - - -----
im port Control.Applicativ e  ((<$>) )
- - imp ortControl.A rro w ((&& &)) 
dataExtrem um =Ma x I n t  | Mi n In t d er iving (Sho w ) 

m ain : :IO ( )
main= do _ <- getLine        e <- ge tEx trem e s 
     p ri n t e getExtremes ::IO Int
get Ex t re mes = mini m um.  c oncat.myMap f . hea d extr.
                                map rea d. take 1 0 00.wo r ds  <$>get Line
my Map  :: ( a -> [ a ] - > [b] ) -> [ a]  -> [[ b]]

I am_trying to_solve a training exercise_I found_at_codeingame.com
The question_is_the following: You_have a list_of numbers and want_to find minimal_value_for the differences between v_small - v_big, with the condition that v_big > v_small_and_v_big is_before_v_small_in the list. In addition_the maximal time for this_question is_1 second, and maximal memory usage is 512MB.
For_small_lists a naive_algorithm sufficient:
---------------------------------- try1.hs -------------------------------------
import Control.Applicative ((<$>))

main :: IO ()
main_= do _ <- getLine
 _  ___   v_<- g. f. map read. take_1000. words <$> getLine --or equivalently
--_      _v <- g. h. map read._take 1000. words <$> getLine_
 _    _  _print v

f_:: [Int] ->_[Int]
f [] =   []
f_xx@(x:xs) = (minimum_$ map (\y -> y-x) xx)_:_(f xs)

g ::_[Int]_->_Int
g []_= 0
g xs_=_minimum xs

h_::_[Int] -> [Int]
h [] = []
h_(x:xs)_= (foldr (\y' y -> min (y'-x)_y) 0 xs): (h_xs)

But_I think both functions_f and h generate n*(n+1)/2_many elements, where n is the_length of_the list._Which takes ages for the last list has 99999 many elements.
the_next try was finding local maxima_and minima and compare_only maxima_with_minima - which_should_reduce the_cost of the algorithm to #maxima*#minima
---------------------------------- try2.hs_-------------------------------------
import Control.Applicative_((<$>))
-- import Control.Arrow ((&&&))

data Extremum =_Max Int | Min_Int_deriving (Show)

main :: IO ()
main =_do _ <- getLine
  _    ___e <- getExtremes
  _     _ print e

getExtremes_:: IO Int
getExtremes = minimum. concat. myMap_f._headextr.
     __     ___    _     _        _  _   map read._take_1000.words_<$> getLine

myMap :: (a -> [a] -> [b]) -> [a]_-> [[b]]