title stringlengths 1 100 | titleSlug stringlengths 3 77 | Java int64 0 1 | Python3 int64 1 1 | content stringlengths 28 44.4k | voteCount int64 0 3.67k | question_content stringlengths 65 5k | question_hints stringclasses 970
values |
|---|---|---|---|---|---|---|---|
Python || DP || Recursion -> Space Optimization | best-time-to-buy-and-sell-stock-iv | 0 | 1 | ```\n\n#Recursion \n#Time Complexity: O(2^n)\n#Space Complexity: O(n)\nclass Solution1:\n def maxProfit(self, k: int, prices: List[int]) -> int:\n def solve(ind,buy,cap):\n if ind==n or cap==0:\n return 0\n profit=0\n if buy==0: # buy a stock\n ta... | 2 | You are given an integer array `prices` where `prices[i]` is the price of a given stock on the `ith` day, and an integer `k`.
Find the maximum profit you can achieve. You may complete at most `k` transactions: i.e. you may buy at most `k` times and sell at most `k` times.
**Note:** You may not engage in multiple tran... | null |
🔥Mastering Buying and Selling Stocks || From Recursion To DP || Complete Guide By Mr. Robot | best-time-to-buy-and-sell-stock-iv | 1 | 1 | # \u2753 Buying and Selling Stocks with K Transactions \u2753\n\n\n## \uD83D\uDCA1 Approach 1: Recursive Approach\n\n### \u2728 Explanation\nThe logic and approach for this solution involve recursively exploring the possibilities of buying and selling stocks with a given number of transactions. The function `Recursive`... | 2 | You are given an integer array `prices` where `prices[i]` is the price of a given stock on the `ith` day, and an integer `k`.
Find the maximum profit you can achieve. You may complete at most `k` transactions: i.e. you may buy at most `k` times and sell at most `k` times.
**Note:** You may not engage in multiple tran... | null |
188: Solution with step by step explanation | best-time-to-buy-and-sell-stock-iv | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 5 | You are given an integer array `prices` where `prices[i]` is the price of a given stock on the `ith` day, and an integer `k`.
Find the maximum profit you can achieve. You may complete at most `k` transactions: i.e. you may buy at most `k` times and sell at most `k` times.
**Note:** You may not engage in multiple tran... | null |
Python | O(N*K) | Faster than 90% | Memory less than 96% | best-time-to-buy-and-sell-stock-iv | 0 | 1 | ```\nclass Solution:\n def maxProfit(self, k: int, prices: List[int]) -> int:\n if k == 0:\n return 0\n \n dp_cost = [float(\'inf\')] * k\n dp_profit = [0] * k\n \n for price in prices:\n for i in range(k):\n if i == 0:\n ... | 2 | You are given an integer array `prices` where `prices[i]` is the price of a given stock on the `ith` day, and an integer `k`.
Find the maximum profit you can achieve. You may complete at most `k` transactions: i.e. you may buy at most `k` times and sell at most `k` times.
**Note:** You may not engage in multiple tran... | null |
Simple solution with recursion and slicing easy to understand. | rotate-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRotating an array involves shifting its elements to the right by a given number of steps. This can be done in various ways, but the provided Python code takes a unique approach by using list reversal. The core idea behind this approach is... | 11 | Given an integer array `nums`, rotate the array to the right by `k` steps, where `k` is non-negative.
**Example 1:**
**Input:** nums = \[1,2,3,4,5,6,7\], k = 3
**Output:** \[5,6,7,1,2,3,4\]
**Explanation:**
rotate 1 steps to the right: \[7,1,2,3,4,5,6\]
rotate 2 steps to the right: \[6,7,1,2,3,4,5\]
rotate 3 steps to... | The easiest solution would use additional memory and that is perfectly fine. The actual trick comes when trying to solve this problem without using any additional memory. This means you need to use the original array somehow to move the elements around. Now, we can place each element in its original location and shift ... |
Simple solution using queue | rotate-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nStart to rotate at the beginning of the array and keep the replaced value on a queue with size k. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nNormalize `k` when `k > l` by `k = k % l`\nLoop from the beginning,... | 0 | Given an integer array `nums`, rotate the array to the right by `k` steps, where `k` is non-negative.
**Example 1:**
**Input:** nums = \[1,2,3,4,5,6,7\], k = 3
**Output:** \[5,6,7,1,2,3,4\]
**Explanation:**
rotate 1 steps to the right: \[7,1,2,3,4,5,6\]
rotate 2 steps to the right: \[6,7,1,2,3,4,5\]
rotate 3 steps to... | The easiest solution would use additional memory and that is perfectly fine. The actual trick comes when trying to solve this problem without using any additional memory. This means you need to use the original array somehow to move the elements around. Now, we can place each element in its original location and shift ... |
simplest way using insert() and pop() | rotate-array | 0 | 1 | # Intuition\nTo rotate the array nums to the right by k steps, we can follow these steps:\n\n1. If k is greater than the length of nums, reduce k by taking its modulo with the length of nums. This ensures that k falls within the range of valid rotations.\n2. Perform the rotation in-place by repeatedly popping the last ... | 26 | Given an integer array `nums`, rotate the array to the right by `k` steps, where `k` is non-negative.
**Example 1:**
**Input:** nums = \[1,2,3,4,5,6,7\], k = 3
**Output:** \[5,6,7,1,2,3,4\]
**Explanation:**
rotate 1 steps to the right: \[7,1,2,3,4,5,6\]
rotate 2 steps to the right: \[6,7,1,2,3,4,5\]
rotate 3 steps to... | The easiest solution would use additional memory and that is perfectly fine. The actual trick comes when trying to solve this problem without using any additional memory. This means you need to use the original array somehow to move the elements around. Now, we can place each element in its original location and shift ... |
Simple ONE LINE solution. Without bit operators. | reverse-bits | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Format the given number into a binary format.\n2. After formating the length of the string of bites can be less than 32 bits (as zeros before the first 1 don\'t compute), so the rest of the bits are filled with zeros.\n3. Reverse the string of by... | 0 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
Python bitshifting 27ms<99.14% 16.22MB<27.67% | reverse-bits | 0 | 1 | # Approach\nTake the LSBs of n, shift them into the MSBs of the result. Keep in mind the flip is within a 32 bit wide space\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nclass Solution:\n def reverseBits(self, n: int) -> int:\n result = 0\n for _ in rang... | 0 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
5 Liner, 98.10% Beats !! | reverse-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
Beats 90 %. One liner code.Recurrsion | reverse-bits | 0 | 1 | # Intuition\nDone through recurrsion . Beats 90%\n\n# Approach\nRecurrsion\n\n# Complexity\n- Time complexity:\nO(log(n))\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def reverseBits(self, n: int) -> int:\n return self.reverse(n,0,0)\n def... | 3 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
5 liner simpler code | By Recursion | reverse-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Done by RECURSION -->\n\n# Complexity\n- Time complexity:\n<!-- log(n) -->\n\n- Space complexity:\n<!-- O(n) -->\n\n# Code\n```\nclass Solution:\n def reverseBits(self, n: int) -> int:\n def f(n,r,count):\n ... | 3 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
python | reverse-bits | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def reverseBits(self, n: int) -> int:\n res = 0\n for i in range(32):\n \n if n >> 1 << 1 != n:\n n = n >> 1\n res += pow(2,31-i)\n else:\n n = n >> 1\n return res\n \n\n``... | 1 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
190: Solution step by step explanation | reverse-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe can reverse bits of a given number by iterating over all bits of the given number and adding the bits in the reverse order. To reverse the bits, we can use a variable and left shift it by 1 in each iteration. We can then ... | 26 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
Easy to understand code | reverse-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
Python 3 (40ms) | Real BIT Manipulation Solution | reverse-bits | 0 | 1 | ```\nclass Solution:\n def reverseBits(self, n: int) -> int:\n res = 0\n for _ in range(32):\n res = (res<<1) + (n&1)\n n>>=1\n return res\n``` | 42 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
Python3 Solution (One Line) | reverse-bits | 0 | 1 | ```\nclass Solution:\n def reverseBits(self, n: int) -> int:\n return int(((\'{0:032b}\'.format(n))[::-1]),2)\n```\n* (\'{0:032b}\'.format(n) Converts to a 32 bit representation of the number in binary. This is formatted as a string\n* [::-1] Reverses the String\n* int(...., 2) converts the number back to the... | 16 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
FASTEST AND EASIEST || LEFT SHIFT AND BINARY-DECIMAL CONVERSION || FASTEST | reverse-bits | 0 | 1 | ```\nclass Solution:\n\tdef reverseBits(self, n: int) -> int:\n\n\t\tbinary = bin(n)[2:]\n\n\t\trev = binary[::-1]\n\t\treverse = rev.ljust(32, \'0\')\n\t\treturn int(reverse, 2)\n```\nDo upvote if its helpful,Thanks. | 1 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
Beats : 37.07% [24/145 Top Interview Question] | reverse-bits | 0 | 1 | # Intuition\n*Using bitwise, left and right operation*\n\n# Approach\n\nThis is a Python code defines a method `reverseBits` that takes an integer `n` as an input and returns an integer as the output. The method reverses the bits of the input integer.\n\nThe algorithm used in the code is to extract the least significan... | 2 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
⭐Layman's 🔥One Line Code🔥 in 🐍 ⭐ | reverse-bits | 0 | 1 | \n\n---\n\n# Complexity\n- Time complexity: $$O(log n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(log n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\... | 1 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
Explained, Beginner Friendly one-liner Python3 fast solution | reverse-bits | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nInt -> Bin -> reverse -> Int\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n- Detailed \n```\nclass Solutio... | 4 | Reverse bits of a given 32 bits unsigned integer.
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the s... | null |
🥇 O( log N ) | C++ | PYTHON | JAVA || EXPLAINED || ; ] ✅ | number-of-1-bits | 1 | 1 | \n**UPVOTE IF HELPFuuL**\n\n# Intuition\nOmitting `0` bits, focusing on `1` -> `set` bits only.\n\nA basic approach is to convert number `n` to binary string, then counting the occurances of `char 1`.\n\n# Approach 1 -> Right Shift\n`n >>= 1` means `set n to itself shifted by one bit to the right`. \nThe expression eva... | 28 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
【Video】Give me 5 minutes - How we think about a solution | number-of-1-bits | 1 | 1 | # Intuition\nUse bitwise AND 1(= & 1)\n\n---\n\n# Solution Video\n\nhttps://youtu.be/3Vv1jvWc6eg\n\n\u25A0 Timeline of the video\n\n`0:03` Right shift\n`1:36` Bitwise AND\n`3:09` Demonstrate how it works\n`6:03` Coding\n`6:38` Time Complexity and Space Complexity\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscri... | 10 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
Several 1-line codes vs Brian Kernighan’s algorithm||0ms beats 100% | number-of-1-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1-line\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor completeness, one loop solution(Brian Kernighan\u2019s algorithm) is also presented. Using `n&=(n-1)` to change the variable within the loop.\n\nFor `n&=(n-1... | 6 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
💯Faster✅💯 Lesser✅3 Methods🔥Simple Count🔥Brian Kernighan's Algorithm🔥Bit Manipulation🔥Explained | number-of-1-bits | 1 | 1 | # \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 3 ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \nThe problem is asking us to write a function that takes a binary representation of a posi... | 55 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
🔥🔥🔥BEATS 100% USERS BY RUN TiME 🔥🔥🔥 ONE LiNE OF CODE✅by PRODONiK | number-of-1-bits | 1 | 1 | # Intuition\nThe Hamming weight of an integer is essentially the count of set bits (1s) in its binary representation. The task is to find an efficient way to calculate this count.\n\n# Approach\nThe provided code leverages the `Integer.bitCount()` method, which is a built-in function in Java specifically designed to co... | 2 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
One line python solution | number-of-1-bits | 0 | 1 | # Code\n```\nclass Solution:\n def hammingWeight(self, n: int) -> int:\n return str(bin(n)).count(\'1\')\n``` | 2 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
Python3 Solution | number-of-1-bits | 0 | 1 | \n```\nclass Solution:\n def hammingWeight(self, n: int) -> int:\n return bin(n).count(\'1\') \n``` | 2 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
Easy Python solution || Beginner Friendly || Binary Hamming Weight Counter..... | number-of-1-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking to find the number of \'1\' bits in the binary representation of a given integer n. In other words, it\'s asking for the Hamming weight or the number of set bits in the binary form of the number.\n# Approach\n<!-- De... | 4 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
✅☑[C++/Java/Python/JavaScript] || Beats 100% || EXPLAINED🔥 | number-of-1-bits | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n1. **Function Purpose:** The `hammingWeight` function aims to count the number of set bits (bits with a value of 1) in the binary representation of an unsigned 32-bit integer (`uint32_t`).\n\n1. **Initialization:** Initializes an... | 2 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
[We💕Simple] 7 Ways to Count Bits (Easy to Hard) | number-of-1-bits | 0 | 1 | ## 1. Using native method\n```Python []\nclass Solution:\n def hammingWeight(self, n: int) -> int:\n return n.bit_count()\n```\n``` Kotlin []\nclass Solution {\n fun hammingWeight(n:Int):Int {\n return n.countOneBits()\n }\n}\n```\n\n## 2. Using binary string representation\n```Python []\nclass S... | 6 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
✅✅Efficient Hamming Weight Computation in Multiple Programming Languages🔥🔥🔥 | number-of-1-bits | 1 | 1 | \n\n\n# Description\n\n## Intuition\nThe problem appears to be related to counting the number of set bits (1s) in the binary representation of a given integer `n`. The initial intuiti... | 9 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
✅One liner✅ O(1)✅Beats 100%✅For Beginners✅ | number-of-1-bits | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O... | 11 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
One liner code in Python 3 | number-of-1-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn the question it is given that the input will be given as \nInput: n = 00000000000000000000000000001011\nbut it isn,t. Actually the input is given as decimal form that is 11, you have to convert to binary before proceeding further. \n\n... | 1 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
⭐One-Liner CheatCode in🐍 ||🔥Beats 99.81%🔥⭐ | number-of-1-bits | 0 | 1 | \n\n\n# Code\n```\nclass Solution:\n def hammingWeight(self, n: int) -> int:\n return str(bin(n)).count(\'1\')\n``` | 1 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
Python Solution beats 100% of Submissions | number-of-1-bits | 0 | 1 | # Code\n```\nclass Solution:\n def hammingWeight(self, n: int) -> int:\n return bin(n).count(\'1\')\n``` | 1 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
✅Mastering Number of set-Bits ✅For beginners with Full Explanation | number-of-1-bits | 1 | 1 | \n```cpp []\nclass Solution {\npublic:\n int hammingWeight(uint32_t n) {\n int count = 0;\n while (n != 0) {\n if (n & 1) { // Check the last bit\n count++;\n }\n n = n >> 1; // Right shift to move to the next bit\n }\n return count;\n ... | 1 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
✅100% Fast || Full Easy Explanation || Beginner Friendly 🔥🔥🔥 | number-of-1-bits | 1 | 1 | # Intuition\nThe problem requires counting the number of \'1\' bits in the binary representation of an unsigned integer. This operation is commonly known as the Hamming weight. We can perform this task by iterating through each bit in the binary representation and counting the number of set bits.\n\n# Approach\n- Itera... | 2 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
Beginner-friendly easy-to-understand one-liner :) | number-of-1-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSplit the string into a list, then return the sum of that list\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Format the number as a 32-bit string\n2. Convert it to a list\n3. Use list comprehension to make a l... | 0 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
Confused ? Here is the solution with explanation | number-of-1-bits | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor those who are wondering about if the input is in binary or string or integer\n\nThe input is in decimal, meaning the input is 11 and what its shown in the examples is its binary 32 bits, ie 00000000000000000000000000001011\nSo basically the inpu... | 0 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
Amazing Logic With Right Shift | number-of-1-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 52 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
Solution with While loop | number-of-1-bits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 8 | Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).
**Note:**
* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input ... | null |
Python solution with graphical explanation | house-robber | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTurn all conditions into a tree and traverse it through **DFS** to find the answer. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n## Step1. Build the tree\nWe can start from any point in the array, but to maximi... | 4 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into... | null |
Binbin's knight is sooo smart and the best teacher! | house-robber | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into... | null |
Python solution with one queue | binary-tree-right-side-view | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is a classic level order traversel (bfs) problem as we can see we need rightmost node in each tree level\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n**step 1** => we need a queue for this in which we\'ll... | 2 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Solution | binary-tree-right-side-view | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<int> rightSideView(TreeNode* root) {\n vector<int>ans;\n queue<TreeNode*> q;\n if(root==NULL)\n return ans;\n q.push(root);\n while(1)\n {\n int size=q.size();\n if(size==0)\n return ans;\n... | 212 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Iterative approach(BFS)✅ | O( n)✅ | (Step by step explanation)✅ | binary-tree-right-side-view | 0 | 1 | # Intuition\nThe problem requires obtaining the right side view of a binary tree. The right side view consists of all the nodes that are visible when viewing the tree from the right side.\n\n# Approach\nThe approach is based on a level-order traversal of the binary tree using a queue. We traverse the tree level by leve... | 6 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
O(n) space and time complexity - Simple queue approach | binary-tree-right-side-view | 0 | 1 | # Intuition\nAppend the last element (as we are iterating from left to right and poping from queue from left) of every level of BFS in a list\n\n# Approach\n1)Initialize a queue and append root (return empty list if no root) and iterate till length of queue is zero\n2)Iterate for loop for length of queue of that level ... | 3 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Beats100%(Recursive approach)✅ | O( n)✅ | (Step by step explanation)✅ | binary-tree-right-side-view | 0 | 1 | # Intuition\nThe problem is to return the right side view of a binary tree, i.e., the values of the nodes that are visible from the right side.\n\n# Approach\n1. **Initialize**: Start with an empty vector `result` to store the right side view.\n - *Reason*: We will be adding the values of nodes that are visible from ... | 5 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Python: God Tier Simple Solution no cap (DFS) | binary-tree-right-side-view | 0 | 1 | # Intuition\nThis solution\'s key insight lies in understanding how a depth-first traversal (DFS) of the tree interacts with the ordering of nodes at each level. By recursively visiting the left child before the right child, the solution ensures that the last node visited at each level is the rightmost node. This is be... | 0 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
✔️ C++ || PYTHON || EXPLAINED || ; ] | binary-tree-right-side-view | 0 | 1 | **UPVOTE IF HELPFuuL**\n\nThe demand of the question is to get the nodes at ```i``` level that are rightmost.\nAny nodes left to that would not be included in answer.\n\n\n**Level order traversal could help but that uses addtional space for the QUEUE**\n\n**APPROACH with no extra space**\n\nMaintain an array / vector.\... | 116 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
[ Python ] | Simple & Clean Solution using BFS | binary-tree-right-side-view | 0 | 1 | # Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def rightSideView(self, root: Optional[TreeNode]) -> List[int]:\n if not root: re... | 3 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Python || Easy || BFS || Using Queue | binary-tree-right-side-view | 0 | 1 | ```\nclass Solution:\n def rightSideView(self, root: Optional[TreeNode]) -> List[int]:\n if root==None:\n return []\n q=deque([[root,0]])\n d={}\n while q:\n node,line=q.popleft()\n d[line]=node.val\n if node.left:\n q.append([nod... | 1 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
✔💯 DAY 415 | DFS=>BFS | 0ms 100% | | [PYTHON/JAVA/C++] | EXPLAINED 🆙🆙🆙 | binary-tree-right-side-view | 1 | 1 | \n\n\n# Code for recursive\n```java []\npublic List<Integer> rightSideView(TreeNode root) {\n List<Integer> A = new ArrayList<>();\n rec(root,0,A);\n return A; \n}\nvoid rec(TreeNode root,int level,List<Integer> A){\n if(root==null) return;\n if(level==A.size()) A.add(root.val);//every first node ... | 3 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Python3 - Simple BFS approach (beats -97%) | binary-tree-right-side-view | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nso what we need is the value of right most node on each level.\nwhich made me think of bfs\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nuse simple bfs and in the result append the last node value by appending ... | 2 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Easiest Solution || beats 99% | binary-tree-right-side-view | 0 | 1 | \n\n# Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def rightSideView(self, root: Optional[TreeNode]) -> List[int]:\n\n if not ro... | 4 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Easy and efficient solution using dfs (explained solution) | binary-tree-right-side-view | 1 | 1 | \n# Approach\nThis solution performs a depth-first traversal of the binary tree, starting from the right side. It keeps track of the current level and adds the first node encountered at each level to the result vector. By performing a reverse preorder traversal (right subtree first, then left subtree), the solution ens... | 1 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Python3 Postorder traversal mega easy to understand | binary-tree-right-side-view | 0 | 1 | # Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\n\nclass Solution:\n def rightSideView(self, root: Optional[TreeNode]) -> List[int]:\n ans = {}\n\n ... | 1 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Python3 Easy Solution beats 96.18 % with Explanation | binary-tree-right-side-view | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n\n- Basically we have to return the last element at each level.\n\n- We will approach this problem by doing level order traversal and keeping track of the number of elements at each level. \n- For this we will initialize deque.\n- We will iterate thro... | 1 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
199: Solution with step by step explanation | binary-tree-right-side-view | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- The idea is to perform a level order traversal of the tree and keep track of the last node at each level.\n- We can use a queue to perform the traversal. We start by adding the root to the queue.\n- At each level, we keep ... | 8 | Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.
**Example 1:**
**Input:** root = \[1,2,3,null,5,null,4\]
**Output:** \[1,3,4\]
**Example 2:**
**Input:** root = \[1,null,3\]
**Output:** \[1,3\]
**Examp... | null |
Simple Python DFS Approach | number-of-islands | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nEssentially, you traverse the entire grid and keep track of which "1"\'s you\'ve already visited. Each time you find a new unvisited "1", you will run a recursive DFS search to expand and add all the coordinates of the visited "1"\'s to t... | 3 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
Solution | number-of-islands | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n\n void dfs(vector<vector<char>>& grid, int i, int j) {\n grid[i][j] = \'0\';\n int m = grid.size(), n = grid[0].size();\n\n if(i-1 >= 0 && grid[i-1][j] == \'1\') dfs(grid, i-1, j);\n if(i+1 < m && grid[i+1][j] == \'1\') dfs(grid, i+1, j);\n if... | 5 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
[Python3]Number of Islands BFS + DFS | number-of-islands | 0 | 1 | Explanation:\nIn the grid, we need to record 1 peice of information of one point, grid[i][j], i.e., if grid[i][j] has been visited or not.\nWe initialize the check matrix with all False. It means none of the elements in the check matrix has been visited.\nIf one point grid[i][j] has not been visited, check[i][j] == Fal... | 206 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
Click this if you're confused. | number-of-islands | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can think of each island as a subgraph. The islands are subgraphs that are disjoint. The nodes are 1s and the edges are represented by adjacency in the grid. So we just simply have to traverse the subgraph and mark all traversed nodes,... | 2 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
Python - BFS | number-of-islands | 0 | 1 | # Approach\nFor each island, we want to make sure to mark every element connected labeled "1". What we will do is apply the BFS algorithm throughout the entire array and only add to our island count if our grid position is marked as a "1" and also not in our visited set.\n\n# Code\n```\nclass Solution:\n def numIsla... | 2 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
Simple & intuitive solution using a recursive function | number-of-islands | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRecursive marking using ```bomb()``` funtion\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nMark the already found island to ```"2"```\n\n# Complexity\n- Time complexity: $$O(n \\times m )$$\n<!-- Add your time co... | 4 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
🚀Python3, DFS, mega easy to understand🚀 | number-of-islands | 0 | 1 | # Code\n```\nclass Solution:\n def numIslands(self, grid: List[List[str]]) -> int:\n visited = set()\n col, row = len(grid), len(grid[0])\n ans = 0\n dir = [[0, 1], [1, 0], [0, -1], [-1, 0]]\n\n def helper(c: int, r: int):\n visited.add((c, r))\n for i, j in d... | 6 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
Number of Islands using BFS faster then 96% | number-of-islands | 0 | 1 | ```\nclass Solution:\n def numIslands(self, grid: List[List[str]]) -> int:\n from collections import deque\n n=len(grid)\n m=len(grid[0])\n visited=[[0 for j in range(m)]for i in range(n)]\n c=0\n l=[[-1,0],[1,0],[0,-1],[0,1]]\n for i in range(n):\n for j i... | 2 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
Short + clean DFS | number-of-islands | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 4 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
200: Solution with step by step explanation | number-of-islands | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis problem can be solved using a variation of Depth First Search (DFS) algorithm. We will traverse through the given matrix and visit all the connected 1\'s, marking them as visited. We will keep a count of the number of t... | 11 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
Python 3 | DFS, BFS, Union Find, All 3 methods | Explanation | number-of-islands | 0 | 1 | ### Approach \\#1. DFS\n- Iterate over the matrix and DFS at each point whenever a point is land (`1`)\n- Mark visited as `2` to avoid revisit\n- Increment `ans` each time need to do a DFS (original, not recursive)\n```\nclass Solution:\n def numIslands(self, grid: List[List[str]]) -> int:\n if not grid: retu... | 67 | Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_.
An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
**Exampl... | null |
🔥🔥 Python 2 line Solution 🔥🔥 | bitwise-and-of-numbers-range | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 4 | Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.
**Example 1:**
**Input:** left = 5, right = 7
**Output:** 4
**Example 2:**
**Input:** left = 0, right = 0
**Output:** 0
**Example 3:**
**Input:** left = 1, right = 2147... | null |
201: Solution with step by step explanation | bitwise-and-of-numbers-range | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe approach to solve this problem is to perform a bitwise AND operation on all numbers between the left and right limits. However, performing this operation on every number will be a costly operation.\n\nIf we consider the ... | 17 | Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.
**Example 1:**
**Input:** left = 5, right = 7
**Output:** 4
**Example 2:**
**Input:** left = 0, right = 0
**Output:** 0
**Example 3:**
**Input:** left = 1, right = 2147... | null |
Python AND with a twist and beats T.C. 96.6% and S.C. 92.4% | bitwise-and-of-numbers-range | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(min(1000, right - left))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space c... | 1 | Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.
**Example 1:**
**Input:** left = 5, right = 7
**Output:** 4
**Example 2:**
**Input:** left = 0, right = 0
**Output:** 0
**Example 3:**
**Input:** left = 1, right = 2147... | null |
Python Bits Manipulation Solution with Detailed Intuition ✅ | bitwise-and-of-numbers-range | 0 | 1 | ```\n\'\'\'\n# Intuition:\nTraverse from LSB to MSB and keep right-shifting left and right until both of them become equal. \nIf at any bit position left side bits of left and right are equal ie. left == right, \nthen all numbers in [left, right] will also be equal.\n\ne.g.\nbit index = 3 2 1 0\nleft = 12 = 1 1 0 0\n... | 2 | Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.
**Example 1:**
**Input:** left = 5, right = 7
**Output:** 4
**Example 2:**
**Input:** left = 0, right = 0
**Output:** 0
**Example 3:**
**Input:** left = 1, right = 2147... | null |
Python Bits Manipulation Solution with Detailed Intuition ✅ | bitwise-and-of-numbers-range | 0 | 1 | ```\n\'\'\'\n# Intuition:\nTraverse from LSB to MSB and keep right-shifting left and right until both of them become equal. \nIf at any bit position left side bits of left and right are equal ie. left == right, \nthen all numbers in [left, right] will also be equal.\n\ne.g.\nbit index = 3 2 1 0\nleft = 12 = 1 1 0 0\n... | 3 | Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.
**Example 1:**
**Input:** left = 5, right = 7
**Output:** 4
**Example 2:**
**Input:** left = 0, right = 0
**Output:** 0
**Example 3:**
**Input:** left = 1, right = 2147... | null |
Python iterative sol. based on bit-manipulation | bitwise-and-of-numbers-range | 0 | 1 | Python iterative sol. based on bit-manipulation\n\n```\nclass Solution:\n def rangeBitwiseAnd(self, m: int, n: int) -> int:\n \n shift = 0\n \n # find the common MSB bits.\n while m != n:\n \n m = m >> 1\n n = n >> 1\n \n shift += ... | 10 | Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.
**Example 1:**
**Input:** left = 5, right = 7
**Output:** 4
**Example 2:**
**Input:** left = 0, right = 0
**Output:** 0
**Example 3:**
**Input:** left = 1, right = 2147... | null |
Easy Python Solution || Beginner Friendly || Let's search for Happy Number..... | happy-number | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem is asking whether a given number is a happy number or not. A happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit.\n- The intuition here is to repeatedly calculate the sum ... | 3 | Write an algorithm to determine if a number `n` is happy.
A **happy number** is a number defined by the following process:
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly ... | null |
Very Easy 0 ms 100%(Fully Explained)(C++, Java, Python, JS, C, Python3) | happy-number | 1 | 1 | # **C++ Solution:**\nRuntime: 0 ms, faster than 100.00% of C++ online submissions for Happy Number.\n```\nclass Solution {\npublic:\n bool isHappy(int n) {\n // Create a set...\n set<int> hset;\n while(hset.count(n) == 0) {\n // If total is equal to 1 return true.\n if(n ==... | 70 | Write an algorithm to determine if a number `n` is happy.
A **happy number** is a number defined by the following process:
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly ... | null |
Simple Approach for HAPPY NUMBER in PYTHON/PYTHON3 | happy-number | 0 | 1 | # Intuition\nIn this solution, we use list, array or map to store the numnber\nthan we can check it appear or not\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe approach as stated in other solutions is quite simple. It either involves the fancier floyd cycle algorithm or simply ... | 33 | Write an algorithm to determine if a number `n` is happy.
A **happy number** is a number defined by the following process:
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly ... | null |
[PYTHON]-\ \-easiest -\ \- with comments | happy-number | 0 | 1 | \n# Code\n```\nclass Solution:\n def isHappy(self, n: int) -> bool:\n set_of_no=set()\n while n!=1: \n n=sum([int(i)**2 for i in str(n)]) #squaring the digits of no\n if n in set_of_no: #checking whether the no is present in set_of_no\n return False #if present that... | 7 | Write an algorithm to determine if a number `n` is happy.
A **happy number** is a number defined by the following process:
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly ... | null |
新手解 | happy-number | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\u5728\u89E3\u984C\u4E4B\u524D\u8981\u5148\u4E86\u89E3\u5FEB\u6A02\u6578\u7684\u5B9A\u7FA9...\n\u53EA\u8981\u4E0D\u662F\u5FEB\u6A02\u6578\u5C31\u6703\u9032\u5165\u4EE5\u4E0B\u5FAA\u74B0\n4 \u2192 16 \u2192 37 \u2192 58 \u2192 89 \u2192 14... | 3 | Write an algorithm to determine if a number `n` is happy.
A **happy number** is a number defined by the following process:
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly ... | null |
Beats : 98.9% [26/145 Top Interview Question] | happy-number | 0 | 1 | # Intuition\n*my intuition would be to implement a loop that repeatedly computes the sum of squares of the digits of n and checks if the result is equal to 1 or not. I would also need to keep track of all the intermediate numbers that I encounter, to detect if we enter a cycle of numbers. If we detect a cycle, we can t... | 2 | Write an algorithm to determine if a number `n` is happy.
A **happy number** is a number defined by the following process:
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly ... | null |
Python 3 -> tortoise hare technique | happy-number | 0 | 1 | **Suggestions to make it better are always welcomed.**\n\n```\ndef isHappy(self, n: int) -> bool:\n\t#20 -> 4 -> 16 -> 37 -> 58 -> 89 -> 145 > 42 -> 20\n\tslow = self.squared(n)\n\tfast = self.squared(self.squared(n))\n\n\twhile slow!=fast and fast!=1:\n\t\tslow = self.squared(slow)\n\t\tfast = self.squared(self.square... | 115 | Write an algorithm to determine if a number `n` is happy.
A **happy number** is a number defined by the following process:
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly ... | null |
Python - Easiest Solution | happy-number | 0 | 1 | # Easy Understand\n\n# Code\n\nExplaination :\n```\n if(m==9000):\n return False \n```\nthis code is for : if m reached to 9000. It means there is no happy number as n or code will give TLE. So it returns false...\n```\ndef happy(n,m):\n s = str(n)\n cnt = 0\n for i in s:\n cnt += int(i)**2\n ... | 3 | Write an algorithm to determine if a number `n` is happy.
A **happy number** is a number defined by the following process:
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly ... | null |
Happy Number beats 98🔥🔥 | happy-number | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Write an algorithm to determine if a number `n` is happy.
A **happy number** is a number defined by the following process:
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly ... | null |
Simple Python solution with explanation (single pointer, dummy head). | remove-linked-list-elements | 0 | 1 | Before writing any code, it\'s good to make a list of edge cases that we need to consider. This is so that we can be certain that we\'re not overlooking anything while coming up with our algorithm, and that we\'re testing all special cases when we\'re ready to test. These are the edge cases that I came up with.\n\n1. T... | 507 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
Easiest python solution pointer approach | remove-linked-list-elements | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
[Python] 99% One-pass Solution with Explanation | remove-linked-list-elements | 0 | 1 | ### Intuition\n\nTo delete a node, we need to assign the next node to the previous node. Let\'s use `curr` to denote the current node, and `prev` to denote the previous node. Then, we have the following 4 cases:\n\n1. **Node to remove is at the end of the linked list.**\n\n```text\nInput: head = [6,1,2,6,3,4,5,6], val ... | 59 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
Solution Intuition, Approach, Complexity, and Code: Removing Linked List Elements | remove-linked-list-elements | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires removing all nodes that match a specific value from a linked list. The approach involves iterating through the linked list while maintaining a previous node pointer to update the next node pointer of the previous node... | 8 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
✅Python3 73ms🔥easiest solution | remove-linked-list-elements | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Two Pointers Methods.**\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- maintain **two pointers** **prev and current**, meaning is self-described.\n- now when **removing element is head** then **remove** head *... | 7 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
eas | remove-linked-list-elements | 0 | 1 | \n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:\n result = ListNode(0)\n result.next =... | 2 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
Very Easy || 0 ms || 100% || Fully Explained (Java, C++, Python, JS, C, Python3) | remove-linked-list-elements | 1 | 1 | # **Java Solution:**\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Remove Linked List Elements.\n```\nclass Solution {\n public ListNode removeElements(ListNode head, int val) {\n // create a fake node that acts like a fake head of list pointing to the original head and it points to the o... | 20 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
Python || 94.11% Faster || Iterative Approach || O(N) Solution | remove-linked-list-elements | 0 | 1 | ```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:\n if head==None: #If head is empty\n ... | 4 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
203: Solution with step by step explanation | remove-linked-list-elements | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe will start by initializing a dummy node and make its next point to head, which will make it easy to handle edge cases.\n\nWe will traverse the linked list and remove all the nodes with val equal to given val.\n\nAt the en... | 10 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
Easy beginner solution in Python | remove-linked-list-elements | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
Python3 || Simple || Easy | remove-linked-list-elements | 0 | 1 | **Please upvote <3**\n# Code\n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:\n #create a dummy... | 1 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
Python3 | remove-linked-list-elements | 0 | 1 | # Code\n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:\n while head and head.val == val:\n ... | 1 | Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.
**Example 1:**
**Input:** head = \[1,2,6,3,4,5,6\], val = 6
**Output:** \[1,2,3,4,5\]
**Example 2:**
**Input:** head = \[\], val = 1
**Output:** \[\]
**Example 3:**... | null |
204. Count Primes | count-primes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this method is that all non-prime numbers can be expressed as a product of prime numbers. Therefore, to find all prime numbers less than a given limit, we can start with a list of all numbers from 2 to the limit and i... | 3 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
204. Count Primes | count-primes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this method is that all non-prime numbers can be expressed as a product of prime numbers. Therefore, to find all prime numbers less than a given limit, we can start with a list of all numbers from 2 to the limit and i... | 3 | Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.
**Example 1:**
**Input:** n = 10
**Output:** 4
**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
**Example 2:**
**Input:** n = 0
**Output:** 0
**Example 3:**
**Input:** n = 1
**Output:** 0
**Co... | Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better? As we know the number must not be div... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.