회수 계산 알고리즘 말할 수 있습니다. 1,2,3,4,7,8,9,10 때때로

계산하는 법을 배우는 아이들은 종종 숫자를 알고 있지만, 그 숫자를 제대로 맞추지 못하는 것 같습니다.

예를 들어 다음과 같이 말할 수 있습니다.

1,2,3,4,7,8,9,10

때때로 아이들은 몇 가지 숫자를 건너 뛴 후 다시 돌아갑니다.

1,2,3,4,7,8,5,6,7,8,9,10

이것은 분명히 우수한 패턴입니다. 우리는 그것들을 식별해야합니다.

이 목록을 식별하려면 다음을 수행하십시오.

  1. 우리 는 목록 의 최소 M및 최대 N를 식별합니다

  2. 우리는 목록을 단계별로 살펴 봅니다. 현재 번호가 오른쪽에있는 목록의 멤버보다 크거나 같은 경우 현재 번호를 제거합니다.

  3. 나머지 목록에에서 M까지의 모든 숫자가 포함 된 경우 N, 진솔한 값을 반환합니다.

입력 목록에 하나 이상의 요소가 포함되어 있다고 가정 할 수 있습니다. 모든 정수가 음이 아닌 것으로 가정 할 수 있습니다.

테스트 사례 :

진실한 :

0
10
0 0 0
1 0 1
0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 0 1 2 3
0 1 2 3 4 5 5
0 1 1 2 2 3
0 3 6 1 4 7 2 5 8 3 4 5 6 7 8
1 3 5 7 2 3 4 5 6 7
5 6 0 1 2 3 6 7 4 5 6 7
5 6 7 8
5 5 6 7 8
4 6 7 8 3 4 5 6 7 8

거짓 :

1 0
4 3 2 1
1 2 3 7 8 9
0 1 2 3 1 3
0 1 2 3 1 3 4
0 1 2 3 1 3 2 4
0 1 2 3 1 3 2 4 3
1 3 5 7 2 4 6 8
0 1 2 1 3 4 5 6
4 5 6 3 4 5

이것은 이므로 가능한 한 빨리 답변하십시오!



답변

05AB1E , 5 바이트

나는 이것이 100 % 확실하지는 않지만 모든 테스트 사례를 통과했으며 실패한 상황을 찾을 수 없었습니다.

Ú¥1QP

온라인으로 사용해보십시오!

Ú¥1QP   Main link. Argument a
Ú       Reverse uniquify a, keeps only last occurence of each element
 ¥      Get all deltas - all 1 if ascending list
  1Q    Compare all deltas to 1
    P   Product of all results

답변

젤리 , 10 9 바이트

ṀrṂɓṚ«\Q⁼

온라인으로 사용해보십시오!

작동 원리

ṀrṂɓṚ«\Q⁼  Main link. Argument: A (array)

Ṁ          Yield the maximum of A.
  Ṃ        Yield the minimum of A.
 r         Yield R := [max(A), ... min(A).
   ɓ       Begin a new chain. Left argument: A. Right argument: R
    Ṛ      Reverse A.
     «\    Take the cumulative minimum.
       Q   Unique; deduplicate the results.
        ⁼  Compare the result with R.

답변

루비 , 59 54 41 바이트

->a{a.reverse|[]==[*a.max.downto(a.min)]}

온라인으로 사용해보십시오!


답변

파이썬 2 , 81 바이트

x=input();r=m=[]
for n in x[::-1]:r=[n][:n<m]+r;m=r[0]
print r==range(m,max(x)+1)

온라인으로 사용해보십시오!


답변

PHP , 148130 바이트

-18 바이트, @Christoph 덕분에

$a=explode(' ',$argn);$b=range(min($a),max($a));foreach($a as$i=>&$k)for(;++$i<count($a);)$k<$a[$i]?:$k=-1;echo!array_diff($b,$a);

온라인으로 사용해보십시오!


답변

자바 8, 264262 바이트

import java.util.*;l->{int m=Collections.max(l),n=Collections.min(l),i=0,q;for(;i<(q=l.size());i++)if(l.subList(i+1,q).size()>0&&l.get(i)>=Collections.min(l.subList(i+1,q)))l.remove(i--);for(i=0;n<=m;)if(i<l.size()&&l.get(i++)==n)n++;else return 0>1;return 1>0;}

설명:

여기에서 시도하십시오.

import java.util.*;                 // Import for Collections

l->{                                // Method with integer-ArrayList parameter and boolean return-type
  int m=Collections.max(l),         //  Max of the list
      n=Collections.min(l),         //  Min of the list
      i=0,q;                        //  Two temp integers
  for(;i<(q=l.size());i++)          //  Loop (1) over the list
    if(l.subList(i+1,q).size()>0    //   If the sublist right of the current item is not empty
    &&l.get(i)>=Collections.min(l.subList(i+1,q)))
                                    //   and if the current item is larger or equal to the lowest value of this sublist
      l.remove(i--);                //    Remove the current item from the main list
                                    //  End of loop (1) (implicit / single-line body)
  for(i=0;n<=m;)                    //  Loop (2) from min to max
    if(i<l.size()                   //   If the current item doesn't exceed the list's size
    &&l.get(i++)==n)                //   and the items are in order so far
      n++;                          //    Go to the next item
    else                            //   Else:
      return 0>1;//false            //    Return false
                                    //  End of loop (2) (implicit / single-line body)
  return 1>0;//true                 //  Return true
}                                   // End of method

답변

R, 88 85 바이트

y=NULL;for(i in x<-scan())if(all(i<x[-(1:(F<-F+1))]))y=c(y,i);all(min(x):max(x)%in%y)

이것은 아마도 더 아래로 골프 수 있습니다. 의 요소를 반복하고 x예정된 모든 값이 더 큰지 확인한 다음 해당 요소 만 유지합니다. 루프 후에서 min(x)까지 시퀀스를 만들고 모든 값이 정리 된 버전의에 포함되어 max(x)있는지 확인합니다 .%in%x