caleb-bb
Below, the problem specs:
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
Note: sequence
a0,a1, …,anis considered to be a strictly increasing ifa0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.Example
- For
sequence = [1, 3, 2, 1], the output should be
solution(sequence) = false.There is no one element in this array that can be removed in order to get a strictly increasing sequence.- For
sequence = [1, 3, 2], the output should be
solution(sequence) = true.You can remove3from the array to get the strictly increasing sequence[1, 2]. Alternately, you can remove2to get the strictly increasing sequence[1, 3].Input/Output
- [execution time limit] 12 seconds (exs)
- [input] array.integer sequenceGuaranteed constraints:
2 ≤ sequence.length ≤ 105,
-105 ≤ sequence[i] ≤ 105.- [output] booleanReturn
trueif it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise returnfalse.
Okay, so I wrote this to solve it:
def solution(sequence) do
iterate(sequence, 0)
end
def strictly_increasing?(list) do
list == Enum.uniq(list) |> Enum.sort
end
def iterate(enumerable, index) do
altered = List.delete_at(enumerable, index)
cond do
strictly_increasing?(altered) -> true
index == length(altered) -> false
true -> iterate(enumerable, index+1)
end
end
The problem with my solution is that it works, but it times out on the “submit” tests. Is there any way to speed this up, or do I need to switch to a totally difference algorithm?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #security
- #elixirconf-eu
- #api
- #forms
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cmo
Try doing it with recursion, checking each pair (or the one before if you’re skipping this one), rather than checking the entire array every iteration.
Eiji
How about this one?
caleb-bb
Thanks to you and @cmo for your responses. There are a few test cases that defeat @Eiji’s solution (it incorrectly returns
truewhen given[1,2,1,2]) but it uses tail recursion, which is the right way to go, I think. Here’s what I wound up with:I think that, in cases where you’re dealing with very long lists, the
Enummodule is gonna be too slow much of the time because it always enumerates over the whole list. In cases like that, you want tail recursion instead.al2o3cr
Ya know what else has to iterate over the whole list?
List.delete_at, especially when called withlength(list). A quick benchmark:produces the output on my machine:
Even calling
lengthon the input is not desirable, since it has to traverse the list all the way to the end.Calling
List.delete_atas part of a recursion is a quick way to end up in theO(N^2)Bad Place.A different way to think about this problem is to ask the question: what are the outcomes of adding an element to the end of an already almost-increasing list?
To express this with functions, we can use
Enum.reduce_while/3because of the control flow it gives us:The trick with
reduceand friends is to pick the right shape for the “state” data.In this problem, we have two important parts of the state:
This suggests that “state” is a tuple of
{status, last_element}.Now we need an “initial” value. The empty list is almost-increasing, and we know the minimum value for elements is
-105so we can start with a value that will always be less than the first element of list:(Note: there’s nothing magical about
:all_increasing, it’s chosen solely for human readability)There are two factors that decide what to do next:
last_elementandelementso let’s use a
case:The first case is easy: if the list is almost-increasing and
elis bigger, then the list is still almost-increasing and we should next look atelement:The next-easiest case is when we’ve already found a “bad” element and we find another one. In that case, it’s time to give up:
Almost as easy: if we’ve seen one wrong element but the current one is increasing, keep going:
The tricky one is if the current element is “bad” but there are no previous errors. In that case, we continue noting that there’s been an error AND skip
el(since it’s bad):Without passing
last_elementin that case, sequences like[1,2,1,2]aren’t correctly detected.Finally, the result of
Enum.reduce_whileis the last accumulator so it needs to be cleaned up into the boolean result.Final code with some cleanup:
Eiji
oh, my bad then
Can you please check the updated version?
When removing
firstfrom unmatched check I have addedpreviousto list in order to perform check properly in that specific case.caleb-bb
Thanks! I had previously tried a solution that sets a flag when the first bad result is found, but I wrote it so clumsily it didn’t work. Thank you for explaining the underlying reasoning.