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 |
|---|---|---|---|---|---|---|---|
Constant time | Mathematical solution | power-of-four | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nfind power of 4 of a given number if its whole number return `True`. if it has decimal number return `False`.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nfew examples \n- log(16,4) => 2\n- log(17,4) => 2.0437... | 0 | Given an integer `n`, return _`true` if it is a power of four. Otherwise, return `false`_.
An integer `n` is a power of four, if there exists an integer `x` such that `n == 4x`.
**Example 1:**
**Input:** n = 16
**Output:** true
**Example 2:**
**Input:** n = 5
**Output:** false
**Example 3:**
**Input:** n = 1
**O... | null |
Python code to return n if it's a power of 4 (TC&SC: O(1)) | power-of-four | 0 | 1 | \n\n# Approach\n\nThe isPowerOfFour function checks if the given integer n is a power of four. It does this by first checking if n is greater than zero and then comparing it to 4 raised to the power of the rounded logarithm of n to the base 4.\n# Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Co... | 2 | Given an integer `n`, return _`true` if it is a power of four. Otherwise, return `false`_.
An integer `n` is a power of four, if there exists an integer `x` such that `n == 4x`.
**Example 1:**
**Input:** n = 16
**Output:** true
**Example 2:**
**Input:** n = 5
**Output:** false
**Example 3:**
**Input:** n = 1
**O... | null |
98.34 Beats 🔥, Easiest solution for beginners (O(1)) , Anyone can Understand. | integer-break | 0 | 1 | # Intuition\nBasically , I tried to find a pattern that would give the maximum product , I got to know that , this can only happen using prime numbers . From prime numbers i understood that any universal number can be written in 2\'s or 3\'s .\n\n# Approach\nChecking for the remainder of number and according to the rem... | 6 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
C++/Python O(log n) Math & MSBF pow||0ms beats 100% | integer-break | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n6 is the number what is split into$2+2+2$ or $3+3$.... Which one is better?\nNote $2^3=8<9=3^2$, so $3+3$ is the most optimal. For integer $n>3$ it is considered $n=3*k+r$ where r=0, 2, 4\nthen compute\n$$\n3^k*f(r) \\ where \\ f(0)=1, f... | 8 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
🔥Easy explanation | Why 3s why not 4s?| 🚀Dynamic Programming Solution with Best Approach ✅✅✅✅✅ | integer-break | 1 | 1 | \n## Upvote if you like the eexplanation and solution \uD83D\uDD25\n\n---\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n We want to break the integer n into the sum of positive integers to maximize the product. By observation, it\'s more beneficial to use 3s instead of 4s becau... | 4 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
easy solution | integer-break | 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 integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
💯Faster✅💯 Lesser✅4 Methods🔥Simple Math🔥Dynamic Programming🔥Greedy Approach🔥Memoization | integer-break | 1 | 1 | # \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 4 ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \n- The goal is to find the maximum product that can be obtained for any given positive int... | 86 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
🚀 100% || DP Solution Then Math || Explained Intuition 🚀 | integer-break | 1 | 1 | # Problem Description\n\nGiven a positive integer `n`, the **task** is to break it into the sum of `k` positive integers, where $$k >= 2$$, in a way that **maximizes** their product. Return the **maximum** product achievable.\n\n- **Constraints:**\n - `2 <= n <= 58`\n\nSeem tough? \uD83E\uDD14\n\n---\n\n\n\n# Intuit... | 57 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
easy solution ✅ beats 93.57% ✅ python 3.0 ✅ hash map | integer-break | 0 | 1 | # Intuition\nwhat if we used a hashmap??\n\n# Approach\nimplement a hashmap!!\n\n# Complexity\n- Time complexity:\nidk\n\n- Space complexity:\nprobably pretty low\n\n# Code\n```\nclass Solution:\n def integerBreak(self, n: int) -> int:\n myDict = {\n 2:1,\n 3:2,\n 4:4,\n ... | 2 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
O(log n) time with O(1) space complexity, math solution with proofs (don't be afraid :D ) | integer-break | 0 | 1 | # Intuition\n\n1. **Small Numbers**: For small numbers like 2 and 3, breaking them up doesn\'t help, as $$ 2 = 1 + 1 \\rightarrow 1 \\times 1 = 1 $$ and $$ 3 = 1 + 2 \\rightarrow 1 \\times 2 = 2 $$, which don\'t yield a product greater than the numbers themselves.\n\n2. **Composite Numbers**: For composite numbers like... | 24 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
✅ 93.65% Why 3s? | integer-break | 1 | 1 | # Intuition\nWhen given an integer `n`, our goal is to break it into sums of smaller integers such that their product is maximized. At first, it might seem that a brute-force approach of trying all combinations is the answer. However, a more in-depth analysis of the numbers reveals patterns that can be utilized to achi... | 68 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
【Video】Give me 10 minutes - How we think about a solution - No difficult mathematics | integer-break | 1 | 1 | Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope it is helpful for someone.\n\n\n# Intuition\nCreate 3 as many ... | 44 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Time - O(1) and no Extra space | Simple 1 line Solution | Beats 99% | 🔥🚀✅✅✅ | integer-break | 0 | 1 | # Code\n```C++ []\nclass Solution {\npublic:\n int integerBreak(int n) {\n return n == 2 ? 1 : n == 3 ? 2 : n % 3 == 0 ? pow(3, n / 3) : n % 3 == 1 ? 4 * pow(3, (int)(n / 3) - 1) : 2 * pow(3, (int)(n / 3));\n }\n};\n```\n```C []\nint integerBreak(int n) {\n return n == 2 ? 1 : n == 3 ? 2 : n % 3 == 0 ? ... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Python Solution Beats 91% with Intuition | integer-break | 0 | 1 | # Intuition\nThe goal is the break the number into as many chunks as possible, the smallest 2 chunks are 2s and 3s, so we will try to break them down into chunks of 3s followed by whatever is remaining into chunks of 2s\n\nGoing by this approach we should stop with 3s chunks at 4 because inorder to break 4 we have 2 op... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
🔥 Easy solution | Python 3 🔥 | integer-break | 0 | 1 | # Intuition\nGiven a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize a list dp of size n + 1 to store the maxi... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
easy solution | integer-break | 0 | 1 | # Intuition\nhardcode\n\n# Approach\nlol\n\n# Complexity\n- Time complexity:\nO(logN) if i wasn\'t lazy i think\n\n- Space complexity:\nirrelevant\n\n# Code\n```\nclass Solution:\n def integerBreak(self, n: int) -> int:\n if n <=29:\n if n < 15:\n if n < 8:\n if n ... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Python3 LogN solution | integer-break | 0 | 1 | - Time complexity: $$O(log N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def integerBreak(self, n: int) -> int:\n if n == 2:\n return 1\n if n == 3:\n return 2\n max_product = 1\n while n > 4:\n max_product *... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
✔️ Short easy solution with explanation || O(log n) time and constant space | integer-break | 0 | 1 | # Approach\nLet\'s say we divide a number n into k parts. It\'s easy to check that we can get the biggest product if we split n into k equal parts and share the remaining equally by adding `1` to the part of the numbers.\n`f.e. n = 10, k = 4 --> 10 // 4 = 2, 10 % 4 == 2 --> combination going to be 3 3 2 2 `\n\nNow al... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Python and swift, bottoms up DP, intuitive, fun and detailed explanation. | integer-break | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nLooking at the problem we get to know that there will be many cases if we try to brute force it. But if you have learnt DP before it is not a surprise that this can be solved using DP, this is just a typical 1D DP but with some additional... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Python solution using DP with easy understanding with optimize solution | integer-break | 0 | 1 | \nEfficient solution for maximizing product when breaking n into positive integers. Employs dynamic programming, handles n=2, n=3, and iterates to optimize splits. Calculates and stores max product in dp[n].\nIf you find this solution helpful, please consider giving it a thumbs-up and sharing your thoughts in the comme... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
2ms Runtime TC O(1) SC O(1) | Easy to understand | Beginner Friendly Solution | integer-break | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGiven an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.\n\nReturn the maximum product you can get.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Easy to Understand. Beat 99% in Time Complexity | integer-break | 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 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Python3 Solution | integer-break | 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 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
✅Beats 100% | O(n) Easy Solution🔥 | Why 3s why not 4s or else, explained in short and easy 🚀 | integer-break | 1 | 1 | # Do upvote if you like the solution and explanation please \uD83D\uDE80\n\n---\n\n\n# First Understand Why 3s why not 4s or else ?\n- The reason we use 3s instead of 4s is to maximize the product. When you break a number into smaller numbers, multiplying several smaller numbers together results in a larger product th... | 9 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Simplest Solution in Python3 | integer-break | 0 | 1 | \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. If n is less than or equal to 3, return n - 1 because for n <= 3, the maximum product can be achieved by breaking n into n - 1.\n\n2. Initialize res to 1. This variable will keep track of the maximum product.\n\n3. Use a while loop to iterate w... | 1 | Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
Return _the maximum product you can get_.
**Example 1:**
**Input:** n = 2
**Output:** 1
**Explanation:** 2 = 1 + 1, 1 \* 1 = 1.
**Example 2:**
**Input:** n = 10
**Output:** 36
**Exp... | There is a simple O(n) solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Simple 99,8% 7 line Python solution | integer-break | 0 | 1 | # Code\n### \n```\nclass Solution:\n def integerBreak(self, n: int) -> int:\n if n < 4:\n n-=1 ## n2 = 1; n3 = 2\n ans = 1\n while n > 4:\n ans *= 3\n n -= 3\n return ans * n \n```\n solution to this problem. You may check the breaking results of n ranging from 7 to 10 to discover the regularities. |
Python 3 ~actually~ easiest solution | reverse-string | 0 | 1 | **It\'s so simiple, a caveman could do it**\n\n```\nclass Solution:\n def reverseString(self, s: List[str]) -> None:\n s[:] = s[::-1]\n```\n\n**Note:**\n`s[:] = s[::-1]` is required **NOT** `s = s[::-1]` because you have to edit the list **inplace**. \nUnder the hood, `s[:] =` is editing the actual memory byt... | 100 | Write a function that reverses a string. The input string is given as an array of characters `s`.
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
**Example 1:**
**Input:** s = \["h","e","l","l","o"\]
**Output:** \["o","l","l","e","h... | The entire logic for reversing a string is based on using the opposite directional two-pointer approach! |
Python | faster than 93% | two pointer | reverse-string | 0 | 1 | ```\n"""https://leetcode.com/problems/reverse-string/"""\nclass Solution:\n def reverseString(self, s: List[str]) -> None:\n """\n Do not return anything, modify s in-place instead.\n """\n l=0\n r=len(s)-1\n while l<r:\n s[l],s[r]=s[r],s[l]\n l+=1\n ... | 11 | Write a function that reverses a string. The input string is given as an array of characters `s`.
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
**Example 1:**
**Input:** s = \["h","e","l","l","o"\]
**Output:** \["o","l","l","e","h... | The entire logic for reversing a string is based on using the opposite directional two-pointer approach! |
Python 99 %beats 3 Approach || 1line code | reverse-string | 0 | 1 | **If you got help from this,... Plz Upvote .. it encourage me**\n\n3 Approachs , 2 are one line code and 3rd is by 2 pointer\n\n# Code\n```\n<!-- One Line Code -->\nclass Solution:\n def reverseString(self, s: List[str]) -> None:\n """\n Do not return anything, modify s in-place instead.\n """\n... | 5 | Write a function that reverses a string. The input string is given as an array of characters `s`.
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
**Example 1:**
**Input:** s = \["h","e","l","l","o"\]
**Output:** \["o","l","l","e","h... | The entire logic for reversing a string is based on using the opposite directional two-pointer approach! |
Reverse String in Python | Faster than 99.33% | reverse-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nReversing a string means changing the order of its characters from the original sequence to the opposite sequence.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can use the following method available for Pytho... | 4 | Write a function that reverses a string. The input string is given as an array of characters `s`.
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
**Example 1:**
**Input:** s = \["h","e","l","l","o"\]
**Output:** \["o","l","l","e","h... | The entire logic for reversing a string is based on using the opposite directional two-pointer approach! |
[Python] - Two Pointer - Clean & Simple Solution | reverse-string | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n Loop goes until n/2 but we represent $$O(n/2)$$ to $$O(n)$$. \nHence TC is $$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```\nclass Solution:\n def rever... | 16 | Write a function that reverses a string. The input string is given as an array of characters `s`.
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
**Example 1:**
**Input:** s = \["h","e","l","l","o"\]
**Output:** \["o","l","l","e","h... | The entire logic for reversing a string is based on using the opposite directional two-pointer approach! |
Beginner friendly code in python | reverse-string | 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 O(n/2)-->O(n)\n- Space complexity:\n<!-- Add your space complexity here,... | 2 | Write a function that reverses a string. The input string is given as an array of characters `s`.
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
**Example 1:**
**Input:** s = \["h","e","l","l","o"\]
**Output:** \["o","l","l","e","h... | The entire logic for reversing a string is based on using the opposite directional two-pointer approach! |
Easy python solution | reverse-string | 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:0(n)\n\n- Space complexity:0(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def reve... | 1 | Write a function that reverses a string. The input string is given as an array of characters `s`.
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
**Example 1:**
**Input:** s = \["h","e","l","l","o"\]
**Output:** \["o","l","l","e","h... | The entire logic for reversing a string is based on using the opposite directional two-pointer approach! |
Python Solution || Super Easy | reverse-string | 0 | 1 | ```\nclass Solution:\n def reverseString(self, s: List[str]) -> None:\n left, right = 0, len(s) - 1\n while (left < right):\n s[left], s[right] = s[right], s[left]\n left += 1\n right -= 1\n\n``` | 1 | Write a function that reverses a string. The input string is given as an array of characters `s`.
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
**Example 1:**
**Input:** s = \["h","e","l","l","o"\]
**Output:** \["o","l","l","e","h... | The entire logic for reversing a string is based on using the opposite directional two-pointer approach! |
✅✅Python3 Two Word Answer not a single line beats 99.89% ✅✅ | reverse-string | 0 | 1 | # Intuition\njust make a copy of its own list\n# Approach\nReversing a list in place\n\n# Complexity\n- Time complexity:\nO[1]\n\n- Space complexity:\nO[1]\n\n# Code\n```\nclass Solution:\n def reverseString(self, s: List[str]) -> None:\n s[:] = s[::-1]\n``` | 3 | Write a function that reverses a string. The input string is given as an array of characters `s`.
You must do this by modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm) with `O(1)` extra memory.
**Example 1:**
**Input:** s = \["h","e","l","l","o"\]
**Output:** \["o","l","l","e","h... | The entire logic for reversing a string is based on using the opposite directional two-pointer approach! |
2 pointer approach | reverse-vowels-of-a-string | 0 | 1 | # Approach\n<!-- 1.firstly we have make a string st containing all vowels\n2.Then by using two pointer approach we are swapping the vowels \n3.we are increasing e and b when they are not present in our string st containing all vowels -->\n\n# Complexity\n- Time complexity:\n<!-- with complexity O(n2)$$ -->\n\n- Space c... | 3 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
SIMPLE EASY SOLUTION PYTHON JUST 3 FOR LOOPS | reverse-vowels-of-a-string | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def reverseVowels(self, s: str) -> str:\n l=[]\n s=list(s)\n for i in range(len(s)):\n if s[i] in "aeiouAEIOU":\n l.append(s[i])\n s[i]="*"\n l=l[::-1]\n start=0\n for i in range(len(s)):\n ... | 1 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Easy Python Solution | reverse-vowels-of-a-string | 0 | 1 | # Intuition\nTwo pointer approach\n\n# Approach\nSwapping left and right if they are vowels.\n\n\n\n# Code\n```\nclass Solution:\n def reverseVowels(self, s: str) -> str:\n s=list(s)\n n=len(s)\n left=0\n right=n-1\n vowels=set(\'AEIOUaeiou\')\n while left<right:\n ... | 38 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Easy solution 70% faster | reverse-vowels-of-a-string | 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 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Reverse Vowels of a String using python (simple solution) | reverse-vowels-of-a-string | 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)$$ --... | 0 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Python - Beginner Friendly - O(N) | reverse-vowels-of-a-string | 0 | 1 | # Complexity\n- Time complexity: **O(N)** where **N** = *Length of the string*\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(N)** where **N** = *Length of the string*\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def reverseVowels(self, ... | 2 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Time 'n', Space 'n' | reverse-vowels-of-a-string | 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 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Python Easy Solution || Two Pointers | reverse-vowels-of-a-string | 0 | 1 | # Code\n```\nclass Solution:\n def reverseVowels(self, s: str) -> str:\n lis=[\'a\', \'e\', \'i\', \'o\',\'u\',\'A\',\'E\',\'I\',\'O\',\'U\']\n i=0\n j=len(s)-1\n s=list(s)\n while(i<j):\n if s[i] not in lis:\n i+=1\n if s[j] not in lis:\n ... | 2 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Python3, simple solution | reverse-vowels-of-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo swap the positions, need to save the position and reverse the position list\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nval stores the value at the index and pos stores the position, next only one of the two... | 3 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
✅ Explained - Simple and Clear Python3 Code✅ | reverse-vowels-of-a-string | 0 | 1 | # Intuition\nThe given problem is to reverse only the vowels in a given string. The vowels are \'a\', \'e\', \'i\', \'o\', and \'u\', and they can appear in both lower and upper case.\n\n\n# Approach\nThe provided solution uses a two-pointer approach to reverse the vowels in the string. It initializes two pointers, sta... | 9 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Easiest Two Pointer Approach to Reverse Vowels of a String | reverse-vowels-of-a-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this solution involves using a two-pointer approach to reverse the vowels in the given string. The idea is to find pairs of vowels from both ends of the string and swap them until the pointers meet in the middle. By s... | 7 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Python solution beats 72.38% : 60ms | reverse-vowels-of-a-string | 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. -->\nwe traverse the list from left and right till we find a vowel from both the directions \nthen we swap them and search for the next pair of vowels \nthis process repeat... | 5 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Python Solution || 99.58% faster || 86.96% less memory | reverse-vowels-of-a-string | 0 | 1 | ```\nclass Solution:\n def reverseVowels(self, s: str) -> str:\n s = list(s)\n left = 0\n right = len(s) - 1\n m = \'aeiouAEIOU\'\n while left < right:\n if s[left] in m and s[right] in m:\n \n s[left], s[right] = s[right], s[left]\n ... | 56 | Given a string `s`, reverse only all the vowels in the string and return it.
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
**Example 1:**
**Input:** s = "hello"
**Output:** "holle"
**Example 2:**
**Input:** s = "leetcode"
**Output:** "leotc... | null |
Solution with FFT lol. | top-k-frequent-elements | 0 | 1 | ```py\nimport numpy as np\nfrom numpy.fft import fft, ifft\ndef solve(nums, k):\n n = len(nums)\n polynomial = np.zeros(n, dtype=complex)\n for num in nums:\n polynomial[num] += 1\n fft_result = fft(polynomial)\n topki = np.argpartition(np.abs(fft_result), -k)[-k:]\n inverted_fft = ifft(fft_res... | 1 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
Quickselect approach O(n) | top-k-frequent-elements | 0 | 1 | # Intuition\nWe can do heap approach and bucket sort which is even better. But somewhere on internet i found that quick select by frequency is also nice linear solution for such type for problems.\n\n# Approach\nQuickselect by frequency. Partition method suffle elements in reverse order. Means that in the end of array ... | 4 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
CodeDominar Solution | top-k-frequent-elements | 0 | 1 | # Code\n```\nclass Solution:\n def topKFrequent(self, nums: List[int], k: int) -> List[int]:\n ans=[]\n d = {}\n for ele in nums:\n d[ele] = d.get(ele,0)+1\n li = [k for k,v in sorted(d.items(), key=lambda item:item[1])]\n li = li[::-1] \n for i in range(k):\n... | 3 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
Easy Python Solution | top-k-frequent-elements | 0 | 1 | \n# Code\n```\nclass Solution:\n def topKFrequent(self, nums: List[int], k: int) -> List[int]:\n count, unique, c = [], [], []\n\n for i in nums:\n if i not in unique:\n unique.append(i)\n c.append(nums.count(i))\n\n for i in range(k):\n m=max(... | 4 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
[ Python ] ✅✅ Simple Python Solution Using Dictionary ( HashMap ) ✌👍 | top-k-frequent-elements | 0 | 1 | # If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F \n# Runtime: 115 ms, faster than 46.79% of Python3 online submissions for Top K Frequent Elements.\n# Memory Usage: 21.1 MB, less than 41.02% of Python3 online submissions for Top K Frequent Elements.\n\n\tclass Solution:\n\... | 62 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
Python O(n) solution without sort, without heap | top-k-frequent-elements | 0 | 1 | \n\n\n\n\n# Code\n```\nclass Solution(object):\n def topKFrequent(self, nums, k):\n m = {}\n r = []\n for n in nums:\n if n in m:\n m[n] += 1\n else:\n m[n] = 1\n\n for j in range(k):\n max_freq = 0\n max_freq_num =... | 5 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
Easy Python Solution | top-k-frequent-elements | 0 | 1 | \n# Code\n```\nfrom collections import Counter\nclass Solution:\n def topKFrequent(self, nums: List[int], k: int) -> List[int]:\n\n hm = dict(Counter(nums))\n hm = dict(sorted(hm.items(), key=lambda item: item[1], reverse = True))\n return list(hm)[:k]\n``` | 2 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
Bucket Sort in Python | O(n) time complexity | top-k-frequent-elements | 0 | 1 | # Intuition\nInitially count the occurance using hash maps & sort them\nBut learning from neetcode the approach can be optimize using **Bucket Sort**\n\n# Approach\n1. Use Hash Maps to count number of instance\n2. Not using sort, but make an array with number of occurance as key, save the num as list\n\n# Complexity\n-... | 0 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
[Python3] BucketSort + Heap (2 lines) || beats 94% | top-k-frequent-elements | 0 | 1 | ### BucketSort approach:\n\n1. Count frequency for all numbers using Counter (or manually).\n2. Create buckets for grouping items by frequency. Buckets count is equal to number of nums. For example: ```nums[1,1,1,1,2,2,3,4] => buckets = [0:[], 1:[3,4], 2:[2], 3:[], 4:[1]]```. Instead of an array, you can use a dictiona... | 13 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
Python Solution Using Heap | top-k-frequent-elements | 0 | 1 | # Code\n```\nclass Solution:\n def topKFrequent(self, nums: List[int], k: int) -> List[int]:\n ht = defaultdict(int)\n for num in nums:\n ht[num] += 1\n\n arr=[]\n heapify(arr)\n for num, freq in ht.items():\n heappush(arr, (freq, num))\n\n\n\n for _ in... | 1 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
Powerful Heapmax and Hash Table | top-k-frequent-elements | 0 | 1 | \n# Heapmax and Hash Table\n```\nclass Solution:\n def topKFrequent(self, nums: List[int], k: int) -> List[int]:\n dic=Counter(nums)\n heapmax=[[-freq,num] for num,freq in dic.items()]\n heapq.heapify(heapmax)\n list1=[]\n for i in range(k):\n poping=heapq.heappop(heapma... | 17 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
Python 3 simple code | top-k-frequent-elements | 0 | 1 | # Intuition\nSimple Python 3 code using dictionaries\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\no(nlogn)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def topKFrequent(self, nums: List[... | 4 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
347: Time 91.58%, Solution with step by step explanation | top-k-frequent-elements | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis problem can be solved by using a hash map to count the frequency of each element in the array. Then we can use a min-heap to store the top k frequent elements. The heap is ordered by the frequency of the elements, with ... | 17 | Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[1,1,1,2,2,3\], k = 2
**Output:** \[1,2\]
**Example 2:**
**Input:** nums = \[1\], k = 1
**Output:** \[1\]
**Constraints:**
* `1 <= nums.lengt... | null |
one string solution | intersection-of-two-arrays | 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 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2\]
**Example 2:**
**Input:** nums1 = \[4,9,5\], nums2 = \... | null |
Two sets, without using built-in intersection function | intersection-of-two-arrays | 0 | 1 | # Approach\nRun through the first array, building a set with its values. Then run through the second array and built a new set only adding elements that are in the first set.\n\n# Complexity\n- Time complexity: O(N), as we are traversing nums1 first then nums2. Checking if an element of nums2 is in the set has time com... | 1 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2\]
**Example 2:**
**Input:** nums1 = \[4,9,5\], nums2 = \... | null |
easy soln | intersection-of-two-arrays | 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 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2\]
**Example 2:**
**Input:** nums1 = \[4,9,5\], nums2 = \... | null |
Beats 96.07%of users with Python3 and Beats 96.71%of users with Python3 | intersection-of-two-arrays | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:\n nums1.sort()\n nums2.sort()\n ans = []\n def bs(i, nums2):\n start = 0\n finish = len(nums2)-1\n while start <= finish:\n mid = (st... | 1 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2\]
**Example 2:**
**Input:** nums1 = \[4,9,5\], nums2 = \... | null |
PYTHON3 Solution | intersection-of-two-arrays | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe map all the values in num1 and num2 in two hashmaps and store the values that are present in both these maps and return the answer\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->... | 1 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2\]
**Example 2:**
**Input:** nums1 = \[4,9,5\], nums2 = \... | null |
Easy solution in python with understandable explanation | intersection-of-two-arrays | 0 | 1 | # On my way of solving this problem: \n1. First we need an empty variable in type set.\n\n2. Secondly: we should change the list \'nums1\' as type set to save only unique numbers in it.\n\n3. After that we use loop for rotating \'nums1\' which we saved only unique numbers only and check if element is in \'nums2\' we ad... | 1 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2\]
**Example 2:**
**Input:** nums1 = \[4,9,5\], nums2 = \... | null |
Single Line Code | intersection-of-two-arrays | 0 | 1 | \n# Super Logic Solution Using Python\n```\nclass Solution:\n def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:\n return set(nums1) & set(nums2)\n``` | 5 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2\]
**Example 2:**
**Input:** nums1 = \[4,9,5\], nums2 = \... | null |
Awesome 2 Lines of Code | intersection-of-two-arrays | 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(n)----->please upvote me\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)----->please upvote me\n<... | 3 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2\]
**Example 2:**
**Input:** nums1 = \[4,9,5\], nums2 = \... | null |
Python Solution || Hashtable | intersection-of-two-arrays | 0 | 1 | ```\nclass Solution:\n def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:\n \n d = {}\n if len(nums1) > len(nums2):\n nums1,nums2=nums2,nums1\n for i in nums1:\n d[i] = 0\n \n \n for i in nums2:\n if i not in d:\n ... | 2 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2\]
**Example 2:**
**Input:** nums1 = \[4,9,5\], nums2 = \... | null |
Very Easy || 100% || Fully Explained || Java, C++, Python, Python3 || HashMap & Two-Pointers | intersection-of-two-arrays-ii | 1 | 1 | # **Java Solution (Two-Pointers Approach):**\n```\n// Runtime: 1 ms, faster than 99.13% of Java online submissions for Intersection of Two Arrays II.\n// Memory Usage: 42.5 MB, less than 92.71% of Java online submissions for Intersection of Two Arrays II.\nclass Solution {\n public int[] intersect(int[] nums1, int[]... | 188 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
Simple brute force that beats 100% | intersection-of-two-arrays-ii | 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 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
1 line code that beats 100% | intersection-of-two-arrays-ii | 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)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$... | 1 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
Simple Python3 Solution || 🤖🧑💻💻|| Beats 97% | intersection-of-two-arrays-ii | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:\n n1=len(nums1)\n n2=len(nums2)\n nums1.sort()\n nums2.sort()\n arr=[]\n i,j=0,0\n while(i<n1 and j<n2):\n if(nums1[i]==nums2[j]):\n arr.... | 1 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
python solution with explanation | intersection-of-two-arrays-ii | 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. -->\nInitialize two empty dictionaries count1 and count2 to count the occurrences of each number in nums1 and nums2, respectively.\n\nLoop through each number in nums1 and ... | 2 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
🔥[Python 3] 3 solutions: 1 line solution, Counters, 2 pointers 🥷🏼 | intersection-of-two-arrays-ii | 0 | 1 | ```python3 []\nclass Solution:\n def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:\n return (Counter(nums1) & Counter(nums2)).elements()\n```\n```python3 []\nclass Solution:\n def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:\n res, cnt1, cnt2 = [], Counter(nu... | 5 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
intersection-of-two-arrays-ii | intersection-of-two-arrays-ii | 0 | 1 | # Code\n```\nclass Solution:\n def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:\n a = Counter(nums1)\n b = Counter(nums2)\n l = []\n for i in a:\n if b.get(i) is not None:\n l+=((min(b[i],a[i]))* [i])\n return l\n \n``` | 2 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
[Python] - clean & simple Solution | intersection-of-two-arrays-ii | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Here firstly we are sorting both of the list.\n- then we are adding infinity to end of the list in both.\n- Now we need to ust compare elements from start of both the list.\n- If both are same so move 1 ahead in both of the list and add it to our ... | 5 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
Simple Loop and if else solution #python3. | intersection-of-two-arrays-ii | 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 integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
Simple Python Solution | intersection-of-two-arrays-ii | 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 two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
Python3 | intersection-of-two-arrays-ii | 0 | 1 | # Code\n```\nclass Solution:\n def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:\n l = []\n a = Counter(nums1)\n b = Counter(nums2)\n for i,j in a.items():\n if i in b:\n l+=[i]*min(j,b[i])\n return l\n\n``` | 6 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
📌 Python3 solution using counters faster than 90% | intersection-of-two-arrays-ii | 0 | 1 | ```\nfrom collections import Counter\nclass Solution:\n def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:\n counter = Counter(nums1)\n \n result = []\n for i in nums2:\n k = counter.get(i)\n if k!=None and k > 0:\n counter[i]-=1\n ... | 7 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
Beginner friendly approach for finding the common elements between 2 arrays | intersection-of-two-arrays-ii | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nIterate through each element of list "nums1" and check if the element exists in list "nums2".\n\nIf the element exists in list "nums2", then append the element to another list and delete the element from the list "nums2" and return the resultant list\... | 3 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
Python3 Easy Solution | intersection-of-two-arrays-ii | 0 | 1 | # Code\n```\nclass Solution:\n def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:\n n1,n2=Counter(nums1),Counter(nums2)\n ans=[]\n for i in n1.keys() and n2.keys():\n ans.extend([i]*min(n1[i],n2[i]))\n return ans\n``` | 2 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
✔Python - Simple approach using hashmap | intersection-of-two-arrays-ii | 0 | 1 | # Approach\nVery simple approach using python dictionary. Please let me know in the comment section if you have any doubts about the solution, and I\'ll be happy to clarify\uD83E\uDD17\n\n> ***And don\'t forget to upvote\u2B06 if you truly like my effort\'s !***\n\n\n\n# Code\n```\nfrom collections import Counter\nclas... | 1 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
Python code for reference | intersection-of-two-arrays-ii | 0 | 1 | \n# O(n) time and O(n) space \n```\n def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:\n hmap = defaultdict(int)\n res = []\n for val in nums1: hmap[val] += 1\n for val in nums2:\n if(hmap[val]>0):\n res.append(val);\n hmap[val]... | 3 | Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
**Example 1:**
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
**Output:** \[2,2\]
**Example 2:**
**I... | null |
📌📌Python3 || ⚡146 ms, faster than 99.33% of Python3 | data-stream-as-disjoint-intervals | 0 | 1 | ```\nclass SummaryRanges:\n\n def __init__(self):\n self.intervals = []\n\n def addNum(self, value: int) -> None:\n left, right = 0, len(self.intervals) - 1\n while left <= right:\n mid = (left + right) // 2\n e = self.intervals[mid]\n if e[0] <= value <= e[1]... | 2 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
352. Data Stream as Disjoint Intervals | Python (set) | data-stream-as-disjoint-intervals | 0 | 1 | # Code\n```\nclass SummaryRanges:\n def __init__(self):\n self.intervals = set()\n\n def addNum(self, value: int) -> None:\n self.intervals.add(value)\n\n def getIntervals(self) -> List[List[int]]:\n ans = []\n arr = sorted(self.intervals)\n start, end = arr[0], arr[0]\n ... | 1 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
[Python] MinHeap + Hashset, Daily Challenge Jan., Day 28 | data-stream-as-disjoint-intervals | 0 | 1 | # Intuition\n\nfirst, think what kind of data structure do we need?\n1. need a sorted containers.\n2. need hashset to prevent duplicate\n\nfor a sorted containers, I think both of these should work:\n1. SortedList\n2. minHeap (lazy sorted)\n\nthen I choose minHeap and try to implement it\n\nit turns out the approach is... | 1 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
BUUM | data-stream-as-disjoint-intervals | 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 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
Clean Python | High Speed | O(n) time, O(1) space | Beats 98.9% | data-stream-as-disjoint-intervals | 0 | 1 | # Code\n```\nclass SummaryRanges:\n def __init__(self):\n self.intervals = []\n def addNum(self, val: int) -> None:\n left, right = 0, len(self.intervals) - 1\n while left <= right:\n mid = (left + right) // 2\n e = self.intervals[mid]\n if e[0] <= val <= e[1]... | 1 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
Stupid Desciption. Just expand the intervals . | data-stream-as-disjoint-intervals | 0 | 1 | # Intuition\n1. Sort all the incoming elements in the tree set.\n2. while implementing getIntervals() check for consecutives if elements are consecutive skip those elements\n3. else create a new interval\n\n# Approach\nI inserted elements into array at correct positions (by the help of lower_bound function)\nThen, crea... | 1 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
Using SortedList and Binary Search | data-stream-as-disjoint-intervals | 0 | 1 | # Intuition\nUsing Sorted Containers to maintain and update intervals for each new number and Binary Search to find where to insert the new number in out intervals list.\n\n# Approach\nFor each new number, find the index `idx` of where to insert the number.\nNow previous interval is `prev` which is `intervals[idx - 1]`... | 1 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
addNum O(log(n)) and getIntervals O(n) Solution | data-stream-as-disjoint-intervals | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy original idea which is shown below was to add the elements to my sorted list using binary search(O(logn)) then to insert would be O(n). Thus this would take O(n*log(n)). \n\nThen iterate through the list in getIntervals and combined an... | 1 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
python solution native approach easy to understand | data-stream-as-disjoint-intervals | 0 | 1 | ```\nclass SummaryRanges:\n\n def __init__(self):\n\t\t# list to store values\n self.rtr = []\n\n def addNum(self, value: int) -> None:\n if not self.rtr:\n\t\t\t# if list is empty add the value\n self.rtr.append(value)\n else:\n\t\t\t# if value is already present just return\n ... | 1 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
[Python] Short O(nlogn) solution using SortedList (binary search tree) | data-stream-as-disjoint-intervals | 0 | 1 | Keep a sorted list of ranges, for each add operations, binary search the insert position, compare val with the previous and next ranges. \n\nThis can be implemented with a `list of ranges` but the complexity\'s gonna be `O(n)` for each addNum() since inserting into a list takes O(n) time. Instead, using a `SortedList o... | 20 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
Python3 Beats 99.33% | data-stream-as-disjoint-intervals | 0 | 1 | \n\n# Code\n```\nclass SummaryRanges:\n\n def __init__(self):\n def cmp():\n return -1\n self.mp=defaultdict(cmp)\n self.a=[]\n \n\n def addNum(self, value: int)... | 14 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
Python 3 || 4 lines, sets, zip || T/M: 100% / 100% | data-stream-as-disjoint-intervals | 0 | 1 | ```\nclass SummaryRanges:\n def __init__(self):\n self.stack = deque()\n \n def dfs(self, nums):\n return list(zip(sorted(set([n for n in nums if n-1 not in nums])),\n sorted(set([n for n in nums if n+1 not in nums]))))\n\n def addNum(self, val):\n self.stack.... | 4 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
python3 Solution | data-stream-as-disjoint-intervals | 0 | 1 | \n```\nclass SummaryRanges:\n\n def __init__(self):\n self.a=[]\n\n def addNum(self, val: int) -> None:\n a=self.a\n left=0\n right=len(a)-1\n while left<=right:\n mid=left+(right-left)//2\n if a[mid][0]<=val<=a[mid][1]:\n return\n ... | 1 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
Python simple approach. Expand around each number to find the interval | data-stream-as-disjoint-intervals | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nConstruct the interval by expanding around each of the added numbers. \n\n# Complexity\n- Time complexity: __O(N)__ for getIntervals, __O(1)__ for addNum\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: __O(N)__ for getInt... | 19 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.