Find Polygon with the Largest Perimeter - Leetcode 2971 - Python

  Рет қаралды 12,783

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
🐦 Twitter: / neetcode1
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
Problem Link: leetcode.com/p...
leetcode 2971
#neetcode #leetcode #python

Пікірлер: 21
@unknown-ut5qn
@unknown-ut5qn 7 ай бұрын
Man is always on top of leetcode, it's almost like they give him insights of a new daily problem
@joelbolkert4273
@joelbolkert4273 7 ай бұрын
You can also sum all the elements then go from right to left subtracting the current element and checking if it's larger than the remainder, in which case you can return the first valid perimeter you find (or -1 if you find none). Obviously you have to sum the array first and the time complexity comes from sorting either way so not a better or worse solution.
@satyamjha68
@satyamjha68 7 ай бұрын
I did the same thing
@sanis85
@sanis85 7 ай бұрын
total = 0 heap = [] for num in nums: total += num heapq.heappush(heap, -num) this approach gave me best time running it in leetcode
@siddhantvishnu4309
@siddhantvishnu4309 7 ай бұрын
Yooo I literally solved this one 10 mins ago and now seeing neetcode upload the same problem!! 🤯🤯
@thiagot7706
@thiagot7706 7 ай бұрын
It is today daily challenge
@eshabaweja
@eshabaweja 7 ай бұрын
I like how none of the drawings on the thumbnail are polygons. they're 3D. great explanation though!
@shaco6630
@shaco6630 7 ай бұрын
Hey Neet! Great video as usual! We can also achieve O(n) time complexity in both Python & C++ by using a max heap instead of sorting. By using heapq._heapify_max(nums) + heapq._heappop_max(nums) in Python or priority_queue and pop(). Time complexity of these two implementations are O(n + 30logn) ~ O(n). Here is an example in python: def largestPerimeter(self, nums: List[int]) -> int: curSum = sum(nums) heapq._heapify_max(nums) while nums and curSum 2 else -1
@alxolr
@alxolr 7 ай бұрын
Max Heap is still O(nlogn) for inserts.
@shaco6630
@shaco6630 7 ай бұрын
​@@alxolr ​ Yes, you are correct. But that statement is not relevant here. You did not understand the solution correctly. Inserting n elements in a heap is O(nlogn), but we are not Inserting n elements. As I explained we build a heap from an array. Heapify (build a heap) of an array in Python is a O(n) operation. And we will pop at most 30 times (since max element is 10^9 according to problem constraints) in our algorithm which boils down to O(30logn) and we have final O(n) time complexity for this solution. I would recommend you familiarize yourself with the inner workings of heapq and C++ priority_queue to understand why this is true. Also, as I mentioned above the same can be achieved in C++.
@leeroymlg4692
@leeroymlg4692 7 ай бұрын
@@shaco6630 even with the constraint in mind, it's still nlogn because the input size influences the amount of iterations.
@ИгорьКабаков-з4м
@ИгорьКабаков-з4м 7 ай бұрын
Thank you!
@banchanbet4524
@banchanbet4524 7 ай бұрын
what i did was assuming that the largest perimeter is the sum of the whole array, then i kept removing the maximum element that doesnt satisfy the condition and subtract that element from the total sum, then return -1 if the array has its length < 3, otherwise return the result
@Baseballchampion
@Baseballchampion 7 ай бұрын
I believe this is similar to the prefix sum solution he describes before his optimal solution.
@yg1095
@yg1095 7 ай бұрын
Can you add this and all the new problems over the past few months to the Neetcode All section?
@vietnguyenquoc4948
@vietnguyenquoc4948 7 ай бұрын
I tried to solve this using top down DP because I thought it is another variant of house robber to no avail then tried greedy. I really still wonder if this can be solve using top down DP with memoize or not, if anyway did please do enlighten me
@xingyuxiang1637
@xingyuxiang1637 7 ай бұрын
Though the input is 10^5, and the solution only looks at its closest neighbor in one direction, it is not a greedy problem.
@bollywoodboxoffice7017
@bollywoodboxoffice7017 7 ай бұрын
please add all your new problem to neetcode all sheet
@rajsuriyang3427
@rajsuriyang3427 7 ай бұрын
Though it was greedy changed my mind to dynamic programming and found out it is greedy😢.
@CS_n00b
@CS_n00b 7 ай бұрын
pretty easy for a medium
@divyanshsharma673
@divyanshsharma673 7 ай бұрын
It's not... you might have solved 500 medium problems but this fact doesn't impact the level of this question.
Naming a Company - Leetcode 2306 - Python
16:38
NeetCodeIO
Рет қаралды 10 М.
Find All People With Secret - Leetcode 2092 - Python
14:35
NeetCodeIO
Рет қаралды 14 М.
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 61 МЛН
小路飞嫁祸姐姐搞破坏 #路飞#海贼王
00:45
路飞与唐舞桐
Рет қаралды 17 МЛН
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 835 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 234 М.
Can you steal something that's already free?
11:48
NeetCodeIO
Рет қаралды 25 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 657 М.
Can I Run Youtube Entirely From My Terminal? (No Browser)
15:31
I regret doing this...
1:20:07
Tsoding Daily
Рет қаралды 75 М.
JavaScript Visualized - Closures
11:34
Lydia Hallie
Рет қаралды 42 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 161 М.
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 61 МЛН