largest time for given digits leetcode | leetcode 949 | two solutions | java

preview_player
Показать описание


#Time #Permutations #Recursive #Largest #LargestTimeforGivenDigits #Coding #Programming #Interview #Practice #Leetcode #Easy #949 #Algorithm #Java #Preparation #NG #nickode #CookCodeTravel #CCT
Рекомендации по теме
Комментарии
Автор

Thanku for the excellent explanation 🙏

rohanjoby
Автор

Glad to see all the effort you are putting in making these videos. Hats off :)

One thing worth mentioning here is - "Why can this be not solved using Greedy Algorithm?" That is if I start to create a valid maximum time greedily using the 4 numbers. For example, with [1, 2, 3, 4] and solving for h1h2:m1m2; I can greedily pick h1 = 2 (it can not be more than 2), then pick h2 = 3 (it can not be more than 3), then pick m1 = 4 (it can reside between 0 and 5. Picking maximum from the leftover 1 and 4), and eventually pick m2 = 1. That would give me 23 : 41 that also happens to be the correct answer. But the problem with this approach is that you are trying to create the maximum time from the start. In case you are not able to create a maximum time, you are going to skip all other possible time combinations. For example, consider [2, 0, 6, 6]. Here, if we start to create maximum time greedily, we would end up with h1 = 2, h2 = 0, m1 = 6 (Invalid). So we will stop iterating as soon as we come across an invalid value of m1. What we are missing are the other valid timestamps that can be created using these 4 numbers (06 : 26 in this case). Hence, the greedy approach would not work for this case.

arpit
Автор

Thanks! Keep making more content like this.
And have a nice day. ✌🏼

sainathan
join shbcf.ru