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 |
|---|---|---|---|---|---|---|---|
O(k(m+n)). Python Solution with monotonically decreasing stack. Commented for clarity. | create-maximum-number | 0 | 1 | This solution is pretty efficient and intuitive.\nTime complexity is **O(k(n+m))** where n and m is length of each list. \n\n\nTo understand this solution you must also do other problems concerning monotonic stack like LC-402 which are pre-requisites for this problem.\n\nDo give me a thumbs up if u like it.\n```\nclass... | 3 | You are given two integer arrays `nums1` and `nums2` of lengths `m` and `n` respectively. `nums1` and `nums2` represent the digits of two numbers. You are also given an integer `k`.
Create the maximum number of length `k <= m + n` from digits of the two numbers. The relative order of the digits from the same array mus... | null |
python 1 hard = 2 easy + 1 medium problems | create-maximum-number | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf there is only one array, how to solve it, use monotonic stack.\nIf we can find subarray in nums1 and nums2, then merge them.\nThe ans is all combinations of the merge\n# Approach\n<!-- Describe your approach to solving the problem. -->... | 1 | You are given two integer arrays `nums1` and `nums2` of lengths `m` and `n` respectively. `nums1` and `nums2` represent the digits of two numbers. You are also given an integer `k`.
Create the maximum number of length `k <= m + n` from digits of the two numbers. The relative order of the digits from the same array mus... | null |
Solution | create-maximum-number | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n #define MIN(a,b) (a<b?a:b)\n #define MAX(a,b) (a>b?a:b)\n void getMax(int* num, int& len, int* result, int& t, int& sortedLen)\n {\n \tint n, top = 0;\n \tresult[0] = num[0];\n \tconst int need2drop = len - t;\n \tfor (int i = 1; i < len; ++i){\n \t\tn =... | 1 | You are given two integer arrays `nums1` and `nums2` of lengths `m` and `n` respectively. `nums1` and `nums2` represent the digits of two numbers. You are also given an integer `k`.
Create the maximum number of length `k <= m + n` from digits of the two numbers. The relative order of the digits from the same array mus... | null |
[Python3] greedy | create-maximum-number | 0 | 1 | \n```\nclass Solution:\n def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:\n \n def fn(arr, k):\n """Return largest sub-sequence of arr of size k."""\n ans = []\n for i, x in enumerate(arr): \n while ans and ans[-1] < x and len... | 2 | You are given two integer arrays `nums1` and `nums2` of lengths `m` and `n` respectively. `nums1` and `nums2` represent the digits of two numbers. You are also given an integer `k`.
Create the maximum number of length `k <= m + n` from digits of the two numbers. The relative order of the digits from the same array mus... | null |
Python3 | Monotonic Stack | Greedy | create-maximum-number | 0 | 1 | ```\nclass Solution:\n def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:\n \n def find_k_max_number_in_an_array(nums, k):\n drop_possible = len(nums) - k\n n = len(nums)\n stack = []\n for i, val in enumerate(nums):\n ... | 1 | You are given two integer arrays `nums1` and `nums2` of lengths `m` and `n` respectively. `nums1` and `nums2` represent the digits of two numbers. You are also given an integer `k`.
Create the maximum number of length `k <= m + n` from digits of the two numbers. The relative order of the digits from the same array mus... | null |
Python | Easy to understand | Greedy + Dynamic Programminng | create-maximum-number | 0 | 1 | To solve the problem, we can define the subproblem "getMaxNumberString(i, j, length)": get the max number string with "length" from index "i" in nums1 and index "j" in nums2.\n\ngetMaxNumberString(i, j, length)\n- if nums1 has bigger digit, getMaxNumberString(i, j, length) = str(nums1[index1]) + getMaxNumberString(inde... | 1 | You are given two integer arrays `nums1` and `nums2` of lengths `m` and `n` respectively. `nums1` and `nums2` represent the digits of two numbers. You are also given an integer `k`.
Create the maximum number of length `k <= m + n` from digits of the two numbers. The relative order of the digits from the same array mus... | null |
DP || MEMOIZATION || EASY | coin-change | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`.
You may a... | null |
Bottom-up DP with 1D array for memoization | coin-change | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`.
You may a... | null |
[VIDEO] Visualization of Bottom-Up Dynamic Programming Solution | coin-change | 0 | 1 | https://youtu.be/SIHLJdF4F8A?si=1zjA4TRmWfCUQp1Y\n\nFor an overview of the greedy approach and the brute force recursive solution, please see the video. This explanation will focus on the dynamic programming solution.\n\nWe start by initializing an array called `dp`. Each element of this array will contain the fewest... | 5 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`.
You may a... | null |
Python Easy 2 DP approaches | coin-change | 0 | 1 | 1. ##### **Bottom Up DP(Tabulation)**\n\n```\nclass Solution:\n def coinChange(self, coins: List[int], amount: int) -> int: \n dp=[math.inf] * (amount+1)\n dp[0]=0\n \n for coin in coins:\n for i in range(coin, amount+1):\n if i-coin>=0:\n ... | 57 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`.
You may a... | null |
DP Solution | coin-change | 0 | 1 | \n# Code\n```\nclass Solution:\n def coinChange(self, coins: List[int], amount: int) -> int:\n def change(n):\n if F[n] != -1:\n return F[n]\n value = float(\'inf\')\n for coin in coins:\n if coin <= n:\n value = min(change(n-co... | 1 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`.
You may a... | null |
324: Time 99.2% and Space 99.16%, Solution with step by step explanation | wiggle-sort-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. We first sort the array nums in ascending order.\n2. We then find the median of the array (i.e., the middle element if the array has odd length, or one of the two middle elements if the array has even length). We can use ... | 15 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
Very easy solution in 5 lines using python | wiggle-sort-ii | 0 | 1 | **1. We have to copy array into temporary space\n2. Sort temporary array in ascending order\n3. Then run loop of size 2\n4. During loop we have to swap elements of orignal array with temporary array\n5. Swap temporary array values form end (j index point to last till i ! = end of array) and keep assigning value in orig... | 5 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
O(n) time, O(1) space. Python code + explanation. | wiggle-sort-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs the quesiton description, we have:\n```\nnums[0] < nums[1] > nums[2]\nnums[2] < nums[3] > nums[4]\nnums[4] < nums[5] > nums[6]\n...\n...\n...\n```\n\nWe can simply split the array into an odd-idx array and an even-idx array where each ... | 2 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
Python Solution || Easy Solution using priority queue | wiggle-sort-ii | 0 | 1 | \n# Code\n```\nclass Solution:\n def wiggleSort(self, nums: List[int]) -> None:\n """\n Do not return anything, modify nums in-place instead.\n """\n\n nums.sort()\n n = len(nums)\n\n # divide the nums into 2 sorted array\n # then each time take the maximum from both ... | 1 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
Python3 O(N) time O(1) space - Wiggle Sort II | wiggle-sort-ii | 0 | 1 | Note that the performance actually sucks. Because it\'s CPython. If a builtin is written in C and it\'s O(NlgN), its performance may be better than an O(N) implementation in pure Python.\n\n```\n# Inspired by https://leetcode.com/problems/wiggle-sort-ii/discuss/77677/O(n)%2BO(1)-after-median-Virtual-Indexing\nclass Sol... | 11 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
just code : ) | wiggle-sort-ii | 0 | 1 | \n# Code\n```\nclass Solution:\n def wiggleSort(self, nums: List[int]) -> None:\n # sorted copy of original array\n array = sorted(nums)\n # take the biggest elements and insert them into odd positions\n for i in range(1, len(nums), 2):\n nums[i] = array.pop()\n # insert... | 0 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
sorting based approach | wiggle-sort-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$$O(nlog(n))$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nclass Solution:\n def wiggleSort(self, nums: List[int]) -> None:... | 0 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
Python with O(n) time and O(1) space | wiggle-sort-ii | 0 | 1 | # Intuition\nUse a fixed size array and assign values to even and odd indexes separately\n\n# Approach\nAssign a fixed size array of size 5000 (as 0 <= nums[i] <= 5000) and put count of numbers present in nums in the array. Then execute two for loops to put values in odd and even indexes.\n\n# Complexity\n- Time comple... | 0 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
[Python3] 2-liner | wiggle-sort-ii | 0 | 1 | # Code\n```\nclass Solution:\n def wiggleSort(self, nums: List[int]) -> None:\n nums.sort(reverse=True)\n nums[::2], nums[1::2] = nums[len(nums)//2:], nums[:len(nums)//2]\n\n \n``` | 0 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
( | wiggle-sort-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)$$ --... | 0 | Given an integer array `nums`, reorder it such that `nums[0] < nums[1] > nums[2] < nums[3]...`.
You may assume the input array always has a valid answer.
**Example 1:**
**Input:** nums = \[1,5,1,1,6,4\]
**Output:** \[1,6,1,5,1,4\]
**Explanation:** \[1,4,1,5,1,6\] is also accepted.
**Example 2:**
**Input:** nums = ... | null |
Binary Search PYTHON ! | power-of-three | 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`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
python3 code | power-of-three | 0 | 1 | # Intuition\nIt first checks if the input integer n is greater than 0. If so, it calculates the logarithm of n to the base 3 using the log() function from the math module. It then rounds the result to one decimal place using the round() function and stores it in the variable x.\n\nFinally, it checks if 3 raised to the ... | 1 | Given an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
speed was 86% in Python3 | power-of-three | 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 an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
Python Recursive and Iterative solution | power-of-three | 0 | 1 | # Iterative 1\n```\ndef isPowerOfThree(self, n: int) -> bool:\n if( n <=0): return False;\n x = 1;\n while(x<n): x *=3\n return x==n\n```\n# Iterative 2\n```\ndef isPowerOfThree(self, n: int) -> bool:\n if(n>1): \n while(n%3==0):n//=3\n return n==1\n```\n\n# Recursi... | 2 | Given an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
Using Log Function | power-of-three | 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\n# Complexity\n- Time complexity: O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g.... | 1 | Given an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
List comprehension Python solution | power-of-three | 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 an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
326: Solution with step by step explanation | power-of-three | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- The condition n > 0 is used to make sure that we only check for positive integers.\n- We check if 3 raised to the power of 19 is divisible by n. Why 19? Because 3**19 is the largest power of 3 that can fit into the integer... | 21 | Given an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
Simplest Python Solution | power-of-three | 0 | 1 | \n\n# Code\n```\nclass Solution(object):\n def isPowerOfThree(self, n):\n """\n :type n: int\n :rtype: bool\n """\n if n<=0:\n return False\n while n!=1:\n if n%3!=0:\n return False\n n//=3\n return True\n``` | 2 | Given an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
Checking if an Integer is a Power of Three Using Logarithm Function. | power-of-three | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo check if an integer n is a power of three, we can use the logarithm function to solve it. Specifically, we can take the logarithm of n with base 3 and check if the result is an integer. If the result is an integer, then n is a power of... | 8 | Given an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
PYTHON WHILE LOOP SOLUTION | power-of-three | 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 an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
Beats : 99.34% [33/145 Top Interview Question] | power-of-three | 0 | 1 | # Intuition\n*One approach to solve this is to repeatedly divide the given number by three until we get a number less than or equal to one. If the number is exactly equal to one, then it is a power of three, otherwise it is not.*\n\n*Another approach is to find the maximum power of three which is less than or equal to ... | 6 | Given an integer `n`, return _`true` if it is a power of three. Otherwise, return `false`_.
An integer `n` is a power of three, if there exists an integer `x` such that `n == 3x`.
**Example 1:**
**Input:** n = 27
**Output:** true
**Explanation:** 27 = 33
**Example 2:**
**Input:** n = 0
**Output:** false
**Explanat... | null |
Python different concise solutions | count-of-range-sum | 0 | 1 | The problem is similiar to [315. Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/), yoc can click **[here](https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/408322/Python-Different-Concise-Solutions)** to check how to slolve it.\nThe differe... | 64 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Python bisect module | count-of-range-sum | 0 | 1 | # Python\n```\ndef countRangeSum(nums, lower, upper):\n \n prefix,cur,res = [0],0,0\n\n for n in nums:\n cur += n\n A = bisect.bisect_right(prefix, cur-lower)\n B = bisect.bisect_left(prefix, cur-upper)\n res += A - B\n bisect.insort(prefix, cur)\n \n return res... | 1 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
[Python] Simple solution with PrefixSum and Bisect || Documented | count-of-range-sum | 0 | 1 | ```\nimport bisect\n\nclass Solution:\n """\n Calculate Prefix Sum: \n Given an array arr[] of size n, \n its prefix sum is another array prefixSum[] of the same size, \n such that the value of prefixSum[i] is arr[0] + arr[1] + arr[2] \u2026 arr[i].\n \n Input : arr[] = {1, 2, 1}, lower = ... | 5 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
O(n logn) in Python faster than more than 80%, easy to understand | count-of-range-sum | 0 | 1 | ``` python\nclass Solution:\n def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:\n # First get the cumulative list\n # takes O(n)\n accumulated = nums.copy()\n for i in range(0, len(nums)):\n if i != 0:\n accumulated[i] = nums[i] + accumulat... | 1 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
327: Solution with step by step explanation | count-of-range-sum | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. The countRangeSum function takes in three parameters: nums, a list of integers; lower, an integer representing the lower bound of the sum range; and upper, an integer representing the upper bound of the sum range.\n\n2. T... | 3 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
[Python3] 4 solutions | count-of-range-sum | 0 | 1 | divide & conquer \n```\nclass Solution:\n def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:\n prefix = [0]\n for x in nums: prefix.append(prefix[-1] + x)\n \n def fn(lo, hi): \n """Return count of range sum between prefix[lo:hi]."""\n if lo+1 >... | 11 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Just 3 Lines Python Code, SortedList, O(NlogN) | count-of-range-sum | 0 | 1 | # Intuition\nuse sortedList to keep track of pre-sums, then bisect for lower and upper bounds for each of pre-sums, any number between the bounds is a valid presum for the current presum\n\n# Code\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n def countRangeSum(self, nums: List[int], lower: int, up... | 1 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Extremely clean solution simply using sortedlist | count-of-range-sum | 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 an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Merge Sort | count-of-range-sum | 0 | 1 | \n# Code\n```\nclass Solution:\n def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:\n def mergeSort(lo, hi):\n if lo == hi:\n return 0\n\n mid = (lo + hi) // 2\n count = mergeSort(lo, mid) + mergeSort(mid + 1, hi)\n\n i = j = mid... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
[Python] Just use sorted list... | O(NlogN) | count-of-range-sum | 0 | 1 | # Code\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n def countRangeSum(self, nums: List[int], l: int, u: int) -> int:\n sc = SortedList()\n sc.add(0);\n sm = 0\n res = 0\n for x in nums:\n sm +=x;\n lb = sm-u;\n up = sm-l;\n ... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
(Python) Count Range Sums | count-of-range-sum | 0 | 1 | # Intuition\nTo find the number of range sums that lie in [lower, upper] inclusive, we need to calculate the range sum S(i, j) for all possible pairs (i,j) where i <= j. Then, we count the number of sums that are between [lower, upper].\n# Approach\n1. Create an array prefix_sum of size n+1 to store the prefix sums of ... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Solution in python | count-of-range-sum | 0 | 1 | # Approach\nUsing Merge-sort\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 complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def _... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Solution | count-of-range-sum | 1 | 1 | ```C++ []\nnamespace std {\n\ntemplate <class Fun> \nclass y_combinator_result {\n Fun fun_;\n public:\n template <class T> \n explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}\n \n template<class ...Args> \n decltype(auto) operator()(Args &&...args) { \n return fun_(std::ref(*this), std::fo... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
The most important thing to use merge sort for these similar hard problems | count-of-range-sum | 0 | 1 | # Approach\nThere are so many merge sort solution posts, and I have read all those with most votes. They all put a fake initial ZERO element in the prefix-sum array but failed to explain why.\n\nHowever, this is the most important thing for merge sort to work!\n\nThe conditions for the original problem, nums, are:\n- i... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Python3 || Prefix Sum + Merge Sort | count-of-range-sum | 0 | 1 | # Code\n```\nclass Solution:\n def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:\n n = len(nums)\n prefixsum = [0]*(n+1)\n for i in range(n):\n prefixsum[i+1] = prefixsum[i] + nums[i]\n \n temp = [0]*(n+1)\n count = 0\n\n def mergesor... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Python Segment Tree Solution. | count-of-range-sum | 0 | 1 | ```\nclass SegmentTreeNode:\n def __init__(self, min, max, left=None, right=None):\n self.min, self.max = min, max\n self.count = 0 # it marks how many sub ranges under this node\n self.left = left\n self.right = right\n\n# Conceptual Reference: \n# - https://leetcode.com/problems/count... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
PYTHON || BIT || EASY SOLUTION | count-of-range-sum | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nbinary indexed Tree can be use instead of segment Tree\n\n# Approach\nuse Binary Indexed Tree (BIT) for updation and setion sum\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n- o(nlog(n))\n<!... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Python with binary search better than 99% (memory) | count-of-range-sum | 0 | 1 | \n# Code\n```\nclass Solution:\n def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:\n from bisect import bisect, bisect_left\n # Counter for sum ranges\n counter = 0\n nums_sum = [0]\n # First i-elements sum elements for i in nums range\n for num in num... | 0 | Given an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
Would an interviewer accept this? | count-of-range-sum | 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 an integer array `nums` and two integers `lower` and `upper`, return _the number of range sums that lie in_ `[lower, upper]` _inclusive_.
Range sum `S(i, j)` is defined as the sum of the elements in `nums` between indices `i` and `j` inclusive, where `i <= j`.
**Example 1:**
**Input:** nums = \[-2,5,-1\], lowe... | null |
🔥Python3🔥 Two pointers | odd-even-linked-list | 0 | 1 | The problem needs to be solved with O(1) space, which leaves us no other choice but to try to use pointers to reorganize the linked list.\n(1) We use a ```odd``` pointer to link all the odd-positioned nodes, and a ```even``` pointer to link all the even-positioned nodes.\n(2) We connect the two linked lists at the end,... | 35 | Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return _the reordered list_.
The **first** node is considered **odd**, and the **second** node is **even**, and so on.
Note that the relative order inside both the even and odd groups s... | null |
Use two pointers - odd pointer and even pointer | odd-even-linked-list | 0 | 1 | # Intuition\nHave one pointer at head - odd pointer\nHave another pointer at head.next - even pointer\nFor both of the pointer we move 2 steps forward to access their type of index.\nWe also have to have access to the even head - head.next pointer.\nOnce we have connected all the odd and even head to each other.\nWe ar... | 2 | Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return _the reordered list_.
The **first** node is considered **odd**, and the **second** node is **even**, and so on.
Note that the relative order inside both the even and odd groups s... | null |
Python Easy Solution || Linked List | odd-even-linked-list | 0 | 1 | # Complexity\n- Time complexity:\n- O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val ... | 1 | Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return _the reordered list_.
The **first** node is considered **odd**, and the **second** node is **even**, and so on.
Note that the relative order inside both the even and odd groups s... | null |
✅ [Python/C++/Java] next = next.next (explained) | odd-even-linked-list | 0 | 1 | **\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\nThis solution employs even and odd pointers to iterate through nodes in the list. Time complexity is linear: **O(N)**. Space complexity is constant: **O(1)**.\n****\n\n**Python #1.**\n```\nclass Solution:\n def oddEvenList(self, head):\n \n if... | 22 | Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return _the reordered list_.
The **first** node is considered **odd**, and the **second** node is **even**, and so on.
Note that the relative order inside both the even and odd groups s... | null |
IK U wont care for explanation Traitor :) | odd-even-linked-list | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nsee the code lil bit**\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nhahahahah\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your spac... | 2 | Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return _the reordered list_.
The **first** node is considered **odd**, and the **second** node is **even**, and so on.
Note that the relative order inside both the even and odd groups s... | null |
328: Space 98.56%, Solution with step by step explanation | odd-even-linked-list | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe need to group all the nodes with odd indices together followed by the nodes with even indices. The relative order inside both the even and odd groups should remain as it was in the input.\n\nTo solve the problem, we can u... | 16 | Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return _the reordered list_.
The **first** node is considered **odd**, and the **second** node is **even**, and so on.
Note that the relative order inside both the even and odd groups s... | null |
Soln | odd-even-linked-list | 0 | 1 | # Code\n```\nclass Solution:\n def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n if not head:\n return None\n odd=head\n dum=evn=head.next\n while evn and evn.next:\n odd.next=odd.next.next\n evn.next=evn.next.next\n odd=o... | 1 | Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return _the reordered list_.
The **first** node is considered **odd**, and the **second** node is **even**, and so on.
Note that the relative order inside both the even and odd groups s... | null |
[ Python ] | Simple & Clean Code | odd-even-linked-list | 0 | 1 | \n\n# Code\n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n if not head or not head.next:\n ret... | 3 | Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return _the reordered list_.
The **first** node is considered **odd**, and the **second** node is **even**, and so on.
Note that the relative order inside both the even and odd groups s... | null |
Python3 Solution | Runtime Beats 92% | DP and Memoization | longest-increasing-path-in-a-matrix | 0 | 1 | # Approach\nTo solve this problem, we can use a **dynamic programming approach** and **caching**. \nThe *"length_paths"* matrix is used for caching. This matrix stores the longest path length from each cell of the matrix *"matrix"*. \nThe dynamic programming approach is implemented by the recursive *"check_cell"* funct... | 5 | Given an `m x n` integers `matrix`, return _the length of the longest increasing path in_ `matrix`.
From each cell, you can either move in four directions: left, right, up, or down. You **may not** move **diagonally** or move **outside the boundary** (i.e., wrap-around is not allowed).
**Example 1:**
**Input:** matr... | null |
Solution | longest-increasing-path-in-a-matrix | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int m, n;\n short path[200][200];\n \n int longestIncreasingPath(vector<vector<int>>& matrix) {\n m = matrix.size();\n n = matrix[0].size();\n\n memset(path, 0, sizeof(path));\n \n int max_path = 1;\n for(int i = 0; i < m; i++)\n ... | 33 | Given an `m x n` integers `matrix`, return _the length of the longest increasing path in_ `matrix`.
From each cell, you can either move in four directions: left, right, up, or down. You **may not** move **diagonally** or move **outside the boundary** (i.e., wrap-around is not allowed).
**Example 1:**
**Input:** matr... | null |
dfs + dp | longest-increasing-path-in-a-matrix | 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*m)$$ \n87% beats\n\n- Space complexity:\n<!-- Add your space complexit... | 1 | Given an `m x n` integers `matrix`, return _the length of the longest increasing path in_ `matrix`.
From each cell, you can either move in four directions: left, right, up, or down. You **may not** move **diagonally** or move **outside the boundary** (i.e., wrap-around is not allowed).
**Example 1:**
**Input:** matr... | null |
DFS with Memoization || Python | longest-increasing-path-in-a-matrix | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt is necessary to try all possible paths and optimize the algorithm\'s complexity by using a map to store already calculated cases.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. A dictionary `memo` is declared,... | 2 | Given an `m x n` integers `matrix`, return _the length of the longest increasing path in_ `matrix`.
From each cell, you can either move in four directions: left, right, up, or down. You **may not** move **diagonally** or move **outside the boundary** (i.e., wrap-around is not allowed).
**Example 1:**
**Input:** matr... | null |
DFS & TOPOLOGICAL SORTING CLEAR EXPLANATION | longest-increasing-path-in-a-matrix | 0 | 1 | ### DFS\n\n\n\n```\nFrom red circle the longest sequence is 6 -> 7 -> 9. So\n matrix[0][2] (6) has 3 unit LONGEST sequence\n matrix[1][2] (7) has 2 unit LONGEST sequence\n matrix[2][2] (9) has ... | 1 | Given an `m x n` integers `matrix`, return _the length of the longest increasing path in_ `matrix`.
From each cell, you can either move in four directions: left, right, up, or down. You **may not** move **diagonally** or move **outside the boundary** (i.e., wrap-around is not allowed).
**Example 1:**
**Input:** matr... | null |
[Python] Solution using Memoization | longest-increasing-path-in-a-matrix | 0 | 1 | ```\nclass Solution:\n def longestIncreasingPath(self, matrix: List[List[int]]) -> int:\n row = len(matrix); col = len(matrix[0])\n res = 0\n memo = {}\n directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]\n \n def helper(i, j):\n if (i,j) in memo: return memo[(i,j)]\... | 3 | Given an `m x n` integers `matrix`, return _the length of the longest increasing path in_ `matrix`.
From each cell, you can either move in four directions: left, right, up, or down. You **may not** move **diagonally** or move **outside the boundary** (i.e., wrap-around is not allowed).
**Example 1:**
**Input:** matr... | null |
329: Solution with step by step explanation | longest-increasing-path-in-a-matrix | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe problem can be solved by performing a depth-first search (DFS) on each cell of the matrix and keeping track of the longest increasing path found so far. We start the search from each cell of the matrix and for each cell,... | 7 | Given an `m x n` integers `matrix`, return _the length of the longest increasing path in_ `matrix`.
From each cell, you can either move in four directions: left, right, up, or down. You **may not** move **diagonally** or move **outside the boundary** (i.e., wrap-around is not allowed).
**Example 1:**
**Input:** matr... | null |
Python DFS with Memoization Beats ~90% | longest-increasing-path-in-a-matrix | 0 | 1 | The solution is quite straightforward. The code just performs a dfs traversal from each cell in the grid and finds the length of valid path.\n\nBelow is the code with the comments\n```\nclass Solution:\n def longestIncreasingPath(self, grid: List[List[int]]) -> int:\n m,n=len(grid),len(grid[0])\n direc... | 5 | Given an `m x n` integers `matrix`, return _the length of the longest increasing path in_ `matrix`.
From each cell, you can either move in four directions: left, right, up, or down. You **may not** move **diagonally** or move **outside the boundary** (i.e., wrap-around is not allowed).
**Example 1:**
**Input:** matr... | null |
330: Solution with step by step explanation | patching-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\nTo solve this problem, we can use a greedy approach. We start with a variable miss that represents the smallest number that cannot be formed by summing up any combination of the numbers in the array. Initially, miss is set... | 3 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
Python || Math | patching-array | 0 | 1 | # Intuition\nIf sum of all the numbers considered till now is x, we can form all the numbers from 1 to x. This condition is True every time.\n\n# Approach\nKeep a variable limit which tracks till what max value we can form which is nothing but sum of all values considered till now.\nIf we encounter any number i.e. num ... | 2 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
Python Greedy : )) | patching-array | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def minPatches(self, nums, n):\n patches = reachable = 0\n for num in nums:\n while reachable < min(num - 1, n):\n reachable += reachable + 1\n patches += 1\n reachable += num\n \n while reachable < n:... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
Python Greedy: variant of 1798 and 2952 | patching-array | 0 | 1 | # Code\n```\nclass Solution:\n def minPatches(self, nums, n):\n patches = reachable = 0\n for num in nums:\n while reachable < min(num - 1, n):\n reachable += reachable + 1\n patches += 1\n reachable += num\n \n while reachable < n:\n ... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
Python 3 | Solution | patching-array | 0 | 1 | # Code\n```\nclass Solution:\n def minPatches(self, nums: List[int], n: int) -> int:\n\n res = 0\n temp = 0\n indx = 0\n\n while temp < n:\n\n if indx < len(nums) and nums[indx] <= temp + 1:\n \n temp += nums[indx]\n indx += 1\n\n ... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
✅[Python] ⏱99% Fast || Easy || Greedy ||Minimum Number of Patches | patching-array | 0 | 1 | # Intuition\nThe problem involves finding the minimum number of "patches" that need to be added to an array of positive integers (represented by the `nums` list) in order to make it contain all positive integers from 1 to `n`. The patches must be added in such a way that the array remains sorted in ascending order.\n\n... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
python easy solution | patching-array | 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 sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
how do i show this to the interviewer LMFAO | patching-array | 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 sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
i dont know how this works | patching-array | 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 sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
Explanation for beginners like me | patching-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ninitially, we assume that 1 is missing from the array, so we set miss to 1. let ```miss``` be the smallest sum in the```[0,n)``` that we might be missing. meaning we already can build all sums in ```[0,miss)```.\n\nif we have a number ```... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
Greedy Approach | patching-array | 0 | 1 | # Code\n```\nclass Solution:\n def minPatches(self, nums: List[int], n: int) -> int:\n patches = 0\n max_reachable = 0\n i = 0\n \n while max_reachable < n:\n if i < len(nums) and nums[i] <= max_reachable + 1:\n max_reachable += nums[i]\n i ... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
simple mathematics solution || O(N) || python | patching-array | 0 | 1 | # Intuition and Approach\n* use previous formed no. to form new numbers.\n\n# Complexity\n- Time complexity: O(N)\n\n- Space complexity:O(1)\n\n# Code\n```\nclass Solution:\n def minPatches(self, nums: List[int], n: int) -> int:\n ans=0\n pre=0\n i=0\n nextn=pre+1\n while(nextn<=n)... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
(Python) Longest Increasing Path in a Matrix | patching-array | 0 | 1 | # Intuition\nOne way to solve this problem is to use a greedy approach. We can keep track of the smallest number we cannot make using the numbers we have encountered so far. We start with missing = 1, since we can always make 1 with an empty array. Then, we iterate through the given array and check if the current numbe... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
Solution | patching-array | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int minPatches(vector<int>& nums, int n) {\n int m=nums.size();\n\n sort(nums.begin(),nums.end());\n\n long long cur=0;\n int cnt=0;\n for(int i=0;i<m;i++){\n\n while(cur<n && nums[i]>cur+1){\n cur+=cur+1;\n ... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
Python solution simple and fast | patching-array | 0 | 1 | \n# Code\n```\nclass Solution:\n def minPatches(self, nums: List[int], n: int) -> int:\n sum, patch, i = 0, 0, 0\n\n while sum < n:\n if i < len(nums) and nums[i] <= sum+1:\n sum += nums[i]\n i += 1\n else:\n patch += 1\n ... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
Go/Pyton O(m+log(n)) time | O(1) space | patching-array | 0 | 1 | # Complexity\n- Time complexity: $$O(m+log(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```golang []\nfunc minPatches(nums []int, n int) int {\n covered := 0\n i := 0\n answer := 0\n for i<len(... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
greedy to find the next number in array or not | patching-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nthe array is sorted, and need patch to make [1, n]\n# Approach\n<!-- Describe your approach to solving the problem. -->\nif nothing: [0, 0]\nif add 1: [0, 1]\nif we need to get [1, n]\ndo we have 2 in nums, then it will become [0, 1] + [2... | 0 | Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
python solution | patching-array | 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 sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
Return _the minimum number of patches required_.
**Example 1:**
**Input:** nums = \[1,3\], n = 6
**Output:** 1
Explanati... | null |
331: Time 98%, Solution with step by step explanation | verify-preorder-serialization-of-a-binary-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nan explicit stack, and instead just uses a single variable degree to keep track of the outDegree (children) - inDegree (parent). It also removes the need for the stack and nodes lists used in the previous solution, which res... | 8 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
Python3 | O(n) Time and O(n) space | verify-preorder-serialization-of-a-binary-tree | 0 | 1 | The idea is to pop the element from the stack if you get the count of the element as 0.\n\nStack is storing the node and number of remaining branch that need to discover.\nExample [2,2] will refer a node having value 2 and we did not traverse any of the branch (left or right) but at the moment we traverse the left bran... | 3 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
Just the code : )) | verify-preorder-serialization-of-a-binary-tree | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def isValidSerialization(self, preorder: str) -> bool:\n preorder = preorder.split(\',\')\n stack = []\n node, left, right, cur = preorder[0], None, None, \'L\'\n n = len(preorder)\n if n == 1:\n return node == \'#\'\n elif node ... | 0 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
Use stack, beats 95% | verify-preorder-serialization-of-a-binary-tree | 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\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n... | 0 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
Python easy solution | verify-preorder-serialization-of-a-binary-tree | 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 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
✅ Fast + Easy + Accurate Solution ✅ | verify-preorder-serialization-of-a-binary-tree | 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 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
Verify preorder structure using recursive simulation | verify-preorder-serialization-of-a-binary-tree | 0 | 1 | - Basic idea is to simulate the preorder construction of a complete binary tree using recursion but just leave out creating the actual nodes. \n- Edge cases:\n - There is only one item and it is an empty node \n - There are more than one item but the first item is an empty node\n- Two failure conditions:\n - R... | 0 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
using stack | verify-preorder-serialization-of-a-binary-tree | 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 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
stack with explanation | verify-preorder-serialization-of-a-binary-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea is to delete the tree node until there is nothing to delete\n\ne.g. \n` 9\nN N\n`\nit will put into stack\nstack: [9, N, N]\nthis is a root node, but it is a leaf node as well\nif it is leaf node: delete [9, N, N] and add N, it... | 0 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
Simple Stack Python | verify-preorder-serialization-of-a-binary-tree | 0 | 1 | # Intuition\nCancel out node after finding both children\n\n# Code\n```\nclass Solution:\n def isValidSerialization(self, preorder: str) -> bool:\n # Time O(N), space O(N)\n val = preorder.split(\',\')\n stack = []\n for v in val:\n if v != \'#\':\n stack.append(... | 0 | One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,# "`, where `'#'` repres... | null |
Python3 Solution | reconstruct-itinerary | 0 | 1 | \n```\nclass Solution:\n def findItinerary(self, tickets: List[List[str]]) -> List[str]:\n adj={}\n for ticket in tickets:\n adj[ticket[0]]=[]\n adj[ticket[1]]=[]\n\n for ticket in tickets:\n adj[ticket[0]].append(ticket[1])\n\n for l in adj.values():\n ... | 2 | You are given a list of airline `tickets` where `tickets[i] = [fromi, toi]` represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from `"JFK "`, thus, the itinerary must begin with `"JFK "`. If there are multiple... | null |
Python3 | reconstruct-itinerary | 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 | You are given a list of airline `tickets` where `tickets[i] = [fromi, toi]` represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from `"JFK "`, thus, the itinerary must begin with `"JFK "`. If there are multiple... | null |
simple approach | reconstruct-itinerary | 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 | You are given a list of airline `tickets` where `tickets[i] = [fromi, toi]` represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from `"JFK "`, thus, the itinerary must begin with `"JFK "`. If there are multiple... | null |
✅ 95.76% DFS Recursive & Iterative | reconstruct-itinerary | 1 | 1 | # Comprehensive Guide to Solving "Reconstruct Itinerary": Navigating Airports Like a Pro\n\n## Introduction & Problem Statement\n\nHello, coding enthusiasts! Today, we\'ll tackle a problem that combines graph theory and real-world scenarios: "Reconstruct Itinerary." The problem asks you to reconstruct a trip\'s itinera... | 124 | You are given a list of airline `tickets` where `tickets[i] = [fromi, toi]` represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from `"JFK "`, thus, the itinerary must begin with `"JFK "`. If there are multiple... | null |
【Video】Visualized Solution - Python, JavaScript, Java, C++ | reconstruct-itinerary | 1 | 1 | # Intuition\nThis code uses airline ticket information to construct a valid itinerary starting from the departure airport "JFK" and organizing destinations into a dictionary-style graph. It manages the routes from departure to arrival using a stack, adding them to a new itinerary when there are no more destinations. Th... | 30 | You are given a list of airline `tickets` where `tickets[i] = [fromi, toi]` represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from `"JFK "`, thus, the itinerary must begin with `"JFK "`. If there are multiple... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.