Partition it into as few disjoint contiguous increasing subsequences as possible

0

Specification

This challenge is simple to state: your input is a non-empty array of nonnegative integers, and your task is to partition it into as few substrings as possible, such that each substring is a permutation of a consecutive integer range. More formally, if the input array is A, then the output is minimum number of partition of A into B(s):

  • Each arrays B form a partition of A into substrings. Inductively, this means that either B is the singleton array containing 1 element from A, or the elements of B is a subsequence of A, which when sorted are contiguous like x,x+1,x+2,...
  • The number of arrays B is minimal.

Example

Consider the input array A = [1,2,3,4,3,5].
Output is 2
One possible minimal partitions are B = [1,2,3],[4,3,5]. That's the only partition to 2 substrings.

{4,3,5} in sorted order is {3,4,5} which is contiguous.

Input

An array of integers A.

Output

A number, indicate the smallest number of parts A can be splitted to.

Winning criteria

Your submissions will be scored in bytes, with less bytes being better.

Optimus Prime

Posted 2018-01-27T07:39:46.983

Reputation: 9

Question was closed 2018-01-27T15:32:47.443

1This site is for programming contests (i.e., you post programming challenges in questions), general programming questions are off-topic here. – user202729 – 2018-01-27T07:52:07.173

3(in other words, because the question is a question, it's off-topic. Counter-intuitive) – user202729 – 2018-01-27T07:52:55.993

Is this a code-golf? Maybe you should clarify and add a [tag:code-golf] tag. – alephalpha – 2018-01-27T08:04:07.717

I have edited your question to suit our standards. I have also added an objective winning criterion, namely [tag:code-golf]. Feel free to edit / rollback if you disagree with the changes I’ve made. Oh, and welcome to PPCG! – Mr. Xcoder – 2018-01-27T09:12:53.553

(well, this way you get completely-useless and very-short programs) – user202729 – 2018-01-27T09:19:37.763

@JonathanAllan "contiguous subsequence" is the same as "substring". – user202729 – 2018-01-27T09:28:19.140

2Is the example answer 2 because the minimal length of any possible B is 2 or because there are 2 such minima? Seems to be some conflict between "your task is to partition it into as few contiguous increasing subsequences as possible" vs "we have Q queries updating single value on A asking minimum number of type B(s) possible" – Jonathan Allan – 2018-01-27T09:38:14.553

1This challenge needs more test cases. I'd suggest [], [3], [1,2], [2,5], [2,6,3], [1,2,11,12,3,4,9,20,22,10,5,6,14,13]. – Zgarb – 2018-01-27T13:42:09.450

Another test case: [3,3,3,2,2,2] – Zgarb – 2018-01-27T13:49:20.413

I VTC as unclear since there seems to still be confusion regarding the specification. The examples do not match up with the literal meaning of the description. – Jonathan Allan – 2018-01-27T14:24:29.030

OP's question on SO might help for clarification. – ovs – 2018-01-27T14:25:01.870

@ovs the question here has probably mutated from its intent. A new, clear question (or even questions) would be better in my opinion. – Jonathan Allan – 2018-01-27T14:27:05.187

@ovs Actually that is also unclear to me. – Jonathan Allan – 2018-01-27T14:31:43.700

@ovs The pdf posted in discussion might though.

– Jonathan Allan – 2018-01-27T14:38:52.560

link to my question : https://expirebox.com/download/d250698abbe222861fe450d7f0ee6297.html

– Optimus Prime – 2018-01-27T14:40:05.867

@JonathanAllan Oh.... The challenge doesn't match with the PDF. In that case just keep the current challenge, because... we don't care about the OP's original problem. – user202729 – 2018-01-27T15:12:33.353

@user202729 Maybe my previous edit was right :p – Jonathan Allan – 2018-01-27T15:15:06.313

@JonathanAllan Anything remaining that's still unclear? – user202729 – 2018-01-27T15:23:49.320

@user202729 yeah 1,2,3,4,5 is not a substring of 1,2,3,4,3,5 - if we go by the examples it should be "subsequence" and the word "contiguous" is just misleading; if we go for substrings then the example is wrong. Which one will lead to less answers being incorrect?! – Jonathan Allan – 2018-01-27T15:29:21.787

@JonathanAllan Edited. – user202729 – 2018-01-27T15:30:50.327

why have you removed the Link to the PDF? – Optimus Prime – 2018-01-27T15:35:49.907

@OptimusPrime "Challenges should be self-contained, because links may expire, especially expirebox links." – user202729 – 2018-01-27T15:40:13.450

@user202729 Kotlin answer returns 3 for 1,2,8,3,4,3,5,7, a substring version would return 1 whereas a subsequence version would return 3 so probably the latter (unless also buggy). – Jonathan Allan – 2018-01-27T15:48:24.200

Answers

1

Kotlin, 91 bytes

fold(mutableListOf<Int>()){r,i->r.indexOfFirst{it<i}.let{if(it<0)r+=i else r[it]=i
r}}.size

Beautified

fold(mutableListOf<Int>()) {r,i->
    r.indexOfFirst { it<i }.let {
        if (it<0) r+=i else r[it]=i
        r
    }
}.size

Test

fun MutableList<Int>.f() =
fold(mutableListOf<Int>()){r,i->r.indexOfFirst{it<i}.let{if(it<0)r+=i else r[it]=i
r}}.size

fun main(args: Array<String>) {
    val i = mutableListOf(1,2,3,4,3,5)
    println(i.f())
}

TIO

TryItOnline

jrtapsell

Posted 2018-01-27T07:39:46.983

Reputation: 915

0

JavaScript (ES6), 82 bytes

f=(a,n=Math.min(...a),i=a.indexOf(n))=>1/a[0]?i<0?1+f(a):f(a,a.splice(i,1)[0]+1):1

Neil

Posted 2018-01-27T07:39:46.983

Reputation: 95 035

0

Haskell, 139 bytes

import Data.List
l=length
q(x:r)=map([x]:)(q r)++[(x:y):v|(y:v)<-q r]
q[]=[[]]
s(x:y)=x+l y==last(x:y)
f x=minimum[l y|y<-q x,all(s.sort)y]

BlackCap

Posted 2018-01-27T07:39:46.983

Reputation: 3 576

0

Jelly, 9 bytes

Œ!Iċ€1ṀạL

Try it online!

How it works

Œ!Iċ€1ṀạL  Main link. Argument: A (array)

Œ!         Take all permutations of A.
  I        Increments; compute the forward differences of each permutation.
   ċ€1     Count the number of 1's i each array of deltas.
      Ṁ    Take the maximum.
        L  Compute the length of A.
       ạ   Take the absolute value of the difference of the maximum and the length.

Dennis

Posted 2018-01-27T07:39:46.983

Reputation: 196 637

I believe [1,2,8,3,4,3,5,7] should actually not return either 3 or 2 but 1 since there is only a single minimal length partition, [[1,2],[8],[3],[4,3,5],[7]], satisfying the criteria. (Either that or 5 since that's the minimal length) – Jonathan Allan – 2018-01-27T14:08:16.523

That may have been what the OP was going for, but the latest revision clearly asks to indicate the smallest number of parts A can be splitted to. – Dennis – 2018-01-27T14:11:06.317

Also, since [1,2,3,4,5],[3] appear as part of the worked example, reordering is clearly allowed. – Dennis – 2018-01-27T14:12:53.513

Ah, hadn't updated since going out.... – Jonathan Allan – 2018-01-27T14:13:58.717

It is not; it has been there since revision 1. By contiguous, the OP appears to mean that the partitions consist of consecutive integers, – Dennis – 2018-01-27T14:17:45.553

Yeah, it was totally unclear then! – Jonathan Allan – 2018-01-27T14:21:31.563