sepsep3
BAN USERThis is my solution for the question in ruby:
def permutation_longest_sequence(str_list)
permutations = str_list.permutation.map &:join
max_sequence_count = Hash.new()
permutations.each do |combined_str|
previous_char = nil
sequence_count = 0
combined_str.split("").each do |char|
if previous_char.nil?
previous_char = char
next
end
if previous_char == char
sequence_count+=1
else
if max_sequence_count[previous_char].nil?
max_sequence_count[previous_char] = sequence_count
else
max_sequence_count[previous_char] = sequence_count if max_sequence_count[previous_char] < sequence_count
end
sequence_count = 1
end
previous_char = char
end
end
max_value = max_sequence_count.values.max
max_sequence_count.select{|k, v| v == max_value}
end
I completed the code:
package Main;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class Main {
public static List<Integer> findMaxProductFromArray(int[] inputArray){
int n = inputArray.length;
int[] indices = new int[n];
Integer[] originalInput = new Integer[inputArray.length];
int i = 0;
for (int value : inputArray) {
originalInput[i++] = Integer.valueOf(value);
}
Arrays.sort(inputArray);
for (i = 0; i < n ; i++) {
indices[i] = Arrays.asList(originalInput).indexOf(inputArray[i]);
}
List<Integer> current_triple = new ArrayList<Integer>();
int max_product = 1;
for (i = n-1 ; i > 1 ; i--) {
List<Integer> triples = new ArrayList<Integer>();
triples.add(inputArray[i]);
for (int k = i - 1; k >= 0; k--) {
//If the index of the previous element in the array is lesser then element I'm looking that I add it to the threeway array
if (indices[k] < indices[i]) {
triples.add(inputArray[k]);
}
if (triples.size() > 2) {
//Ok so I found the best triple available for this one.
// The array was already sorted so, I already found the highest triple in ascending order
break;
}
}
if (triples.size() != 3) {
//Ignore this case as there is no valid triple
continue;
}
int tmp_product = triples.get(0) * triples.get(1) * triples.get(2);
if (tmp_product > max_product) {
max_product = tmp_product;
current_triple = triples;
}
}
return current_triple;
}
public static void main(String[] args) {
int input[] = {6,7,8,1,2,3,9,10};
System.out.println(findMaxProductFromArray(input));
}
}
Since the code was not complete, I completed it. It's a bit messy but works:
package Main;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class Main {
public static List<Integer> findMaxProductFromArray(int[] inputArray){
int n = inputArray.length;
int[] indices = new int[n];
Integer[] originalInput = new Integer[inputArray.length];
int i = 0;
for (int value : inputArray) {
originalInput[i++] = Integer.valueOf(value);
}
Arrays.sort(inputArray);
for (i = 0; i < n ; i++) {
indices[i] = Arrays.asList(originalInput).indexOf(inputArray[i]);
}
List<Integer> current_triple = new ArrayList<Integer>();
int max_product = 1;
for (i = n-1 ; i > 1 ; i--) {
List<Integer> triples = new ArrayList<Integer>();
triples.add(inputArray[i]);
for (int k = i - 1; k >= 0; k--) {
//If the index of the previous element in the array is lesser then element I'm looking that I add it to the threeway array
if (indices[k] < indices[i]) {
triples.add(inputArray[k]);
}
if (triples.size() > 2) {
//Ok so I found the best triple available for this one.
// The array was already sorted so, I already found the highest triple in ascending order
break;
}
}
if (triples.size() != 3) {
//Ignore this case as there is no valid triple
continue;
}
int tmp_product = triples.get(0) * triples.get(1) * triples.get(2);
if (tmp_product > max_product) {
max_product = tmp_product;
current_triple = triples;
}
}
return current_triple;
}
public static void main(String[] args) {
int input[] = {6,7,8,1,2,3,9,10};
System.out.println(findMaxProductFromArray(input));
}
}
and here is the code in Ruby (much cleaner):
def find_max_product(input_array)
sorted_input_array = input_array.sort
indices = []
sorted_input_array.each do |number|
indices.push(input_array.index(number))
end
current_triple = []
max_product = 1
(input_array.length - 1).downto(0) do |i|
triples = [sorted_input_array[i]]
(i-1).downto(0) do |k|
if indices[k] < indices[i]
triples.push(sorted_input_array[k])
end
if triples.length > 2
break
end
end
if triples.length != 3
next
end
tmp_product = triples.inject(:*)
if tmp_product > max_product
max_product = tmp_product
current_triple = triples
end
end
current_triple.reverse
end
puts find_max_product([1, 5, 10, 8, 9])
What is "current" used for?
- sepsep3 May 09, 2015Also, the logic before the while loop in convertQueueToLinkedList seems redundant. Doesn't the while loop handle that already?