Power of Two - Leetcode 231 - Python

  Рет қаралды 15,254

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 35
@edmonddantes587
@edmonddantes587 7 ай бұрын
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.
@servantofthelord8147
@servantofthelord8147 2 ай бұрын
ikr!
@李炎-d5q
@李炎-d5q 7 ай бұрын
another similar solution: return n > 0 and n == (n & -n)
@wp4444
@wp4444 7 ай бұрын
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!
@ecchioni
@ecchioni 7 ай бұрын
Funny how the fastest solution for me was a loop.
@DeathSugar
@DeathSugar 7 ай бұрын
Coz under hood it could optimize out your loop to popcnt instruction which makes your loop hardware accelerated.
@ecchioni
@ecchioni 7 ай бұрын
@@DeathSugar Yes, I know that. The moral of the story, don't try to be too clever.
@edis671games
@edis671games 7 ай бұрын
cool info about bit operations
@jshaikh3897
@jshaikh3897 4 ай бұрын
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
@satyamjha68
@satyamjha68 7 ай бұрын
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 7 ай бұрын
Practice Practice.... That's it
@axaide4210
@axaide4210 7 ай бұрын
Practice as much as you can
@Munchen888
@Munchen888 7 ай бұрын
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 7 ай бұрын
@@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 7 ай бұрын
Didn't spend more than 30 mins
@kartikeysaini7272
@kartikeysaini7272 5 ай бұрын
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
@shenawy04
@shenawy04 3 ай бұрын
This is a bit more intuitive I think. class Solution: def isPowerOfTwo(self, n: int) -> bool: if n
@ETR12935
@ETR12935 2 ай бұрын
Here after getting runtime error with loops haha
@Goe-k5w
@Goe-k5w 4 ай бұрын
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
@MarriRahul
@MarriRahul 7 ай бұрын
similar answer: if n
@anon_y_mousse
@anon_y_mousse 7 ай бұрын
I personally prefer the subtractive method because not every processor has divide, and divide is slower anyway even when available.
@dingus2332
@dingus2332 7 ай бұрын
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
@StephenRoseDuo
@StephenRoseDuo 7 ай бұрын
My solution is 99th percentile and uses sets
@MehmetDemir-xi3yy
@MehmetDemir-xi3yy 7 ай бұрын
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 7 ай бұрын
Btw, I am referring the first solution which uses loop. It will iterate logn times as you said.
@NeetCodeIO
@NeetCodeIO 7 ай бұрын
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 7 ай бұрын
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 7 ай бұрын
@@MehmetDemir-xi3yy no worries, i think you're right that btw
@leeroymlg4692
@leeroymlg4692 7 ай бұрын
@@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
@MohanRaj-vp1zt
@MohanRaj-vp1zt 7 ай бұрын
Mathematically, even -1 is an integer.
@NeetCodeIO
@NeetCodeIO 7 ай бұрын
Yes that's right. I don't think i stated otherwise anywhere in the video.
@MohanRaj-vp1zt
@MohanRaj-vp1zt 7 ай бұрын
@@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 7 ай бұрын
@@MohanRaj-vp1zt and what does -1 have to do with a negative exponent ?
@lakshaybansal2470
@lakshaybansal2470 7 ай бұрын
return n > 0 && (__builtin_popcount(n) == 1);
Find the Town Judge - Leetcode 997 - Python
9:55
NeetCodeIO
Рет қаралды 18 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 90 М.
How Notion Cut Latency by 20%
19:00
NeetCodeIO
Рет қаралды 62 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 38 М.
Bitwise AND of Numbers Range - Leetcode 201 - Python
18:25
NeetCodeIO
Рет қаралды 14 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 236 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 310 М.
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 84 М.
Greatest Common Divisor Traversal - Leetcode 2709 - Python
24:30
Sequential Digits - Leetcode 1291 - Python
15:02
NeetCodeIO
Рет қаралды 13 М.
The Midpoint Circle Algorithm Explained Step by Step
13:33
NoBS Code
Рет қаралды 61 М.
Compiled Python is FAST
12:57
Doug Mercer
Рет қаралды 111 М.