LeetCode 1169 | Invalid Transactions | OOP, HashMap | Java

  Рет қаралды 2,041

Sleepy Cracker

Sleepy Cracker

Күн бұрын

Пікірлер: 10
@akarsh1821
@akarsh1821 3 ай бұрын
If we’re putting all the transactions in hashmap in the first for loop, then in the 2nd for loop when we’re going through all the transactions, wouldn’t we see the same transaction at both places and time will be the same too and so it will be within the 60 minute window since you put
@akarsh1821
@akarsh1821 3 ай бұрын
Oh nvm, there’s another condition that the city should be different, if it could be the same then there would be some problems. Thank you, very helpful video, learnt alot!
@sleepycracker4732
@sleepycracker4732 3 ай бұрын
good comment. glad you get it
@ryanhossain6718
@ryanhossain6718 2 жыл бұрын
Can you explain what you’re doing in the first for loop that has map.putIfAbsent
@sleepycracker4732
@sleepycracker4732 2 жыл бұрын
Good question. Using putIfAbsent function is to check do I have my "tran.name(transaction name)" in the map or not. If there is a tran.name in my map, I do not overwrite my existed key and also I append every "tran(transaction data)" along with the tran.name. If I do not have this tran.name in the map, I will create it and also create an empty list for its map value. note: You gonna initialize your array before you append any data into the list. You can use map.containsKey to check it as well, but I will have to write out an if statement for it. This is just an alternative way for me to practice one of the vary solutions to check "map.containsKey" is this helpful?
@ryanhossain6718
@ryanhossain6718 2 жыл бұрын
@@sleepycracker4732 so you are using Tran.name as the key for the hashmap map and if that name doesn’t exist add an empty array for it Next line you get the trans by name as the key and the value of that would be the empty array which you populate with the trans data? So then what happens when the name already exists you just never add it to map?
@sleepycracker4732
@sleepycracker4732 2 жыл бұрын
Key is the train.name, and value is the trans data. Once again, I do check whether or not the key exists or not in the map first. If so, I don't overwrite my data. If the key doesn't exist in the map, I create the key and initialize the arraylist for this key. for the second for loop, I check with my map(a fully built transactions for the people) to see if I have invalid transactions. (>1000 or 60 minutes same person in different city) Resources regarding putIfAbsent: www.geeksforgeeks.org/hashmap-putifabsentkey-value-method-in-java-with-examples/ docs.oracle.com/javase/8/docs/api/java/util/Map.html
@ryanhossain6718
@ryanhossain6718 2 жыл бұрын
@@sleepycracker4732 sorry for bothering you but so doesn’t this assume the name will only show up max twice how would you compare [name 1 t30, name 1 t91, name 1 t92] (dif location) Name 1 : time 30 added to map Second for loop Name 1: time 91 so wouldn’t be invalid Name 1: time 92 would be invalid because the previous persons time was within 60 and assuming dif location Is it not comparing to the trans in the map which is time 30 making both valid
@sleepycracker4732
@sleepycracker4732 2 жыл бұрын
That's ok. Glad you asking question. Let's just use the same dataset. [(name1, 30, 600, cityA), (name1, 91, 700, cityB), (name1, 92, 800, cityC)] only one person in this dataset, but for every transaction, its city is different. First loop: *First Iteration* I use putIfAbsent function to check does "name1" in the map or not, and map said "No", so I create a key for this transaction using name1. In the meanwhile, I create an empty list for this key. Once, I created a key for it. I append the current transaction into the map. map data: {name1, [(name1, 30, 600, cityA)]} *Second Iteration* I used putIfAbsent function to see does "name1" in the map or not, and map said "Yes", so I don't do anything. the next line is "map.get(tran.name).add(tran)". So I push the current tran into map map data: {name1, [(name1, 30, 600, cityA), (name1, 91, 700, cityB)]} *Third Iteration* I used putIfAbsent function to see does "name1" in the map or not, and map said "Yes", so I don't do anything. the next line is "map.get(tran.name).add(tran)". So I push the current tran into map map data: {name1, [(name1, 30, 600, cityA), (name1, 91, 700, cityB), (name1, 92, 800, cityC)]} Second loop: please describe it to me what happens for the second loop. #YourTurn
LeetCode 950 | Reveal Cards In Increasing Order | Queue | Java
5:21
Sleepy Cracker
Рет қаралды 1,7 М.
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.
Student Attendance Record II - Leetcode 552 - Python
27:10
NeetCodeIO
Рет қаралды 10 М.
3Sum With Multiplicity | Leetcode 923 | Maps | Live Coding Session
25:14
Insert Delete GetRandom O(1) - Leetcode 380 - Python
13:27
NeetCode
Рет қаралды 54 М.
Evaluate Division - Leetcode 399 - Python
17:37
NeetCodeIO
Рет қаралды 35 М.
DESIGN A LEADERBOARD | LEETCODE 1244 | PYTHON SOLUTION
12:20
Cracking FAANG
Рет қаралды 3,8 М.
Best of CES 2025
14:50
The Verge
Рет қаралды 637 М.
Lexicographical Numbers - Leetcode 386 - Python
12:44
NeetCodeIO
Рет қаралды 13 М.
Jump Game - Greedy - Leetcode 55
16:28
NeetCode
Рет қаралды 268 М.