The following leetcode exercise is one to die for.
A transaction is possibly invalid if:
- the amount exceeds $1000, or;
- if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i]
consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions
, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
- Each
transactions[i]
takes the form"{name},{time},{amount},{city}"
- Each
{name}
and{city}
consist of lowercase English letters, and have lengths between1
and10
. - Each
{time}
consist of digits, and represent an integer between0
and1000
. - Each
{amount}
consist of digits, and represent an integer between0
and2000
.
Solution:
class Solution {
public List<String> invalidTransactions(String[] transactions) {
List<String> invalidList=new ArrayList<String>();if(transactions == null || transactions.length <0 ) return invalidList;
boolean[] isInvalid=new boolean[transactions.length];
sortTransactions(transactions);
for(int i=0; i < transactions.length;i++){
checkifInvalid(transactions,isInvalid,i );
if(isInvalid[i] ){ invalidList.add(transactions[i]); }
}
return invalidList;
}
public void sortTransactions(String[] transactions){
Arrays.sort(transactions, (a, b) -> {
String[] aArr = a.split(",");
String[] bArr = b.split(",");//compare name
int temp = aArr[0].compareTo(bArr[0]);
if (temp != 0) {
return temp;
} else {
//compare time
temp = Integer.compare(Integer.parseInt(aArr[1]), Integer.parseInt(bArr[1]));
if (temp != 0) {
return temp;
} else {
//compare place
temp = aArr[3].compareTo(bArr[3]);
if (temp != 0) {
return temp;
}
}
}
//compare amount
return Integer.compare(Integer.parseInt(aArr[2]), Integer.parseInt(bArr[2]));
});
}
public void checkifInvalid(String[] transactions, boolean[] isInvalid, int pos ){
if(isInvalid[pos]) return ;
if(isInvalidPrice(transactions,pos ) ) isInvalid[pos]=true;
boolean validIsFound=false;
while(!validIsFound && pos +1 < transactions.length ){
if( (getName(transactions[pos]).equals( getName(transactions[pos+1]) )) &&
(!(getCity(transactions[pos]).equals( getCity(transactions[pos+1])))) &&
( (getTime(transactions[pos+1]) - getTime(transactions[pos])) <61 )
){
//we found two invalids
isInvalid[pos] = true;
isInvalid[pos+1] = true;
}else{
//next is valid. stop
validIsFound=true;
}
pos++;
}
}
public boolean isInvalidPrice(String[] transactions, int pos){
return getAmmount(transactions[pos]) > 1000;
}
//get name
public String getName(String sentence){
String[] components = sentence.split(",");
return components[0];
}
//get time
public int getTime(String sentence){
String[] components = sentence.split(",");
return Integer.parseInt(components[1]);
}
//get ammount
public int getAmmount(String sentence){
String[] components = sentence.split(",");
return Integer.parseInt(components[2]);
}
//get city
public String getCity(String sentence){
String[] components = sentence.split(",");
return components[3];
}
}