All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1101_1200.s1169_invalid_transactions.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy