Created
September 4, 2016 23:40
-
-
Save srajappa/046cda88ff0ea6955accdcff4c532c61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
/* | |
* To execute Java, please define "static void main" on a class | |
* named Solution. | |
* | |
* If you need more classes, simply define them inline. | |
*/ | |
class Solution { | |
static final int TOTAL = 20; | |
public static void main(String[] args) { | |
List<String> input = new ArrayList<String>(); | |
for(int i=0; i< TOTAL; i++){ | |
StringBuilder strb = new StringBuilder(); | |
for(int j = 0; j < 4; j++){ | |
int x = (int) ( Math.random() * 10 + 1); | |
strb.append(x); | |
if(j<3) | |
strb.append("."); | |
} | |
input.add(strb.toString()); | |
} | |
System.out.println("The versions are below: "); | |
for(int i = 0 ; i<TOTAL; i++ ) | |
System.out.println(input.get(i)); | |
String result = findTheMaximumVersion(input); | |
System.out.println("--------------"); | |
System.out.println("Latest version is here: "+result); | |
} | |
public static String findTheMaximumVersion(List<String> input){ | |
int level = 0; | |
List<String> temp = new ArrayList<>(); | |
while(input.size()>1){ | |
//Now perform revising the list | |
int max = Integer.MIN_VALUE; | |
for(int i=0; i<input.size(); i++){ | |
String[] arr = input.get(i).split("\\."); | |
int val = Integer.parseInt(arr[level]); | |
if(val > max) | |
max = val; | |
} | |
for(int i=0; i<input.size(); i++){ | |
String[] arr = input.get(i).split("\\."); | |
int val = Integer.parseInt(arr[level]); | |
if(val== max) | |
temp.add(input.get(i)); | |
} | |
level++; | |
input = temp; | |
temp = new ArrayList<>(); | |
} | |
return input.get(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment