forked from code-clash/find-longest-distinct-subarray
optimized sample solution
This commit is contained in:
parent
f876f51e51
commit
d5ad7082a1
src
@ -1,10 +1,4 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.io.*;
|
|
||||||
import java.math.*;
|
|
||||||
|
|
||||||
import org.apache.commons.collections4.*;
|
|
||||||
import org.apache.commons.lang3.*;
|
|
||||||
import org.apache.commons.math3.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Template code to help you parse the standard input
|
* Template code to help you parse the standard input
|
||||||
@ -12,16 +6,29 @@ import org.apache.commons.math3.*;
|
|||||||
**/
|
**/
|
||||||
class Solution {
|
class Solution {
|
||||||
|
|
||||||
public static void main( String[] args ) {
|
public static void main( String[] args ) {
|
||||||
// hint: read values via Scanner methods
|
// hint: read values via Scanner methods
|
||||||
Scanner in = new Scanner( System.in );
|
var scanner = new Scanner( System.in );
|
||||||
|
|
||||||
// code your solution here
|
// code your solution here
|
||||||
|
Map<String, Integer> lastOccurrences = new HashMap<>();
|
||||||
|
|
||||||
// use error to write to console
|
int startIndex = 0;
|
||||||
System.err.println( "debug" );
|
int maxLength = 0;
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
// Write result with System.out.println()
|
while ( scanner.hasNext() ) {
|
||||||
System.out.println( "value" );
|
Integer previousOccurrence = lastOccurrences.put( scanner.next(), index++ );
|
||||||
}
|
if ( previousOccurrence != null ) {
|
||||||
|
if ( previousOccurrence >= startIndex ) {
|
||||||
|
maxLength = Math.max( maxLength, index - 1 - startIndex );
|
||||||
|
startIndex = previousOccurrence + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
maxLength = Math.max( maxLength, index - startIndex );
|
||||||
|
|
||||||
|
// Write result with System.out.println()
|
||||||
|
System.out.println( maxLength );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,10 @@ a, 1
|
|||||||
a b, 2
|
a b, 2
|
||||||
a a, 1
|
a a, 1
|
||||||
f s f e t w e n w e, 5
|
f s f e t w e n w e, 5
|
||||||
|
s f e t w s l o p q r, 10
|
||||||
|
s f e t w s f e t l o, 7
|
||||||
|
s f e t w s w t e f s, 5
|
||||||
|
s f e t w s w t e f s l, 6
|
||||||
k i u w y x f l e c g, 11
|
k i u w y x f l e c g, 11
|
||||||
b u e v l w u d t r a b q x a h x b y s y w w s k, 12
|
b u e v l w u d t r a b q x a h x b y s y w w s k, 12
|
||||||
z d k g x p a f h b c p y r y o r y k k o n u t h, 11
|
z d k g x p a f h b c p y r y o r y k k o n u t h, 11
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user