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 |
|---|---|---|---|---|---|---|---|
Easy but there are room for optimization! | chalkboard-xor-game | 0 | 1 | # Code\n```\nfrom enum import Enum\n\nclass Solution:\n\n def xorGame(self, nums: List[int]) -> bool:\n self.used = set()\n\n class Result(Enum):\n WIN = 1\n LOST = 2\n TURN = 3\n\n class Player(Enum):\n Alice = 1\n Bob = 2\n\n def pl... | 0 | You are given an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
Easy but there are room for optimization! | chalkboard-xor-game | 0 | 1 | # Code\n```\nfrom enum import Enum\n\nclass Solution:\n\n def xorGame(self, nums: List[int]) -> bool:\n self.used = set()\n\n class Result(Enum):\n WIN = 1\n LOST = 2\n TURN = 3\n\n class Player(Enum):\n Alice = 1\n Bob = 2\n\n def pl... | 0 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
python solution in O(N) and O(N) | chalkboard-xor-game | 0 | 1 | # Code\n```\nclass Solution:\n def xorGame(self, nums: List[int]) -> bool:\n xor=0\n for item in nums:xor^=item\n d={}\n for item in nums:\n if(item in d):\n d[item]+=1\n else:\n d[item]=1\n count=0\n for item in d:\n ... | 0 | You are given an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
python solution in O(N) and O(N) | chalkboard-xor-game | 0 | 1 | # Code\n```\nclass Solution:\n def xorGame(self, nums: List[int]) -> bool:\n xor=0\n for item in nums:xor^=item\n d={}\n for item in nums:\n if(item in d):\n d[item]+=1\n else:\n d[item]=1\n count=0\n for item in d:\n ... | 0 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
Python (Simple Maths) | chalkboard-xor-game | 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 an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
Python (Simple Maths) | chalkboard-xor-game | 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 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
Time: O(n)O(n) Space: O(1)O(1) | chalkboard-xor-game | 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 an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
Time: O(n)O(n) Space: O(1)O(1) | chalkboard-xor-game | 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 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
Rigorous proof | chalkboard-xor-game | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is a finite state game, given any initial list of numbers. With each step the list becomes shorter by one element, and there is no drawing state.\nSo there will always be a winner.\n\n# Approach\n<!-- Describe your approach to solvin... | 0 | You are given an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
Rigorous proof | chalkboard-xor-game | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is a finite state game, given any initial list of numbers. With each step the list becomes shorter by one element, and there is no drawing state.\nSo there will always be a winner.\n\n# Approach\n<!-- Describe your approach to solvin... | 0 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
Python one-liner | chalkboard-xor-game | 0 | 1 | \n# Code\n```\nclass Solution:\n def xorGame(self, nums: List[int]) -> bool:\n return reduce(lambda x,y:x^y, nums) == 0 or len(nums) % 2 == 0\n # return reduce(xor, nums) == 0 or len(nums) % 2 == 0\n```\n | 0 | You are given an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
Python one-liner | chalkboard-xor-game | 0 | 1 | \n# Code\n```\nclass Solution:\n def xorGame(self, nums: List[int]) -> bool:\n return reduce(lambda x,y:x^y, nums) == 0 or len(nums) % 2 == 0\n # return reduce(xor, nums) == 0 or len(nums) % 2 == 0\n```\n | 0 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
Simple python code with explanation | chalkboard-xor-game | 0 | 1 | ```\nclass Solution:\n def xorGame(self, nums):\n #create a variable 0 \n x = 0 \n #iterate over the elements in the nums\n for i in nums:\n #do xor of all the elements\n x ^= i \n #Alice wins in two situations :\n #1.if the xor is already 0 (x == 0 )\n... | 0 | You are given an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
Simple python code with explanation | chalkboard-xor-game | 0 | 1 | ```\nclass Solution:\n def xorGame(self, nums):\n #create a variable 0 \n x = 0 \n #iterate over the elements in the nums\n for i in nums:\n #do xor of all the elements\n x ^= i \n #Alice wins in two situations :\n #1.if the xor is already 0 (x == 0 )\n... | 0 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
Most Ever Easy Solution 🔥🔥🔥 | subdomain-visit-count | 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 | A website domain `"discuss.leetcode.com "` consists of various subdomains. At the top level, we have `"com "`, at the next level, we have `"leetcode.com "` and at the lowest level, `"discuss.leetcode.com "`. When we visit a domain like `"discuss.leetcode.com "`, we will also visit the parent domains `"leetcode.com "` a... | null |
Most Ever Easy Solution 🔥🔥🔥 | subdomain-visit-count | 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 _the number of ways you can write_ `n` _as the sum of consecutive positive integers._
**Example 1:**
**Input:** n = 5
**Output:** 2
**Explanation:** 5 = 2 + 3
**Example 2:**
**Input:** n = 9
**Output:** 3
**Explanation:** 9 = 4 + 5 = 2 + 3 + 4
**Example 3:**
**Input:** n = 15
**Output... | null |
Solution | subdomain-visit-count | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<string> subdomainVisits(vector<string>& cp) { \n unordered_map<string,int>mpp;\n vector<string>res;\n int n = cp.size();\n for(int i = 0; i < n; ++i)\n { \n int j = cp[i].size() - 1;\n string temp;\n i... | 2 | A website domain `"discuss.leetcode.com "` consists of various subdomains. At the top level, we have `"com "`, at the next level, we have `"leetcode.com "` and at the lowest level, `"discuss.leetcode.com "`. When we visit a domain like `"discuss.leetcode.com "`, we will also visit the parent domains `"leetcode.com "` a... | null |
Solution | subdomain-visit-count | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<string> subdomainVisits(vector<string>& cp) { \n unordered_map<string,int>mpp;\n vector<string>res;\n int n = cp.size();\n for(int i = 0; i < n; ++i)\n { \n int j = cp[i].size() - 1;\n string temp;\n i... | 2 | Given an integer `n`, return _the number of ways you can write_ `n` _as the sum of consecutive positive integers._
**Example 1:**
**Input:** n = 5
**Output:** 2
**Explanation:** 5 = 2 + 3
**Example 2:**
**Input:** n = 9
**Output:** 3
**Explanation:** 9 = 4 + 5 = 2 + 3 + 4
**Example 3:**
**Input:** n = 15
**Output... | null |
Python3 Dictionary 90% faster | subdomain-visit-count | 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 | A website domain `"discuss.leetcode.com "` consists of various subdomains. At the top level, we have `"com "`, at the next level, we have `"leetcode.com "` and at the lowest level, `"discuss.leetcode.com "`. When we visit a domain like `"discuss.leetcode.com "`, we will also visit the parent domains `"leetcode.com "` a... | null |
Python3 Dictionary 90% faster | subdomain-visit-count | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | Given an integer `n`, return _the number of ways you can write_ `n` _as the sum of consecutive positive integers._
**Example 1:**
**Input:** n = 5
**Output:** 2
**Explanation:** 5 = 2 + 3
**Example 2:**
**Input:** n = 9
**Output:** 3
**Explanation:** 9 = 4 + 5 = 2 + 3 + 4
**Example 3:**
**Input:** n = 15
**Output... | null |
Python3 100% faster, 100% less memory (32ms, 14.1mb) | subdomain-visit-count | 0 | 1 | ```\nclass Solution:\n def subdomainVisits(self, cpdomains: List[str]) -> List[str]:\n d = defaultdict(int)\n for s in cpdomains:\n cnt, s = s.split()\n cnt = int(cnt)\n d[s] += cnt\n pos = s.find(\'.\') + 1\n while pos > 0:\n d[s[po... | 10 | A website domain `"discuss.leetcode.com "` consists of various subdomains. At the top level, we have `"com "`, at the next level, we have `"leetcode.com "` and at the lowest level, `"discuss.leetcode.com "`. When we visit a domain like `"discuss.leetcode.com "`, we will also visit the parent domains `"leetcode.com "` a... | null |
Python3 100% faster, 100% less memory (32ms, 14.1mb) | subdomain-visit-count | 0 | 1 | ```\nclass Solution:\n def subdomainVisits(self, cpdomains: List[str]) -> List[str]:\n d = defaultdict(int)\n for s in cpdomains:\n cnt, s = s.split()\n cnt = int(cnt)\n d[s] += cnt\n pos = s.find(\'.\') + 1\n while pos > 0:\n d[s[po... | 10 | Given an integer `n`, return _the number of ways you can write_ `n` _as the sum of consecutive positive integers._
**Example 1:**
**Input:** n = 5
**Output:** 2
**Explanation:** 5 = 2 + 3
**Example 2:**
**Input:** n = 9
**Output:** 3
**Explanation:** 9 = 4 + 5 = 2 + 3 + 4
**Example 3:**
**Input:** n = 15
**Output... | null |
Solution | largest-triangle-area | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n double largestTriangleArea(vector<vector<int>>& points) {\n double maxarea=INT_MIN;\n double area=0;\n int i,j,k;\n double x1,x2,y1,y2,x3,y3;\n int n=points.size();\n for(i=0;i<n;i++)\n {\n x1=points[i][0];\n ... | 1 | Given an array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
Solution | largest-triangle-area | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n double largestTriangleArea(vector<vector<int>>& points) {\n double maxarea=INT_MIN;\n double area=0;\n int i,j,k;\n double x1,x2,y1,y2,x3,y3;\n int n=points.size();\n for(i=0;i<n;i++)\n {\n x1=points[i][0];\n ... | 1 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
Python || Faster than 93% || Simple maths || with explanation | largest-triangle-area | 0 | 1 | I used my highschool determinants formula of area of a triangle\n if coordinates are given\n 1/2(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))\n Explanation link: https://www.cuemath.com/geometry/area-of-triangle-in-coordinate-geometry/\n```\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) ->... | 31 | Given an array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
Python || Faster than 93% || Simple maths || with explanation | largest-triangle-area | 0 | 1 | I used my highschool determinants formula of area of a triangle\n if coordinates are given\n 1/2(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))\n Explanation link: https://www.cuemath.com/geometry/area-of-triangle-in-coordinate-geometry/\n```\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) ->... | 31 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
Only one line on Python | largest-triangle-area | 0 | 1 | \n# Code\n```\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) -> float:\n\n \n return max(abs(0.5*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))) for [x1,y1], [x2,y2], [x3,y3] in combinations(points, 3))\n\n\n\n\n\n\n``` | 2 | Given an array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
Only one line on Python | largest-triangle-area | 0 | 1 | \n# Code\n```\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) -> float:\n\n \n return max(abs(0.5*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))) for [x1,y1], [x2,y2], [x3,y3] in combinations(points, 3))\n\n\n\n\n\n\n``` | 2 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
Python Solution 99% Faster | largest-triangle-area | 0 | 1 | Using the coordinate geometry area of triangle formula:\nhttps://www.brainkart.com/article/Area-of-a-Triangle_39390/\n\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) -> float:\n \n area = 0\n \n for i in range(len(points)-2):\n x1,y1 = points[i]\n ... | 3 | Given an array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
Python Solution 99% Faster | largest-triangle-area | 0 | 1 | Using the coordinate geometry area of triangle formula:\nhttps://www.brainkart.com/article/Area-of-a-Triangle_39390/\n\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) -> float:\n \n area = 0\n \n for i in range(len(points)-2):\n x1,y1 = points[i]\n ... | 3 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
Python 1 line solution | largest-triangle-area | 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 array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
Python 1 line solution | largest-triangle-area | 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 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
812: Solution with step by step explanation | largest-triangle-area | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWithin the largest Triangle Area method, we define a helper function triangle_area which takes three points and calculates the area of the triangle formed by these points using the Shoelace formula. The formula is based on t... | 0 | Given an array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
812: Solution with step by step explanation | largest-triangle-area | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWithin the largest Triangle Area method, we define a helper function triangle_area which takes three points and calculates the area of the triangle formed by these points using the Shoelace formula. The formula is based on t... | 0 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
Trivial af | largest-triangle-area | 0 | 1 | # Intuition\nbasic stuff frr\n\n# Code\n```\nSolution = type.__new__(type, "Solution", (), {"largestTriangleArea": lambda self, points : max(0.5*abs(a[0]*(b[1]-c[1])+b[0]*(c[1]-a[1])+c[0]*(a[1]-b[1])) for a, b, c in __import__("itertools").combinations(points, 3))})\n \n``` | 0 | Given an array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
Trivial af | largest-triangle-area | 0 | 1 | # Intuition\nbasic stuff frr\n\n# Code\n```\nSolution = type.__new__(type, "Solution", (), {"largestTriangleArea": lambda self, points : max(0.5*abs(a[0]*(b[1]-c[1])+b[0]*(c[1]-a[1])+c[0]*(a[1]-b[1])) for a, b, c in __import__("itertools").combinations(points, 3))})\n \n``` | 0 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
[python3] fast and simple | largest-triangle-area | 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 array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
[python3] fast and simple | largest-triangle-area | 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 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
Readable solutions | largest-triangle-area | 0 | 1 | # Code\n```\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) -> float:\n # 110ms\n # Beats 46.44% of users with Python3\n ret, n = 0, len(points)\n for i in range(n):\n for j in range(i+1, n):\n for k in range(j+1, n):\n ... | 0 | Given an array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
Readable solutions | largest-triangle-area | 0 | 1 | # Code\n```\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) -> float:\n # 110ms\n # Beats 46.44% of users with Python3\n ret, n = 0, len(points)\n for i in range(n):\n for j in range(i+1, n):\n for k in range(j+1, n):\n ... | 0 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
Simple with unit tests | largest-triangle-area | 0 | 1 | ```\nfrom typing import List\n\n\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) -> float:\n """\n Calculate the surface of the largest triangle from 3 points on 2-D plain from the list.\n\n Triangle size is height/2 * b. Height is perpendicular to on the b side and tou... | 0 | Given an array of points on the **X-Y** plane `points` where `points[i] = [xi, yi]`, return _the area of the largest triangle that can be formed by any three different points_. Answers within `10-5` of the actual answer will be accepted.
**Example 1:**
**Input:** points = \[\[0,0\],\[0,1\],\[1,0\],\[0,2\],\[2,0\]\]
*... | null |
Simple with unit tests | largest-triangle-area | 0 | 1 | ```\nfrom typing import List\n\n\nclass Solution:\n def largestTriangleArea(self, points: List[List[int]]) -> float:\n """\n Calculate the surface of the largest triangle from 3 points on 2-D plain from the list.\n\n Triangle size is height/2 * b. Height is perpendicular to on the b side and tou... | 0 | In a string `s` of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like `s = "abbxxxxzyy "` has the groups `"a "`, `"bb "`, `"xxxx "`, `"z "`, and `"yy "`.
A group is identified by an interval `[start, end]`, where `start` and `end` denote the start and end indice... | null |
Dynamic Programming. Super easy to understand. Python Solution. | largest-sum-of-averages | 0 | 1 | # Intuition\nRecursively parition at each index and call the function with the next index i.e j ( We do this to find maximum average in each parition)\n\nIf we have only one more partition left, it means to calculate the average for the rest of the elements \n\nWhy?\n\nIn each parition we divide the array into 2\n\n**F... | 1 | You are given an integer array `nums` and an integer `k`. You can partition the array into **at most** `k` non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray.
Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.
... | null |
Dynamic Programming. Super easy to understand. Python Solution. | largest-sum-of-averages | 0 | 1 | # Intuition\nRecursively parition at each index and call the function with the next index i.e j ( We do this to find maximum average in each parition)\n\nIf we have only one more partition left, it means to calculate the average for the rest of the elements \n\nWhy?\n\nIn each parition we divide the array into 2\n\n**F... | 1 | You are given a personal information string `s`, representing either an **email address** or a **phone number**. Return _the **masked** personal information using the below rules_.
**Email address:**
An email address is:
* A **name** consisting of uppercase and lowercase English letters, followed by
* The `'@'` ... | null |
Solution | largest-sum-of-averages | 1 | 1 | ```C++ []\n#define inf -100000.0\n\nclass Solution {\npublic:\n double large_sum(vector<vector<double>> &dp,vector<int> & nums,int i, int k ){\n if(i>=nums.size()){\n return 0.0;\n }\n if(k<=0){\n return inf;\n }\n if(dp[i][k]!=-1){\n return dp[i][k... | 1 | You are given an integer array `nums` and an integer `k`. You can partition the array into **at most** `k` non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray.
Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.
... | null |
Solution | largest-sum-of-averages | 1 | 1 | ```C++ []\n#define inf -100000.0\n\nclass Solution {\npublic:\n double large_sum(vector<vector<double>> &dp,vector<int> & nums,int i, int k ){\n if(i>=nums.size()){\n return 0.0;\n }\n if(k<=0){\n return inf;\n }\n if(dp[i][k]!=-1){\n return dp[i][k... | 1 | You are given a personal information string `s`, representing either an **email address** or a **phone number**. Return _the **masked** personal information using the below rules_.
**Email address:**
An email address is:
* A **name** consisting of uppercase and lowercase English letters, followed by
* The `'@'` ... | null |
70% TC and 56% SC easy python solution | largest-sum-of-averages | 0 | 1 | ```\ndef largestSumOfAverages(self, nums: List[int], k: int) -> float:\n\tn = len(nums)\n\tpre = [0]\n\tfor i in nums:\n\t\tpre.append(pre[-1] + i)\n\tdef solve(i, k):\n\t\tif(k == 1): return sum(nums[i:])/(n-i)\n\n\t\tif(n-i < k): return -1\n\n\t\tif((i, k) in d): return d[(i, k)]\n\n\t\ttemp = 0\n\t\tfor j in range(i... | 1 | You are given an integer array `nums` and an integer `k`. You can partition the array into **at most** `k` non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray.
Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.
... | null |
70% TC and 56% SC easy python solution | largest-sum-of-averages | 0 | 1 | ```\ndef largestSumOfAverages(self, nums: List[int], k: int) -> float:\n\tn = len(nums)\n\tpre = [0]\n\tfor i in nums:\n\t\tpre.append(pre[-1] + i)\n\tdef solve(i, k):\n\t\tif(k == 1): return sum(nums[i:])/(n-i)\n\n\t\tif(n-i < k): return -1\n\n\t\tif((i, k) in d): return d[(i, k)]\n\n\t\ttemp = 0\n\t\tfor j in range(i... | 1 | You are given a personal information string `s`, representing either an **email address** or a **phone number**. Return _the **masked** personal information using the below rules_.
**Email address:**
An email address is:
* A **name** consisting of uppercase and lowercase English letters, followed by
* The `'@'` ... | null |
python || dynamic programming || faster than 99% | largest-sum-of-averages | 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. -->\nDynamic Programming\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n * k)\n\n- Space complexity:\n<!-- Add your space co... | 0 | You are given an integer array `nums` and an integer `k`. You can partition the array into **at most** `k` non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray.
Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.
... | null |
python || dynamic programming || faster than 99% | largest-sum-of-averages | 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. -->\nDynamic Programming\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n * k)\n\n- Space complexity:\n<!-- Add your space co... | 0 | You are given a personal information string `s`, representing either an **email address** or a **phone number**. Return _the **masked** personal information using the below rules_.
**Email address:**
An email address is:
* A **name** consisting of uppercase and lowercase English letters, followed by
* The `'@'` ... | null |
Similar to traditional dp problem | largest-sum-of-averages | 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 an integer array `nums` and an integer `k`. You can partition the array into **at most** `k` non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray.
Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.
... | null |
Similar to traditional dp problem | largest-sum-of-averages | 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 a personal information string `s`, representing either an **email address** or a **phone number**. Return _the **masked** personal information using the below rules_.
**Email address:**
An email address is:
* A **name** consisting of uppercase and lowercase English letters, followed by
* The `'@'` ... | null |
813: Beats 96.31%, Solution with step by step explanation | largest-sum-of-averages | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n n = len(nums)\n```\n\nWe store the length of nums in n, which will be used frequently.\n\n```\n prefix_sum = [0] * (n + 1)\n for i in range(n):\n prefix_sum[i + 1] = prefix_sum[i] + nums... | 0 | You are given an integer array `nums` and an integer `k`. You can partition the array into **at most** `k` non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray.
Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.
... | null |
813: Beats 96.31%, Solution with step by step explanation | largest-sum-of-averages | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n n = len(nums)\n```\n\nWe store the length of nums in n, which will be used frequently.\n\n```\n prefix_sum = [0] * (n + 1)\n for i in range(n):\n prefix_sum[i + 1] = prefix_sum[i] + nums... | 0 | You are given a personal information string `s`, representing either an **email address** or a **phone number**. Return _the **masked** personal information using the below rules_.
**Email address:**
An email address is:
* A **name** consisting of uppercase and lowercase English letters, followed by
* The `'@'` ... | null |
Short DP solution beats 99% | largest-sum-of-averages | 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 an integer array `nums` and an integer `k`. You can partition the array into **at most** `k` non-empty adjacent subarrays. The **score** of a partition is the sum of the averages of each subarray.
Note that the partition must use every integer in `nums`, and that the score is not necessarily an integer.
... | null |
Short DP solution beats 99% | largest-sum-of-averages | 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 a personal information string `s`, representing either an **email address** or a **phone number**. Return _the **masked** personal information using the below rules_.
**Email address:**
An email address is:
* A **name** consisting of uppercase and lowercase English letters, followed by
* The `'@'` ... | null |
Simple solution with Binary Tree in Python3 | binary-tree-pruning | 0 | 1 | # Intuition\nHere we have:\n- a **Binary Tree** `root`\n- our goal is to remove **all** subtrees, that consist **only** with `0`\n\nAn algorithm is simple:\n- traverse over tree\n- for each node check if it has leaves and they\'re **both** `0` or don\'t exist, than **remove** this node from `root`\n- otherwise check, *... | 1 | Given the `root` of a binary tree, return _the same tree where every subtree (of the given tree) not containing a_ `1` _has been removed_.
A subtree of a node `node` is `node` plus every node that is a descendant of `node`.
**Example 1:**
**Input:** root = \[1,null,0,0,1\]
**Output:** \[1,null,0,null,1\]
**Explanati... | null |
Simple solution with Binary Tree in Python3 | binary-tree-pruning | 0 | 1 | # Intuition\nHere we have:\n- a **Binary Tree** `root`\n- our goal is to remove **all** subtrees, that consist **only** with `0`\n\nAn algorithm is simple:\n- traverse over tree\n- for each node check if it has leaves and they\'re **both** `0` or don\'t exist, than **remove** this node from `root`\n- otherwise check, *... | 1 | Given an `n x n` binary matrix `image`, flip the image **horizontally**, then invert it, and return _the resulting image_.
To flip an image horizontally means that each row of the image is reversed.
* For example, flipping `[1,1,0]` horizontally results in `[0,1,1]`.
To invert an image means that each `0` is repla... | null |
Solution | binary-tree-pruning | 1 | 1 | ```C++ []\nclass Solution {\n public:\n TreeNode* pruneTree(TreeNode* root) {\n if (root == nullptr)\n return nullptr;\n root->left = pruneTree(root->left);\n root->right = pruneTree(root->right);\n if (root->left == nullptr && root->right == nullptr && root->val == 0)\n return nullptr;\n retu... | 1 | Given the `root` of a binary tree, return _the same tree where every subtree (of the given tree) not containing a_ `1` _has been removed_.
A subtree of a node `node` is `node` plus every node that is a descendant of `node`.
**Example 1:**
**Input:** root = \[1,null,0,0,1\]
**Output:** \[1,null,0,null,1\]
**Explanati... | null |
Solution | binary-tree-pruning | 1 | 1 | ```C++ []\nclass Solution {\n public:\n TreeNode* pruneTree(TreeNode* root) {\n if (root == nullptr)\n return nullptr;\n root->left = pruneTree(root->left);\n root->right = pruneTree(root->right);\n if (root->left == nullptr && root->right == nullptr && root->val == 0)\n return nullptr;\n retu... | 1 | Given an `n x n` binary matrix `image`, flip the image **horizontally**, then invert it, and return _the resulting image_.
To flip an image horizontally means that each row of the image is reversed.
* For example, flipping `[1,1,0]` horizontally results in `[0,1,1]`.
To invert an image means that each `0` is repla... | null |
Python | Recursive Postorder | binary-tree-pruning | 0 | 1 | ```python
class Solution:
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return None
if self.pruneTree(root.left) is None:
root.left = None
if self.pruneTree(root.right) is None:
root.right = None
... | 3 | Given the `root` of a binary tree, return _the same tree where every subtree (of the given tree) not containing a_ `1` _has been removed_.
A subtree of a node `node` is `node` plus every node that is a descendant of `node`.
**Example 1:**
**Input:** root = \[1,null,0,0,1\]
**Output:** \[1,null,0,null,1\]
**Explanati... | null |
Python | Recursive Postorder | binary-tree-pruning | 0 | 1 | ```python
class Solution:
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return None
if self.pruneTree(root.left) is None:
root.left = None
if self.pruneTree(root.right) is None:
root.right = None
... | 3 | Given an `n x n` binary matrix `image`, flip the image **horizontally**, then invert it, and return _the resulting image_.
To flip an image horizontally means that each row of the image is reversed.
* For example, flipping `[1,1,0]` horizontally results in `[0,1,1]`.
To invert an image means that each `0` is repla... | null |
Python3 Solution | bus-routes | 0 | 1 | \n```\nclass Solution:\n def numBusesToDestination(self, routes: List[List[int]], source: int, target: int) -> int:\n if source==target:\n return 0\n graph=defaultdict(set)\n for routes_id,stops in enumerate(routes):\n for stop in stops:\n graph[stop].add(rou... | 4 | You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
Python3 Solution | bus-routes | 0 | 1 | \n```\nclass Solution:\n def numBusesToDestination(self, routes: List[List[int]], source: int, target: int) -> int:\n if source==target:\n return 0\n graph=defaultdict(set)\n for routes_id,stops in enumerate(routes):\n for stop in stops:\n graph[stop].add(rou... | 4 | You are given a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
✅ Beats 100% - Explained With [ Video ] - Modified Bellman Ford - Visualized Too | bus-routes | 1 | 1 | \n\n# YouTube Video Explanation:\n\n\n[https://youtu.be/uvAuQVvGBts](https://youtu.be/uvAuQVvGBts)\n**\uD83D\uDD25 Please like, share, and subscribe to support our channel\'s mission o... | 104 | You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
✅ Beats 100% - Explained With [ Video ] - Modified Bellman Ford - Visualized Too | bus-routes | 1 | 1 | \n\n# YouTube Video Explanation:\n\n\n[https://youtu.be/uvAuQVvGBts](https://youtu.be/uvAuQVvGBts)\n**\uD83D\uDD25 Please like, share, and subscribe to support our channel\'s mission o... | 104 | You are given a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
【Video】Give me 10 minutes - Beats 99.53% - How we think about a solution | bus-routes | 1 | 1 | # Intuition\nKeep track of route relation.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/LcJYpE92UHs\n\n\u25A0 Timeline of the video\n\n`0:03` Explain difficulty and a key point of the question\n`1:31` Demonstrate how it works\n`4:08` Coding\n`10:15` Time Complexity and Space Complexity\n\n### \u2B50\uFE0F\u2B50\uFE0F... | 50 | You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
【Video】Give me 10 minutes - Beats 99.53% - How we think about a solution | bus-routes | 1 | 1 | # Intuition\nKeep track of route relation.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/LcJYpE92UHs\n\n\u25A0 Timeline of the video\n\n`0:03` Explain difficulty and a key point of the question\n`1:31` Demonstrate how it works\n`4:08` Coding\n`10:15` Time Complexity and Space Complexity\n\n### \u2B50\uFE0F\u2B50\uFE0F... | 50 | You are given a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
JS/Ts and Pythong | bus-routes | 0 | 1 | # JS/TS\n```\nfunction numBusesToDestination(routes: number[][], s: number, t: number): number {\n const buses = routes.length\n const mp = {}\n for(let i =0;i<buses;i++){\n for(let j =0;j<routes[i].length;j++){\n let stop = routes[i][j]\n let list = mp[stop]?mp[stop]:[]\n ... | 1 | You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
JS/Ts and Pythong | bus-routes | 0 | 1 | # JS/TS\n```\nfunction numBusesToDestination(routes: number[][], s: number, t: number): number {\n const buses = routes.length\n const mp = {}\n for(let i =0;i<buses;i++){\n for(let j =0;j<routes[i].length;j++){\n let stop = routes[i][j]\n let list = mp[stop]?mp[stop]:[]\n ... | 1 | You are given a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
Python Approach | bus-routes | 0 | 1 | # Code\n```\nclass Solution(object):\n def numBusesToDestination(self, routes, source, target):\n if source == target:\n return 0\n\n max_stop = max(max(route) for route in routes)\n if max_stop < target:\n return -1\n\n n = len(routes)\n min_buses_to_reach = ... | 1 | You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
Python Approach | bus-routes | 0 | 1 | # Code\n```\nclass Solution(object):\n def numBusesToDestination(self, routes, source, target):\n if source == target:\n return 0\n\n max_stop = max(max(route) for route in routes)\n if max_stop < target:\n return -1\n\n n = len(routes)\n min_buses_to_reach = ... | 1 | You are given a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
shortest path between routes | bus-routes | 0 | 1 | A possible solution includes the steps below. \n- minimum path to be found is between routes, so convert the problem to bi-directional graph between routes. \n- convert source to list of routes to start from. \n- run djkshtra on number of buses exchanged in between. \n- exit when you land on one of the routes containin... | 1 | You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
shortest path between routes | bus-routes | 0 | 1 | A possible solution includes the steps below. \n- minimum path to be found is between routes, so convert the problem to bi-directional graph between routes. \n- convert source to list of routes to start from. \n- run djkshtra on number of buses exchanged in between. \n- exit when you land on one of the routes containin... | 1 | You are given a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
✅☑[C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥 | bus-routes | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(BFS with bus stop as nodes)***\n1. **BFS Approach:**\n\n - The code uses Breadth-First Search (BFS) to find the minimum number of buses needed to reach the target from the source.\n1. **Adjacency List:**\n\n... | 12 | You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
✅☑[C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥 | bus-routes | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(BFS with bus stop as nodes)***\n1. **BFS Approach:**\n\n - The code uses Breadth-First Search (BFS) to find the minimum number of buses needed to reach the target from the source.\n1. **Adjacency List:**\n\n... | 12 | You are given a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
ez solution | bus-routes | 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 an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
ez solution | bus-routes | 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 a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
python3 BFS | bus-routes | 0 | 1 | ```\nclass Solution:\n def numBusesToDestination(self, routes: List[List[int]], source: int, target: int) -> int:\n if source == target: return 0\n adj = defaultdict(set)\n \n for bus,locations in enumerate(routes):\n for location in locations:\n adj[location].ad... | 2 | You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
python3 BFS | bus-routes | 0 | 1 | ```\nclass Solution:\n def numBusesToDestination(self, routes: List[List[int]], source: int, target: int) -> int:\n if source == target: return 0\n adj = defaultdict(set)\n \n for bus,locations in enumerate(routes):\n for location in locations:\n adj[location].ad... | 2 | You are given a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
Python3 solution beats 100% | bus-routes | 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 an array `routes` representing bus routes where `routes[i]` is a bus route that the `ith` bus repeats forever.
* For example, if `routes[0] = [1, 5, 7]`, this means that the `0th` bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
You will start at the bus stop `source` (You... | null |
Python3 solution beats 100% | bus-routes | 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 a **0-indexed** string `s` that you must perform `k` replacement operations on. The replacement operations are given as three **0-indexed** parallel arrays, `indices`, `sources`, and `targets`, all of length `k`.
To complete the `ith` replacement operation:
1. Check if the **substring** `sources[i]` oc... | null |
Solution | ambiguous-coordinates | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<string> ambiguousCoordinates(string s) {\n string cur1 = "";\n string cur2 = "";\n vector<string> res;\n bool confirmedFirstPart = false;\n bool usedDot = false;\n backtrack(s, cur1, cur2, res, 0, confirmedFirstPart, usedDot);\n ... | 1 | We had some 2-dimensional coordinates, like `"(1, 3) "` or `"(2, 0.5) "`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
* For example, `"(1, 3) "` becomes `s = "(13) "` and `"(2, 0.5) "` becomes `s = "(205) "`.
Return _a list of strings representing all possibilities for wh... | null |
Solution | ambiguous-coordinates | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<string> ambiguousCoordinates(string s) {\n string cur1 = "";\n string cur2 = "";\n vector<string> res;\n bool confirmedFirstPart = false;\n bool usedDot = false;\n backtrack(s, cur1, cur2, res, 0, confirmedFirstPart, usedDot);\n ... | 1 | There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.
Return an array `answer` of length `n` where `answer[i]` is the s... | null |
[Python3] valid numbers | ambiguous-coordinates | 0 | 1 | Algo\nThe key is to deal with extraneous zeros. Below rule gives the what\'s required\n1) if a string has length 1, return it;\n2) if a string with length larger than 1 and starts and ends with 0, it cannot contain valid number;\n3) if a string starts with 0, return `0.xxx`;\n4) if a string ends with 0, return `xxx0`;\... | 9 | We had some 2-dimensional coordinates, like `"(1, 3) "` or `"(2, 0.5) "`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
* For example, `"(1, 3) "` becomes `s = "(13) "` and `"(2, 0.5) "` becomes `s = "(205) "`.
Return _a list of strings representing all possibilities for wh... | null |
[Python3] valid numbers | ambiguous-coordinates | 0 | 1 | Algo\nThe key is to deal with extraneous zeros. Below rule gives the what\'s required\n1) if a string has length 1, return it;\n2) if a string with length larger than 1 and starts and ends with 0, it cannot contain valid number;\n3) if a string starts with 0, return `0.xxx`;\n4) if a string ends with 0, return `xxx0`;\... | 9 | There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.
Return an array `answer` of length `n` where `answer[i]` is the s... | null |
Why the downvotes? Clean code - Python/Java | ambiguous-coordinates | 1 | 1 | > # Intuition\n\nThis question definitely doesn\'t deserve so many downvotes. The edge cases may seem bad initially, but in code it\'s only a couple `if elif` conditionals to make this pass.\n\nMy initial thoughts with this question were [Leetcode 22](https://leetcode.com/problems/generate-parentheses/description/). Bu... | 0 | We had some 2-dimensional coordinates, like `"(1, 3) "` or `"(2, 0.5) "`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
* For example, `"(1, 3) "` becomes `s = "(13) "` and `"(2, 0.5) "` becomes `s = "(205) "`.
Return _a list of strings representing all possibilities for wh... | null |
Why the downvotes? Clean code - Python/Java | ambiguous-coordinates | 1 | 1 | > # Intuition\n\nThis question definitely doesn\'t deserve so many downvotes. The edge cases may seem bad initially, but in code it\'s only a couple `if elif` conditionals to make this pass.\n\nMy initial thoughts with this question were [Leetcode 22](https://leetcode.com/problems/generate-parentheses/description/). Bu... | 0 | There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.
Return an array `answer` of length `n` where `answer[i]` is the s... | null |
✅simple iterative solution || python | ambiguous-coordinates | 0 | 1 | \n# Code\n```\nclass Solution:\n\n def check(self,s):\n i=0\n j=len(s)-1\n if(s[i]==\'(\'):i+=1\n elif(s[j]==\')\'):j-=1\n s=s[i:j+1]\n k=s.find(".")\n if(k!=-1):\n if(1<k and s[0]==\'0\'):return 0 \n if(s[len(s)-1]==\'0\'):return 0\n ... | 0 | We had some 2-dimensional coordinates, like `"(1, 3) "` or `"(2, 0.5) "`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
* For example, `"(1, 3) "` becomes `s = "(13) "` and `"(2, 0.5) "` becomes `s = "(205) "`.
Return _a list of strings representing all possibilities for wh... | null |
✅simple iterative solution || python | ambiguous-coordinates | 0 | 1 | \n# Code\n```\nclass Solution:\n\n def check(self,s):\n i=0\n j=len(s)-1\n if(s[i]==\'(\'):i+=1\n elif(s[j]==\')\'):j-=1\n s=s[i:j+1]\n k=s.find(".")\n if(k!=-1):\n if(1<k and s[0]==\'0\'):return 0 \n if(s[len(s)-1]==\'0\'):return 0\n ... | 0 | There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.
Return an array `answer` of length `n` where `answer[i]` is the s... | null |
[Python] Solution + explanation | ambiguous-coordinates | 0 | 1 | We can split this problem into two subproblems:\n1. Get all the possible values for coordinates substring (`102` -> `[1.02, 10.2, 102]`, `00` -> `[]`)\n2. Get all the possible combinations for two coordinates substrings\n\nFor the first subproblem, refer to `variants` function. It should be self-explanatory: we iterate... | 0 | We had some 2-dimensional coordinates, like `"(1, 3) "` or `"(2, 0.5) "`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
* For example, `"(1, 3) "` becomes `s = "(13) "` and `"(2, 0.5) "` becomes `s = "(205) "`.
Return _a list of strings representing all possibilities for wh... | null |
[Python] Solution + explanation | ambiguous-coordinates | 0 | 1 | We can split this problem into two subproblems:\n1. Get all the possible values for coordinates substring (`102` -> `[1.02, 10.2, 102]`, `00` -> `[]`)\n2. Get all the possible combinations for two coordinates substrings\n\nFor the first subproblem, refer to `variants` function. It should be self-explanatory: we iterate... | 0 | There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.
Return an array `answer` of length `n` where `answer[i]` is the s... | null |
Python3 - Ye Olde One Liner | ambiguous-coordinates | 0 | 1 | # Intuition\nCuz why not?\n\n# Code\n```\nclass Solution:\n def ambiguousCoordinates(self, s: str) -> List[str]:\n return [f"({x}, {y})" for d in [lambda n: [x for x in [f"{n[:i]}.{n[i:]}".strip(\'.\') for i in range(1, len(n)+1)] if (\'.\' not in x or x[-1] != \'0\') and x[:2] != \'00\' and (x[0] != \'0\' o... | 0 | We had some 2-dimensional coordinates, like `"(1, 3) "` or `"(2, 0.5) "`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
* For example, `"(1, 3) "` becomes `s = "(13) "` and `"(2, 0.5) "` becomes `s = "(205) "`.
Return _a list of strings representing all possibilities for wh... | null |
Python3 - Ye Olde One Liner | ambiguous-coordinates | 0 | 1 | # Intuition\nCuz why not?\n\n# Code\n```\nclass Solution:\n def ambiguousCoordinates(self, s: str) -> List[str]:\n return [f"({x}, {y})" for d in [lambda n: [x for x in [f"{n[:i]}.{n[i:]}".strip(\'.\') for i in range(1, len(n)+1)] if (\'.\' not in x or x[-1] != \'0\') and x[:2] != \'00\' and (x[0] != \'0\' o... | 0 | There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.
Return an array `answer` of length `n` where `answer[i]` is the s... | null |
Python3 🐍 concise solution beats 99% | ambiguous-coordinates | 0 | 1 | # Algo\nThe key is to deal with extraneous zeros. Below rule gives the what\'s required\n\n1. if a string has length 1, return it;\n2. if a string with length larger than 1 and starts and ends with 0, it cannot contain valid number;\n3. if a string starts with 0, return 0.xxx;\n4. if a string ends with 0, return xxx0;\... | 0 | We had some 2-dimensional coordinates, like `"(1, 3) "` or `"(2, 0.5) "`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
* For example, `"(1, 3) "` becomes `s = "(13) "` and `"(2, 0.5) "` becomes `s = "(205) "`.
Return _a list of strings representing all possibilities for wh... | null |
Python3 🐍 concise solution beats 99% | ambiguous-coordinates | 0 | 1 | # Algo\nThe key is to deal with extraneous zeros. Below rule gives the what\'s required\n\n1. if a string has length 1, return it;\n2. if a string with length larger than 1 and starts and ends with 0, it cannot contain valid number;\n3. if a string starts with 0, return 0.xxx;\n4. if a string ends with 0, return xxx0;\... | 0 | There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.
Return an array `answer` of length `n` where `answer[i]` is the s... | null |
Python3 iterative with regex | ambiguous-coordinates | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n- First, split the input into two numbers with a comma at every valid index. \n - e.g. `1234 -> (1, 234), (12, 34), (1,234)`\n- For each of those pairs of numbers, further split each number with a period at every valid index.\n - e.g. `(12,34) -... | 0 | We had some 2-dimensional coordinates, like `"(1, 3) "` or `"(2, 0.5) "`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
* For example, `"(1, 3) "` becomes `s = "(13) "` and `"(2, 0.5) "` becomes `s = "(205) "`.
Return _a list of strings representing all possibilities for wh... | null |
Python3 iterative with regex | ambiguous-coordinates | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n- First, split the input into two numbers with a comma at every valid index. \n - e.g. `1234 -> (1, 234), (12, 34), (1,234)`\n- For each of those pairs of numbers, further split each number with a period at every valid index.\n - e.g. `(12,34) -... | 0 | There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.
Return an array `answer` of length `n` where `answer[i]` is the s... | null |
Python | Clear explained | Clean | ambiguous-coordinates | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDivide it into sub problems:\n - split left hand side and right hand side (123 | 456)\n - split the \'.\' (123, 1.23, 12.3)\n# Approach\n<!-- Describe your approach to solving the problem. -->\nNested for loops\n```\nfor (lhs, rhs):\n ... | 0 | We had some 2-dimensional coordinates, like `"(1, 3) "` or `"(2, 0.5) "`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
* For example, `"(1, 3) "` becomes `s = "(13) "` and `"(2, 0.5) "` becomes `s = "(205) "`.
Return _a list of strings representing all possibilities for wh... | null |
Python | Clear explained | Clean | ambiguous-coordinates | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDivide it into sub problems:\n - split left hand side and right hand side (123 | 456)\n - split the \'.\' (123, 1.23, 12.3)\n# Approach\n<!-- Describe your approach to solving the problem. -->\nNested for loops\n```\nfor (lhs, rhs):\n ... | 0 | There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
You are given the integer `n` and the array `edges` where `edges[i] = [ai, bi]` indicates that there is an edge between nodes `ai` and `bi` in the tree.
Return an array `answer` of length `n` where `answer[i]` is the s... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.