Power of Two - Leetcode 231 - Python

  Рет қаралды 13,875

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/problems/power-o...
0:00 - Read the problem
0:10 - Drawing Explanation
2:00 - Coding Explanation
4:21 - Drawing Explanation
7:33 - Coding Explanation
8:50 - Drawing Explanation
11:41 - Coding Explanation
leetcode 231
#neetcode #leetcode #python

Пікірлер: 33
@edmonddantes587
@edmonddantes587 4 ай бұрын
it's worth studying bit manipulation. Not really that tricky (I'm dumb and can understand it), and feels good when you solve problems like this.
@edis671games
@edis671games 4 ай бұрын
cool info about bit operations
@user-tg6jl2nz7o
@user-tg6jl2nz7o 4 ай бұрын
another similar solution: return n > 0 and n == (n & -n)
@piotrwszoek9387
@piotrwszoek9387 4 ай бұрын
I'm new to code katas and python in general: Why not just: return n > 0 and math.log2(n).is_integer()? Using built-in libraries is forbidden during code interviews? I know it's probably not the fastest and clearest solution but I think it is still easier to get then bitwise operations. Thank you!
@user-kw5hm7fq5m
@user-kw5hm7fq5m Ай бұрын
What about a binary search solution For example 2 ^ x = 16 We will calculate all the possible values to x is between 0 & 16 and by using binary search. we will get the mid check if 2 ^ mid is greater or smaller or equal
@shenawy04
@shenawy04 16 күн бұрын
This is a bit more intuitive I think. class Solution: def isPowerOfTwo(self, n: int) -> bool: if n
@anon_y_mousse
@anon_y_mousse 4 ай бұрын
I personally prefer the subtractive method because not every processor has divide, and divide is slower anyway even when available.
@jshaikh3897
@jshaikh3897 Ай бұрын
Hi all, I came up with the following solution when I was trying for a brute force approach. Apologies if this sounds silly but Is this good or bad brute force approach to come up with, while interviewing : class Solution: def isPowerOfTwo(self, n: int) -> bool: # base case: if n==1: return True # check if n is odd or less than 1 or check if n has more than one bits set to 1 if ((n%2)!=0) or n
@ecchioni
@ecchioni 4 ай бұрын
Funny how the fastest solution for me was a loop.
@DeathSugar
@DeathSugar 4 ай бұрын
Coz under hood it could optimize out your loop to popcnt instruction which makes your loop hardware accelerated.
@ecchioni
@ecchioni 4 ай бұрын
@@DeathSugar Yes, I know that. The moral of the story, don't try to be too clever.
@satyamjha68
@satyamjha68 4 ай бұрын
A one liner problem!! I am actually unable to solve hard questions with acceptance rete less than 60%. Do you have any tips on how I can improve?
@SairamDasari2000
@SairamDasari2000 4 ай бұрын
Practice Practice.... That's it
@axaide4210
@axaide4210 4 ай бұрын
Practice as much as you can
@Munchen888
@Munchen888 4 ай бұрын
Even if you can’t pass the task don’t use chat gpt. If you don’t bring the task to end you are sure that you tried to solve YOURSELF.
@satyamjha68
@satyamjha68 4 ай бұрын
@@Munchen888 I never use gpt for dsa. Even I am unable to solve it like after an hour or 2 , I try to see hints. If I am still unable to solve the problem , I look at the solution. Is this approach fine?
@suhasnadiga
@suhasnadiga 4 ай бұрын
Didn't spend more than 30 mins
@MarriRahul
@MarriRahul 4 ай бұрын
similar answer: if n
@dingus2332
@dingus2332 4 ай бұрын
You can achieve the answer by left shift as well -> class Solution: def isPowerOfTwo(self, n: int) -> bool: for i in range(32): ans = 1
@MehmetDemir-xi3yy
@MehmetDemir-xi3yy 4 ай бұрын
I couldn't get it the constant time explanation. How it is constant time even if it is integer max value? If so let's say when we do linear search in list which has size of int max value, can we say it is constant time?
@MehmetDemir-xi3yy
@MehmetDemir-xi3yy 4 ай бұрын
Btw, I am referring the first solution which uses loop. It will iterate logn times as you said.
@NeetCodeIO
@NeetCodeIO 4 ай бұрын
Technically the if input n is no larger than 2^32 so its O(32) = O(1). In theory, if the input isnt bounded it's O(logn). I guess O(logn) is the more correct time complexity.
@MehmetDemir-xi3yy
@MehmetDemir-xi3yy 4 ай бұрын
So I guess you said it for in practical terms and it makes really sense. Don't get me wrong I am just trying to understand correctly not trying to be smart mouth. I understood it will iterate max 32 times(ish) but it is still bounded to N even if they are small numbers because we will log it. It may iterate 4 or 16 times according to n as you know.
@NeetCodeIO
@NeetCodeIO 4 ай бұрын
@@MehmetDemir-xi3yy no worries, i think you're right that btw
@leeroymlg4692
@leeroymlg4692 4 ай бұрын
@@MehmetDemir-xi3yy it's technically O(logn). Since the time complexity does scale as the input size increases. But we know worst case it won't iterate more than 30 times. So it can be thought of as 0(30) since big O does focus on the worst case performance This function below is slower but it's 0(1) because it's guaranteed to loop 30 times regardless of n. powers = [1] p = 1 for i in range(30): p *= 2 powers.append(p) return True if n in powers else False
@StephenRoseDuo
@StephenRoseDuo 4 ай бұрын
My solution is 99th percentile and uses sets
@kartikeysaini7272
@kartikeysaini7272 2 ай бұрын
should i quit programming I think my solution is shit: class Solution: def isPowerOfTwo(self, n: int) -> bool: l=[] for i in range(0,100): l.append(2**i) for i in range(0,len(l)): if n == l[i]: return True else: return False
@MohanRaj-vp1zt
@MohanRaj-vp1zt 4 ай бұрын
Mathematically, even -1 is an integer.
@NeetCodeIO
@NeetCodeIO 4 ай бұрын
Yes that's right. I don't think i stated otherwise anywhere in the video.
@MohanRaj-vp1zt
@MohanRaj-vp1zt 4 ай бұрын
@@NeetCodeIO : Thanks for your prompt reply :) . But you said - "We are told, that we given integers only, so we don't worry about negative exponents."
@driss9949
@driss9949 4 ай бұрын
@@MohanRaj-vp1zt and what does -1 have to do with a negative exponent ?
@lakshaybansal2470
@lakshaybansal2470 4 ай бұрын
return n > 0 && (__builtin_popcount(n) == 1);
Find the Town Judge - Leetcode 997 - Python
9:55
NeetCodeIO
Рет қаралды 16 М.
Bitwise AND of Numbers Range - Leetcode 201 - Python
18:25
NeetCodeIO
Рет қаралды 12 М.
MEU IRMÃO FICOU FAMOSO
00:52
Matheus Kriwat
Рет қаралды 40 МЛН
He sees meat everywhere 😄🥩
00:11
AngLova
Рет қаралды 10 МЛН
Must-have gadget for every toilet! 🤩 #gadget
00:27
GiGaZoom
Рет қаралды 12 МЛН
Rearrange Array Elements by Sign - Leetcode 2149 - Python
10:25
Move Zeroes - Leetcode 283 - Python
8:15
NeetCode
Рет қаралды 67 М.
How to disarm Rust
4:01
Rust without rust
Рет қаралды 75
Power of 2 | Leetcode #231 | 4 methods explained
10:17
Techdose
Рет қаралды 37 М.
Out of Boundary Paths - Leetcode 576 - Python
18:20
NeetCodeIO
Рет қаралды 15 М.
Sequential Digits - Leetcode 1291 - Python
15:02
NeetCodeIO
Рет қаралды 12 М.
Word Pattern - Leetcode 290 - Python
9:23
NeetCode
Рет қаралды 34 М.
Power of Two | LeetCode problem 231
4:03
Technosage
Рет қаралды 4,3 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 270 М.
MEU IRMÃO FICOU FAMOSO
00:52
Matheus Kriwat
Рет қаралды 40 МЛН