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 |
|---|---|---|---|---|---|---|---|
Solution | arithmetic-slices-ii-subsequence | 1 | 1 | ```C++ []\nusing ll = long;\nconst uint max_n = 1000;\nuint dp[max_n][max_n];\n\nclass Solution {\npublic:\n int numberOfArithmeticSlices(vector<int>& a) {\n uint n = a.size();\n uint res = 0;\n unordered_map<int, vector<uint>> ai = {};\n for (uint i = 0; i < n; i++) ai[a[i]].push_back(i)... | 1 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
8 lines solution in Python | arithmetic-slices-ii-subsequence | 0 | 1 | \n\n# Complexity\n- Time complexity: $$O(n**2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n**2)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def numberOfArithmeticSlices(self, nums: List[int]) -> int:\n n = len(nums);ans =... | 1 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
Arithmetic Slices II - Subsequence - Python Simple Solution | arithmetic-slices-ii-subsequence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n* Think about dp solution.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n* Storing all subsequence ending at i\'th index with all possible arithimetic difference at nums[i]\n\n# Complexity\n- Time complexity: O(n... | 1 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
Python3 solution | arithmetic-slices-ii-subsequence | 0 | 1 | \n# Code\n```\nclass Solution:\n def numberOfArithmeticSlices(self, A):\n total, n = 0, len(A)\n dp = [Counter() for item in A]\n for i in range(n):\n for j in range(i):\n dp[i][A[i] - A[j]] += (dp[j][A[i] - A[j]] + 1) \n total += sum(dp[i].values())\n ... | 1 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
Python3 - Recursion + Memoization | arithmetic-slices-ii-subsequence | 0 | 1 | # Intuition\nForce the first and second item of the subsequence. This will force a delta. Then you can recurse looking for expected values moving right. At each point you can continue or stop, so keep adding one.\n\n\n# Code\n```\nclass Solution:\n def numberOfArithmeticSlices(self, nums: List[int]) -> int:\n ... | 1 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
Python3 | Freq Tables | arithmetic-slices-ii-subsequence | 0 | 1 | \n```\nclass Solution:\n def numberOfArithmeticSlices(self, nums: List[int]) -> int:\n if len(nums) <= 2:\n return 0\n\n rst = 0\n freq = [defaultdict(int) for _ in range(len(nums))] # arithmetic sub-seqs\n for i, x in enumerate(nums):\n for j in range(i):\n ... | 1 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
FASTEST || BEATS 96% SUBMISSIONS || EASIEST || DP | arithmetic-slices-ii-subsequence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDYNAMIC PROGRAMMING\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCREATE A HASHMAP OF THE DIFFERENCE AND COUNT OF SUBSEQUENCE AND STORE THEM IN DP ARRAY.\n\n# Complexity\n- Time complexity:\n<!-- Add your time co... | 1 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
Python3 Solution with freq table | arithmetic-slices-ii-subsequence | 0 | 1 | \nclass Solution:\n def numberOfArithmeticSlices(self, nums: List[int]) -> int:\n \n n=len(nums)\n dp=[defaultdict(int) for _ in range(n)]\n res=0\n for i in range(n):\n for j in range(i):\n diff=nums[i]-nums[j]\n dp[i][diff]+=(1+dp[j][diff]... | 3 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
[Python] Recursion with Memoization | arithmetic-slices-ii-subsequence | 0 | 1 | # Intuition\nRecursion with Memoization\n\n# Approach\nConsider an element to be part of an Arithmetic Progression with\ndiffence d. See if the next term(s) exists in the array, get their\nindexes and recursively call for them increasing the count by 1.\nRecursive function will have 3 parameters- index,difference and c... | 4 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
10 line short Python Solution - 98% Faster | arithmetic-slices-ii-subsequence | 1 | 1 | \tclass Solution:\n\t\tdef numberOfArithmeticSlices(self, nums: List[int]) -> int:\n\t\t\td = [defaultdict(int) for _ in range(len(nums))]\n\t\t\tans=0\n\t\t\tfor i in range(1,len(nums)):\n\t\t\t\tfor j in range(i):\n\t\t\t\t\tcd = nums[i]-nums[j]\n\t\t\t\t\tjj = d[j][cd]\n\t\t\t\t\tii = d[i][cd]\n\t\t\t\t\tans+=jj\n\t... | 3 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
Intuitive DP || Python | arithmetic-slices-ii-subsequence | 0 | 1 | \n# Code\n```\nclass Solution:\n def numberOfArithmeticSlices(self, nums: List[int]) -> int:\n \n \n dp = [defaultdict(int) for _ in range(len(nums))]\n count = 0\n \n for ind, num in enumerate(nums):\n for sec_ind in range(ind):\n dif = num - nums[... | 0 | Given an integer array `nums`, return _the number of all the **arithmetic subsequences** of_ `nums`.
A sequence of numbers is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
* For example, `[1, 3, 5, 7, 9]`, `[7, 7, 7, 7]`, and ... | null |
Solution | number-of-boomerangs | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int numberOfBoomerangs(vector<vector<int>>& points) {\n int n = points.size ();\n \n vector <vector <int>> dis (n, vector <int> (n, 0));\n for (int i = 1; i < n; i++)\n for (int j = 0; j < i; j++)\n {\n int dist =... | 1 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Fast Python Solution | number-of-boomerangs | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to find the number of boomerangs in a given set of points. A boomerang is a set of three points such that the middle point is equidistant from the other two points.\n\n\n# Approach\n<!-- Describe your approach to solving th... | 2 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
447: Solution with step by step explanation | number-of-boomerangs | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define a function named numberOfBoomerangs that takes in a list of lists called points and returns an integer.\n2. Initialize a variable called boomerangs to 0 to keep track of the number of boomerangs.\n3. Iterate throug... | 5 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Python | High Speed | Fast O(n^2) | number-of-boomerangs | 0 | 1 | **Python | High Speed | Fast O(n^2)**\n\n```\nclass Solution:\n def numberOfBoomerangs(self, points):\n n = 0\n for a,b in points:\n counter = {}\n for x,y in points:\n # NOTE: x,y == a,b can only be registered once, so...\n\t\t\t\t# radius=0 never has enough... | 12 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Easy, with Process, explanation | number-of-boomerangs | 0 | 1 | 1. You just need the distance, not that very point.\n2. Store the distance in a way, you can have the ans.\n3. If m points are located a x distance from a point, you can choose 2 of them in m*(m-1) ways.(m c 2)\n4. All done, ya very simple.\n```\nn = len(points)\nt = [defaultdict(int) for _ in range(n)]\nfor i in range... | 1 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Python3 oneliner | number-of-boomerangs | 0 | 1 | # Intuition\nFor each potential point i, we can calculate the frequencies of distances to i and then use formula for the partial sum. \n\n# Approach\nAfter solving it iterativley I tought I could make it a oneliner using generators.\n\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n)\n\n# Code\n```\n... | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Python Counter and permutations solution | number-of-boomerangs | 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 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Math Equation, O(n^2) | number-of-boomerangs | 0 | 1 | # Intuition\nEach points $$p_i$$ has distinct boomerangs $$(p_i, p_j,p_q)$$, i.e. different points cannot have the same boomerangs $$(p_i, p_j,p_q)$$ since the order is matter and it\'s defined by the distance from $$p_i$$.\nTherefore, the total number of boomerangs is the sum of each point total boomerang $$Result=\\s... | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Python3 - 598ms, O(n^2) | number-of-boomerangs | 0 | 1 | ```\ndef numberOfBoomerangs(self, points: List[List[int]]) -> int:\n res = 0\n for p1 in points:\n d = {}\n for p2 in points:\n distance = math.dist(p1, p2)\n if distance not in d: d[distance] = 1\n else: d[distance] += 1\n for val in d.values():\n ... | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Python Easy Solution | number-of-boomerangs | 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 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
hash map + distance as key | number-of-boomerangs | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nthe idea is to keep a distance as key, check how many points in the same key\n# Approach\n<!-- Describe your approach to solving the problem. -->\nres += 2 * counter[key] if to check a new points with the same key, if add 1 point, we need... | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Python in 5 lines | number-of-boomerangs | 0 | 1 | ```\nclass Solution:\n def numberOfBoomerangs(self, points: List[List[int]]) -> int:\n ans = 0\n for p in points:\n cnt = collections.Counter([(p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2 for q in points])\n ans += sum([n*(n-1) for n in cnt.values()])\n return ans\n``` | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Easy and Simple Approach in Python | number-of-boomerangs | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nHHashmap and iteration\n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nfrom math import*\nclass Solution:\n def numberOfBoomerangs(self, points: List[List[int]]) -> int:\n c... | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Python Solution | number-of-boomerangs | 0 | 1 | \n\n\n# Approach\nThe solution calculates the number of boomerangs in a given set of points in the plane. A boomerang is a tuple of three points (i, j, k) such that the distance ... | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Number of Boomerangs | number-of-boomerangs | 0 | 1 | # Code\n```\nclass Solution:\n def numberOfBoomerangs(self, points: List[List[int]]) -> int:\n def distance(p1, p2):\n x1, y1 = p1\n x2, y2 = p2\n return sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)\n\n\n if len(points) <= 2:\n return 0\n\n ans = 0\n\n ... | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
<easy solution using hash table and math> | number-of-boomerangs | 0 | 1 | \n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def numberOfBoomerangs(self, points: List[List[int]]) -> int:\n def distance(p1, p2):\n ... | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Easy Python Solution | number-of-boomerangs | 0 | 1 | # Intuition\nUsed dictionary to store the mapping between the distances\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nCalculate all possible combination which can be formed if new equal distance is found\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n... | 0 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Python | number-of-boomerangs | 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 | You are given `n` `points` in the plane that are all **distinct**, where `points[i] = [xi, yi]`. A **boomerang** is a tuple of points `(i, j, k)` such that the distance between `i` and `j` equals the distance between `i` and `k` **(the order of the tuple matters)**.
Return _the number of boomerangs_.
**Example 1:**
... | null |
Easy-to-understand approach using hashtable. | find-all-numbers-disappeared-in-an-array | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def findDisappearedNumbers(self, nums: List[int]) -> List[int]:\n \n d = {}\n for i in range(1, len(nums)+1): \n d[i] = 0\n for i in nums:\n d[i] += 1\n result = []\n for key, val in d.items():\n if val == ... | 0 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
✔Beats 99.45% TC || Python Solution | find-all-numbers-disappeared-in-an-array | 0 | 1 | \n\n# Complexity\n- Time complexity:\nBeats 99.45%\n\n# Do Upvote if you like it :)\n\n# Code\n```\n\nclass Solution:\n def findDisappearedNumbers(self, nums: List[int]) -> List[int]:\n set_nums = set(nums)\n missing = []\n\n for i in range(1,len(nums)+1):\n if i not in set_nums:\n ... | 17 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
🐍 python solution; O(N) with no extra space | find-all-numbers-disappeared-in-an-array | 0 | 1 | # Approach\nwe use cycle sort. Each number after cycle sort should be at its relevant index (which here is value-1). If that is not the case, then that index+1 is missing. \n\n# Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution:\n def findDisappearedNumbers(self, nums: Li... | 1 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
Python3 0(n) optimal Solution Negative marking | find-all-numbers-disappeared-in-an-array | 0 | 1 | ```\nclass Solution(object):\n def findDisappearedNumbers(self, nums):\n """\n :type nums: List[int]\n :rtype: List[int]\n """\n \n for i in range(len(nums)):\n \n x = abs(nums[i])\n \n if x-1 < len(nums) and nums[x-1] > 0:\n ... | 1 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
448: Solution with step by step explanation | find-all-numbers-disappeared-in-an-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Iterate through the input list nums, and for each element nums[i]:\na. Calculate the index of nums[i] in the list by taking the absolute value of nums[i] and subtracting 1. This is because the input list contains integers... | 9 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
Python 3 solutions || O(1) SC || One Line Solution | find-all-numbers-disappeared-in-an-array | 0 | 1 | **Python :**\n\n\nTime complexity : *O(n)*\nSpace complexity : *O(n)*\n```\ndef findDisappearedNumbers(self, nums: List[int]) -> List[int]:\n\tallNums = [0] * len(nums)\n\tfor i in nums:\n\t\tallNums[i - 1] = i\n\n\treturn [i + 1 for i in range(len(allNums)) if allNums[i] == 0]\n```\n\nTime complexity : *O(n)*\nSpace c... | 28 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
Python | Easy Solution✅ | find-all-numbers-disappeared-in-an-array | 0 | 1 | ```\ndef findDisappearedNumbers(self, nums: List[int]) -> List[int]: # nums = [4,3,2,7,8,2,3,1]\n full_list = [i for i in range(1,len(nums)+1)] # [1, 2, 3, 4, 5, 6, 7, 8]\n return list(set(full_list) - set(nums)) # {1, 2, 3, 4, 5, 6, 7, 8} - {1, 2, 3, 4, 7, 8} = [5,6]\n``` | 16 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
Python 3 | find-all-numbers-disappeared-in-an-array | 0 | 1 | ```\nclass Solution:\n def findDisappearedNumbers(self, nums: List[int]) -> List[int]:\n for n in nums:\n a = abs(n) - 1\n if nums[a] > 0: nums[a] *= -1\n return [i+1 for i in range(len(nums)) if nums[i] > 0]\n```\n\nPlease let me know if any improvements can be made.\n\nThank you... | 58 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
Python3|| Beats 98.89%|| Simple solution | find-all-numbers-disappeared-in-an-array | 0 | 1 | # Please upvote\n\n\n\n# Code\n```\nclass Solution:\n def findDisappearedNumbers(self, nums: List[int]) -> List[int]:\n nums1 = set(nums)\n lst=[]\n for i in range(1,len(nums)+1):\n ... | 2 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
1 liner no brainer - fast than 95% | find-all-numbers-disappeared-in-an-array | 0 | 1 | ```\nclass Solution:\n def findDisappearedNumbers(self, nums: List[int]) -> List[int]:\n return set(nums) ^ set(range(1,len(nums)+1))\n``` | 2 | Given an array `nums` of `n` integers where `nums[i]` is in the range `[1, n]`, return _an array of all the integers in the range_ `[1, n]` _that do not appear in_ `nums`.
**Example 1:**
**Input:** nums = \[4,3,2,7,8,2,3,1\]
**Output:** \[5,6\]
**Example 2:**
**Input:** nums = \[1,1\]
**Output:** \[2\]
**Constrain... | This is a really easy problem if you decide to use additional memory. For those trying to write an initial solution using additional memory, think counters! However, the trick really is to not use any additional space than what is already available to use. Sometimes, multiple passes over the input array help find the s... |
449: Time 91.2%, Solution with step by step explanation | serialize-and-deserialize-bst | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nFor the serialize function:\n\n1. If the given root is None, return an empty string.\n2. Create an empty stack and push the root node to it.\n3. Create an empty string called serialized.\n4. While the stack is not empty, pop... | 2 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
mY BfS | serialize-and-deserialize-bst | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. --> bfs with \'N\'\n\n# Approach\n<!-- Describe your approach to solving the problem. --> bfs\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> O(N)\n\n- Space complexity:\n<!-- Add your space complexit... | 1 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
Python solution | serialize-and-deserialize-bst | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking to implement a serialization and deserialization method for a binary tree. The serialization method should take a binary tree and convert it into a string format, while the deserialization method should take the stri... | 2 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
Python, BFS > 90% | serialize-and-deserialize-bst | 0 | 1 | ```\nclass Codec:\n\n def serialize(self, root: TreeNode) -> str:\n """Encodes a tree to a single string.\n """\n if not root: return \'\'\n tree = []\n queue = deque([root])\n while queue:\n node = queue.popleft()\n if node:\n tree.appen... | 18 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
Simple solution, beats 65%, 59% | serialize-and-deserialize-bst | 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 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
Simple DFS solution | serialize-and-deserialize-bst | 0 | 1 | ```\nclass Codec:\n def serialize(self, root: Optional[TreeNode]) -> str:\n """Encodes a tree to a single string.\n """\n \n def dfs(root):\n return [] if not root else [str(root.val)] + dfs(root.left) + dfs(root.right)\n \n return \' \'.join(reversed(dfs(root)))\n ... | 0 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
Python3 - preorder + stack | serialize-and-deserialize-bst | 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)\n- Space complexity: O(N)\n\n# Code\n```\nclass Codec:\n def serialize(self, root) -> str:\n preorder = []\n d... | 0 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
Python O(N): deserialize from pre order using Stack | serialize-and-deserialize-bst | 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- To serialize: pre order traversal\n- To deserialize: If next value is less than current node value, we create new node to the left of current node and move down. Els... | 0 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
Begineer friendly Python3 solution using BFS | serialize-and-deserialize-bst | 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 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
🐍 python, O(N) | serialize-and-deserialize-bst | 0 | 1 | # Approach\nThe serialization is very similar to inorder traversal DFS. \nAnd for deserialize we can take a very similar apporach to [1008. Construct Binary Search Tree from Preorder Traversal\n](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)\n\nKeep in mind the fact that we know i... | 0 | Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a **binary search t... | null |
Recursive solution with o(found node) space complexity and o(n) time complexity | delete-node-in-a-bst | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nthis is based on two problems. first one is to find the node that is required and second one is to remove it \n\nto remove it we can observe three scenerios:\n1) it will have no children\n2) it has 2 childrens\n3) it has both childrens\n\... | 2 | Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return _the **root node reference** (possibly updated) of the BST_.
Basically, the deletion can be divided into two stages:
1. Search for a node to remove.
2. If the node is found, delete the node.
**Example 1:**
**Inpu... | null |
[ Python ] | Simple & Clean Solution | delete-node-in-a-bst | 0 | 1 | \n# Code\n```\nclass Solution:\n def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:\n if not root: return root\n\n if root.val > key:\n root.left = self.deleteNode(root.left, key)\n elif root.val < key:\n root.right = self.deleteNode(root.right,... | 14 | Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return _the **root node reference** (possibly updated) of the BST_.
Basically, the deletion can be divided into two stages:
1. Search for a node to remove.
2. If the node is found, delete the node.
**Example 1:**
**Inpu... | null |
450: Solution with step by step explanation | delete-node-in-a-bst | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. If the root is None, return None.\n2. If the key to be deleted is less than the root\'s key, then recursively call the function with the left subtree as the new root.\n3. If the key to be deleted is greater than the root\... | 11 | Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return _the **root node reference** (possibly updated) of the BST_.
Basically, the deletion can be divided into two stages:
1. Search for a node to remove.
2. If the node is found, delete the node.
**Example 1:**
**Inpu... | null |
Python 3 -> 97.55% faster. Explanation added | delete-node-in-a-bst | 0 | 1 | **Suggestions to make it better are always welcomed.**\n\nKey Learnings for me:\n1. First find the node that we need to delete.\n2. After it\'s found, think about ways to keep the tree BST after deleting the node. \n\t1. If there\'s no left or right subtree, we found the leaf. Delete this node without any further trave... | 42 | Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return _the **root node reference** (possibly updated) of the BST_.
Basically, the deletion can be divided into two stages:
1. Search for a node to remove.
2. If the node is found, delete the node.
**Example 1:**
**Inpu... | null |
Solution | delete-node-in-a-bst | 1 | 1 | ```C++ []\nclass Solution {\npublic:\nTreeNode* helper(TreeNode* root){\n if(root->left==NULL){\n return root->right;\n }\n else if(root->right==NULL){\n return root->left;\n }\n else{\n TreeNode* rightchild = root->right;\n TreeNode* lastright = find(root->left);\n lastright->righ... | 5 | Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return _the **root node reference** (possibly updated) of the BST_.
Basically, the deletion can be divided into two stages:
1. Search for a node to remove.
2. If the node is found, delete the node.
**Example 1:**
**Inpu... | null |
Easy Dictionary approach | Python 3 | Beats 84.6% ( time ) | sort-characters-by-frequency | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nusing dictionary to find all the frequencies of alphabets\nO(n)\nfor loop in sorted dictionary (sort by values)\nO(n*log(n))\nFinally we return the string in reverse... | 1 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Python3|| O(N) | sort-characters-by-frequency | 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- O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(N)\n<!-- Add your space complexity here, ... | 1 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
🐍 Python | Easy Explantation | 3 line | sort-characters-by-frequency | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n- Count frequency of each character in input string.\n- Sort dictionary was get in descending \n- Loop dictionary and append that in result\n# Complexity\n\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def sort(self,nums: list)-> list:\n i=0\n while(i<len(nums)):\n swapped=0\n j=0\n while(j<len(nums)-i-1):\n if nums[j]>nums[j+1]:\n ... | 1 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Simple Python solution using Hashmaps | sort-characters-by-frequency | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def frequencySort(self, s: str) -> str:\n d = {}\n for i in s:\n if i in d:\n d[i] += 1\n else:\n d[i] = 1\n \n c = [[i, d[i]] for i in d ]\n c.sort(key = lambda x: x[1], reverse = True)\n\n ... | 1 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Python Easy Solution with simple explaination | sort-characters-by-frequency | 0 | 1 | \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n<b>step 1 :</b> Create a dictionary to store the frequency of elements\n<b>step 2 :</b> Define a helper method inside. which returns the key associated with the frequency\n<b>step 3 :</b> create an empty string, the fetch the dictionary values in ... | 3 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Simple solution using dictionary/hashmap in O(n). Beginners Friendly approach!! 🔥🔥 | sort-characters-by-frequency | 1 | 1 | \n# Code\n```\nclass Solution:\n def frequencySort(self, s: str) -> str:\n count_dict={}\n for i in s:\n count_dict[i]=count_dict.get(i,0)+1\n sorted_dict=dict(sorted(count_dict.items(),key = lambda item : item[1], reverse=True))\n ans=\'\'\n print(sorted_dict)\n ... | 2 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Python Easy Solution ^_____^ | sort-characters-by-frequency | 0 | 1 | # **PLEASE UPVOTE ^_^ \u2764**\n# Code\n```\nclass Solution:\n def frequencySort(self, s: str) -> str:\n l={}\n for i in s:\n l[i]=l.get(i,0)+1\n l=[[k,v] for k,v in (sorted(l.items(),key=lambda a: a[1],reverse=True))]\n ans=""\n for i,j in l:\n ans+=i*j\n ... | 1 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Grasping LOGICAL Solution | sort-characters-by-frequency | 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:99%\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:75%\n<!-- Add your space complexity here, e.g. $$O(n... | 3 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Python || Hash Table || 96% | sort-characters-by-frequency | 0 | 1 | **If you got help from this,... Plz Upvote .. it encourage me**\n\n# Code\n> # Use lambda function to sort Dict in Descending Order by Value\n```\nclass Solution:\n def frequencySort(self, s: str) -> str:\n d = {}\n for char in s:\n d[char] = d.get(char,0) + 1\n\n sorted_d = sorted(d.... | 2 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
[Python/Java] HashTable + Heap/Sort, Easy to understand and Detailed Explanation | sort-characters-by-frequency | 1 | 1 | I understand there are a tons of solution like mine, but here I offered a clean and detailed explanation on this kind of problem.\n\nFirst, the idea of solution as follows.\n1. HashTable + Sort: We use HashTable to count the occurence and sort the entries based on the occurence, then build the string.\n2. HashTable + H... | 74 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Python Bucket Sort - Video Solution | sort-characters-by-frequency | 0 | 1 | I have tried to explain the intution behind Bucket Sort and other similar solutions in this [video](https://youtu.be/ofCIa35IIEI).\n\n```\nclass Solution:\n def frequencySort(self, s: str) -> str:\n n = len(s)\n \n c = Counter(s)\n \n bucket = [[] for _ in range(n+1)]\n \n ... | 3 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Two approaches 94% faster one using dictionary and counter . Another one using map and then sorting | sort-characters-by-frequency | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nin first one i optimised the second code using counter from collections. WHta it does is counts the occurence a particular string and stores it in a dictionary. then i did the basic steps for sorting the dictionary using lam... | 2 | Given a string `s`, sort it in **decreasing order** based on the **frequency** of the characters. The **frequency** of a character is the number of times it appears in the string.
Return _the sorted string_. If there are multiple answers, return _any of them_.
**Example 1:**
**Input:** s = "tree "
**Output:** "eer... | null |
Python3: Counter Intersections | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | # Intuition\nthe number of arrows == the number of intersections among all ranges\n\n# Approach\n1. Sort (as you need)\n2. Kinda the same approach for finding the unions: use heapq to track the rightmost intersection (since the heapq implementation in Python library is minheap, I took the negative to make sure the righ... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
easy python solution | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n- O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def findMinArrowShots(self, points: List[List[int]]) -> int:\n points.sort()\n c=1\n ... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Python3 Solution | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | # Intuition\nWe can sort our points array, loop through our sorted values and find overlap by keeping track of lower and upper bounds.\n\n# Approach\nFirst sort the array, initialize variables lower and upper, loop through values and update lower and upper bounds as we go. If we find overlap, update our lower and upper... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Using Disjoint Set Union (DSU), Python3 | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDisjoint Set Union\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing Disjoint Set Union approach for maintaining tree that serves intersections.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity he... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Python Lambda Sorting Maths Solution | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | # Code\n```\nclass Solution:\n def findMinArrowShots(self, points: List[List[int]]) -> int:\n n=len(points)\n points = sorted(points, key = lambda x: x[1])\n maxa=-float(\'inf\')\n \n ans=0\n\n for i in range(0,n):\n if maxa<points[i][0]:\n ans+=1\n ... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Python3 Simple Solution Using Sort and Greedy | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBy looking at the question and points input we figure its a range type question in which we have to find common intervals\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIn any range or interval type related proble... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Greedy 🔥Approach Python | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | \n# Approach\nGreedy Appoach\n\n# Complexity\n- Time complexity:\nO(N logN)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def findMinArrowShots(self, p: List[List[int]]) -> int:\n lp=(p[0][1])\n cnt=0\n for i in sorted(p):\n if i[0]>lp:\n cnt+=1\n ... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
SIMPLE PYTHON GREEDY SOLUTION | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nSort the point according to their ending point.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:$$O(NlogN)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space compl... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Easy to understand Python3 solution with Commentsssss!!😸 | minimum-number-of-arrows-to-burst-balloons | 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 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Python | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | ```\nclass Solution:\n def findMinArrowShots(self, points: List[List[int]]) -> int:\n points.sort(key=lambda x: x[0])\n xstart = points[0][0]\n xend = points[0][1]\n arrow = 1\n for i in range(1,len(points)):\n if points[i][0] <= xend:\n xstart = max(xstar... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Python Elegant & Short | Greedy | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | ```\ndef findMinArrowShots(self, points: List[List[int]]) -> int:\n points.sort(key=lambda x: x[1])\n\n arrows = 0\n end = -maxsize\n\n for s, e in points:\n if s > end:\n arrows += 1\n end = e\n\n return arrows\n\n``` | 3 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
🎀✨ Easy Code with Kawaii Illustration | 10 lines only | Python | O(n) Complexity ✨🎀 | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | # Visualization\n\n\nConsider the first example: \nInput: points = [[10,16],[2,8],[1,6],[7,12]]\nOutput: 2\nExplanation: The balloons can be burst by 2 arrows:\n- Shoot an arrow at x = 6, bursting the ba... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Easy Python Code in O(NlogN) Time Complexity | minimum-number-of-arrows-to-burst-balloons | 0 | 1 | # Code\n```\nclass Solution:\n def findMinArrowShots(self, points: List[List[int]]) -> int:\n\n points = sorted(points, key = lambda x:x[1])\n cnt = len(points)\n\n #x = points[0][0]\n #Just imagine of maintaining a range like merge intervals\n y = points[0][1]\n\n for i in ... | 1 | There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where `points[i] = [xstart, xend]` denotes a balloon whose **horizontal diameter** stretches between `xstart` and `xend`. You do not know the exact y-coordinates of the ball... | null |
Simplest Approach✔ || Easy and Understandable (Java / Python) Code | minimum-moves-to-equal-array-elements | 1 | 1 | # Approach\nA simple approach could be to find out the minimum element of the array in the first traversal and then in the next traversal find the difference between each element of the array with the minimum one and sum them up.\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n# Code\n\n```java []\nclass Solution {\n ... | 4 | Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
In one move, you can increment `n - 1` elements of the array by `1`.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** 3
**Explanation:** Only three moves are needed (remember each move inc... | null |
[Python] 2 Short and Simple Approaches with Explanation | minimum-moves-to-equal-array-elements | 0 | 1 | ### Introduction\n\nWe want to find the minimum number of moves needed to make the elements of `nums` equal. A move is defined by incrementing any `n - 1` elements in `nums` by 1, where `n` is the number of elements in `nums`. This can be re-written to incrementing **all elements but one** in `nums` by 1.\n\n---\n\n###... | 10 | Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
In one move, you can increment `n - 1` elements of the array by `1`.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** 3
**Explanation:** Only three moves are needed (remember each move inc... | null |
1 line solution; beats 98% O(N) time and 96% O(1) space; easy to understand | minimum-moves-to-equal-array-elements | 0 | 1 | Let,\n\t`k` = number of elements in `nums`\n\t`s` = sum of elements in `nums` \n\t`m` = minumum number of moves \n\t`e` = element at all indices in final array (Eg., [a, b, d, a, c] will turn to [e, e, e, e, e])\n\n\nSum of all elements in the final array is going to be `k * e`.\n\nOn each move, the sum of the original... | 16 | Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
In one move, you can increment `n - 1` elements of the array by `1`.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** 3
**Explanation:** Only three moves are needed (remember each move inc... | null |
453: Solution with step by step explanation | minimum-moves-to-equal-array-elements | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define a class named Solution.\n2. Define a method named minMoves that takes an input array nums of integers and returns an integer.\n3. Find the minimum element in the input array nums using the min() function and store ... | 5 | Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
In one move, you can increment `n - 1` elements of the array by `1`.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** 3
**Explanation:** Only three moves are needed (remember each move inc... | null |
Simple solution with description of the intuitive approach | minimum-moves-to-equal-array-elements | 0 | 1 | ```\nclass Solution:\n def minMoves(self, nums: List[int]) -> int:\n\t\t# If we observe some sample arrays we will see that the minimum number has to equal the maximum number so that they are equal\n\t\t# However during this time (n - 2) elements will also increase by 1 for each step where n is the length of the arr... | 6 | Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
In one move, you can increment `n - 1` elements of the array by `1`.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** 3
**Explanation:** Only three moves are needed (remember each move inc... | null |
Python3 Detailed Explanation in comments O(N), O(1) | minimum-moves-to-equal-array-elements | 0 | 1 | ***Please upvote if this helps!***\n```\nclass Solution:\n def minMoves(self, nums: List[int]) -> int:\n\t\n # Approach:\n # we want to make all the elements equal ; question does not \n # say "to which element" they should be made equal so that means\n # we can "choose" to what element t... | 2 | Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
In one move, you can increment `n - 1` elements of the array by `1`.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** 3
**Explanation:** Only three moves are needed (remember each move inc... | null |
Intuitive approach with Python example | minimum-moves-to-equal-array-elements | 0 | 1 | We are only trying to match all the numbers in an array, so operations should be viewed by how they effect elements relative to the rest. Subtracting 1 to a single element is relatively equivalent to adding 1 to all the others. So the question is equivalent to asking "How many time must we subtract 1 to any element to ... | 3 | Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
In one move, you can increment `n - 1` elements of the array by `1`.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** 3
**Explanation:** Only three moves are needed (remember each move inc... | null |
One line Python solution with clear explanation | minimum-moves-to-equal-array-elements | 0 | 1 | Here is the simple idea.\nEach time we increase n-1 elements by 1, which equals to decrease the one which is not increased by 1, then the idea is clear.\n```\nclass Solution:\n def minMoves(self, nums: List[int]) -> int:\n return sum(nums) - min(nums)*len(nums)\n``` | 9 | Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
In one move, you can increment `n - 1` elements of the array by `1`.
**Example 1:**
**Input:** nums = \[1,2,3\]
**Output:** 3
**Explanation:** Only three moves are needed (remember each move inc... | null |
✔️ [Python3] O(n^2) ¯\_( ͡👁️ ͜ʖ ͡👁️)_/¯, Explained | 4sum-ii | 0 | 1 | **UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nThe simple bruteforce solution would take `n^4` time complexity here. We can do better by dividing given lists into two groups and precalculating all possible sums for the first group. Results go to a hashmap where k... | 107 | Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
**Example 1:**
**Input:** nums1 = \[1,2\], nums2 = \[-2,-1\], nums3 = \[-1,2\], nums4 = \[0,2\]
**Ou... | null |
Solution | 4sum-ii | 1 | 1 | ```C++ []\nint smallest(vector<int> const &v)\n{\n return *min_element(begin(v), end(v));\n}\nint greatest(vector<int> const &v)\n{\n return *max_element(begin(v), end(v));\n}\nclass Solution\n{\npublic:\n int fourSumCount(vector<int> &nums1, vector<int> &nums2, vector<int> &nums3, vector<int> &nums4)\n {\n... | 1 | Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
**Example 1:**
**Input:** nums1 = \[1,2\], nums2 = \[-2,-1\], nums3 = \[-1,2\], nums4 = \[0,2\]
**Ou... | null |
[Python] Clean and concise | Detail explanation | One linear | 4sum-ii | 0 | 1 | The first things to note is unlike the prevoius variation of this problem (3 Sum, 2 Sum) where they asked us to return the indexes of the array, here we are supposed to return only the no of ways in which we can achieve it. \n\nSo, first lets loop through any of the two array and store all possible combination of sum w... | 22 | Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
**Example 1:**
**Input:** nums1 = \[1,2\], nums2 = \[-2,-1\], nums3 = \[-1,2\], nums4 = \[0,2\]
**Ou... | null |
Python3 two-liner | 4sum-ii | 0 | 1 | Instead of using a usual dictionary to store 2-sums and their counts we can use standard Counter class that does that automatically and write solution in the most pythonic way possible:\n\n```\nclass Solution:\n def fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int:\n sums = co... | 33 | Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
**Example 1:**
**Input:** nums1 = \[1,2\], nums2 = \[-2,-1\], nums3 = \[-1,2\], nums4 = \[0,2\]
**Ou... | null |
Python || 96.13% Faster || O(n) || Counter | 4sum-ii | 0 | 1 | ```\nclass Solution:\n def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:\n AB = Counter(a + b for a in nums1 for b in nums2)\n CD = Counter(c + d for c in nums3 for d in nums4)\n count = 0\n for key in AB:\n count += AB[key] ... | 1 | Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
**Example 1:**
**Input:** nums1 = \[1,2\], nums2 = \[-2,-1\], nums3 = \[-1,2\], nums4 = \[0,2\]
**Ou... | null |
Python 3 (500ms) | N^2 Approach | Easy to Understand | 4sum-ii | 0 | 1 | ```\nclass Solution:\n def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:\n ht = defaultdict(int)\n for n1 in nums1:\n for n2 in nums2:\n ht[n1 + n2] += 1\n ans = 0\n c=0\n for n3 in nums3:\n f... | 3 | Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
**Example 1:**
**Input:** nums1 = \[1,2\], nums2 = \[-2,-1\], nums3 = \[-1,2\], nums4 = \[0,2\]
**Ou... | null |
454: Solution with step by step explanation | 4sum-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Create a defaultdict(int) called sums12 to store the counts of all possible sums of elements in nums1 and nums2.\n\n2. For each pair of elements num1 and num2 in nums1 and nums2, calculate their sum num1 + num2 and increm... | 2 | Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
**Example 1:**
**Input:** nums1 = \[1,2\], nums2 = \[-2,-1\], nums3 = \[-1,2\], nums4 = \[0,2\]
**Ou... | null |
4 Sum II | Python | Easy to understand | 4sum-ii | 0 | 1 | ```\ndef fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int:\n \n h = dict()\n \n for a in A:\n for b in B:\n p = -(a+b)\n if p in h:\n h[p]+=1\n else:\n h[p]=1\n ... | 10 | Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
**Example 1:**
**Input:** nums1 = \[1,2\], nums2 = \[-2,-1\], nums3 = \[-1,2\], nums4 = \[0,2\]
**Ou... | null |
Python3 Solution || Beats 92.70% || Dictionary Approach | 4sum-ii | 0 | 1 | ```\nclass Solution:\n def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:\n dictionary = defaultdict(int)\n for n1 in nums1:\n for n2 in nums2:\n numberNeeded = -(n1 + n2)\n dictionary[numberNeeded] += 1\n ... | 2 | Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
**Example 1:**
**Input:** nums1 = \[1,2\], nums2 = \[-2,-1\], nums3 = \[-1,2\], nums4 = \[0,2\]
**Ou... | null |
455: Solution with step by step explanation | assign-cookies | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the findContentChildren function that takes in two lists of integers, g and s, and returns an integer representing the number of content children.\n2. Sort the lists g and s in non-decreasing order using the sort()... | 19 | Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
Each child `i` has a greed factor `g[i]`, which is the minimum size of a cookie that the child will be content with; and each cookie `j` has a size `s[j]`. If `s[j] >= g[i]`, we can assign ... | null |
Simple Explanation 😎 | assign-cookies | 0 | 1 | # Intuition\nThink like dad\uD83D\uDC68\u200D\uD83D\uDC66\u200D\uD83D\uDC66!\n- You have a cookies & you first think how to give cookies to childrens :\n- case1 : Random order\n- case2 : Give minimum size cookie which child have minimum greed & then jump into another child (when first child satisfy).\n- At the end you ... | 5 | Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
Each child `i` has a greed factor `g[i]`, which is the minimum size of a cookie that the child will be content with; and each cookie `j` has a size `s[j]`. If `s[j] >= g[i]`, we can assign ... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.