Created
June 13, 2018 18:20
-
-
Save jD91mZM2/f4aea566aff5c252c487627103b1ba31 to your computer and use it in GitHub Desktop.
This file contains 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.util.ArrayList; | |
public class Main { | |
public static void main(String[] args) { | |
System.out.println(answer(new int[] {1, 1, 1})); | |
System.out.println(answer(new int[] {1, 2, 3, 4, 5, 6})); | |
int[] array = new int[2000]; | |
for (int i = 0; i < array.length; i += 1) { | |
array[i] = i % 6 + 1; | |
} | |
System.out.println(answer(array)); | |
} | |
public static int answer(int[] l) { | |
// I use Java here because python is too darn slow. | |
int count = 0; | |
for (int i = 0; i < l.length-2; ++i) { | |
int x = l[i]; | |
// Here I could store a list of things above x | |
// and use that since y>x and z>y means z>x. | |
// However, the allocation seems to make it even slower. | |
for (int j = i+1; j < l.length-1; j += 1) { | |
int y = l[j]; | |
// FOR SOME REASON y % x != 0 IS SO GOSH DARN SLOW | |
// THAT SIMPLY MAKING IT NOT CHECK IT WHEN x == y MAKES IT | |
// JUST ABOUT FAST ENOUGH TO PASS ALL TESTS. | |
// WHAT THE CRAP IS THIS??! | |
if (i == j || y < x || (x != y && y % x != 0)) { | |
continue; | |
} | |
for (int k = j+1; k < l.length; k += 1) { | |
int z = l[k]; | |
if (j == k || z < y || (y != z && z % y != 0)) { | |
continue; | |
} | |
count += 1; | |
} | |
} | |
} | |
return count; | |
} | |
//public static int answer(int[] l) { | |
// int count = 0; | |
// //for (int i = 0; i < l.length-2; i += 1) { | |
// for (int i = 0; i < l.length-2; i += 1) { | |
// int x = l[i]; | |
// ArrayList<Integer> above_x = new ArrayList<>(); | |
// for (int j = i+1; j < l.length; j += 1) { | |
// int y = l[j]; | |
// if (y >= x && y % x == 0) { | |
// above_x.add(y); | |
// } | |
// } | |
// for (int j = 0; j < above_x.size()-1; j += 1) { | |
// //for (int j = 0; j < l.length-1; j += 1) { | |
// int y = above_x.get(j); | |
// for (int k = j+1; k < above_x.size(); k += 1) { | |
// //for (int k = 0; k < l.length; k += 1) { | |
// int z = above_x.get(k); | |
// if (z < y || z % y != 0) { | |
// continue; | |
// } | |
// count += 1; | |
// } | |
// } | |
// } | |
// return count; | |
//} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment