lostintranslation
BAN USERI added each item iteratively checking if it can be added to position. Below is the code:
public static void main(String[] args) throws Exception {
printAllowedLists(Lists.newArrayList(), Lists.newArrayList(Cloth.values()));
}
private static void printAllowedLists(List<Cloth> existingList, List<Cloth> remaining) {
if (CollectionUtils.isEmpty(remaining)) {
System.out.println(existingList);
return;
}
for (int i = 0; i < remaining.size(); i++) {
Cloth clothUnderConsideration = remaining.get(i);
List<Cloth> existingListNew = Lists.newArrayList(existingList);
List<Cloth> remainingNew = Lists.newArrayList(remaining);
if (prereqExists(clothUnderConsideration, existingList)) {
existingListNew.add(clothUnderConsideration);
remainingNew.remove(clothUnderConsideration);
printAllowedLists(existingListNew, remainingNew);
}
}
}
private static Boolean prereqExists(Cloth cloth, List<Cloth> cloths) {
if (CollectionUtils.isEmpty(cloth.preconditions)) {
return true;
}
List<Cloth> prereq = cloth.preconditions;
return cloths.containsAll(prereq);
}
static enum Cloth {
SHIRT(),
TIE(SHIRT),
TROUSER(),
SOCKS(TROUSER),
SHOES(SOCKS),
BELT(SHIRT, TROUSER);
List<Cloth> preconditions;
Cloth(Cloth... cloths) {
this.preconditions = Lists.newArrayList(cloths);
}
}
- lostintranslation June 09, 2017public static void main(String[] args) {
System.out.println(firstUniqueChar("abcdeabcd"));
}
private static char firstUniqueChar(String input) {
char[] inputArray = input.toCharArray();
Integer[] frequency = new Integer[26];
List<Character> inputList = new LinkedList<>();
for (char c : inputArray) {
if (frequency[positionOfChar(c)] == null) {
frequency[positionOfChar(c)] = 1;
} else {
frequency[positionOfChar(c)]++;
}
inputList.add(c);
}
for(char c : inputList){
if(frequency[positionOfChar(c)] == 1){
return c;
}
}
return ' ';
}
private static int positionOfChar(char c) {
return (c - 'a');
}
public class MainTest {
public static void main(String[] args) {
int m=3,n=20;
Log[] x=new Log[10];
Scanner s=new Scanner(System.in);
x[0]=new Log("a",10);
x[1]=new Log("b",12);
x[2]=new Log("a",13);
x[3]=new Log("c",16);
x[4]=new Log("a",19);
x[5]=new Log("b",26);
x[6]=new Log("d",37);
x[7]=new Log("e",46);
x[8]=new Log("f",47);
x[9]=new Log("g",54);
Set<String> p = getBots(x,m,n);
System.out.println(p);
}
static class Log {
String id;
int time;
public Log(String id, int time) {
this.id = id;
this.time = time;
}
}
public static HashSet<String> getBots(Log[] logs, int m, int n) {
Queue<Log> logQueue = new LinkedList<Log>();
HashSet<String> violatingIds = new HashSet<>();
int lastTimeOnQueue = logs[0].time;
Map<String, Integer> frequency = new HashedMap<>();
for (Log log : logs) {
if (log.time - lastTimeOnQueue > n) {
for (String id : frequency.keySet()) {
if (frequency.get(id) >= m) {
violatingIds.add(id);
}
}
}
while (log.time - lastTimeOnQueue > n) {
Log lastLog = logQueue.poll();
lastTimeOnQueue = logQueue.peek().time;
frequency.put(lastLog.id, frequency.get(lastLog.id) - 1);
}
logQueue.add(log);
if (frequency.containsKey(log.id)) {
frequency.put(log.id, frequency.get(log.id) + 1);
} else {
frequency.put(log.id, 1);
}
}
return violatingIds;
}
}
We can use a linkedhashset(contains unique items) and hashset(contains repeated items) for the purpose. On reading an element if it is there in linkedhashset or hashset add it to hashset. Else if the item is not their on both linkedhashset and hashset add it to hashset. Whenever asked get the first element of linkedhashset.
- lostintranslation June 08, 2017
}}
- lostintranslation June 09, 2017