{"id":3894,"date":"2018-03-22T06:00:48","date_gmt":"2018-03-22T04:00:48","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3894"},"modified":"2018-03-30T10:08:46","modified_gmt":"2018-03-30T08:08:46","slug":"mayores-sublistas-crecientes-2","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/mayores-sublistas-crecientes-2\/","title":{"rendered":"Mayores sublistas crecientes"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   mayoresCrecientes :: Ord a => [a] -> [[a]]\n<\/pre>\n<p>tal que (mayoresCrecientes xs) es la lista de las sublistas crecientes de xs de mayor longitud. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> mayoresCrecientes [3,2,6,4,5,1]\n   [[3,4,5],[2,4,5]]\n   \u03bb> mayoresCrecientes [3,2,3,2,3,1]\n   [[2,3],[2,3],[2,3]]\n   \u03bb> mayoresCrecientes [10,22,9,33,21,50,41,60,80]\n   [[10,22,33,50,60,80],[10,22,33,41,60,80]]\n   \u03bb> mayoresCrecientes [0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15]\n   [[0,4,6,9,13,15],[0,2,6,9,13,15],[0,4,6,9,11,15],[0,2,6,9,11,15]]\n   \u03bb> length (head (mayoresCrecientes (show (2^300))))\n   10\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (subsequences)\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nmayoresCrecientes1 :: Ord a => [a] -> [[a]]\nmayoresCrecientes1 xs =\n  [ys | ys <- xss\n      , length ys == m]\n  where xss = sublistasCrecientes xs\n        m   = maximum (map length xss)\n\n-- (sublistasCrecientes1 xs) es la lista de las sublistas crecientes de\n-- xs. Por ejemplo,\n--    \u03bb> sublistasCrecientes [3,2,5]\n--    [[],[3],[2],[5],[3,5],[2,5]]\nsublistasCrecientes :: Ord a => [a] -> [[a]]\nsublistasCrecientes xs =\n  [ys | ys <- subsequences xs\n      , esCreciente ys]\n\n-- (esCreciente xs) se verifica si la lista xs es creciente. Por\n-- ejemplo,  \n--    esCreciente [2,3,5]  ==  True\n--    esCreciente [2,3,1]  ==  False\n--    esCreciente [2,3,3]  ==  False\nesCreciente :: Ord a => [a] -> Bool\nesCreciente (x:y:zs) = x < y &#038;&#038; esCreciente (y:zs)\nesCreciente _        = True\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nmayoresCrecientes2 :: Ord a => [a] -> [[a]]\nmayoresCrecientes2 xs =\n  [ys | ys <- xss\n      , length ys == m]\n  where xss = sublistasCrecientes2 xs\n        m   = maximum (map length xss)\n\n-- (sublistasCrecientes2 xs) es la lista de las sublistas crecientes de\n-- xs. Por ejemplo,\n--    \u03bb> sublistasCrecientes2 [3,2,5]\n--    [[3,5],[3],[2,5],[2],[5],[]]\nsublistasCrecientes2 :: Ord a => [a] -> [[a]]\nsublistasCrecientes2 []  = [[]]\nsublistasCrecientes2 (x:xs) =\n  [x:ys | ys <- yss, null ys || x < head ys] ++ yss\n  where yss = sublistasCrecientes2 xs\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    \u03bb> length (head (mayoresCrecientes1 (show (2^70))))\n--    5\n--    (10.93 secs, 1,958,822,896 bytes)\n--    \u03bb> length (head (mayoresCrecientes2 (show (2^70))))\n--    5\n--    (0.02 secs, 0 bytes)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n mayoresCrecientes :: Ord a => [a] -> [[a]] tal que (mayoresCrecientes xs) es la lista de las sublistas crecientes de xs de mayor longitud. Por ejemplo, \u03bb> mayoresCrecientes [3,2,6,4,5,1] [[3,4,5],[2,4,5]] \u03bb> mayoresCrecientes [3,2,3,2,3,1] [[2,3],[2,3],[2,3]] \u03bb> mayoresCrecientes [10,22,9,33,21,50,41,60,80] [[10,22,33,50,60,80],[10,22,33,41,60,80]] \u03bb> mayoresCrecientes [0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15] [[0,4,6,9,13,15],[0,2,6,9,13,15],[0,4,6,9,11,15],[0,2,6,9,11,15]] \u03bb> length (head (mayoresCrecientes (show (2^300)))) 10 Soluciones import Data.List&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[2],"tags":[8,71,28,10,15,141,11,6,88],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3894"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=3894"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3894\/revisions"}],"predecessor-version":[{"id":3927,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3894\/revisions\/3927"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}