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 |
|---|---|---|---|---|---|---|---|
Easiest one line solution with explanation || Python | transpose-matrix | 0 | 1 | \n\nIn this code, the `zip(*matrix)` function is used to transpose the matrix. The `*` operator is used for unpacking the rows of the matrix, and `zip` combines the elements with the same index into tuples, effectively transposing the rows and columns. Finally, a list comprehension is used to convert the transposed tup... | 1 | Given a 2D integer array `matrix`, return _the **transpose** of_ `matrix`.
The **transpose** of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
**Example 1:**
**Input:** matrix = \[\[1,2,3\],\[4,5,6\],\[7,8,9\]\]
**Output:** \[\[1,4,7\],\[2,5,8\],\[3,6,9\]\]
**E... | null |
Easiest one line solution with explanation || Python | transpose-matrix | 0 | 1 | \n\nIn this code, the `zip(*matrix)` function is used to transpose the matrix. The `*` operator is used for unpacking the rows of the matrix, and `zip` combines the elements with the same index into tuples, effectively transposing the rows and columns. Finally, a list comprehension is used to convert the transposed tup... | 1 | Given an integer array `arr`, return _the number of distinct bitwise ORs of all the non-empty subarrays of_ `arr`.
The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A **subarray** is a contiguous non-empty sequence of elements ... | We don't need any special algorithms to do this. You just need to know what the transpose of a matrix looks like. Rows become columns and vice versa! |
🔥 Easy solution | JAVA | Python 3 🔥| | transpose-matrix | 1 | 1 | # Intuition\nTranspose Matrix\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Input Validation:\n\n 1. It assumes a non-empty input matrix with at least one row and one column.\n 1. It calculates the number of rows (rows) and columns (cols) in the input matrix.\n1. Creating ... | 1 | Given a 2D integer array `matrix`, return _the **transpose** of_ `matrix`.
The **transpose** of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
**Example 1:**
**Input:** matrix = \[\[1,2,3\],\[4,5,6\],\[7,8,9\]\]
**Output:** \[\[1,4,7\],\[2,5,8\],\[3,6,9\]\]
**E... | null |
🔥 Easy solution | JAVA | Python 3 🔥| | transpose-matrix | 1 | 1 | # Intuition\nTranspose Matrix\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Input Validation:\n\n 1. It assumes a non-empty input matrix with at least one row and one column.\n 1. It calculates the number of rows (rows) and columns (cols) in the input matrix.\n1. Creating ... | 1 | Given an integer array `arr`, return _the number of distinct bitwise ORs of all the non-empty subarrays of_ `arr`.
The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A **subarray** is a contiguous non-empty sequence of elements ... | We don't need any special algorithms to do this. You just need to know what the transpose of a matrix looks like. Rows become columns and vice versa! |
simple and advanced solution using java and python easy to understand with explanation | transpose-matrix | 1 | 1 | # Intuition\nThe goal is to transpose the given matrix, swapping rows and columns. The idea is to iterate through each element of the original matrix and place it in the corresponding position in the transposed matrix.\n\n# Approach\nInitialize an empty list transposed_matrix to store the transposed elements.\nIterate ... | 1 | Given a 2D integer array `matrix`, return _the **transpose** of_ `matrix`.
The **transpose** of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
**Example 1:**
**Input:** matrix = \[\[1,2,3\],\[4,5,6\],\[7,8,9\]\]
**Output:** \[\[1,4,7\],\[2,5,8\],\[3,6,9\]\]
**E... | null |
simple and advanced solution using java and python easy to understand with explanation | transpose-matrix | 1 | 1 | # Intuition\nThe goal is to transpose the given matrix, swapping rows and columns. The idea is to iterate through each element of the original matrix and place it in the corresponding position in the transposed matrix.\n\n# Approach\nInitialize an empty list transposed_matrix to store the transposed elements.\nIterate ... | 1 | Given an integer array `arr`, return _the number of distinct bitwise ORs of all the non-empty subarrays of_ `arr`.
The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A **subarray** is a contiguous non-empty sequence of elements ... | We don't need any special algorithms to do this. You just need to know what the transpose of a matrix looks like. Rows become columns and vice versa! |
Solution | binary-gap | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int binaryGap(int N) {\n int result = 0;\n int last = -1;\n for (int i = 0; i < 32; ++i) {\n if ((N >> i) & 1) {\n if (last != -1) {\n result = max(result, i - last);\n }\n last = i;... | 1 | Given a positive integer `n`, find and return _the **longest distance** between any two **adjacent**_ `1`_'s in the binary representation of_ `n`_. If there are no two adjacent_ `1`_'s, return_ `0`_._
Two `1`'s are **adjacent** if there are only `0`'s separating them (possibly no `0`'s). The **distance** between two `... | null |
Solution | binary-gap | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int binaryGap(int N) {\n int result = 0;\n int last = -1;\n for (int i = 0; i < 32; ++i) {\n if ((N >> i) & 1) {\n if (last != -1) {\n result = max(result, i - last);\n }\n last = i;... | 1 | You are given a string `s` and an integer `k`. You can choose one of the first `k` letters of `s` and append it at the end of the string..
Return _the lexicographically smallest string you could have after applying the mentioned step any number of moves_.
**Example 1:**
**Input:** s = "cba ", k = 1
**Output:** "ac... | null |
Python Simple Solution II O(n) || One Pass | binary-gap | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def binaryGap(self, n: int) -> int:\n binary = bin(n)\n binary= binary[2:]\n found = False\n max_count =0\n for i in range(len(binary)):\n if(binary[i]==\'1\' and found ==False):\n start= i\n found = Tru... | 1 | Given a positive integer `n`, find and return _the **longest distance** between any two **adjacent**_ `1`_'s in the binary representation of_ `n`_. If there are no two adjacent_ `1`_'s, return_ `0`_._
Two `1`'s are **adjacent** if there are only `0`'s separating them (possibly no `0`'s). The **distance** between two `... | null |
Python Simple Solution II O(n) || One Pass | binary-gap | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def binaryGap(self, n: int) -> int:\n binary = bin(n)\n binary= binary[2:]\n found = False\n max_count =0\n for i in range(len(binary)):\n if(binary[i]==\'1\' and found ==False):\n start= i\n found = Tru... | 1 | You are given a string `s` and an integer `k`. You can choose one of the first `k` letters of `s` and append it at the end of the string..
Return _the lexicographically smallest string you could have after applying the mentioned step any number of moves_.
**Example 1:**
**Input:** s = "cba ", k = 1
**Output:** "ac... | null |
[Python3 Easy] | binary-gap | 0 | 1 | # Code\n```\nclass Solution:\n def binaryGap(self, n: int) -> int:\n \n ans = 0\n m = -1\n while n:\n if 1&n:\n if m==-1:\n m = 1\n else:\n ans = max(ans,m)\n m=1\n else:\n ... | 1 | Given a positive integer `n`, find and return _the **longest distance** between any two **adjacent**_ `1`_'s in the binary representation of_ `n`_. If there are no two adjacent_ `1`_'s, return_ `0`_._
Two `1`'s are **adjacent** if there are only `0`'s separating them (possibly no `0`'s). The **distance** between two `... | null |
[Python3 Easy] | binary-gap | 0 | 1 | # Code\n```\nclass Solution:\n def binaryGap(self, n: int) -> int:\n \n ans = 0\n m = -1\n while n:\n if 1&n:\n if m==-1:\n m = 1\n else:\n ans = max(ans,m)\n m=1\n else:\n ... | 1 | You are given a string `s` and an integer `k`. You can choose one of the first `k` letters of `s` and append it at the end of the string..
Return _the lexicographically smallest string you could have after applying the mentioned step any number of moves_.
**Example 1:**
**Input:** s = "cba ", k = 1
**Output:** "ac... | null |
Easy Solution Using Bin Function !! | binary-gap | 0 | 1 | \n\n# Approach\nFirst of all ,We have to convert the given integer into its equivalent binary string. Then we will store all the indexes of "1" in an array.\nThen we will calculate the difference between adjacent elements. After calculating the differences we will return the maximum difference ..\n\n# Complexity\n- Tim... | 1 | Given a positive integer `n`, find and return _the **longest distance** between any two **adjacent**_ `1`_'s in the binary representation of_ `n`_. If there are no two adjacent_ `1`_'s, return_ `0`_._
Two `1`'s are **adjacent** if there are only `0`'s separating them (possibly no `0`'s). The **distance** between two `... | null |
Easy Solution Using Bin Function !! | binary-gap | 0 | 1 | \n\n# Approach\nFirst of all ,We have to convert the given integer into its equivalent binary string. Then we will store all the indexes of "1" in an array.\nThen we will calculate the difference between adjacent elements. After calculating the differences we will return the maximum difference ..\n\n# Complexity\n- Tim... | 1 | You are given a string `s` and an integer `k`. You can choose one of the first `k` letters of `s` and append it at the end of the string..
Return _the lexicographically smallest string you could have after applying the mentioned step any number of moves_.
**Example 1:**
**Input:** s = "cba ", k = 1
**Output:** "ac... | null |
Python My Soln | reordered-power-of-2 | 0 | 1 | class Solution:\n\n def reorderedPowerOf2(self, n: int) -> bool:\n n1 = sorted(str(n))\n \n for i in range(30):\n res = sorted(str(2 ** i))\n if res == n1:\n return True\n \n \n return False | 4 | You are given an integer `n`. We reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return `true` _if and only if we can do this so that the resulting number is a power of two_.
**Example 1:**
**Input:** n = 1
**Output:** true
**Example 2:**
**Input:** n = 10
**... | null |
Python My Soln | reordered-power-of-2 | 0 | 1 | class Solution:\n\n def reorderedPowerOf2(self, n: int) -> bool:\n n1 = sorted(str(n))\n \n for i in range(30):\n res = sorted(str(2 ** i))\n if res == n1:\n return True\n \n \n return False | 4 | We can use run-length encoding (i.e., **RLE**) to encode a sequence of integers. In a run-length encoded array of even length `encoding` (**0-indexed**), for all even `i`, `encoding[i]` tells us the number of times that the non-negative integer value `encoding[i + 1]` is repeated in the sequence.
* For example, the ... | null |
python3 | easy understanding | sort | reordered-power-of-2 | 0 | 1 | ```\nclass Solution:\n def reorderedPowerOf2(self, n: int) -> bool:\n i, arr = 0, []\n v = 2**i\n while v <= 10**9: arr.append(sorted(str(v))); i+=1; v = 2**i\n return sorted(str(n)) in arr\n``` | 3 | You are given an integer `n`. We reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return `true` _if and only if we can do this so that the resulting number is a power of two_.
**Example 1:**
**Input:** n = 1
**Output:** true
**Example 2:**
**Input:** n = 10
**... | null |
python3 | easy understanding | sort | reordered-power-of-2 | 0 | 1 | ```\nclass Solution:\n def reorderedPowerOf2(self, n: int) -> bool:\n i, arr = 0, []\n v = 2**i\n while v <= 10**9: arr.append(sorted(str(v))); i+=1; v = 2**i\n return sorted(str(n)) in arr\n``` | 3 | We can use run-length encoding (i.e., **RLE**) to encode a sequence of integers. In a run-length encoded array of even length `encoding` (**0-indexed**), for all even `i`, `encoding[i]` tells us the number of times that the non-negative integer value `encoding[i + 1]` is repeated in the sequence.
* For example, the ... | null |
python short and precise answer | reordered-power-of-2 | 0 | 1 | ```\nclass Solution:\n def reorderedPowerOf2(self, n: int) -> bool:\n for i in range(32):\n if Counter(str(n))==Counter(str(2**i)):\n return True\n return False\n | 3 | You are given an integer `n`. We reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return `true` _if and only if we can do this so that the resulting number is a power of two_.
**Example 1:**
**Input:** n = 1
**Output:** true
**Example 2:**
**Input:** n = 10
**... | null |
python short and precise answer | reordered-power-of-2 | 0 | 1 | ```\nclass Solution:\n def reorderedPowerOf2(self, n: int) -> bool:\n for i in range(32):\n if Counter(str(n))==Counter(str(2**i)):\n return True\n return False\n | 3 | We can use run-length encoding (i.e., **RLE**) to encode a sequence of integers. In a run-length encoded array of even length `encoding` (**0-indexed**), for all even `i`, `encoding[i]` tells us the number of times that the non-negative integer value `encoding[i + 1]` is repeated in the sequence.
* For example, the ... | null |
[ Python ] ✅✅ Simple Python Solution Using Two Different Approach 🥳✌👍 | reordered-power-of-2 | 0 | 1 | # If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# 1. First Apporach Using Permutation Concept : \n# Runtime: 8288 ms, faster than 5.35% of Python3 online submissions for Reordered Power of 2.\n# Memory Usage: 28.1 MB, less than 11.23% of Python3 online submissions for Re... | 2 | You are given an integer `n`. We reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return `true` _if and only if we can do this so that the resulting number is a power of two_.
**Example 1:**
**Input:** n = 1
**Output:** true
**Example 2:**
**Input:** n = 10
**... | null |
[ Python ] ✅✅ Simple Python Solution Using Two Different Approach 🥳✌👍 | reordered-power-of-2 | 0 | 1 | # If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# 1. First Apporach Using Permutation Concept : \n# Runtime: 8288 ms, faster than 5.35% of Python3 online submissions for Reordered Power of 2.\n# Memory Usage: 28.1 MB, less than 11.23% of Python3 online submissions for Re... | 2 | We can use run-length encoding (i.e., **RLE**) to encode a sequence of integers. In a run-length encoded array of even length `encoding` (**0-indexed**), for all even `i`, `encoding[i]` tells us the number of times that the non-negative integer value `encoding[i + 1]` is repeated in the sequence.
* For example, the ... | null |
SIMPLE C++ SOLUTION | reordered-power-of-2 | 1 | 1 | ```\nclass Solution {\npublic:\n bool reorderedPowerOf2(int n) {\n if(n == 1) return true;\n unordered_map<int, int> map;\n \n string temp = to_string(n);\n for(int i = 0; i < temp.size(); i++){\n map[int(temp[i])-48]++;\n } \n \n int digits = temp.... | 2 | You are given an integer `n`. We reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return `true` _if and only if we can do this so that the resulting number is a power of two_.
**Example 1:**
**Input:** n = 1
**Output:** true
**Example 2:**
**Input:** n = 10
**... | null |
SIMPLE C++ SOLUTION | reordered-power-of-2 | 1 | 1 | ```\nclass Solution {\npublic:\n bool reorderedPowerOf2(int n) {\n if(n == 1) return true;\n unordered_map<int, int> map;\n \n string temp = to_string(n);\n for(int i = 0; i < temp.size(); i++){\n map[int(temp[i])-48]++;\n } \n \n int digits = temp.... | 2 | We can use run-length encoding (i.e., **RLE**) to encode a sequence of integers. In a run-length encoded array of even length `encoding` (**0-indexed**), for all even `i`, `encoding[i]` tells us the number of times that the non-negative integer value `encoding[i + 1]` is repeated in the sequence.
* For example, the ... | null |
Solution | advantage-shuffle | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2) {\n vector<pair<int,int>>v;\n for(int i=0;i<nums2.size();i++)\n {\n v.push_back({nums2[i],i});\n }\n sort(nums1.begin(),nums1.end());\n sort(v.begin(),v.end()... | 2 | You are given two integer arrays `nums1` and `nums2` both of the same length. The **advantage** of `nums1` with respect to `nums2` is the number of indices `i` for which `nums1[i] > nums2[i]`.
Return _any permutation of_ `nums1` _that maximizes its **advantage** with respect to_ `nums2`.
**Example 1:**
**Input:** nu... | null |
Solution | advantage-shuffle | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2) {\n vector<pair<int,int>>v;\n for(int i=0;i<nums2.size();i++)\n {\n v.push_back({nums2[i],i});\n }\n sort(nums1.begin(),nums1.end());\n sort(v.begin(),v.end()... | 2 | Design an algorithm that collects daily price quotes for some stock and returns **the span** of that stock's price for the current day.
The **span** of the stock's price in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to ... | null |
Easy Pyton Solution Beats 98% | advantage-shuffle | 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 two integer arrays `nums1` and `nums2` both of the same length. The **advantage** of `nums1` with respect to `nums2` is the number of indices `i` for which `nums1[i] > nums2[i]`.
Return _any permutation of_ `nums1` _that maximizes its **advantage** with respect to_ `nums2`.
**Example 1:**
**Input:** nu... | null |
Easy Pyton Solution Beats 98% | advantage-shuffle | 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 | Design an algorithm that collects daily price quotes for some stock and returns **the span** of that stock's price for the current day.
The **span** of the stock's price in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to ... | null |
Python | Video Walkthrough | Time: O(nlogn) | Space: O(n) | advantage-shuffle | 0 | 1 | [Click Here For Video](https://youtu.be/nz0_sRskx5U)\n```\nclass Solution:\n def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:\n I, res, _ = deque(sorted(range(len(nums2)),key = lambda idx: nums2[idx])), [-1] * len(nums1), nums1.sort()\n for boy in nums1:\n if boy > ... | 0 | You are given two integer arrays `nums1` and `nums2` both of the same length. The **advantage** of `nums1` with respect to `nums2` is the number of indices `i` for which `nums1[i] > nums2[i]`.
Return _any permutation of_ `nums1` _that maximizes its **advantage** with respect to_ `nums2`.
**Example 1:**
**Input:** nu... | null |
Python | Video Walkthrough | Time: O(nlogn) | Space: O(n) | advantage-shuffle | 0 | 1 | [Click Here For Video](https://youtu.be/nz0_sRskx5U)\n```\nclass Solution:\n def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:\n I, res, _ = deque(sorted(range(len(nums2)),key = lambda idx: nums2[idx])), [-1] * len(nums1), nums1.sort()\n for boy in nums1:\n if boy > ... | 0 | Design an algorithm that collects daily price quotes for some stock and returns **the span** of that stock's price for the current day.
The **span** of the stock's price in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to ... | null |
Python3 || 10 lines, heap, w/explanation || T/M: 90%/ 98% | minimum-number-of-refueling-stops | 0 | 1 | ```\nclass Solution: # Here\'s the plan:\n # \n # 1) We only need to be concerned with two quantities: the dist traveled (pos)\n # and the fuel acquired (fuel). We have to refuel before pos > fuel.\n # \n ... | 3 | A car travels from a starting position to a destination which is `target` miles east of the starting position.
There are gas stations along the way. The gas stations are represented as an array `stations` where `stations[i] = [positioni, fueli]` indicates that the `ith` gas station is `positioni` miles east of the sta... | null |
Python3 || 10 lines, heap, w/explanation || T/M: 90%/ 98% | minimum-number-of-refueling-stops | 0 | 1 | ```\nclass Solution: # Here\'s the plan:\n # \n # 1) We only need to be concerned with two quantities: the dist traveled (pos)\n # and the fuel acquired (fuel). We have to refuel before pos > fuel.\n # \n ... | 3 | Given an array of `digits` which is sorted in **non-decreasing** order. You can write numbers using each `digits[i]` as many times as we want. For example, if `digits = ['1','3','5']`, we may write numbers such as `'13'`, `'551'`, and `'1351315'`.
Return _the number of positive integers that can be generated_ that are... | null |
Solution | length-of-longest-fibonacci-subsequence | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int lenLongestFibSubseq(vector<int>& arr) {\n int n = arr.size();\n map<pair<int,int>,int>indexes;\n\n for(int i = 2 ;i < n;i++) {\n int start = 0;\n int end = i-1;\n long long reqSum = arr[i];\n long long sum;\n ... | 1 | A sequence `x1, x2, ..., xn` is _Fibonacci-like_ if:
* `n >= 3`
* `xi + xi+1 == xi+2` for all `i + 2 <= n`
Given a **strictly increasing** array `arr` of positive integers forming a sequence, return _the **length** of the longest Fibonacci-like subsequence of_ `arr`. If one does not exist, return `0`.
A **subseq... | null |
Solution | length-of-longest-fibonacci-subsequence | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int lenLongestFibSubseq(vector<int>& arr) {\n int n = arr.size();\n map<pair<int,int>,int>indexes;\n\n for(int i = 2 ;i < n;i++) {\n int start = 0;\n int end = i-1;\n long long reqSum = arr[i];\n long long sum;\n ... | 1 | Given an integer array `nums`, move all the even integers at the beginning of the array followed by all the odd integers.
Return _**any array** that satisfies this condition_.
**Example 1:**
**Input:** nums = \[3,1,2,4\]
**Output:** \[2,4,3,1\]
**Explanation:** The outputs \[4,2,3,1\], \[2,4,1,3\], and \[4,2,1,3\] w... | null |
Python Elegant & Short | length-of-longest-fibonacci-subsequence | 0 | 1 | # Complexity\n- Time complexity: $$O(n^{2})$$\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution:\n def lenLongestFibSubseq(self, arr: List[int]) -> int:\n nums = set(arr)\n longest = 0\n\n for i in range(len(arr)):\n for j in range(i + 1, len(arr)):\n a, b = ... | 3 | A sequence `x1, x2, ..., xn` is _Fibonacci-like_ if:
* `n >= 3`
* `xi + xi+1 == xi+2` for all `i + 2 <= n`
Given a **strictly increasing** array `arr` of positive integers forming a sequence, return _the **length** of the longest Fibonacci-like subsequence of_ `arr`. If one does not exist, return `0`.
A **subseq... | null |
Python Elegant & Short | length-of-longest-fibonacci-subsequence | 0 | 1 | # Complexity\n- Time complexity: $$O(n^{2})$$\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution:\n def lenLongestFibSubseq(self, arr: List[int]) -> int:\n nums = set(arr)\n longest = 0\n\n for i in range(len(arr)):\n for j in range(i + 1, len(arr)):\n a, b = ... | 3 | Given an integer array `nums`, move all the even integers at the beginning of the array followed by all the odd integers.
Return _**any array** that satisfies this condition_.
**Example 1:**
**Input:** nums = \[3,1,2,4\]
**Output:** \[2,4,3,1\]
**Explanation:** The outputs \[4,2,3,1\], \[2,4,1,3\], and \[4,2,1,3\] w... | null |
Solution | walking-robot-simulation | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {\n set<pair<int,int>> obs_maps;\n int orientation = 1;\n int movement[4][4] = {{1,0},{0,1},{-1,0},{0,-1}};\n int x = 0, y = 0, x_max = 0, y_max = 0;\n for(auto &obs:obstacles... | 1 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Solution | walking-robot-simulation | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {\n set<pair<int,int>> obs_maps;\n int orientation = 1;\n int movement[4][4] = {{1,0},{0,1},{-1,0},{0,-1}};\n int x = 0, y = 0, x_max = 0, y_max = 0;\n for(auto &obs:obstacles... | 1 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
python solution | walking-robot-simulation | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this problem is that we can use a simple simulation of the robot\'s movements, updating its position and keeping track of the maximum distance it has traveled so far.\n\n\n# Approach\n<!-- Describe your approach to so... | 1 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
python solution | walking-robot-simulation | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this problem is that we can use a simple simulation of the robot\'s movements, updating its position and keeping track of the maximum distance it has traveled so far.\n\n\n# Approach\n<!-- Describe your approach to so... | 1 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Python, simple, 99% fast | walking-robot-simulation | 0 | 1 | ```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n obstacles = {(x, y) for x, y in obstacles}\n \n dist = 0\n x, y = 0, 0\n dx, dy = 0, 1\n \n for move in commands:\n if move == -2:\n dx, dy =... | 11 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Python, simple, 99% fast | walking-robot-simulation | 0 | 1 | ```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n obstacles = {(x, y) for x, y in obstacles}\n \n dist = 0\n x, y = 0, 0\n dx, dy = 0, 1\n \n for move in commands:\n if move == -2:\n dx, dy =... | 11 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Python brute-force solution. | walking-robot-simulation | 0 | 1 | # Intuition\nI figured there was no better way than actually making the moves and calculating the distances.\n\n# Approach\nMy emphasis here was on properly separating the parts of the code. The "algorithm" itself isn\'t very complicated but I felt that at the very least writing the ```make_move``` function separately ... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Python brute-force solution. | walking-robot-simulation | 0 | 1 | # Intuition\nI figured there was no better way than actually making the moves and calculating the distances.\n\n# Approach\nMy emphasis here was on properly separating the parts of the code. The "algorithm" itself isn\'t very complicated but I felt that at the very least writing the ```make_move``` function separately ... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Easiest Solution | walking-robot-simulation | 1 | 1 | \n\n# Code\n```java []\nclass Solution {\n public int robotSim(int[] commands, int[][] obstacles) {\n int x = 0, y = 0, j = 0, result = 0;\n\t\tint[][] dirs = {{0,1},{1,0},{0,-1},{-1,0}};\n Set<String> set = new HashSet<>();\n for(int[] ob : obstacles) set.add(ob[0] + "_" + ob[1]);\n for(... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Easiest Solution | walking-robot-simulation | 1 | 1 | \n\n# Code\n```java []\nclass Solution {\n public int robotSim(int[] commands, int[][] obstacles) {\n int x = 0, y = 0, j = 0, result = 0;\n\t\tint[][] dirs = {{0,1},{1,0},{0,-1},{-1,0}};\n Set<String> set = new HashSet<>();\n for(int[] ob : obstacles) set.add(ob[0] + "_" + ob[1]);\n for(... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Python3 - Simultation + Set of Obstacles | walking-robot-simulation | 0 | 1 | # Intuition\nOriginally I was going to have a dictionary of obstacles for every column and row (which had an obstacle) and a sorted list to look for the obstacles. But then I realized, that it could only ever move 9 at a time. That solution makes sense if it is moving thousands or even hundreds, it might even be slowe... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Python3 - Simultation + Set of Obstacles | walking-robot-simulation | 0 | 1 | # Intuition\nOriginally I was going to have a dictionary of obstacles for every column and row (which had an obstacle) and a sorted list to look for the obstacles. But then I realized, that it could only ever move 9 at a time. That solution makes sense if it is moving thousands or even hundreds, it might even be slowe... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Python easy to understand beats 87.99% | walking-robot-simulation | 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 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Python easy to understand beats 87.99% | walking-robot-simulation | 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 say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Intuitive and Readable Python Solution (Beats 97%) | walking-robot-simulation | 0 | 1 | # Code\n```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n dir = ((0,1),(1,0),(0,-1),(-1,0)) # North, East, South, West\n curr_dir = 0\n curr_pos=[0,0]\n\n max_dist = 0\n\n obstacle_set = set()\n for obstacle in obstacles:\n ... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Intuitive and Readable Python Solution (Beats 97%) | walking-robot-simulation | 0 | 1 | # Code\n```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n dir = ((0,1),(1,0),(0,-1),(-1,0)) # North, East, South, West\n curr_dir = 0\n curr_pos=[0,0]\n\n max_dist = 0\n\n obstacle_set = set()\n for obstacle in obstacles:\n ... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
C++ and Python Stimulation | walking-robot-simulation | 0 | 1 | # Code\n```\nclass Solution {\npublic:\n int directions[4][2] = {\n {0, 1}, {1, 0}, {0, -1}, {-1, 0},\n };\n\n int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {\n int farthest = 0;\n int d = 0;\n int curX = 0, curY = 0;\n\n set<vector<int>> blockers(obstac... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
C++ and Python Stimulation | walking-robot-simulation | 0 | 1 | # Code\n```\nclass Solution {\npublic:\n int directions[4][2] = {\n {0, 1}, {1, 0}, {0, -1}, {-1, 0},\n };\n\n int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {\n int farthest = 0;\n int d = 0;\n int curX = 0, curY = 0;\n\n set<vector<int>> blockers(obstac... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
My Python3 Solution hash into set | walking-robot-simulation | 0 | 1 | # Code\n```\n dirs_l = {"l":"b","b":"r","r":"u","u":"l"}\n dirs_r = {"l":"u","u":"r","r":"b","b":"l"}\n move = {"l":[-1,0],"r":[1,0],"u":[0,1],"b":[0,-1]}\n\n temp = \'u\'\n position = [0,0]\n res = 0\n\n # convert obstacles list of lists into set of tuples\n obst... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
My Python3 Solution hash into set | walking-robot-simulation | 0 | 1 | # Code\n```\n dirs_l = {"l":"b","b":"r","r":"u","u":"l"}\n dirs_r = {"l":"u","u":"r","r":"b","b":"l"}\n move = {"l":[-1,0],"r":[1,0],"u":[0,1],"b":[0,-1]}\n\n temp = \'u\'\n position = [0,0]\n res = 0\n\n # convert obstacles list of lists into set of tuples\n obst... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Python Solution | walking-robot-simulation | 0 | 1 | Posting because didn\'t see anyone handle directions like this. Kinda neat.\n\n# Code\n```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n\n obs = {(coords[0], coords[1]) for coords in obstacles}\n\n x,y = 0,0\n dx,dy = 0,1\n\n dist = 0\n\n... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Python Solution | walking-robot-simulation | 0 | 1 | Posting because didn\'t see anyone handle directions like this. Kinda neat.\n\n# Code\n```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n\n obs = {(coords[0], coords[1]) for coords in obstacles}\n\n x,y = 0,0\n dx,dy = 0,1\n\n dist = 0\n\n... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Python Solution | walking-robot-simulation | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n x,y = 0,0\n dire = [(0,1),(1,0),(0,-1),(-1,0)]\n d = 0\n ret = 0\n obstacles = {tuple(x) for x in obstacles}\n\n for step in commands:\n if step == -2:... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Python Solution | walking-robot-simulation | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n x,y = 0,0\n dire = [(0,1),(1,0),(0,-1),(-1,0)]\n d = 0\n ret = 0\n obstacles = {tuple(x) for x in obstacles}\n\n for step in commands:\n if step == -2:... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Walking Robot Simulation | walking-robot-simulation | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe basic approach is straightforward: keep track of x, y, and the direction, represented as a two-vector dx, dy. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nBasically, a case statement for the three types of ... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
Walking Robot Simulation | walking-robot-simulation | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe basic approach is straightforward: keep track of x, y, and the direction, represented as a two-vector dx, dy. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nBasically, a case statement for the three types of ... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
PYTHON EZ MODE CHECKING ROTATE | walking-robot-simulation | 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 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
PYTHON EZ MODE CHECKING ROTATE | walking-robot-simulation | 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 say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
<py> | walking-robot-simulation | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n \'\'\'\n 0 1 2 3\n directions = ["North", "East", "South", "West"]\n (0, 1) (1, 0) (0, -1) (-1, 0)\n \'\'\'\n ... | 0 | A robot on an infinite XY-plane starts at point `(0, 0)` facing north. The robot can receive a sequence of these three possible types of `commands`:
* `-2`: Turn left `90` degrees.
* `-1`: Turn right `90` degrees.
* `1 <= k <= 9`: Move forward `k` units, one unit at a time.
Some of the grid squares are `obstacl... | null |
<py> | walking-robot-simulation | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:\n \'\'\'\n 0 1 2 3\n directions = ["North", "East", "South", "West"]\n (0, 1) (1, 0) (0, -1) (-1, 0)\n \'\'\'\n ... | 0 | Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
**Example 1:**
**Input:** left ... | null |
Binary search over answer in python | koko-eating-bananas | 0 | 1 | # Approach\nBinary search over answer\n\n# Complexity\n- Time complexity: $$O(n * log n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution:\n def minEatingSpeed(self, piles: List[int], h: int) -> int:\n def check_can_eat(k):\n actual_h = 0\n for pile in piles:\n ... | 1 | Koko loves to eat bananas. There are `n` piles of bananas, the `ith` pile has `piles[i]` bananas. The guards have gone and will come back in `h` hours.
Koko can decide her bananas-per-hour eating speed of `k`. Each hour, she chooses some pile of bananas and eats `k` bananas from that pile. If the pile has less than `k... | null |
Binary search over answer in python | koko-eating-bananas | 0 | 1 | # Approach\nBinary search over answer\n\n# Complexity\n- Time complexity: $$O(n * log n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution:\n def minEatingSpeed(self, piles: List[int], h: int) -> int:\n def check_can_eat(k):\n actual_h = 0\n for pile in piles:\n ... | 1 | Given an array of integers arr, find the sum of `min(b)`, where `b` ranges over every (contiguous) subarray of `arr`. Since the answer may be large, return the answer **modulo** `109 + 7`.
**Example 1:**
**Input:** arr = \[3,1,2,4\]
**Output:** 17
**Explanation:**
Subarrays are \[3\], \[1\], \[2\], \[4\], \[3,1\], \... | null |
Optimized Bounds + Binary Search Python3 Solution | koko-eating-bananas | 0 | 1 | # Optimized Bounds + Binary Search\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nIn addition to the binary search solution, I also added an optimization at the beginning to find the bounds of where k could be. This should be better than the posted solutions, since it\'s able to find the bounds... | 3 | Koko loves to eat bananas. There are `n` piles of bananas, the `ith` pile has `piles[i]` bananas. The guards have gone and will come back in `h` hours.
Koko can decide her bananas-per-hour eating speed of `k`. Each hour, she chooses some pile of bananas and eats `k` bananas from that pile. If the pile has less than `k... | null |
Optimized Bounds + Binary Search Python3 Solution | koko-eating-bananas | 0 | 1 | # Optimized Bounds + Binary Search\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nIn addition to the binary search solution, I also added an optimization at the beginning to find the bounds of where k could be. This should be better than the posted solutions, since it\'s able to find the bounds... | 3 | Given an array of integers arr, find the sum of `min(b)`, where `b` ranges over every (contiguous) subarray of `arr`. Since the answer may be large, return the answer **modulo** `109 + 7`.
**Example 1:**
**Input:** arr = \[3,1,2,4\]
**Output:** 17
**Explanation:**
Subarrays are \[3\], \[1\], \[2\], \[4\], \[3,1\], \... | null |
Easy python solution using binary search, beginners friendly!!🔥🔥 | koko-eating-bananas | 0 | 1 | # Code\n```\nimport math\nclass Solution:\n def minEatingSpeed(self, piles: List[int], h: int) -> int:\n a = 1\n b=max(piles)\n while a<b:\n mid=(a+b)//2\n c=0\n for j in piles:\n c+=math.ceil(j/mid)\n if c>h:\n a=mid+1\n ... | 2 | Koko loves to eat bananas. There are `n` piles of bananas, the `ith` pile has `piles[i]` bananas. The guards have gone and will come back in `h` hours.
Koko can decide her bananas-per-hour eating speed of `k`. Each hour, she chooses some pile of bananas and eats `k` bananas from that pile. If the pile has less than `k... | null |
Easy python solution using binary search, beginners friendly!!🔥🔥 | koko-eating-bananas | 0 | 1 | # Code\n```\nimport math\nclass Solution:\n def minEatingSpeed(self, piles: List[int], h: int) -> int:\n a = 1\n b=max(piles)\n while a<b:\n mid=(a+b)//2\n c=0\n for j in piles:\n c+=math.ceil(j/mid)\n if c>h:\n a=mid+1\n ... | 2 | Given an array of integers arr, find the sum of `min(b)`, where `b` ranges over every (contiguous) subarray of `arr`. Since the answer may be large, return the answer **modulo** `109 + 7`.
**Example 1:**
**Input:** arr = \[3,1,2,4\]
**Output:** 17
**Explanation:**
Subarrays are \[3\], \[1\], \[2\], \[4\], \[3,1\], \... | null |
Assalomu aleykum... | middle-of-the-linked-list | 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 the `head` of a singly linked list, return _the middle node of the linked list_.
If there are two middle nodes, return **the second middle** node.
**Example 1:**
**Input:** head = \[1,2,3,4,5\]
**Output:** \[3,4,5\]
**Explanation:** The middle node of the list is node 3.
**Example 2:**
**Input:** head = \[1,... | null |
Assalomu aleykum... | middle-of-the-linked-list | 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`.
In one operation, you can choose any index `i` where `0 <= i < nums.length` and change `nums[i]` to `nums[i] + x` where `x` is an integer from the range `[-k, k]`. You can apply this operation **at most once** for each index `i`.
The **score** of `nums` is the... | null |
Simple solution with Dynamic Programming in Python3 | stone-game | 0 | 1 | # Intuition\nWe have `piles` with stones to play game.\nThere\'re two participants: `A` and `B`.\nOur goal is to check, if it\'s feasible for `A` to win a game in a such way, that `A` has amount of stones **more than** `B` has.\nEach player take `piles[i]` by their turn from **left/right side**. \nThe game continues **... | 1 | Alice and Bob play a game with piles of stones. There are an **even** number of piles arranged in a row, and each pile has a **positive** integer number of stones `piles[i]`.
The objective of the game is to end with the most stones. The **total** number of stones across all the piles is **odd**, so there are no ties.
... | null |
Simple solution with Dynamic Programming in Python3 | stone-game | 0 | 1 | # Intuition\nWe have `piles` with stones to play game.\nThere\'re two participants: `A` and `B`.\nOur goal is to check, if it\'s feasible for `A` to win a game in a such way, that `A` has amount of stones **more than** `B` has.\nEach player take `piles[i]` by their turn from **left/right side**. \nThe game continues **... | 1 | You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to `n2` in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row.
You start on square `1` of the board. In each ... | null |
Python short and clean. Functional programming. | stone-game | 0 | 1 | # Approach 1: Recursive DP\n\n# Complexity\n- Time complexity: $$O(n ^ 2)$$\n\n- Space complexity: $$O(n ^ 2)$$\n\nwhere, `n is number of piles.`\n\n# Code\n```python\nclass Solution:\n def stoneGame(self, piles: list[int]) -> bool:\n @cache\n def score(i: int, j: int) -> int:\n return (i < ... | 2 | Alice and Bob play a game with piles of stones. There are an **even** number of piles arranged in a row, and each pile has a **positive** integer number of stones `piles[i]`.
The objective of the game is to end with the most stones. The **total** number of stones across all the piles is **odd**, so there are no ties.
... | null |
Python short and clean. Functional programming. | stone-game | 0 | 1 | # Approach 1: Recursive DP\n\n# Complexity\n- Time complexity: $$O(n ^ 2)$$\n\n- Space complexity: $$O(n ^ 2)$$\n\nwhere, `n is number of piles.`\n\n# Code\n```python\nclass Solution:\n def stoneGame(self, piles: list[int]) -> bool:\n @cache\n def score(i: int, j: int) -> int:\n return (i < ... | 2 | You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to `n2` in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row.
You start on square `1` of the board. In each ... | null |
MIND BLOW UP SOLUTION, Beginer Friendly ✅ | stone-game | 0 | 1 | # Intuition\nAll the time Alice take the first number. So if alice isn\'t stupid she takes the big number of the list. Alice never lose the game.\n\n# Approach\nWrite to the return True\n\n# Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def stoneGame(self, piles... | 4 | Alice and Bob play a game with piles of stones. There are an **even** number of piles arranged in a row, and each pile has a **positive** integer number of stones `piles[i]`.
The objective of the game is to end with the most stones. The **total** number of stones across all the piles is **odd**, so there are no ties.
... | null |
MIND BLOW UP SOLUTION, Beginer Friendly ✅ | stone-game | 0 | 1 | # Intuition\nAll the time Alice take the first number. So if alice isn\'t stupid she takes the big number of the list. Alice never lose the game.\n\n# Approach\nWrite to the return True\n\n# Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def stoneGame(self, piles... | 4 | You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to `n2` in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row.
You start on square `1` of the board. In each ... | null |
EASIEST SOLUTION EVER ONE LINER PYTHON | stone-game | 0 | 1 | \n# Code\n```\nclass Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n return True\n\n``` | 2 | Alice and Bob play a game with piles of stones. There are an **even** number of piles arranged in a row, and each pile has a **positive** integer number of stones `piles[i]`.
The objective of the game is to end with the most stones. The **total** number of stones across all the piles is **odd**, so there are no ties.
... | null |
EASIEST SOLUTION EVER ONE LINER PYTHON | stone-game | 0 | 1 | \n# Code\n```\nclass Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n return True\n\n``` | 2 | You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to `n2` in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row.
You start on square `1` of the board. In each ... | null |
Python 3, dp | stone-game | 0 | 1 | # Intuition\ndp\n\n# Approach\ndp\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n^2)\n\n# Code\n```\nclass Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n\n n: int = len(piles)\n first: List[List[int]] = [[0] * n for _ in range(n)]\n second: List[List[int]] = [[0... | 1 | Alice and Bob play a game with piles of stones. There are an **even** number of piles arranged in a row, and each pile has a **positive** integer number of stones `piles[i]`.
The objective of the game is to end with the most stones. The **total** number of stones across all the piles is **odd**, so there are no ties.
... | null |
Python 3, dp | stone-game | 0 | 1 | # Intuition\ndp\n\n# Approach\ndp\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n^2)\n\n# Code\n```\nclass Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n\n n: int = len(piles)\n first: List[List[int]] = [[0] * n for _ in range(n)]\n second: List[List[int]] = [[0... | 1 | You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to `n2` in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row.
You start on square `1` of the board. In each ... | null |
Faster than 89.38% and memory beats 80.57% | stone-game | 0 | 1 | \nRuntime = 48ms\n# Code\n```\nclass Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n a,b = 0,0\n l = 0\n h = len(piles)-1\n while l<h:\n a += max(piles[l],piles[h])\n b += min(piles[l],piles[h])\n l += 1\n h -= 1\n return a>b... | 1 | Alice and Bob play a game with piles of stones. There are an **even** number of piles arranged in a row, and each pile has a **positive** integer number of stones `piles[i]`.
The objective of the game is to end with the most stones. The **total** number of stones across all the piles is **odd**, so there are no ties.
... | null |
Faster than 89.38% and memory beats 80.57% | stone-game | 0 | 1 | \nRuntime = 48ms\n# Code\n```\nclass Solution:\n def stoneGame(self, piles: List[int]) -> bool:\n a,b = 0,0\n l = 0\n h = len(piles)-1\n while l<h:\n a += max(piles[l],piles[h])\n b += min(piles[l],piles[h])\n l += 1\n h -= 1\n return a>b... | 1 | You are given an `n x n` integer matrix `board` where the cells are labeled from `1` to `n2` in a [**Boustrophedon style**](https://en.wikipedia.org/wiki/Boustrophedon) starting from the bottom left of the board (i.e. `board[n - 1][0]`) and alternating direction each row.
You start on square `1` of the board. In each ... | null |
easiest binary search approach in python. | nth-magical-number | 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 positive integer is _magical_ if it is divisible by either `a` or `b`.
Given the three integers `n`, `a`, and `b`, return the `nth` magical number. Since the answer may be very large, **return it modulo** `109 + 7`.
**Example 1:**
**Input:** n = 1, a = 2, b = 3
**Output:** 2
**Example 2:**
**Input:** n = 4, a = ... | null |
easiest binary search approach in python. | nth-magical-number | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | You are given an integer array `nums` and an integer `k`.
For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`.
The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.
Return _the minimum **score** of_ `nums` _after changi... | null |
Awesome Trick With Binary Tree | nth-magical-number | 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 positive integer is _magical_ if it is divisible by either `a` or `b`.
Given the three integers `n`, `a`, and `b`, return the `nth` magical number. Since the answer may be very large, **return it modulo** `109 + 7`.
**Example 1:**
**Input:** n = 1, a = 2, b = 3
**Output:** 2
**Example 2:**
**Input:** n = 4, a = ... | null |
Awesome Trick With Binary Tree | nth-magical-number | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given an integer array `nums` and an integer `k`.
For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`.
The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.
Return _the minimum **score** of_ `nums` _after changi... | null |
Solution | nth-magical-number | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int gcdHelper(int r, int d){\n if(r == 0)\n return d;\n return gcd(d%r, r);\n }\n int nthMagicalNumber(int n, int a, int b) {\n long long int low = min(a, b);\n long long int high = (n*low);\n long long int lcm = (a*b)/gcdHelp... | 2 | A positive integer is _magical_ if it is divisible by either `a` or `b`.
Given the three integers `n`, `a`, and `b`, return the `nth` magical number. Since the answer may be very large, **return it modulo** `109 + 7`.
**Example 1:**
**Input:** n = 1, a = 2, b = 3
**Output:** 2
**Example 2:**
**Input:** n = 4, a = ... | null |
Solution | nth-magical-number | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int gcdHelper(int r, int d){\n if(r == 0)\n return d;\n return gcd(d%r, r);\n }\n int nthMagicalNumber(int n, int a, int b) {\n long long int low = min(a, b);\n long long int high = (n*low);\n long long int lcm = (a*b)/gcdHelp... | 2 | You are given an integer array `nums` and an integer `k`.
For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`.
The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.
Return _the minimum **score** of_ `nums` _after changi... | null |
Python Hard | nth-magical-number | 0 | 1 | ```\nclass Solution:\n def nthMagicalNumber(self, n: int, a: int, b: int) -> int: \n MOD = 10 ** 9 + 7\n lcm = math.lcm(a, b)\n l, r = min(a, b), max(a, b) * n\n\n while l < r:\n mid = (l + r) // 2\n\n if (mid // a) + (mid // b) - (mid // lcm) < n:\n l... | 0 | A positive integer is _magical_ if it is divisible by either `a` or `b`.
Given the three integers `n`, `a`, and `b`, return the `nth` magical number. Since the answer may be very large, **return it modulo** `109 + 7`.
**Example 1:**
**Input:** n = 1, a = 2, b = 3
**Output:** 2
**Example 2:**
**Input:** n = 4, a = ... | null |
Python Hard | nth-magical-number | 0 | 1 | ```\nclass Solution:\n def nthMagicalNumber(self, n: int, a: int, b: int) -> int: \n MOD = 10 ** 9 + 7\n lcm = math.lcm(a, b)\n l, r = min(a, b), max(a, b) * n\n\n while l < r:\n mid = (l + r) // 2\n\n if (mid // a) + (mid // b) - (mid // lcm) < n:\n l... | 0 | You are given an integer array `nums` and an integer `k`.
For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`.
The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.
Return _the minimum **score** of_ `nums` _after changi... | null |
Solution using binary search | nth-magical-number | 0 | 1 | # Complexity\n- Time complexity:$$O(log(n*min(a,b)))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def nthMagicalNumber(self, n: int, a: int, b: int) -> int:\n mod = (10**9)+7\n ... | 0 | A positive integer is _magical_ if it is divisible by either `a` or `b`.
Given the three integers `n`, `a`, and `b`, return the `nth` magical number. Since the answer may be very large, **return it modulo** `109 + 7`.
**Example 1:**
**Input:** n = 1, a = 2, b = 3
**Output:** 2
**Example 2:**
**Input:** n = 4, a = ... | null |
Solution using binary search | nth-magical-number | 0 | 1 | # Complexity\n- Time complexity:$$O(log(n*min(a,b)))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def nthMagicalNumber(self, n: int, a: int, b: int) -> int:\n mod = (10**9)+7\n ... | 0 | You are given an integer array `nums` and an integer `k`.
For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`.
The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.
Return _the minimum **score** of_ `nums` _after changi... | null |
Recursive Binary Search | nth-magical-number | 0 | 1 | # Intuition\nProblem looks simple but can get tricky to understand. We need to find the number at a particular rank\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nRecursive Binary Search:\n\nRequired number has to be a multiple of a or b, once we decide on multiplier for a, we can find overall ... | 0 | A positive integer is _magical_ if it is divisible by either `a` or `b`.
Given the three integers `n`, `a`, and `b`, return the `nth` magical number. Since the answer may be very large, **return it modulo** `109 + 7`.
**Example 1:**
**Input:** n = 1, a = 2, b = 3
**Output:** 2
**Example 2:**
**Input:** n = 4, a = ... | null |
Recursive Binary Search | nth-magical-number | 0 | 1 | # Intuition\nProblem looks simple but can get tricky to understand. We need to find the number at a particular rank\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nRecursive Binary Search:\n\nRequired number has to be a multiple of a or b, once we decide on multiplier for a, we can find overall ... | 0 | You are given an integer array `nums` and an integer `k`.
For each index `i` where `0 <= i < nums.length`, change `nums[i]` to be either `nums[i] + k` or `nums[i] - k`.
The **score** of `nums` is the difference between the maximum and minimum elements in `nums`.
Return _the minimum **score** of_ `nums` _after changi... | null |
Python3 Solution | profitable-schemes | 0 | 1 | \n```\nclass Solution:\n def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:\n m=len(group)\n dp=[[[0]*(minProfit+1) for _ in range(n+1)] for _ in range(m+1)]\n for j in range(n+1):\n dp[m][j][0]=1\n\n for i in range(m-1,-1,-1):\n ... | 6 | There is a group of `n` members, and a list of various crimes they could commit. The `ith` crime generates a `profit[i]` and requires `group[i]` members to participate in it. If a member participates in one crime, that member can't participate in another crime.
Let's call a **profitable scheme** any subset of these cr... | null |
Python3 Solution | profitable-schemes | 0 | 1 | \n```\nclass Solution:\n def profitableSchemes(self, n: int, minProfit: int, group: List[int], profit: List[int]) -> int:\n m=len(group)\n dp=[[[0]*(minProfit+1) for _ in range(n+1)] for _ in range(m+1)]\n for j in range(n+1):\n dp[m][j][0]=1\n\n for i in range(m-1,-1,-1):\n ... | 6 | You are given two integer arrays `persons` and `times`. In an election, the `ith` vote was cast for `persons[i]` at time `times[i]`.
For each query at a time `t`, find the person that was leading the election at time `t`. Votes cast at time `t` will count towards our query. In the case of a tie, the most recent vote (... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.