g1101_1200.s1169_invalid_transactions.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1101_1200.s1169_invalid_transactions
// #Medium #Array #String #Hash_Table #Sorting
// #2023_05_25_Time_362_ms_(57.14%)_Space_62.4_MB_(7.14%)
class Solution {
internal class Transaction(trans: String) {
var name: String
var time: Int
var amount: Int
var city: String
init {
val s = trans.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
name = s[0]
time = s[1].toInt()
amount = s[2].toInt()
city = s[3]
}
}
fun invalidTransactions(input: Array?): List {
val res: MutableList = ArrayList()
if (input == null || input.size == 0) {
return res
}
val map: MutableMap> = HashMap()
for (s in input) {
val trans = Transaction(s)
if (!map.containsKey(trans.name)) {
map[trans.name] = ArrayList()
}
map.getValue(trans.name).add(trans)
}
for (s in input) {
val trans = Transaction(s)
if (!isValid(trans, map)) {
res.add(s)
}
}
return res
}
private fun isValid(transaction: Transaction, map: Map>): Boolean {
if (transaction.amount > 1000) {
return false
}
for (s in map.getValue(transaction.name)) {
if (Math.abs(s.time - transaction.time) <= 60 && s.city != transaction.city) {
return false
}
}
return true
}
}