In approach 2, instead of shifting 1 to left i times and checking if AND result is not 0, we can also shift num to right by i and AND with 1, result will be always 0 or 1 , exactly what we need to add in curr counter, which removes inner if condition. public int largestCombination(int[] nums) { int ans = 0; for(int i = 0; i < 24; i++){ int curr = 0; for(int num : nums){ curr += (num >> i) & 1; } ans = Math.max(ans, curr); } return ans; }
Per the constraints 24 loops are ok: def largestCombination(self, candidates: List[int]) -> int: largest = 0 for i in range(24): curr = 0 for n in candidates: if (1
bro wtf 😭 my brain hurts. i am trying to keep a daily streak but these types of problems makes it hard. please make tomorows problem easy for us noobs sake
That second method with fixing a position and then counting the number of candidates that had a 1 in that position was really clever! I'm a bit surprised the run time was so much slower though. Perhaps setting the position to a variable (eg. have pos = 1, and then at the end of each loop update pos = pos
Bro has insider information
My O(n^2) beats 90% on time complexity, and 5% on memory complexity. For a guy that never do bit manipulation i will take it
In approach 2, instead of shifting 1 to left i times and checking if AND result is not 0, we can also shift num to right by i and AND with 1, result will be always 0 or 1 , exactly what we need to add in curr counter, which removes inner if condition.
public int largestCombination(int[] nums) {
int ans = 0;
for(int i = 0; i < 24; i++){
int curr = 0;
for(int num : nums){
curr += (num >> i) & 1;
}
ans = Math.max(ans, curr);
}
return ans;
}
Per the constraints 24 loops are ok:
def largestCombination(self, candidates: List[int]) -> int:
largest = 0
for i in range(24):
curr = 0
for n in candidates:
if (1
Would it really affect the time complexity? It's already O(1) in the outer loop with 32 iterations ...
@@nodeUser same time complexity but better runtime
You are amazing I don't know what more to say.
Thanks
This problem is actually simple
bro wtf 😭 my brain hurts. i am trying to keep a daily streak but these types of problems makes it hard. please make tomorows problem easy for us noobs sake
thanks :)
you are really amazing . I am following you channel daily, i won't deny that i get better by the way you teach.keep the good work!
A little confused here. How we are sure that the maximum would be 32? is it due to integer being 4 bytes or am i missing something?
An int, -2147483648 to 2147483647, uses 4 bytes(32 bits) for storage.
That second method with fixing a position and then counting the number of candidates that had a 1 in that position was really clever! I'm a bit surprised the run time was so much slower though. Perhaps setting the position to a variable (eg. have pos = 1, and then at the end of each loop update pos = pos
I tried solving with a decision tree and I blocked trying to optimize it with DP. Turned out the answer is a lot simpler.
Hey man I had fired you being papa neetcode recently (because Papas do not skip days) but you are close to getting that title again.
claude gave me the same solution, but i only can understand after i wathcing this video😜
lol how is that even possible
Love finds a way ❤️
My love for leetcode, that is.