title stringlengths 1 100 | titleSlug stringlengths 3 77 | Java int64 0 1 | Python3 int64 1 1 | content stringlengths 28 44.4k | voteCount int64 0 3.67k | question_content stringlengths 65 5k | question_hints stringclasses 970
values |
|---|---|---|---|---|---|---|---|
[Python, Java] Elegant & Short | One Pass | sign-of-the-product-of-an-array | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Code\n\n```python []\nclass Solution:\n def arraySign(self, nums: List[int]) -> int:\n sign = 1\n\n for num in nums:\n if num < 0:\n sign *= -1\n if num == 0:\n return 0\n\n ... | 1 | You are given an integer array `nums` of size `n`. You are asked to solve `n` queries for each integer `i` in the range `0 <= i < n`.
To solve the `ith` query:
1. Find the **minimum value** in each possible subarray of size `i + 1` of the array `nums`.
2. Find the **maximum** of those minimum values. This maximum i... | If there is a 0 in the array the answer is 0 To avoid overflow make all the negative numbers -1 and all positive numbers 1 and calculate the prod |
Simple Python3 & Java Solution|| Upto 100% Faster || O(n) | sign-of-the-product-of-an-array | 0 | 1 | # Intuition\nFor either python or Java we check if Zero or number of negative numbers and return respectively.\nIn Java we donot need to use a Double or Long to get the prod equivalent just check if the number is positive or negative.\n\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n\n```java []\nc... | 1 | There is a function `signFunc(x)` that returns:
* `1` if `x` is positive.
* `-1` if `x` is negative.
* `0` if `x` is equal to `0`.
You are given an integer array `nums`. Let `product` be the product of all values in the array `nums`.
Return `signFunc(product)`.
**Example 1:**
**Input:** nums = \[-1,-2,-3,-4,... | As with any good dp problem that uses palindromes, try building the palindrome from the edges The prime point is to check that no two adjacent characters are equal, so save the past character while building the palindrome. |
Simple Python3 & Java Solution|| Upto 100% Faster || O(n) | sign-of-the-product-of-an-array | 0 | 1 | # Intuition\nFor either python or Java we check if Zero or number of negative numbers and return respectively.\nIn Java we donot need to use a Double or Long to get the prod equivalent just check if the number is positive or negative.\n\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n\n```java []\nc... | 1 | You are given an integer array `nums` of size `n`. You are asked to solve `n` queries for each integer `i` in the range `0 <= i < n`.
To solve the `ith` query:
1. Find the **minimum value** in each possible subarray of size `i + 1` of the array `nums`.
2. Find the **maximum** of those minimum values. This maximum i... | If there is a 0 in the array the answer is 0 To avoid overflow make all the negative numbers -1 and all positive numbers 1 and calculate the prod |
Easy Python Solution | sign-of-the-product-of-an-array | 0 | 1 | \n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```Python []\nclass Solution:\n def arraySign(self, nums: List[int]) -> int:\n re = 1\n for i in nums:\n re = re*i\n if re<0:\n return -1\n elif re>1:\n return 1\n els... | 1 | There is a function `signFunc(x)` that returns:
* `1` if `x` is positive.
* `-1` if `x` is negative.
* `0` if `x` is equal to `0`.
You are given an integer array `nums`. Let `product` be the product of all values in the array `nums`.
Return `signFunc(product)`.
**Example 1:**
**Input:** nums = \[-1,-2,-3,-4,... | As with any good dp problem that uses palindromes, try building the palindrome from the edges The prime point is to check that no two adjacent characters are equal, so save the past character while building the palindrome. |
Easy Python Solution | sign-of-the-product-of-an-array | 0 | 1 | \n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```Python []\nclass Solution:\n def arraySign(self, nums: List[int]) -> int:\n re = 1\n for i in nums:\n re = re*i\n if re<0:\n return -1\n elif re>1:\n return 1\n els... | 1 | You are given an integer array `nums` of size `n`. You are asked to solve `n` queries for each integer `i` in the range `0 <= i < n`.
To solve the `ith` query:
1. Find the **minimum value** in each possible subarray of size `i + 1` of the array `nums`.
2. Find the **maximum** of those minimum values. This maximum i... | If there is a 0 in the array the answer is 0 To avoid overflow make all the negative numbers -1 and all positive numbers 1 and calculate the prod |
Beginner-friendly || Simple solution with using Queue DS on Python3 | find-the-winner-of-the-circular-game | 0 | 1 | # Intuition\nLet\'s briefly explain the task and provide some `pseudocode`:\n```\n# There\'re n-friends, playing a game\nfriends = [1,2,3,4,5]\n\n# at each step there\'s an option to SKIP some players in order\nn = 2\ni = 0\n\n# to EXCLUDE the player, that the pointer has stopped at\n# [1, (2), 3, 4, 5]\n# i=1 i=2==k\n... | 4 | There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend... | Create a function that checks if a character is a vowel, either uppercase or lowercase. |
O(n)->O(klg(n)) with table explanation | find-the-winner-of-the-circular-game | 0 | 1 | This is the josephus problem, [wikipedia](https://en.wikipedia.org/wiki/Josephus_problem)\nThe classic algorithm to this problem is very weird, let me talk with the example1 in "leetcode problem description". \nAt first [aaa,bbb,cac,ddd,eee] is playing this game.\n\n| #alive | aaa | bbb | cac | ddd | eee |\n|----------... | 71 | There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend... | Create a function that checks if a character is a vowel, either uppercase or lowercase. |
Python code with Intuition | O(n) | find-the-winner-of-the-circular-game | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe solution code is of few lines. You just need to understand the pattern here.\n\n## 1) Iterating over queue (If you know how to can skip this)\nBefore that understand how to iterate over the circular queue.\nSuppose no of students in c... | 15 | There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend... | Create a function that checks if a character is a vowel, either uppercase or lowercase. |
Python3 Solutions | find-the-winner-of-the-circular-game | 0 | 1 | # Approach 1: Straight Forward\n```\nclass Solution:\n def findTheWinner(self, n: int, k: int) -> int:\n players = [playerNum+1 for playerNum in range(n)] #setup player numbers\n idx = 0 #starting player index\n while len(players)>1:\n idx = (idx+k-1) % len(players) #get last counter ... | 2 | There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend... | Create a function that checks if a character is a vowel, either uppercase or lowercase. |
A solution using Python | minimum-sideway-jumps | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\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 minSideJumps(self, obstacles: List[int]) -> int:\n dp = [[float("inf")] * len(obstacles... | 0 | There is a **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
A solution using Python | minimum-sideway-jumps | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\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 minSideJumps(self, obstacles: List[int]) -> int:\n dp = [[float("inf")] * len(obstacles... | 0 | Given an integer `n`, return `true` _if_ `n` _has **exactly three positive divisors**. Otherwise, return_ `false`.
An integer `m` is a **divisor** of `n` if there exists an integer `k` such that `n = k * m`.
**Example 1:**
**Input:** n = 2
**Output:** false
**Explantion:** 2 has only two divisors: 1 and 2.
**Exampl... | At a given point, there are only 3 possible states for where the frog can be. Check all the ways to move from one point to the next and update the minimum side jumps for each lane. |
[Python3] Fenwick tree | finding-mk-average | 0 | 1 | \n```\nclass Fenwick: \n\n def __init__(self, n: int):\n self.nums = [0]*(n+1)\n\n def sum(self, k: int) -> int: \n k += 1\n ans = 0\n while k:\n ans += self.nums[k]\n k &= k-1 # unset last set bit \n return ans\n\n def add(self, k: int, x: int) -> None:... | 46 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
[Python3] Fenwick tree | finding-mk-average | 0 | 1 | \n```\nclass Fenwick: \n\n def __init__(self, n: int):\n self.nums = [0]*(n+1)\n\n def sum(self, k: int) -> int: \n k += 1\n ans = 0\n while k:\n ans += self.nums[k]\n k &= k-1 # unset last set bit \n return ans\n\n def add(self, k: int, x: int) -> None:... | 46 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Python3 solution w/ SortedList, O(logM) add, O(1) calculate | finding-mk-average | 0 | 1 | python3 doesn\'t have a built-in [order statistic tree](https://en.wikipedia.org/wiki/Order_statistic_tree) or even a basic BST and that may be why `sortedcontainers` is one of the few third-party libraries allowed.\n\nAnyway, with a [SortedList](http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html) this sol... | 39 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Python3 solution w/ SortedList, O(logM) add, O(1) calculate | finding-mk-average | 0 | 1 | python3 doesn\'t have a built-in [order statistic tree](https://en.wikipedia.org/wiki/Order_statistic_tree) or even a basic BST and that may be why `sortedcontainers` is one of the few third-party libraries allowed.\n\nAnyway, with a [SortedList](http://www.grantjenks.com/docs/sortedcontainers/sortedlist.html) this sol... | 39 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Python3 Keeping Three Containers For Small/Middle/Large Values (Runtime 98%, Memory 94%) | finding-mk-average | 0 | 1 | # Intuition\nSince this data structure deals only with the last m elements from a data stream, it\'s natural to use a deque that only stores the latest m elements (*self.container*). The first solution I came up was precisely that, as can be seen from the commented data block. \n\nIn that straight-forward approach, whe... | 2 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Python3 Keeping Three Containers For Small/Middle/Large Values (Runtime 98%, Memory 94%) | finding-mk-average | 0 | 1 | # Intuition\nSince this data structure deals only with the last m elements from a data stream, it\'s natural to use a deque that only stores the latest m elements (*self.container*). The first solution I came up was precisely that, as can be seen from the commented data block. \n\nIn that straight-forward approach, whe... | 2 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Python Solution SortedList | finding-mk-average | 0 | 1 | ```\nfrom sortedcontainers import SortedList\n\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.arr = SortedList()\n self.m = m\n self.k = k\n self.q = deque()\n self.total = None\n\n def addElement(self, num: int) -> None:\n self.q.append(num)\n m ... | 1 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Python Solution SortedList | finding-mk-average | 0 | 1 | ```\nfrom sortedcontainers import SortedList\n\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.arr = SortedList()\n self.m = m\n self.k = k\n self.q = deque()\n self.total = None\n\n def addElement(self, num: int) -> None:\n self.q.append(num)\n m ... | 1 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Solution using BST. Easy to understand | finding-mk-average | 0 | 1 | # Intuition\nUse BST as we are keeping track of sorted items. \n\n# Approach\nThere can be multiple approaches to solve this problem. Let me describe three approaches (note that first two approaches lead to TLE here):\n\n1. Brute force: We can do exactly as the problem says. Maintain a list of all elements. When an add... | 0 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Solution using BST. Easy to understand | finding-mk-average | 0 | 1 | # Intuition\nUse BST as we are keeping track of sorted items. \n\n# Approach\nThere can be multiple approaches to solve this problem. Let me describe three approaches (note that first two approaches lead to TLE here):\n\n1. Brute force: We can do exactly as the problem says. Maintain a list of all elements. When an add... | 0 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Python, Binary search and double-ended queue. Showing clear thread of thoughts | finding-mk-average | 0 | 1 | # Intuition\n\nThought 1: We start from the naive solution\n* Idea: Keep a container of m elements (data structure: double-ended queue, probably a doubly-linked list in exact implementation)\n* AddElement -> cost O(1)\n* calculateMKAverage -> sort (O(m log m)))\n\nThought 2: Can we do better for calculate MKAverage?\n*... | 0 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Python, Binary search and double-ended queue. Showing clear thread of thoughts | finding-mk-average | 0 | 1 | # Intuition\n\nThought 1: We start from the naive solution\n* Idea: Keep a container of m elements (data structure: double-ended queue, probably a doubly-linked list in exact implementation)\n* AddElement -> cost O(1)\n* calculateMKAverage -> sort (O(m log m)))\n\nThought 2: Can we do better for calculate MKAverage?\n*... | 0 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Python3 sortedlist, barely passes | finding-mk-average | 0 | 1 | # Code\n```\nfrom sortedcontainers import SortedList\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.l = SortedList([])\n self.k = k\n self.m = m\n self.queue = []\n \n \n\n def addElement(self, num: int) -> None:\n self.l.add(num)\n self.qu... | 0 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Python3 sortedlist, barely passes | finding-mk-average | 0 | 1 | # Code\n```\nfrom sortedcontainers import SortedList\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.l = SortedList([])\n self.k = k\n self.m = m\n self.queue = []\n \n \n\n def addElement(self, num: int) -> None:\n self.l.add(num)\n self.qu... | 0 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Straight forward solution | finding-mk-average | 0 | 1 | # Intuition\nTo compute the MKAverage, we need a rolling window of the last m numbers and an efficient way to exclude the smallest and largest k numbers. A deque is suitable for the rolling window, and a Counter can efficiently track number frequencies.\n\n# Approach\n1. Initialization: Use a deque to maintain the last... | 0 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Straight forward solution | finding-mk-average | 0 | 1 | # Intuition\nTo compute the MKAverage, we need a rolling window of the last m numbers and an efficient way to exclude the smallest and largest k numbers. A deque is suitable for the rolling window, and a Counter can efficiently track number frequencies.\n\n# Approach\n1. Initialization: Use a deque to maintain the last... | 0 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Simple and clean Python code to balance 3 sorted lists | finding-mk-average | 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 two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Simple and clean Python code to balance 3 sorted lists | finding-mk-average | 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 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Easiest Python Solution | finding-mk-average | 0 | 1 | ```\nfrom sortedcontainers import SortedList\n\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.left = SortedList()\n self.mid = SortedList()\n self.right = SortedList()\n self.total = deque()\n self.m = m\n self.k = k\n self.sum = 0\n\n def addElem... | 0 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Easiest Python Solution | finding-mk-average | 0 | 1 | ```\nfrom sortedcontainers import SortedList\n\nclass MKAverage:\n\n def __init__(self, m: int, k: int):\n self.left = SortedList()\n self.mid = SortedList()\n self.right = SortedList()\n self.total = deque()\n self.m = m\n self.k = k\n self.sum = 0\n\n def addElem... | 0 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Python SortedList - intuitive logic and explanation | finding-mk-average | 0 | 1 | Algorithm:\n1. Maintaina queue for keeping items in stream order and 3 sorted lists - lesser, mid and greater for keeping track of the k lesser items, k greater items and m-2k selected items that need to be averaged\n2. When the stream size exceeds m, we need to remove the oldest entry from the stream i.e. the first en... | 0 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Python SortedList - intuitive logic and explanation | finding-mk-average | 0 | 1 | Algorithm:\n1. Maintaina queue for keeping items in stream order and 3 sorted lists - lesser, mid and greater for keeping track of the k lesser items, k greater items and m-2k selected items that need to be averaged\n2. When the stream size exceeds m, we need to remove the oldest entry from the stream i.e. the first en... | 0 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
[Python3] SortedList + Deque - O(1) and O(log(N)) | finding-mk-average | 0 | 1 | # Intuition\nUse a queue to store the incoming elements, so we always know which one is the oldest\nKeep 3 sorted lists, one for the leftmost $$k$$ elements, one for the central part (which is used to calculate the average) and one for the rightmost $$k$$ elements.\n\nIf you don\'t have enough elements ($$length < m$$)... | 0 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
[Python3] SortedList + Deque - O(1) and O(log(N)) | finding-mk-average | 0 | 1 | # Intuition\nUse a queue to store the incoming elements, so we always know which one is the oldest\nKeep 3 sorted lists, one for the leftmost $$k$$ elements, one for the central part (which is used to calculate the average) and one for the rightmost $$k$$ elements.\n\nIf you don\'t have enough elements ($$length < m$$)... | 0 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
O(logN) addElement, O(1) calculateMKAverage Python SortedList | finding-mk-average | 0 | 1 | The idea is to keep a sorted list of the most recent m elements. the first k elements and last k elements are the k smallest and k largest elements, respectively. Another twist is instead of calculating the sum every time we got an update, we keep a running sum on the two ends of the list (k largest and k smallest) and... | 0 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
O(logN) addElement, O(1) calculateMKAverage Python SortedList | finding-mk-average | 0 | 1 | The idea is to keep a sorted list of the most recent m elements. the first k elements and last k elements are the k smallest and k largest elements, respectively. Another twist is instead of calculating the sum every time we got an update, we keep a running sum on the two ends of the list (k largest and k smallest) and... | 0 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Python (Simple Deque + SortedList) | finding-mk-average | 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 two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Python (Simple Deque + SortedList) | finding-mk-average | 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 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Use Sorted Containers to help solving this problem | finding-mk-average | 0 | 1 | # Intuition & Approach\nDivide array into 3 sorted arrays: `s1`, `s2` and `s3`; `s1` contains smallest k number, and `s3` contains largest k elements. Use `nums` to remember last m numbers.\n\n# Code\n```\nimport sortedcontainers\nfrom sortedcontainers import SortedList\nfrom collections import deque\nclass MKAverage:\... | 0 | You are given two integers, `m` and `k`, and a stream of integers. You are tasked to implement a data structure that calculates the **MKAverage** for the stream.
The **MKAverage** can be calculated using these steps:
1. If the number of the elements in the stream is less than `m` you should consider the **MKAverage*... | We can select a subset of tasks and assign it to a worker then solve the subproblem on the remaining tasks |
Use Sorted Containers to help solving this problem | finding-mk-average | 0 | 1 | # Intuition & Approach\nDivide array into 3 sorted arrays: `s1`, `s2` and `s3`; `s1` contains smallest k number, and `s3` contains largest k elements. Use `nums` to remember last m numbers.\n\n# Code\n```\nimport sortedcontainers\nfrom sortedcontainers import SortedList\nfrom collections import deque\nclass MKAverage:\... | 0 | There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the `ith` project has.
You can work on the projects following these two rules:
* Every week, you will finish **exactly one** milestone of **one** project. You ... | At each query, try to save and update the sum of the elements needed to calculate MKAverage. You can use BSTs for fast insertion and deletion of the elements. |
Python Easy and Efficient Solution . | minimum-operations-to-make-the-array-increasing | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe have to Return the minimum number of operations needed to make nums strictly increasing.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTaking the previous value and comparing with the current value and increasin... | 3 | You are given an integer array `nums` (**0-indexed**). In one operation, you can choose an element of the array and increment it by `1`.
* For example, if `nums = [1,2,3]`, you can choose to increment `nums[1]` to make `nums = [1,**3**,3]`.
Return _the **minimum** number of operations needed to make_ `nums` _**stri... | null |
Python Easy and Efficient Solution . | minimum-operations-to-make-the-array-increasing | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe have to Return the minimum number of operations needed to make nums strictly increasing.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTaking the previous value and comparing with the current value and increasin... | 3 | There is a rooted tree consisting of `n` nodes numbered `0` to `n - 1`. Each node's number denotes its **unique genetic value** (i.e. the genetic value of node `x` is `x`). The **genetic difference** between two genetic values is defined as the **bitwise-****XOR** of their values. You are given the integer array `paren... | nums[i+1] must be at least equal to nums[i] + 1. Think greedily. You don't have to increase nums[i+1] beyond nums[i]+1. Iterate on i and set nums[i] = max(nums[i-1]+1, nums[i]) . |
python 3 solution beats 99.8% TIME , O(1) SPACE used | minimum-operations-to-make-the-array-increasing | 0 | 1 | # stats\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe can have a temporary... | 3 | You are given an integer array `nums` (**0-indexed**). In one operation, you can choose an element of the array and increment it by `1`.
* For example, if `nums = [1,2,3]`, you can choose to increment `nums[1]` to make `nums = [1,**3**,3]`.
Return _the **minimum** number of operations needed to make_ `nums` _**stri... | null |
python 3 solution beats 99.8% TIME , O(1) SPACE used | minimum-operations-to-make-the-array-increasing | 0 | 1 | # stats\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe can have a temporary... | 3 | There is a rooted tree consisting of `n` nodes numbered `0` to `n - 1`. Each node's number denotes its **unique genetic value** (i.e. the genetic value of node `x` is `x`). The **genetic difference** between two genetic values is defined as the **bitwise-****XOR** of their values. You are given the integer array `paren... | nums[i+1] must be at least equal to nums[i] + 1. Think greedily. You don't have to increase nums[i+1] beyond nums[i]+1. Iterate on i and set nums[i] = max(nums[i-1]+1, nums[i]) . |
Beginner-friendly || Simple solution in Python3/TypeScript | minimum-operations-to-make-the-array-increasing | 0 | 1 | # Intuition\nThe problem description is the following:\n- there\'s a list of `nums`\n- our goal is to make `nums` **strongly increasing**\n\nThere\'s no need in sorting. The approach is straightforward and requires to calculate difference between adjacent integers.\n\n# Approach\n1. initialize `ans` to store the answer... | 2 | You are given an integer array `nums` (**0-indexed**). In one operation, you can choose an element of the array and increment it by `1`.
* For example, if `nums = [1,2,3]`, you can choose to increment `nums[1]` to make `nums = [1,**3**,3]`.
Return _the **minimum** number of operations needed to make_ `nums` _**stri... | null |
Beginner-friendly || Simple solution in Python3/TypeScript | minimum-operations-to-make-the-array-increasing | 0 | 1 | # Intuition\nThe problem description is the following:\n- there\'s a list of `nums`\n- our goal is to make `nums` **strongly increasing**\n\nThere\'s no need in sorting. The approach is straightforward and requires to calculate difference between adjacent integers.\n\n# Approach\n1. initialize `ans` to store the answer... | 2 | There is a rooted tree consisting of `n` nodes numbered `0` to `n - 1`. Each node's number denotes its **unique genetic value** (i.e. the genetic value of node `x` is `x`). The **genetic difference** between two genetic values is defined as the **bitwise-****XOR** of their values. You are given the integer array `paren... | nums[i+1] must be at least equal to nums[i] + 1. Think greedily. You don't have to increase nums[i+1] beyond nums[i]+1. Iterate on i and set nums[i] = max(nums[i-1]+1, nums[i]) . |
8 lines solution using python3 simplest of all | minimum-operations-to-make-the-array-increasing | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | You are given an integer array `nums` (**0-indexed**). In one operation, you can choose an element of the array and increment it by `1`.
* For example, if `nums = [1,2,3]`, you can choose to increment `nums[1]` to make `nums = [1,**3**,3]`.
Return _the **minimum** number of operations needed to make_ `nums` _**stri... | null |
8 lines solution using python3 simplest of all | minimum-operations-to-make-the-array-increasing | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | There is a rooted tree consisting of `n` nodes numbered `0` to `n - 1`. Each node's number denotes its **unique genetic value** (i.e. the genetic value of node `x` is `x`). The **genetic difference** between two genetic values is defined as the **bitwise-****XOR** of their values. You are given the integer array `paren... | nums[i+1] must be at least equal to nums[i] + 1. Think greedily. You don't have to increase nums[i+1] beyond nums[i]+1. Iterate on i and set nums[i] = max(nums[i-1]+1, nums[i]) . |
Python3 simple solution beats 90% users | minimum-operations-to-make-the-array-increasing | 0 | 1 | ```\nclass Solution:\n def minOperations(self, nums: List[int]) -> int:\n count = 0\n for i in range(1,len(nums)):\n if nums[i] <= nums[i-1]:\n x = nums[i]\n nums[i] += (nums[i-1] - nums[i]) + 1\n count += nums[i] - x\n return count\n```\n*... | 12 | You are given an integer array `nums` (**0-indexed**). In one operation, you can choose an element of the array and increment it by `1`.
* For example, if `nums = [1,2,3]`, you can choose to increment `nums[1]` to make `nums = [1,**3**,3]`.
Return _the **minimum** number of operations needed to make_ `nums` _**stri... | null |
Python3 simple solution beats 90% users | minimum-operations-to-make-the-array-increasing | 0 | 1 | ```\nclass Solution:\n def minOperations(self, nums: List[int]) -> int:\n count = 0\n for i in range(1,len(nums)):\n if nums[i] <= nums[i-1]:\n x = nums[i]\n nums[i] += (nums[i-1] - nums[i]) + 1\n count += nums[i] - x\n return count\n```\n*... | 12 | There is a rooted tree consisting of `n` nodes numbered `0` to `n - 1`. Each node's number denotes its **unique genetic value** (i.e. the genetic value of node `x` is `x`). The **genetic difference** between two genetic values is defined as the **bitwise-****XOR** of their values. You are given the integer array `paren... | nums[i+1] must be at least equal to nums[i] + 1. Think greedily. You don't have to increase nums[i+1] beyond nums[i]+1. Iterate on i and set nums[i] = max(nums[i-1]+1, nums[i]) . |
Python O(n) simple and short solution | minimum-operations-to-make-the-array-increasing | 0 | 1 | **Python :**\n\n```\ndef minOperations(self, nums: List[int]) -> int:\n\tans = 0\n\n\tfor i in range(1, len(nums)):\n\t\tif nums[i] <= nums[i - 1]:\n\t\t\tans += (nums[i - 1] - nums[i] + 1)\n\t\t\tnums[i] = (nums[i - 1] + 1)\n\n\treturn ans\n```\n\n**Like it ? please upvote !** | 9 | You are given an integer array `nums` (**0-indexed**). In one operation, you can choose an element of the array and increment it by `1`.
* For example, if `nums = [1,2,3]`, you can choose to increment `nums[1]` to make `nums = [1,**3**,3]`.
Return _the **minimum** number of operations needed to make_ `nums` _**stri... | null |
Python O(n) simple and short solution | minimum-operations-to-make-the-array-increasing | 0 | 1 | **Python :**\n\n```\ndef minOperations(self, nums: List[int]) -> int:\n\tans = 0\n\n\tfor i in range(1, len(nums)):\n\t\tif nums[i] <= nums[i - 1]:\n\t\t\tans += (nums[i - 1] - nums[i] + 1)\n\t\t\tnums[i] = (nums[i - 1] + 1)\n\n\treturn ans\n```\n\n**Like it ? please upvote !** | 9 | There is a rooted tree consisting of `n` nodes numbered `0` to `n - 1`. Each node's number denotes its **unique genetic value** (i.e. the genetic value of node `x` is `x`). The **genetic difference** between two genetic values is defined as the **bitwise-****XOR** of their values. You are given the integer array `paren... | nums[i+1] must be at least equal to nums[i] + 1. Think greedily. You don't have to increase nums[i+1] beyond nums[i]+1. Iterate on i and set nums[i] = max(nums[i-1]+1, nums[i]) . |
Python Easy || Simple Geometry | queries-on-number-of-points-inside-a-circle | 0 | 1 | # Code\n```\nclass Solution:\n def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:\n lis=[]\n for circle in queries:\n cnt=0\n for pt in points:\n if ((((circle[0]-pt[0])**2)+((circle[1]-pt[1])**2))**0.5)<=circle[2]:\n ... | 1 | You are given an array `points` where `points[i] = [xi, yi]` is the coordinates of the `ith` point on a 2D plane. Multiple points can have the **same** coordinates.
You are also given an array `queries` where `queries[j] = [xj, yj, rj]` describes a circle centered at `(xj, yj)` with a radius of `rj`.
For each query `... | Try to define a recursive approach. For the ith candies, there will be one of the two following cases: If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). If the i - 1 previous... |
Python very easy solution | queries-on-number-of-points-inside-a-circle | 0 | 1 | # Very Easy Soln\n\n# Code\n```\nclass Solution(object):\n def countPoints(self, points, q):\n for i in range(len(q)):\n x,y,r=q[i]\n c=0\n for j,k in points:\n if ((j-x)**2+(k-y)**2)**0.5<=r: c+=1\n q[i]=c\n return q\n``` | 1 | You are given an array `points` where `points[i] = [xi, yi]` is the coordinates of the `ith` point on a 2D plane. Multiple points can have the **same** coordinates.
You are also given an array `queries` where `queries[j] = [xj, yj, rj]` describes a circle centered at `(xj, yj)` with a radius of `rj`.
For each query `... | Try to define a recursive approach. For the ith candies, there will be one of the two following cases: If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). If the i - 1 previous... |
Hashtable + Binary Search, O(RlogN), explained! [Python3] | queries-on-number-of-points-inside-a-circle | 0 | 1 | # Intuition\n\nMost simple solution would be for each circle, check if every point is inside circle using math, then time for each query would be O(N).\n\nAnother idea would be sort points then binary search for points that are within the "square" range that the circle would form from x - r to x + r and y - r to y + r.... | 2 | You are given an array `points` where `points[i] = [xi, yi]` is the coordinates of the `ith` point on a 2D plane. Multiple points can have the **same** coordinates.
You are also given an array `queries` where `queries[j] = [xj, yj, rj]` describes a circle centered at `(xj, yj)` with a radius of `rj`.
For each query `... | Try to define a recursive approach. For the ith candies, there will be one of the two following cases: If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). If the i - 1 previous... |
Python One-liner | queries-on-number-of-points-inside-a-circle | 0 | 1 | ```\nclass Solution:\n def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:\n return [sum(math.sqrt((x0-x1)**2 + (y0-y1)**2) <= r for x1, y1 in points) for x0, y0, r in queries]\n``` | 28 | You are given an array `points` where `points[i] = [xi, yi]` is the coordinates of the `ith` point on a 2D plane. Multiple points can have the **same** coordinates.
You are also given an array `queries` where `queries[j] = [xj, yj, rj]` describes a circle centered at `(xj, yj)` with a radius of `rj`.
For each query `... | Try to define a recursive approach. For the ith candies, there will be one of the two following cases: If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). If the i - 1 previous... |
Solution of queries on number of points inside a circle problem | queries-on-number-of-points-inside-a-circle | 0 | 1 | # Approach\n- If you select a point on the coordinate plane, you can see that the projections of its coordinates on the x and y axes are the legs of a right triangle. And the hypotenuse of this right triangle just shows the distance from the origin to the point.\n- So, if the length of the hypotenuse is no greater than... | 3 | You are given an array `points` where `points[i] = [xi, yi]` is the coordinates of the `ith` point on a 2D plane. Multiple points can have the **same** coordinates.
You are also given an array `queries` where `queries[j] = [xj, yj, rj]` describes a circle centered at `(xj, yj)` with a radius of `rj`.
For each query `... | Try to define a recursive approach. For the ith candies, there will be one of the two following cases: If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). If the i - 1 previous... |
Simple python code with explanation | queries-on-number-of-points-inside-a-circle | 0 | 1 | # **MOST UNDERSTANDABLE CODE**\n```\nclass Solution:\n def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:\n #create a empty list k \n k = []\n #create a variable and initialise it to 0 \n count = 0 \n #... | 11 | You are given an array `points` where `points[i] = [xi, yi]` is the coordinates of the `ith` point on a 2D plane. Multiple points can have the **same** coordinates.
You are also given an array `queries` where `queries[j] = [xj, yj, rj]` describes a circle centered at `(xj, yj)` with a radius of `rj`.
For each query `... | Try to define a recursive approach. For the ith candies, there will be one of the two following cases: If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). If the i - 1 previous... |
[Python 3] 512 ms, faster than 100% using complex numbers | queries-on-number-of-points-inside-a-circle | 0 | 1 | ```\nclass Solution:\n def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:\n points = list(map(complex, *zip(*points)))\n\t\tqueries = ((complex(x, y), r) for x, y, r in queries)\n return [sum(abs(p - q) <= r for p in points) for q, r in queries]\n``` | 17 | You are given an array `points` where `points[i] = [xi, yi]` is the coordinates of the `ith` point on a 2D plane. Multiple points can have the **same** coordinates.
You are also given an array `queries` where `queries[j] = [xj, yj, rj]` describes a circle centered at `(xj, yj)` with a radius of `rj`.
For each query `... | Try to define a recursive approach. For the ith candies, there will be one of the two following cases: If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). If the i - 1 previous... |
Python Easy solution | queries-on-number-of-points-inside-a-circle | 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 array `points` where `points[i] = [xi, yi]` is the coordinates of the `ith` point on a 2D plane. Multiple points can have the **same** coordinates.
You are also given an array `queries` where `queries[j] = [xj, yj, rj]` describes a circle centered at `(xj, yj)` with a radius of `rj`.
For each query `... | Try to define a recursive approach. For the ith candies, there will be one of the two following cases: If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). If the i - 1 previous... |
Python My Soln Easy to Follow | queries-on-number-of-points-inside-a-circle | 0 | 1 | def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:\n\n res = []\n for circle in queries:\n count = 0\n for point in points:\n if (circle[0]-point[0]) ** 2 + (circle[1]-point[1]) ** 2 <= circle[2] ** 2:\n count += 1... | 1 | You are given an array `points` where `points[i] = [xi, yi]` is the coordinates of the `ith` point on a 2D plane. Multiple points can have the **same** coordinates.
You are also given an array `queries` where `queries[j] = [xj, yj, rj]` describes a circle centered at `(xj, yj)` with a radius of `rj`.
For each query `... | Try to define a recursive approach. For the ith candies, there will be one of the two following cases: If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). If the i - 1 previous... |
Beginner-friendly || Simple solution with PrefixSum in Python3 / TypeScript | maximum-xor-for-each-query | 0 | 1 | # Intuition\nLet\'s briefly explain, what the problem is:\n- there\'s a list of `nums` and an integer `maximumBit`\n- our goal is to find at each step an integer, that represent **maximum XOR**\n\nThis could be implemented with **Prefix Sum**.\n\nFollowing to the problem\'s description, at each step we should **remove ... | 1 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
reduce() | maximum-xor-for-each-query | 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 a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Simple solution beats 99% | maximum-xor-for-each-query | 0 | 1 | # Code\n```\nclass Solution:\n def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:\n ans = [0] * len(nums)\n x = (2**maximumBit-1)\n for i, n in enumerate(nums):\n x = x ^ n\n ans[-1-i] = x\n return ans \n``` | 2 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Python3 | Prefix Sum With Bit Manip. Fact Max XOR = 2^maximumBit - 1 | maximum-xor-for-each-query | 0 | 1 | ```\nclass Solution:\n #Let n = len(nums)!\n #Time-Complexity: O(n + n) -> O(N)\n #Space-Complexity: O(N)\n def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:\n #Approach:\n \n #1. First, initialize prefix sum array of exclusive or results up to each index position!... | 1 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Simple fast solution TIME COMPLEXITY O(n) | maximum-xor-for-each-query | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```python []\nclass Solution:\n def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:\n ans = [(1 << maximumBit) - 1]\n for n in nums:\n ans.append(ans[-1] ^ n)\n\n return ans[len(ans)... | 2 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
One line solution | maximum-xor-for-each-query | 0 | 1 | ```\nclass Solution:\n def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:\n return list(map(xor, accumulate(nums, xor), repeat((1 << maximumBit) - 1)))[::-1] \n``` | 0 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Python Solution | Easy To Understand | O(N) Runtime & O(1) Space Complexity | maximum-xor-for-each-query | 0 | 1 | \n# Code\n```\nclass Solution:\n def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:\n \n n = len(nums)\n result = [0] * n\n val = 2 ** maximumBit - 1\n\n for i in range(n):\n result[n - i - 1] = val ^ nums[i]\n val = val ^ nums[i]\n\n ... | 0 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
[Python3] O(n) use combination count and gcd | minimum-number-of-operations-to-make-string-sorted | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nEvery operation equals to find previous permutation of the tail of s. \nWe process the suffix step by step, to find the correct insert position to insert the current string.\ncnt is used to count all chars after i.\nt is used to store cur... | 1 | You are given a string `s` (**0-indexed**)ββββββ. You are asked to perform the following operation on `s`ββββββ until you get a sorted string:
1. Find **the largest index** `i` such that `1 <= i < s.length` and `s[i] < s[i - 1]`.
2. Find **the largest index** `j` such that `i <= j < s.length` and `s[k] < s[i - 1]` f... | Note that the number of powers of 2 is at most 21 so this turns the problem to a classic find the number of pairs that sum to a certain value but for 21 values You need to use something fasters than the NlogN approach since there is already the log of iterating over the powers so one idea is two pointers |
[Python3] O(n) use combination count and gcd | minimum-number-of-operations-to-make-string-sorted | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nEvery operation equals to find previous permutation of the tail of s. \nWe process the suffix step by step, to find the correct insert position to insert the current string.\ncnt is used to count all chars after i.\nt is used to store cur... | 1 | Given a string `s`, return `true` _if_ `s` _is a **good** string, or_ `false` _otherwise_.
A string `s` is **good** if **all** the characters that appear in `s` have the **same** number of occurrences (i.e., the same frequency).
**Example 1:**
**Input:** s = "abacbc "
**Output:** true
**Explanation:** The character... | Note that the operations given describe getting the previous permutation of s To solve this problem you need to solve every suffix separately |
[Python3] math solution | minimum-number-of-operations-to-make-string-sorted | 0 | 1 | \n```\nclass Solution:\n def makeStringSorted(self, s: str) -> int:\n freq = [0]*26\n for c in s: freq[ord(c) - 97] += 1\n \n MOD = 1_000_000_007\n fac = cache(lambda x: x*fac(x-1)%MOD if x else 1)\n ifac = cache(lambda x: pow(fac(x), MOD-2, MOD)) # Fermat\'s little theorem ... | 2 | You are given a string `s` (**0-indexed**)ββββββ. You are asked to perform the following operation on `s`ββββββ until you get a sorted string:
1. Find **the largest index** `i` such that `1 <= i < s.length` and `s[i] < s[i - 1]`.
2. Find **the largest index** `j` such that `i <= j < s.length` and `s[k] < s[i - 1]` f... | Note that the number of powers of 2 is at most 21 so this turns the problem to a classic find the number of pairs that sum to a certain value but for 21 values You need to use something fasters than the NlogN approach since there is already the log of iterating over the powers so one idea is two pointers |
[Python3] math solution | minimum-number-of-operations-to-make-string-sorted | 0 | 1 | \n```\nclass Solution:\n def makeStringSorted(self, s: str) -> int:\n freq = [0]*26\n for c in s: freq[ord(c) - 97] += 1\n \n MOD = 1_000_000_007\n fac = cache(lambda x: x*fac(x-1)%MOD if x else 1)\n ifac = cache(lambda x: pow(fac(x), MOD-2, MOD)) # Fermat\'s little theorem ... | 2 | Given a string `s`, return `true` _if_ `s` _is a **good** string, or_ `false` _otherwise_.
A string `s` is **good** if **all** the characters that appear in `s` have the **same** number of occurrences (i.e., the same frequency).
**Example 1:**
**Input:** s = "abacbc "
**Output:** true
**Explanation:** The character... | Note that the operations given describe getting the previous permutation of s To solve this problem you need to solve every suffix separately |
Python Intuitive solution with dictionary | check-if-the-sentence-is-pangram | 0 | 1 | ```\ndef checkIfPangram(self, sentence: str) -> bool:\n ref = {v:False for i,v in enumerate(\'abcdefghijklmnopqrstuvwxyz\')}\n for i in sentence: \n ref[i] = True\n for i in ref: \n if not ref[i]: \n return False\n return True | 2 | A **pangram** is a sentence where every letter of the English alphabet appears at least once.
Given a string `sentence` containing only lowercase English letters, return `true` _if_ `sentence` _is a **pangram**, or_ `false` _otherwise._
**Example 1:**
**Input:** sentence = "thequickbrownfoxjumpsoverthelazydog "
**O... | The problem can be reduced to computing Longest Common Subsequence between both arrays. Since one of the arrays has distinct elements, we can consider that these elements describe an arrangement of numbers, and we can replace each element in the other array with the index it appeared at in the first array. Then the pro... |
Python Intuitive solution with dictionary | check-if-the-sentence-is-pangram | 0 | 1 | ```\ndef checkIfPangram(self, sentence: str) -> bool:\n ref = {v:False for i,v in enumerate(\'abcdefghijklmnopqrstuvwxyz\')}\n for i in sentence: \n ref[i] = True\n for i in ref: \n if not ref[i]: \n return False\n return True | 2 | You are given a **0-indexed** string `s` and are tasked with finding two **non-intersecting palindromic** substrings of **odd** length such that the product of their lengths is maximized.
More formally, you want to choose four integers `i`, `j`, `k`, `l` such that `0 <= i <= j < k <= l < s.length` and both the substri... | Iterate over the string and mark each character as found (using a boolean array, bitmask, or any other similar way). Check if the number of found characters equals the alphabet length. |
One if statement python | check-if-the-sentence-is-pangram | 0 | 1 | \n# Code\n```\nclass Solution:\n def checkIfPangram(self, sentence: str) -> bool:\n if \'a\' in sentence and \'s\' in sentence and \'d\' in sentence and \'f\' in sentence and \'g\' in sentence and \'h\' in sentence and \'j\' in sentence and \'k\' in sentence and \'l\' in sentence and \'q\' in sentence and \'w... | 1 | A **pangram** is a sentence where every letter of the English alphabet appears at least once.
Given a string `sentence` containing only lowercase English letters, return `true` _if_ `sentence` _is a **pangram**, or_ `false` _otherwise._
**Example 1:**
**Input:** sentence = "thequickbrownfoxjumpsoverthelazydog "
**O... | The problem can be reduced to computing Longest Common Subsequence between both arrays. Since one of the arrays has distinct elements, we can consider that these elements describe an arrangement of numbers, and we can replace each element in the other array with the index it appeared at in the first array. Then the pro... |
One if statement python | check-if-the-sentence-is-pangram | 0 | 1 | \n# Code\n```\nclass Solution:\n def checkIfPangram(self, sentence: str) -> bool:\n if \'a\' in sentence and \'s\' in sentence and \'d\' in sentence and \'f\' in sentence and \'g\' in sentence and \'h\' in sentence and \'j\' in sentence and \'k\' in sentence and \'l\' in sentence and \'q\' in sentence and \'w... | 1 | You are given a **0-indexed** string `s` and are tasked with finding two **non-intersecting palindromic** substrings of **odd** length such that the product of their lengths is maximized.
More formally, you want to choose four integers `i`, `j`, `k`, `l` such that `0 <= i <= j < k <= l < s.length` and both the substri... | Iterate over the string and mark each character as found (using a boolean array, bitmask, or any other similar way). Check if the number of found characters equals the alphabet length. |
Python || O(n) || Easy Understanding | check-if-the-sentence-is-pangram | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThinking array of Alphabet Character.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIterating each character in given string mark 1 in charater array.\nThen checking sum of character array if the sum is 26 then i... | 1 | A **pangram** is a sentence where every letter of the English alphabet appears at least once.
Given a string `sentence` containing only lowercase English letters, return `true` _if_ `sentence` _is a **pangram**, or_ `false` _otherwise._
**Example 1:**
**Input:** sentence = "thequickbrownfoxjumpsoverthelazydog "
**O... | The problem can be reduced to computing Longest Common Subsequence between both arrays. Since one of the arrays has distinct elements, we can consider that these elements describe an arrangement of numbers, and we can replace each element in the other array with the index it appeared at in the first array. Then the pro... |
Python || O(n) || Easy Understanding | check-if-the-sentence-is-pangram | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThinking array of Alphabet Character.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIterating each character in given string mark 1 in charater array.\nThen checking sum of character array if the sum is 26 then i... | 1 | You are given a **0-indexed** string `s` and are tasked with finding two **non-intersecting palindromic** substrings of **odd** length such that the product of their lengths is maximized.
More formally, you want to choose four integers `i`, `j`, `k`, `l` such that `0 <= i <= j < k <= l < s.length` and both the substri... | Iterate over the string and mark each character as found (using a boolean array, bitmask, or any other similar way). Check if the number of found characters equals the alphabet length. |
Easy Python solution | check-if-the-sentence-is-pangram | 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 | A **pangram** is a sentence where every letter of the English alphabet appears at least once.
Given a string `sentence` containing only lowercase English letters, return `true` _if_ `sentence` _is a **pangram**, or_ `false` _otherwise._
**Example 1:**
**Input:** sentence = "thequickbrownfoxjumpsoverthelazydog "
**O... | The problem can be reduced to computing Longest Common Subsequence between both arrays. Since one of the arrays has distinct elements, we can consider that these elements describe an arrangement of numbers, and we can replace each element in the other array with the index it appeared at in the first array. Then the pro... |
Easy Python solution | check-if-the-sentence-is-pangram | 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 a **0-indexed** string `s` and are tasked with finding two **non-intersecting palindromic** substrings of **odd** length such that the product of their lengths is maximized.
More formally, you want to choose four integers `i`, `j`, `k`, `l` such that `0 <= i <= j < k <= l < s.length` and both the substri... | Iterate over the string and mark each character as found (using a boolean array, bitmask, or any other similar way). Check if the number of found characters equals the alphabet length. |
One_liner.py | maximum-ice-cream-bars | 0 | 1 | # Code\n```\nclass Solution:\n def maxIceCream(self, costs: List[int], coins: int) -> int:\n return sum(1 for icecream in sorted(costs) if (coins:= coins-icecream) >= 0)\n```\n -> int:\n return sum(1 for icecream in sorted(costs) if (coins:= coins-icecream) >= 0)\n```\n -> int:\n return bisect_right(list(accumulate(sorted(costs))), coins)\n\n``` | 4 | It is a sweltering summer day, and a boy wants to buy some ice cream bars.
At the store, there are `n` ice cream bars. You are given an array `costs` of length `n`, where `costs[i]` is the price of the `ith` ice cream bar in coins. The boy initially has `coins` coins to spend, and he wants to buy as many ice cream bar... | Let's note that the altitude of an element is the sum of gains of all the elements behind it Getting the altitudes can be done by getting the prefix sum array of the given array |
Python 1 Line | maximum-ice-cream-bars | 0 | 1 | ```\nclass Solution:\n def maxIceCream(self, costs: List[int], coins: int) -> int:\n return bisect_right(list(accumulate(sorted(costs))), coins)\n\n``` | 4 | Given a string `s` and an array of strings `words`, determine whether `s` is a **prefix string** of `words`.
A string `s` is a **prefix string** of `words` if `s` can be made by concatenating the first `k` strings in `words` for some **positive** `k` no larger than `words.length`.
Return `true` _if_ `s` _is a **prefi... | It is always optimal to buy the least expensive ice cream bar first. Sort the prices so that the cheapest ice cream bar comes first. |
ππPython3 || 838 ms, faster than 92.67% of Python3 || Clean and Easy to Understand | maximum-ice-cream-bars | 0 | 1 | **Logic**\n*1. Sort the Cost array so that we can buy maximum low cost Ice Cream Bars*\n*2. Buy the low cost Ice Cream Bars and reduce the coins as you buy*\n```\ndef maxIceCream(self, costs: List[int], coins: int) -> int:\n result=0\n costs.sort()\n for x,y in enumerate(costs):\n if coi... | 3 | It is a sweltering summer day, and a boy wants to buy some ice cream bars.
At the store, there are `n` ice cream bars. You are given an array `costs` of length `n`, where `costs[i]` is the price of the `ith` ice cream bar in coins. The boy initially has `coins` coins to spend, and he wants to buy as many ice cream bar... | Let's note that the altitude of an element is the sum of gains of all the elements behind it Getting the altitudes can be done by getting the prefix sum array of the given array |
ππPython3 || 838 ms, faster than 92.67% of Python3 || Clean and Easy to Understand | maximum-ice-cream-bars | 0 | 1 | **Logic**\n*1. Sort the Cost array so that we can buy maximum low cost Ice Cream Bars*\n*2. Buy the low cost Ice Cream Bars and reduce the coins as you buy*\n```\ndef maxIceCream(self, costs: List[int], coins: int) -> int:\n result=0\n costs.sort()\n for x,y in enumerate(costs):\n if coi... | 3 | Given a string `s` and an array of strings `words`, determine whether `s` is a **prefix string** of `words`.
A string `s` is a **prefix string** of `words` if `s` can be made by concatenating the first `k` strings in `words` for some **positive** `k` no larger than `words.length`.
Return `true` _if_ `s` _is a **prefi... | It is always optimal to buy the least expensive ice cream bar first. Sort the prices so that the cheapest ice cream bar comes first. |
Daily LeetCode Challenge (Python 3) | maximum-ice-cream-bars | 0 | 1 | # Intuition\nTo get the maximum amount of icecream in given coins the basic idea is to choose as cheap as icecreams possible.\nFor this we will make the array sorted in increasing order first.\nFor early return of function where it hits the base case of having less coins than req to purchase atleast one icecream compar... | 2 | It is a sweltering summer day, and a boy wants to buy some ice cream bars.
At the store, there are `n` ice cream bars. You are given an array `costs` of length `n`, where `costs[i]` is the price of the `ith` ice cream bar in coins. The boy initially has `coins` coins to spend, and he wants to buy as many ice cream bar... | Let's note that the altitude of an element is the sum of gains of all the elements behind it Getting the altitudes can be done by getting the prefix sum array of the given array |
Daily LeetCode Challenge (Python 3) | maximum-ice-cream-bars | 0 | 1 | # Intuition\nTo get the maximum amount of icecream in given coins the basic idea is to choose as cheap as icecreams possible.\nFor this we will make the array sorted in increasing order first.\nFor early return of function where it hits the base case of having less coins than req to purchase atleast one icecream compar... | 2 | Given a string `s` and an array of strings `words`, determine whether `s` is a **prefix string** of `words`.
A string `s` is a **prefix string** of `words` if `s` can be made by concatenating the first `k` strings in `words` for some **positive** `k` no larger than `words.length`.
Return `true` _if_ `s` _is a **prefi... | It is always optimal to buy the least expensive ice cream bar first. Sort the prices so that the cheapest ice cream bar comes first. |
Simple Solution using Greedy Algorithm! | maximum-ice-cream-bars | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nFirst we will sort the given list `costs` in ascending order, then check if the value of `coins` is strictly less than the 1st element of the sorted `costs` list, If yes then the boy can not buy any ice cream.\n\nIf No, then we will iterate through th... | 2 | It is a sweltering summer day, and a boy wants to buy some ice cream bars.
At the store, there are `n` ice cream bars. You are given an array `costs` of length `n`, where `costs[i]` is the price of the `ith` ice cream bar in coins. The boy initially has `coins` coins to spend, and he wants to buy as many ice cream bar... | Let's note that the altitude of an element is the sum of gains of all the elements behind it Getting the altitudes can be done by getting the prefix sum array of the given array |
Simple Solution using Greedy Algorithm! | maximum-ice-cream-bars | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nFirst we will sort the given list `costs` in ascending order, then check if the value of `coins` is strictly less than the 1st element of the sorted `costs` list, If yes then the boy can not buy any ice cream.\n\nIf No, then we will iterate through th... | 2 | Given a string `s` and an array of strings `words`, determine whether `s` is a **prefix string** of `words`.
A string `s` is a **prefix string** of `words` if `s` can be made by concatenating the first `k` strings in `words` for some **positive** `k` no larger than `words.length`.
Return `true` _if_ `s` _is a **prefi... | It is always optimal to buy the least expensive ice cream bar first. Sort the prices so that the cheapest ice cream bar comes first. |
Python minHeap solution. | single-threaded-cpu | 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:\nO(nlogn)\n- Space complexity:\nO(n)\n# Code\n```\nimport heapq\nclass Solution:\n def getOrder(self, tasks: List[List[int]]) -> ... | 1 | You are given `n`ββββββ tasks labeled from `0` to `n - 1` represented by a 2D integer array `tasks`, where `tasks[i] = [enqueueTimei, processingTimei]` means that the `iββββββth`ββββ task will be available to process at `enqueueTimei` and will take `processingTimei` to finish processing.
You have a single-threaded CPU... | You can just use brute force and find out for each language the number of users you need to teach Note that a user can appear in multiple friendships but you need to teach that user only once |
Python minHeap solution. | single-threaded-cpu | 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:\nO(nlogn)\n- Space complexity:\nO(n)\n# Code\n```\nimport heapq\nclass Solution:\n def getOrder(self, tasks: List[List[int]]) -> ... | 1 | You are given a **0-indexed** integer array `piles`, where `piles[i]` represents the number of stones in the `ith` pile, and an integer `k`. You should apply the following operation **exactly** `k` times:
* Choose any `piles[i]` and **remove** `floor(piles[i] / 2)` stones from it.
**Notice** that you can apply the ... | To simulate the problem we first need to note that if at any point in time there are no enqueued tasks we need to wait to the smallest enqueue time of a non-processed element We need a data structure like a min-heap to support choosing the task with the smallest processing time from all the enqueued tasks |
Python | Sort and Heap | Simplified | 100% | Tough Question | single-threaded-cpu | 0 | 1 | # Simplified Code\n```\nfrom heapq import heappush, heappop\nclass Solution:\n def getOrder(self, tasks: List[List[int]]) -> List[int]:\n tasks = sorted([(s,p,i) for i,(s,p) in enumerate(tasks)])\n tasks = tasks+[(float(\'inf\'),0,0)]\n n = len(tasks)\n res, h = [], []\n j = far = ... | 2 | You are given `n`ββββββ tasks labeled from `0` to `n - 1` represented by a 2D integer array `tasks`, where `tasks[i] = [enqueueTimei, processingTimei]` means that the `iββββββth`ββββ task will be available to process at `enqueueTimei` and will take `processingTimei` to finish processing.
You have a single-threaded CPU... | You can just use brute force and find out for each language the number of users you need to teach Note that a user can appear in multiple friendships but you need to teach that user only once |
Python | Sort and Heap | Simplified | 100% | Tough Question | single-threaded-cpu | 0 | 1 | # Simplified Code\n```\nfrom heapq import heappush, heappop\nclass Solution:\n def getOrder(self, tasks: List[List[int]]) -> List[int]:\n tasks = sorted([(s,p,i) for i,(s,p) in enumerate(tasks)])\n tasks = tasks+[(float(\'inf\'),0,0)]\n n = len(tasks)\n res, h = [], []\n j = far = ... | 2 | You are given a **0-indexed** integer array `piles`, where `piles[i]` represents the number of stones in the `ith` pile, and an integer `k`. You should apply the following operation **exactly** `k` times:
* Choose any `piles[i]` and **remove** `floor(piles[i] / 2)` stones from it.
**Notice** that you can apply the ... | To simulate the problem we first need to note that if at any point in time there are no enqueued tasks we need to wait to the smallest enqueue time of a non-processed element We need a data structure like a min-heap to support choosing the task with the smallest processing time from all the enqueued tasks |
Two Heaps | single-threaded-cpu | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe always want to find some minimum (enqueueTime and processingTime) in a list, so using heaps might be a good choice. Therefore we choose to use the Standard library, **heapq**, in Python.\n\n# Approach\n<!-- Describe your approach to so... | 1 | You are given `n`ββββββ tasks labeled from `0` to `n - 1` represented by a 2D integer array `tasks`, where `tasks[i] = [enqueueTimei, processingTimei]` means that the `iββββββth`ββββ task will be available to process at `enqueueTimei` and will take `processingTimei` to finish processing.
You have a single-threaded CPU... | You can just use brute force and find out for each language the number of users you need to teach Note that a user can appear in multiple friendships but you need to teach that user only once |
Two Heaps | single-threaded-cpu | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe always want to find some minimum (enqueueTime and processingTime) in a list, so using heaps might be a good choice. Therefore we choose to use the Standard library, **heapq**, in Python.\n\n# Approach\n<!-- Describe your approach to so... | 1 | You are given a **0-indexed** integer array `piles`, where `piles[i]` represents the number of stones in the `ith` pile, and an integer `k`. You should apply the following operation **exactly** `k` times:
* Choose any `piles[i]` and **remove** `floor(piles[i] / 2)` stones from it.
**Notice** that you can apply the ... | To simulate the problem we first need to note that if at any point in time there are no enqueued tasks we need to wait to the smallest enqueue time of a non-processed element We need a data structure like a min-heap to support choosing the task with the smallest processing time from all the enqueued tasks |
Python Two Heaps - Intuitive | single-threaded-cpu | 0 | 1 | # Complexity\n- Time complexity: O(N lg N), for pushing and popping all tasks to both heaps (each individual operation is a lg N operation)\n\n- Space complexity: O(N), for both heaps\n\n# Code\n```\nimport heapq\n\nclass Solution:\n def getOrder(self, tasks: List[List[int]]) -> List[int]:\n not_yet_ready = [... | 1 | You are given `n`ββββββ tasks labeled from `0` to `n - 1` represented by a 2D integer array `tasks`, where `tasks[i] = [enqueueTimei, processingTimei]` means that the `iββββββth`ββββ task will be available to process at `enqueueTimei` and will take `processingTimei` to finish processing.
You have a single-threaded CPU... | You can just use brute force and find out for each language the number of users you need to teach Note that a user can appear in multiple friendships but you need to teach that user only once |
Python Two Heaps - Intuitive | single-threaded-cpu | 0 | 1 | # Complexity\n- Time complexity: O(N lg N), for pushing and popping all tasks to both heaps (each individual operation is a lg N operation)\n\n- Space complexity: O(N), for both heaps\n\n# Code\n```\nimport heapq\n\nclass Solution:\n def getOrder(self, tasks: List[List[int]]) -> List[int]:\n not_yet_ready = [... | 1 | You are given a **0-indexed** integer array `piles`, where `piles[i]` represents the number of stones in the `ith` pile, and an integer `k`. You should apply the following operation **exactly** `k` times:
* Choose any `piles[i]` and **remove** `floor(piles[i] / 2)` stones from it.
**Notice** that you can apply the ... | To simulate the problem we first need to note that if at any point in time there are no enqueued tasks we need to wait to the smallest enqueue time of a non-processed element We need a data structure like a min-heap to support choosing the task with the smallest processing time from all the enqueued tasks |
ππPython3 || 2007 ms, faster than 90.49% of Python3 || Clean and Easy to Understand | single-threaded-cpu | 0 | 1 | ```\ndef getOrder(self, tasks: List[List[int]]) -> List[int]:\n arr = []\n prev = 0\n output = [] \n tasks= sorted((tasks,i) for i,tasks in enumerate(tasks))\n for (m,n), i in tasks:\n while arr and prev < m:\n p,j,k = heappop(arr)\n prev = ma... | 8 | You are given `n`ββββββ tasks labeled from `0` to `n - 1` represented by a 2D integer array `tasks`, where `tasks[i] = [enqueueTimei, processingTimei]` means that the `iββββββth`ββββ task will be available to process at `enqueueTimei` and will take `processingTimei` to finish processing.
You have a single-threaded CPU... | You can just use brute force and find out for each language the number of users you need to teach Note that a user can appear in multiple friendships but you need to teach that user only once |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.