1uttMentek
BAN USERWell, it doesn't solve the question.
It would be great if the question asked the natural ordering of the destinations but it asks for the itinerary route.
Your solution gives the result:
[CDG, LHR, MUC, SFO, SJC]
However it should give this one instead:
[CDG, MUC, LHR, SFO, SJC]
You are right! However all the method parameters should be final. My Eclipse -> Save Action didnt work properly that time...
- 1uttMentek June 03, 2015public class CareerCup5136629696036864 {
public static void main(String[] args) {
int[] array = new int[] { 2, 4, 3, 5 };
int busCapacity = 7;
int rounds = 4;
System.out.println(calculateEarning(array, busCapacity, rounds));
}
private static int calculateEarning(int[] array, int busCapacity, int rounds) {
int sum = 0;
int index = 0;
while (rounds > 0) {
int sumOfGroups = 0;
for (int iteration = 0; iteration < array.length; iteration++) {
int sumWithTheNextGroup = sumOfGroups + array[index % array.length];
if (busCapacity >= sumWithTheNextGroup) {
sumOfGroups = sumWithTheNextGroup;
} else {
break;
}
index++;
}
sum += sumOfGroups;
rounds--;
}
return sum;
}
}
public class CareerCup4783236498587648 {
public static void main(String[] args) {
final Lock lock = new ReentrantLock();
final Condition condition = lock.newCondition();
ThreadId threadId = new CareerCup4783236498587648.ThreadId();
threadId.setId(1);
Thread t1 = setThread(lock, condition, 1, 2, threadId);
Thread t2 = setThread(lock, condition, 2, 3, threadId);
Thread t3 = setThread(lock, condition, 3, 1, threadId);
t1.start();
t2.start();
t3.start();
}
private static class ThreadId {
private int id;
public ThreadId(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
private static Thread setThread(final Lock lock, final Condition condition, int actualThreadId, int nextThreadId, ThreadId threadId) {
Thread thread = new Thread() {
@Override
public void run() {
while (true) {
lock.lock();
try {
while (threadId.getId() != actualThreadId)
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("" + actualThreadId);
threadId.setId(nextThreadId);
condition.signalAll();
} finally {
lock.unlock();
}
}
}
};
return thread;
}
}
Your solution would be OK if there is a rowId column but what if there were no id column(s). Then there needs to be some kind of LIMIT magic to get the same result, I think..
- 1uttMentek June 16, 2015