/*Problem Statement
We have a sequence of integers, and we would like to remove all duplicate elements from this sequence.
There may be multiple ways to perform this task. For example, given the sequence { 1, 2, 1, 3 },
we could end up with either { 1, 2, 3 } or { 2, 1, 3 } as the remaining sequence, depending on which duplicate 1 we remove from the original sequence.
For this problem, we want to return the lexicographically first of of all possible remaining sequences.
A sequence S1 comes before sequence S2 lexicographically if and only if S1 has a smaller value than S2 at the lowest index at which the two sequences differ
(so, for example, { 1, 2, 3 } comes before { 2, 3, 1 }).
You will be given a int[] sequence. Return a int[] containing the sequence after all the duplicates are removed. See the examples for further clarification.
Definition
Class: HardDuplicateRemover
Method: process
Parameters: int[]
Returns: int[]
Method signature: int[] process(int[] sequence)
(be sure your method is public)
Constraints
- sequence will have between 1 and 50 elements, inclusive.
- Each element of sequence will be between 1 and 1000, inclusive.
Examples
0){5, 6, 5, 1, 6, 5} Returns: {1, 6, 5 }
There are six different ways to remove duplicates (remaining numbers are marked by '*'):
{ *5, *6, 5, *1, 6, 5},
{ *5, 6, 5, *1, *6, 5},
{ 5, *6, *5, *1, 6, 5},
{ 5, 6, *5, *1, *6, 5},
{ 5, *6, 5, *1, 6, *5},
{ 5, 6, 5, *1, *6, *5}.
The last variant is the lexicographically first.
1){3, 2, 4, 2, 4, 4} Returns: {3, 2, 4 }
2){6, 6, 6, 6, 6, 6} Returns: {6 }
3){1, 3, 2, 4, 2, 3} Returns: {1, 2, 4, 3 }
4){5, 4, 1, 5} Returns: {4, 1, 5 }
This problem statement is the exclusive and proprietary property of TopCoder, Inc.
Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited.
(c)2003, TopCoder, Inc. All rights reserved.*/
package hardDuplicateRemover;
import java.util.ArrayList;
public class HardDuplicateRemover {
private static int start = 0;
private static int end = 0;
public static int[] process(int[] sequence) {
ArrayList list = generateMinArray(sequence);
int[] result = new int[sequence.length];
for(int i = 0; i < list.size(); i++) {
Node node = (Node)list.get(i);
if(i == 0) {
int position = ((Integer)node.positions.get(0)).intValue();
result[position] = node.value;
start = end = position;
} else {
int position = 0;
int preValue = -1;
int middleValue = -1;
int postValue = -1;
ArrayList positions = node.positions;
for(int j = 0; j < positions.size(); j++) {
int p = ((Integer)node.positions.get(j)).intValue();
if(p < start) preValue = p;
else if(p > start && p < end && middleValue == -1) middleValue = p;
else if(p > end && postValue == -1) postValue = p;
}
if(postValue != -1) position = postValue;
else if (middleValue != -1) position = middleValue;
else position = preValue;
if(position < start) start = position;
else if(position > end) end = position;
result[position] = node.value;
}
}
return result;
}
/**
* Insert Sort method, a litlle bit ugly...
*/
private static ArrayList generateMinArray(int[] seq) {
ArrayList list = new ArrayList();
for(int i = 0; i < seq.length; i++) {
if(list.size() == 0)
{
ArrayList temp = new ArrayList();
temp.add(i);
list.add(new Node(seq[i], temp));
}
else {
boolean flag = false;
for(int j = 0; j < list.size(); j++) {
if(((Node)list.get(j)).value == seq[i])
{
((Node)list.get(j)).positions.add(i);
flag = true;
break;
}else if(((Node)list.get(j)).value > seq[i]) {
for(int k = list.size() - 1; k >= j; k--) {
Node temp = (Node)list.get(k);
if(k + 1 >= list.size()) list.add(temp);
else list.set(k + 1, temp);
}
ArrayList temp = new ArrayList();
temp.add(i);
list.set(j, new Node(seq[i], temp));
flag = true;
break;
}
}
if(!flag) {
ArrayList temp = new ArrayList();
temp.add(i);
list.add(new Node(seq[i], temp));
}
}
}
return list;
}
static class Node {
int value = 0;
ArrayList positions = null;
public Node(int val, ArrayList pos) {
value = val;
positions = pos;
}
}
}
We have a sequence of integers, and we would like to remove all duplicate elements from this sequence.
There may be multiple ways to perform this task. For example, given the sequence { 1, 2, 1, 3 },
we could end up with either { 1, 2, 3 } or { 2, 1, 3 } as the remaining sequence, depending on which duplicate 1 we remove from the original sequence.
For this problem, we want to return the lexicographically first of of all possible remaining sequences.
A sequence S1 comes before sequence S2 lexicographically if and only if S1 has a smaller value than S2 at the lowest index at which the two sequences differ
(so, for example, { 1, 2, 3 } comes before { 2, 3, 1 }).
You will be given a int[] sequence. Return a int[] containing the sequence after all the duplicates are removed. See the examples for further clarification.
Definition
Class: HardDuplicateRemover
Method: process
Parameters: int[]
Returns: int[]
Method signature: int[] process(int[] sequence)
(be sure your method is public)
Constraints
- sequence will have between 1 and 50 elements, inclusive.
- Each element of sequence will be between 1 and 1000, inclusive.
Examples
0){5, 6, 5, 1, 6, 5} Returns: {1, 6, 5 }
There are six different ways to remove duplicates (remaining numbers are marked by '*'):
{ *5, *6, 5, *1, 6, 5},
{ *5, 6, 5, *1, *6, 5},
{ 5, *6, *5, *1, 6, 5},
{ 5, 6, *5, *1, *6, 5},
{ 5, *6, 5, *1, 6, *5},
{ 5, 6, 5, *1, *6, *5}.
The last variant is the lexicographically first.
1){3, 2, 4, 2, 4, 4} Returns: {3, 2, 4 }
2){6, 6, 6, 6, 6, 6} Returns: {6 }
3){1, 3, 2, 4, 2, 3} Returns: {1, 2, 4, 3 }
4){5, 4, 1, 5} Returns: {4, 1, 5 }
This problem statement is the exclusive and proprietary property of TopCoder, Inc.
Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited.
(c)2003, TopCoder, Inc. All rights reserved.*/
package hardDuplicateRemover;
import java.util.ArrayList;
public class HardDuplicateRemover {
private static int start = 0;
private static int end = 0;
public static int[] process(int[] sequence) {
ArrayList list = generateMinArray(sequence);
int[] result = new int[sequence.length];
for(int i = 0; i < list.size(); i++) {
Node node = (Node)list.get(i);
if(i == 0) {
int position = ((Integer)node.positions.get(0)).intValue();
result[position] = node.value;
start = end = position;
} else {
int position = 0;
int preValue = -1;
int middleValue = -1;
int postValue = -1;
ArrayList positions = node.positions;
for(int j = 0; j < positions.size(); j++) {
int p = ((Integer)node.positions.get(j)).intValue();
if(p < start) preValue = p;
else if(p > start && p < end && middleValue == -1) middleValue = p;
else if(p > end && postValue == -1) postValue = p;
}
if(postValue != -1) position = postValue;
else if (middleValue != -1) position = middleValue;
else position = preValue;
if(position < start) start = position;
else if(position > end) end = position;
result[position] = node.value;
}
}
return result;
}
/**
* Insert Sort method, a litlle bit ugly...
*/
private static ArrayList generateMinArray(int[] seq) {
ArrayList list = new ArrayList();
for(int i = 0; i < seq.length; i++) {
if(list.size() == 0)
{
ArrayList temp = new ArrayList();
temp.add(i);
list.add(new Node(seq[i], temp));
}
else {
boolean flag = false;
for(int j = 0; j < list.size(); j++) {
if(((Node)list.get(j)).value == seq[i])
{
((Node)list.get(j)).positions.add(i);
flag = true;
break;
}else if(((Node)list.get(j)).value > seq[i]) {
for(int k = list.size() - 1; k >= j; k--) {
Node temp = (Node)list.get(k);
if(k + 1 >= list.size()) list.add(temp);
else list.set(k + 1, temp);
}
ArrayList temp = new ArrayList();
temp.add(i);
list.set(j, new Node(seq[i], temp));
flag = true;
break;
}
}
if(!flag) {
ArrayList temp = new ArrayList();
temp.add(i);
list.add(new Node(seq[i], temp));
}
}
}
return list;
}
static class Node {
int value = 0;
ArrayList positions = null;
public Node(int val, ArrayList pos) {
value = val;
positions = pos;
}
}
}