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 |
|---|---|---|---|---|---|---|---|
Simple solution | rle-iterator | 0 | 1 | ```\nclass RLEIterator:\n\n def __init__(self, encoding: List[int]):\n self.arr = encoding\n self.curr = 0\n self.cnt = self.arr[self.curr]\n\n def next(self, n: int) -> int:\n while self.curr < len(self.arr) and n > 0:\n if n <= self.cnt:\n self.cnt -= n\n ... | 0 | 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 solution | rle-iterator | 0 | 1 | ```\nclass RLEIterator:\n\n def __init__(self, encoding: List[int]):\n self.arr = encoding\n self.curr = 0\n self.cnt = self.arr[self.curr]\n\n def next(self, n: int) -> int:\n while self.curr < len(self.arr) and n > 0:\n if n <= self.cnt:\n self.cnt -= n\n ... | 0 | You are given two strings `stamp` and `target`. Initially, there is a string `s` of length `target.length` with all `s[i] == '?'`.
In one turn, you can place `stamp` over `s` and replace every letter in the `s` with the corresponding letter from `stamp`.
* For example, if `stamp = "abc "` and `target = "abcba "`, t... | null |
Python Solution with Pointer O(n) time where n is len of encoding O(1) space | rle-iterator | 0 | 1 | \n# Code\n```\nclass RLEIterator:\n\n def __init__(self, encoding: List[int]):\n self.encoding = encoding\n self.ptr = 0\n\n def next(self, n: int) -> int:\n if self.ptr > len(self.encoding)-2:\n return -1\n while not self.encoding[self.ptr]:\n self.ptr += 2\n... | 0 | 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 Solution with Pointer O(n) time where n is len of encoding O(1) space | rle-iterator | 0 | 1 | \n# Code\n```\nclass RLEIterator:\n\n def __init__(self, encoding: List[int]):\n self.encoding = encoding\n self.ptr = 0\n\n def next(self, n: int) -> int:\n if self.ptr > len(self.encoding)-2:\n return -1\n while not self.encoding[self.ptr]:\n self.ptr += 2\n... | 0 | You are given two strings `stamp` and `target`. Initially, there is a string `s` of length `target.length` with all `s[i] == '?'`.
In one turn, you can place `stamp` over `s` and replace every letter in the `s` with the corresponding letter from `stamp`.
* For example, if `stamp = "abc "` and `target = "abcba "`, t... | null |
Beats 99.11%of users with Python3 // CLEAN SOL | rle-iterator | 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 | 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 |
Beats 99.11%of users with Python3 // CLEAN SOL | rle-iterator | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given two strings `stamp` and `target`. Initially, there is a string `s` of length `target.length` with all `s[i] == '?'`.
In one turn, you can place `stamp` over `s` and replace every letter in the `s` with the corresponding letter from `stamp`.
* For example, if `stamp = "abc "` and `target = "abcba "`, t... | null |
Python3 Easy solution, beat 82% | rle-iterator | 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 | 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 solution, beat 82% | rle-iterator | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given two strings `stamp` and `target`. Initially, there is a string `s` of length `target.length` with all `s[i] == '?'`.
In one turn, you can place `stamp` over `s` and replace every letter in the `s` with the corresponding letter from `stamp`.
* For example, if `stamp = "abc "` and `target = "abcba "`, t... | null |
Python3 - Save timestamp and price into a stack | online-stock-span | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing stack to save local maximum price.\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass StockSpanner:\n\n def __init__(self):\n self.memo = []\n self.timestamp = 0... | 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 |
Python3 - Save timestamp and price into a stack | online-stock-span | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing stack to save local maximum price.\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass StockSpanner:\n\n def __init__(self):\n self.memo = []\n self.timestamp = 0... | 1 | You are given an array of `logs`. Each log is a space-delimited string of words, where the first word is the **identifier**.
There are two types of logs:
* **Letter-logs**: All words (except the identifier) consist of lowercase English letters.
* **Digit-logs**: All words (except the identifier) consist of digits... | null |
Python Solution using Stack || 90%beats | online-stock-span | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nstack\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:90.21%\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:53.61%\n<!-- Add your space complexity her... | 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 Solution using Stack || 90%beats | online-stock-span | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nstack\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:90.21%\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:53.61%\n<!-- Add your space complexity her... | 1 | You are given an array of `logs`. Each log is a space-delimited string of words, where the first word is the **identifier**.
There are two types of logs:
* **Letter-logs**: All words (except the identifier) consist of lowercase English letters.
* **Digit-logs**: All words (except the identifier) consist of digits... | null |
EASY PYTHON SOLUTION | online-stock-span | 0 | 1 | ```\nclass StockSpanner:\n\n def __init__(self):\n self.st=[]\n self.lst=[]\n \n\n def next(self, price: int) -> int:\n if self.st==[]:\n self.st.append(price)\n self.lst.append(1)\n return 1\n ct=1\n while self.st:\n if self.st... | 9 | 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 PYTHON SOLUTION | online-stock-span | 0 | 1 | ```\nclass StockSpanner:\n\n def __init__(self):\n self.st=[]\n self.lst=[]\n \n\n def next(self, price: int) -> int:\n if self.st==[]:\n self.st.append(price)\n self.lst.append(1)\n return 1\n ct=1\n while self.st:\n if self.st... | 9 | You are given an array of `logs`. Each log is a space-delimited string of words, where the first word is the **identifier**.
There are two types of logs:
* **Letter-logs**: All words (except the identifier) consist of lowercase English letters.
* **Digit-logs**: All words (except the identifier) consist of digits... | null |
Fast and Easy Solution || Leetcode 75 | online-stock-span | 0 | 1 | \n# Code\n```\nclass StockSpanner:\n\n def __init__(self):\n self.stack=[]\n \n\n def next(self, price: int) -> int:\n counter = 1\n while self.stack and self.stack[-1][0] <= price:\n counter += self.stack.pop()[1]\n\n self.stack.append((price,counter))\n retur... | 5 | 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 |
Fast and Easy Solution || Leetcode 75 | online-stock-span | 0 | 1 | \n# Code\n```\nclass StockSpanner:\n\n def __init__(self):\n self.stack=[]\n \n\n def next(self, price: int) -> int:\n counter = 1\n while self.stack and self.stack[-1][0] <= price:\n counter += self.stack.pop()[1]\n\n self.stack.append((price,counter))\n retur... | 5 | You are given an array of `logs`. Each log is a space-delimited string of words, where the first word is the **identifier**.
There are two types of logs:
* **Letter-logs**: All words (except the identifier) consist of lowercase English letters.
* **Digit-logs**: All words (except the identifier) consist of digits... | null |
Solution | numbers-at-most-n-given-digit-set | 1 | 1 | ```C++ []\nclass Solution {\n public:\n int atMostNGivenDigitSet(vector<string>& D, int N) {\n int ans = 0;\n string num = to_string(N);\n\n for (int i = 1; i < num.length(); ++i)\n ans += pow(D.size(), i);\n\n for (int i = 0; i < num.length(); ++i) {\n bool dHasSameNum = false;\n for (const... | 1 | 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 | numbers-at-most-n-given-digit-set | 1 | 1 | ```C++ []\nclass Solution {\n public:\n int atMostNGivenDigitSet(vector<string>& D, int N) {\n int ans = 0;\n string num = to_string(N);\n\n for (int i = 1; i < num.length(); ++i)\n ans += pow(D.size(), i);\n\n for (int i = 0; i < num.length(); ++i) {\n bool dHasSameNum = false;\n for (const... | 1 | Given the `root` node of a binary search tree and two integers `low` and `high`, return _the sum of values of all nodes with a value in the **inclusive** range_ `[low, high]`.
**Example 1:**
**Input:** root = \[10,5,15,3,7,null,18\], low = 7, high = 15
**Output:** 32
**Explanation:** Nodes 7, 10, and 15 are in the ra... | null |
Python 3 || 8 lines, mostly mathematics || T/M: 97% / 98% | numbers-at-most-n-given-digit-set | 0 | 1 | ```\nclass Solution:\n def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:\n\n f = lambda x: sum(ch < x for ch in digits)\n\n d, k = len(digits), len(str(n))\n \n ans = k-1 if d == 1 else (d**k - d)//(d - 1)\n \n for i, ch in enumerate(str(n)):\n\n a... | 4 | 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 |
Python 3 || 8 lines, mostly mathematics || T/M: 97% / 98% | numbers-at-most-n-given-digit-set | 0 | 1 | ```\nclass Solution:\n def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:\n\n f = lambda x: sum(ch < x for ch in digits)\n\n d, k = len(digits), len(str(n))\n \n ans = k-1 if d == 1 else (d**k - d)//(d - 1)\n \n for i, ch in enumerate(str(n)):\n\n a... | 4 | Given the `root` node of a binary search tree and two integers `low` and `high`, return _the sum of values of all nodes with a value in the **inclusive** range_ `[low, high]`.
**Example 1:**
**Input:** root = \[10,5,15,3,7,null,18\], low = 7, high = 15
**Output:** 32
**Explanation:** Nodes 7, 10, and 15 are in the ra... | null |
✔️ [Python3] NOT BEGINNER FRIENDLY, Explained | numbers-at-most-n-given-digit-set | 0 | 1 | We can approach this as a counting problem. Digits of the integer `n` are just slots where we put elements of `digits` (repetition is allowed). For example, for 2 given `digits` and an integer `n` with 3 digits, we can form 3 slots `_ _ _ `, then put there the number of `digits` and multiply them: `2 * 2 * 2 = 8`. That... | 8 | 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 |
✔️ [Python3] NOT BEGINNER FRIENDLY, Explained | numbers-at-most-n-given-digit-set | 0 | 1 | We can approach this as a counting problem. Digits of the integer `n` are just slots where we put elements of `digits` (repetition is allowed). For example, for 2 given `digits` and an integer `n` with 3 digits, we can form 3 slots `_ _ _ `, then put there the number of `digits` and multiply them: `2 * 2 * 2 = 8`. That... | 8 | Given the `root` node of a binary search tree and two integers `low` and `high`, return _the sum of values of all nodes with a value in the **inclusive** range_ `[low, high]`.
**Example 1:**
**Input:** root = \[10,5,15,3,7,null,18\], low = 7, high = 15
**Output:** 32
**Explanation:** Nodes 7, 10, and 15 are in the ra... | null |
Python, confidence destroyer | numbers-at-most-n-given-digit-set | 0 | 1 | This is one of those seemingly-easy problems, that hit you later on as all of your straightforward solutions fail and you start questioning yourself. After trying the first two obvious solutions that come to mind (DFS and BFS) and failing, I figured out the third one (mathy), much later.\n1. DFS, will result in TLE:\n`... | 6 | 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 |
Python, confidence destroyer | numbers-at-most-n-given-digit-set | 0 | 1 | This is one of those seemingly-easy problems, that hit you later on as all of your straightforward solutions fail and you start questioning yourself. After trying the first two obvious solutions that come to mind (DFS and BFS) and failing, I figured out the third one (mathy), much later.\n1. DFS, will result in TLE:\n`... | 6 | Given the `root` node of a binary search tree and two integers `low` and `high`, return _the sum of values of all nodes with a value in the **inclusive** range_ `[low, high]`.
**Example 1:**
**Input:** root = \[10,5,15,3,7,null,18\], low = 7, high = 15
**Output:** 32
**Explanation:** Nodes 7, 10, and 15 are in the ra... | null |
[Python3] dp | numbers-at-most-n-given-digit-set | 0 | 1 | \n```\nclass Solution:\n def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:\n s = str(n)\n prev = 1\n for i, ch in enumerate(reversed(s)): \n k = bisect_left(digits, ch)\n ans = k*len(digits)**i\n if k < len(digits) and digits[k] == ch: ans += prev... | 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 |
[Python3] dp | numbers-at-most-n-given-digit-set | 0 | 1 | \n```\nclass Solution:\n def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int:\n s = str(n)\n prev = 1\n for i, ch in enumerate(reversed(s)): \n k = bisect_left(digits, ch)\n ans = k*len(digits)**i\n if k < len(digits) and digits[k] == ch: ans += prev... | 3 | Given the `root` node of a binary search tree and two integers `low` and `high`, return _the sum of values of all nodes with a value in the **inclusive** range_ `[low, high]`.
**Example 1:**
**Input:** root = \[10,5,15,3,7,null,18\], low = 7, high = 15
**Output:** 32
**Explanation:** Nodes 7, 10, and 15 are in the ra... | null |
Python simple solution | numbers-at-most-n-given-digit-set | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can clearly observe that any numbers where len(number) is less than n will definitely be accepted. Anything more than will obviously fail. That is if n is 345, any 2 digit or 1 digit will pass but 4 digit or more will fail. So we iterate all poss... | 0 | 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 |
Python simple solution | numbers-at-most-n-given-digit-set | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can clearly observe that any numbers where len(number) is less than n will definitely be accepted. Anything more than will obviously fail. That is if n is 345, any 2 digit or 1 digit will pass but 4 digit or more will fail. So we iterate all poss... | 0 | Given the `root` node of a binary search tree and two integers `low` and `high`, return _the sum of values of all nodes with a value in the **inclusive** range_ `[low, high]`.
**Example 1:**
**Input:** root = \[10,5,15,3,7,null,18\], low = 7, high = 15
**Output:** 32
**Explanation:** Nodes 7, 10, and 15 are in the ra... | null |
DP Solution | valid-permutations-for-di-sequence | 0 | 1 | To add a new character to a sequence we only have to consider the last element-\n\nLets say currently DID sequence is 1032- this can form\n\nDIDI - in cases where we end with 3,4\nDIDD - in cases where we end with 0,1,2\n\nSo just use the last element value to create a new sequence.\n\n```\nclass Solution:\n def num... | 1 | You are given a string `s` of length `n` where `s[i]` is either:
* `'D'` means decreasing, or
* `'I'` means increasing.
A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` is called a **valid permutation** if for all valid `i`:
* If `s[i] == 'D'`, then `perm[i] > perm[i + 1]`, an... | null |
DP Solution | valid-permutations-for-di-sequence | 0 | 1 | To add a new character to a sequence we only have to consider the last element-\n\nLets say currently DID sequence is 1032- this can form\n\nDIDI - in cases where we end with 3,4\nDIDD - in cases where we end with 0,1,2\n\nSo just use the last element value to create a new sequence.\n\n```\nclass Solution:\n def num... | 1 | You are given an array of points in the **X-Y** plane `points` where `points[i] = [xi, yi]`.
Return _the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes_. If there is not any such rectangle, return `0`.
**Example 1:**
**Input:** points = \[\[1,1\],\[1,3\],\[3,1\],\[3,3\]... | null |
python DP top down + backtracking | valid-permutations-for-di-sequence | 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 string `s` of length `n` where `s[i]` is either:
* `'D'` means decreasing, or
* `'I'` means increasing.
A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` is called a **valid permutation** if for all valid `i`:
* If `s[i] == 'D'`, then `perm[i] > perm[i + 1]`, an... | null |
python DP top down + backtracking | valid-permutations-for-di-sequence | 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 points in the **X-Y** plane `points` where `points[i] = [xi, yi]`.
Return _the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes_. If there is not any such rectangle, return `0`.
**Example 1:**
**Input:** points = \[\[1,1\],\[1,3\],\[3,1\],\[3,3\]... | null |
Editorial-like solution, simple to understand with multiple approaches from Brute-force to optimal | valid-permutations-for-di-sequence | 0 | 1 | The problem asks what is the number of valid permutations that follow a string of `"DI"` instructions, if a number `i` is used in a podition with `\'D\'` the next number must be in the range `0 <= j < i`, likewise if it\'s in an `\'I\'` position the next number must be `i < j <= n` where `n` is the length if the string... | 0 | You are given a string `s` of length `n` where `s[i]` is either:
* `'D'` means decreasing, or
* `'I'` means increasing.
A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` is called a **valid permutation** if for all valid `i`:
* If `s[i] == 'D'`, then `perm[i] > perm[i + 1]`, an... | null |
Editorial-like solution, simple to understand with multiple approaches from Brute-force to optimal | valid-permutations-for-di-sequence | 0 | 1 | The problem asks what is the number of valid permutations that follow a string of `"DI"` instructions, if a number `i` is used in a podition with `\'D\'` the next number must be in the range `0 <= j < i`, likewise if it\'s in an `\'I\'` position the next number must be `i < j <= n` where `n` is the length if the string... | 0 | You are given an array of points in the **X-Y** plane `points` where `points[i] = [xi, yi]`.
Return _the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes_. If there is not any such rectangle, return `0`.
**Example 1:**
**Input:** points = \[\[1,1\],\[1,3\],\[3,1\],\[3,3\]... | null |
Python (Simple DP) | valid-permutations-for-di-sequence | 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 string `s` of length `n` where `s[i]` is either:
* `'D'` means decreasing, or
* `'I'` means increasing.
A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` is called a **valid permutation** if for all valid `i`:
* If `s[i] == 'D'`, then `perm[i] > perm[i + 1]`, an... | null |
Python (Simple DP) | valid-permutations-for-di-sequence | 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 points in the **X-Y** plane `points` where `points[i] = [xi, yi]`.
Return _the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes_. If there is not any such rectangle, return `0`.
**Example 1:**
**Input:** points = \[\[1,1\],\[1,3\],\[3,1\],\[3,3\]... | null |
[Python3] | Top-Down DP O(N^2) | valid-permutations-for-di-sequence | 0 | 1 | ```\nclass Solution:\n def numPermsDISequence(self, s: str) -> int:\n n,ans = len(s),0\n mod = 1_00_00_00_000 + 7\n memo = [[-1] * 201 for i in range(201)]\n def dp(ind,prevNum):\n if ind < 0:\n return 1\n if memo[ind][prevNum] != -1:\n ... | 0 | You are given a string `s` of length `n` where `s[i]` is either:
* `'D'` means decreasing, or
* `'I'` means increasing.
A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` is called a **valid permutation** if for all valid `i`:
* If `s[i] == 'D'`, then `perm[i] > perm[i + 1]`, an... | null |
[Python3] | Top-Down DP O(N^2) | valid-permutations-for-di-sequence | 0 | 1 | ```\nclass Solution:\n def numPermsDISequence(self, s: str) -> int:\n n,ans = len(s),0\n mod = 1_00_00_00_000 + 7\n memo = [[-1] * 201 for i in range(201)]\n def dp(ind,prevNum):\n if ind < 0:\n return 1\n if memo[ind][prevNum] != -1:\n ... | 0 | You are given an array of points in the **X-Y** plane `points` where `points[i] = [xi, yi]`.
Return _the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes_. If there is not any such rectangle, return `0`.
**Example 1:**
**Input:** points = \[\[1,1\],\[1,3\],\[3,1\],\[3,3\]... | null |
[Python3] top-down dp | valid-permutations-for-di-sequence | 0 | 1 | \n```\nclass Solution:\n def numPermsDISequence(self, s: str) -> int:\n \n @cache \n def fn(i, x): \n """Return number of valid permutation given x numbers smaller than previous one."""\n if i == len(s): return 1 \n if s[i] == "D": \n if x == 0: re... | 1 | You are given a string `s` of length `n` where `s[i]` is either:
* `'D'` means decreasing, or
* `'I'` means increasing.
A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` is called a **valid permutation** if for all valid `i`:
* If `s[i] == 'D'`, then `perm[i] > perm[i + 1]`, an... | null |
[Python3] top-down dp | valid-permutations-for-di-sequence | 0 | 1 | \n```\nclass Solution:\n def numPermsDISequence(self, s: str) -> int:\n \n @cache \n def fn(i, x): \n """Return number of valid permutation given x numbers smaller than previous one."""\n if i == len(s): return 1 \n if s[i] == "D": \n if x == 0: re... | 1 | You are given an array of points in the **X-Y** plane `points` where `points[i] = [xi, yi]`.
Return _the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes_. If there is not any such rectangle, return `0`.
**Example 1:**
**Input:** points = \[\[1,1\],\[1,3\],\[3,1\],\[3,3\]... | null |
Python || Sliding Window Technique | fruit-into-baskets | 0 | 1 | ```\nfrom collections import Counter\n\nMAX_BASKETS = 2\n\n\nclass Solution:\n def totalFruit(self, fruits: list[int]) -> int:\n # return self.ensure_first_that_fruit_can_be_added(fruits)\n return self.ensure_later_that_fruit_can_be_added(fruits)\n\n @staticmethod\n def ensure_first_that_fruit_ca... | 1 | You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array `fruits` where `fruits[i]` is the **type** of fruit the `ith` tree produces.
You want to collect as much fruit as possible. However, the owner has some strict rules that you must foll... | null |
Python || Sliding Window Technique | fruit-into-baskets | 0 | 1 | ```\nfrom collections import Counter\n\nMAX_BASKETS = 2\n\n\nclass Solution:\n def totalFruit(self, fruits: list[int]) -> int:\n # return self.ensure_first_that_fruit_can_be_added(fruits)\n return self.ensure_later_that_fruit_can_be_added(fruits)\n\n @staticmethod\n def ensure_first_that_fruit_ca... | 1 | Given a string s, return _the number of **distinct non-empty subsequences** of_ `s`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative... | null |
My Solution using Sliding Window | fruit-into-baskets | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSliding Window approach\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Use Two Pointers left(i) and right(j)\n2. Keep adding the fruits[j] into the hashmap and keep checking if length of the map is > k(2) \n3. ... | 1 | You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array `fruits` where `fruits[i]` is the **type** of fruit the `ith` tree produces.
You want to collect as much fruit as possible. However, the owner has some strict rules that you must foll... | null |
My Solution using Sliding Window | fruit-into-baskets | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSliding Window approach\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Use Two Pointers left(i) and right(j)\n2. Keep adding the fruits[j] into the hashmap and keep checking if length of the map is > k(2) \n3. ... | 1 | Given a string s, return _the number of **distinct non-empty subsequences** of_ `s`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative... | null |
Solution | fruit-into-baskets | 1 | 1 | ```C++ []\nconst static auto fastio = [] {\n ios::sync_with_stdio(0);\n cin.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n int totalFruit(vector<int>& fruits)\n {\n if (fruits.empty())\n return 0;\n int maxSequence = 0;\n int firstType = -1, lastType = -1;\n ... | 1 | You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array `fruits` where `fruits[i]` is the **type** of fruit the `ith` tree produces.
You want to collect as much fruit as possible. However, the owner has some strict rules that you must foll... | null |
Solution | fruit-into-baskets | 1 | 1 | ```C++ []\nconst static auto fastio = [] {\n ios::sync_with_stdio(0);\n cin.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n int totalFruit(vector<int>& fruits)\n {\n if (fruits.empty())\n return 0;\n int maxSequence = 0;\n int firstType = -1, lastType = -1;\n ... | 1 | Given a string s, return _the number of **distinct non-empty subsequences** of_ `s`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative... | null |
Python solution faster than 97% | fruit-into-baskets | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nusing a dictionary to store the begining and end index of the fruits stored in the bucket.\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... | 1 | You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array `fruits` where `fruits[i]` is the **type** of fruit the `ith` tree produces.
You want to collect as much fruit as possible. However, the owner has some strict rules that you must foll... | null |
Python solution faster than 97% | fruit-into-baskets | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nusing a dictionary to store the begining and end index of the fruits stored in the bucket.\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... | 1 | Given a string s, return _the number of **distinct non-empty subsequences** of_ `s`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative... | null |
Fastest solution Python | fruit-into-baskets | 0 | 1 | # Intuition\nImagine you are standing near the tree `i`. You have to choose between two options:\n 1. Add `fruits[i]` to one of your busket which has the same type of fruits\n 2. Empty one of your busket and put there `fruits[i]`\n\nThink about the information you need to know about the trees `[0, i - 1]` in order to p... | 1 | You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array `fruits` where `fruits[i]` is the **type** of fruit the `ith` tree produces.
You want to collect as much fruit as possible. However, the owner has some strict rules that you must foll... | null |
Fastest solution Python | fruit-into-baskets | 0 | 1 | # Intuition\nImagine you are standing near the tree `i`. You have to choose between two options:\n 1. Add `fruits[i]` to one of your busket which has the same type of fruits\n 2. Empty one of your busket and put there `fruits[i]`\n\nThink about the information you need to know about the trees `[0, i - 1]` in order to p... | 1 | Given a string s, return _the number of **distinct non-empty subsequences** of_ `s`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative... | null |
Sliding window approach , beats 82.41% | fruit-into-baskets | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince the question was asking about longest sub array kind of thing so i have selected this approach.Please leave any suggestion if you have any idea to optimize this code. \n\n# Approach\n<!-- Describe your approach to solving the proble... | 1 | You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array `fruits` where `fruits[i]` is the **type** of fruit the `ith` tree produces.
You want to collect as much fruit as possible. However, the owner has some strict rules that you must foll... | null |
Sliding window approach , beats 82.41% | fruit-into-baskets | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince the question was asking about longest sub array kind of thing so i have selected this approach.Please leave any suggestion if you have any idea to optimize this code. \n\n# Approach\n<!-- Describe your approach to solving the proble... | 1 | Given a string s, return _the number of **distinct non-empty subsequences** of_ `s`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative... | null |
|| 🐍 Python3 O(n) Easiest ✔ Solution 🔥 || Beats 97% (Runtime) & 80% (Memory) || | fruit-into-baskets | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create a dictionary where the fruits will be stored as keys and their count as values.\n2. A start variable is initialized pointing to first tree i.e. 0th index.\n3. Iterating through the fruits list, we update the count in the dictionary.\n4. If a... | 1 | You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array `fruits` where `fruits[i]` is the **type** of fruit the `ith` tree produces.
You want to collect as much fruit as possible. However, the owner has some strict rules that you must foll... | null |
|| 🐍 Python3 O(n) Easiest ✔ Solution 🔥 || Beats 97% (Runtime) & 80% (Memory) || | fruit-into-baskets | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create a dictionary where the fruits will be stored as keys and their count as values.\n2. A start variable is initialized pointing to first tree i.e. 0th index.\n3. Iterating through the fruits list, we update the count in the dictionary.\n4. If a... | 1 | Given a string s, return _the number of **distinct non-empty subsequences** of_ `s`. Since the answer may be very large, return it **modulo** `109 + 7`.
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative... | null |
Python3 Solution | sort-array-by-parity | 0 | 1 | \n```\nclass Solution:\n def sortArrayByParity(self, nums: List[int]) -> List[int]:\n return sorted(nums,key=lambda x:x%2)\n \n``` | 2 | 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 |
Python3 Solution | sort-array-by-parity | 0 | 1 | \n```\nclass Solution:\n def sortArrayByParity(self, nums: List[int]) -> List[int]:\n return sorted(nums,key=lambda x:x%2)\n \n``` | 2 | Given an array of integers `arr`, return _`true` if and only if it is a valid mountain array_.
Recall that arr is a mountain array if and only if:
* `arr.length >= 3`
* There exists some `i` with `0 < i < arr.length - 1` such that:
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
* `arr[i] > arr[i + 1]... | null |
Solution of sort array by parity problem | sort-array-by-parity | 0 | 1 | # Approach\nSolved using two-pointer algorithm\n- Step one: create two_pointers: `left` and `right`\n- Step two: create new list called `answer` \n- Step three: if number is even: `answer[left] = i` and `left += 1`\n- Step four: if number is odd: `answer[right] = i` and `right -= 1`\n- Step five: \u0441ontinue this pro... | 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 |
Solution of sort array by parity problem | sort-array-by-parity | 0 | 1 | # Approach\nSolved using two-pointer algorithm\n- Step one: create two_pointers: `left` and `right`\n- Step two: create new list called `answer` \n- Step three: if number is even: `answer[left] = i` and `left += 1`\n- Step four: if number is odd: `answer[right] = i` and `right -= 1`\n- Step five: \u0441ontinue this pro... | 1 | Given an array of integers `arr`, return _`true` if and only if it is a valid mountain array_.
Recall that arr is a mountain array if and only if:
* `arr.length >= 3`
* There exists some `i` with `0 < i < arr.length - 1` such that:
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
* `arr[i] > arr[i + 1]... | null |
5 lines || beats 94.30% time complexity ||beats 96.40 % space complexity || simple explanation 💡 | sort-array-by-parity | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBasic Array traversal is used.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nConsider two arrays as even and odd.Then traverse the array and if the `nums[i]` is even then append it to even array, else append it t... | 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 |
5 lines || beats 94.30% time complexity ||beats 96.40 % space complexity || simple explanation 💡 | sort-array-by-parity | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBasic Array traversal is used.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nConsider two arrays as even and odd.Then traverse the array and if the `nums[i]` is even then append it to even array, else append it t... | 1 | Given an array of integers `arr`, return _`true` if and only if it is a valid mountain array_.
Recall that arr is a mountain array if and only if:
* `arr.length >= 3`
* There exists some `i` with `0 < i < arr.length - 1` such that:
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
* `arr[i] > arr[i + 1]... | null |
Solution | super-palindromes | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int superpalindromesInRange(string left, string right) {\n int ans = 9 >= stol(left) && 9 <= stol(right) ? 1 : 0;\n for (int dig = 1; dig < 10; dig++) {\n bool isOdd = dig % 2 && dig != 1;\n int innerLen = (dig >> 1) - 1,\n inn... | 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 |
Solution | super-palindromes | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int superpalindromesInRange(string left, string right) {\n int ans = 9 >= stol(left) && 9 <= stol(right) ? 1 : 0;\n for (int dig = 1; dig < 10; dig++) {\n bool isOdd = dig % 2 && dig != 1;\n int innerLen = (dig >> 1) - 1,\n inn... | 1 | A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where:
* `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
* `s[i] == 'D'` if `perm[i] > perm[i + 1]`.
Given a string `s`, reconstruct the permutation `perm` and return it. If there are ... | null |
Efficient in terms of number of iterations for a very large range | super-palindromes | 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. -->\n1. Generate Base palindromes -\n- Iterate over a range of numbers, considering both odd and even lengths. This is done by createing a palindrome from each number by ... | 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 |
Efficient in terms of number of iterations for a very large range | super-palindromes | 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. -->\n1. Generate Base palindromes -\n- Iterate over a range of numbers, considering both odd and even lengths. This is done by createing a palindrome from each number by ... | 0 | A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where:
* `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
* `s[i] == 'D'` if `perm[i] > perm[i + 1]`.
Given a string `s`, reconstruct the permutation `perm` and return it. If there are ... | null |
easyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy | super-palindromes | 0 | 1 | # Very easyyyyyyyy\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# kindly upvote\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.... | 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 |
easyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy | super-palindromes | 0 | 1 | # Very easyyyyyyyy\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# kindly upvote\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.... | 0 | A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where:
* `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
* `s[i] == 'D'` if `perm[i] > perm[i + 1]`.
Given a string `s`, reconstruct the permutation `perm` and return it. If there are ... | null |
Python (Simple Maths) | super-palindromes | 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 |
Python (Simple Maths) | super-palindromes | 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 permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where:
* `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
* `s[i] == 'D'` if `perm[i] > perm[i + 1]`.
Given a string `s`, reconstruct the permutation `perm` and return it. If there are ... | null |
Slow and ugly but one of the first genuine solutions? | super-palindromes | 0 | 1 | # Intuition\nSeems a bit contrived and like one of those annoying string manipulation challenges.\n\n# Approach\nTake the square root of the target range to have a smaller set of candidates. The difficult/annoying part is generating the palindromes, which involves increasing the innermost digits to get the next largest... | 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 |
Slow and ugly but one of the first genuine solutions? | super-palindromes | 0 | 1 | # Intuition\nSeems a bit contrived and like one of those annoying string manipulation challenges.\n\n# Approach\nTake the square root of the target range to have a smaller set of candidates. The difficult/annoying part is generating the palindromes, which involves increasing the innermost digits to get the next largest... | 0 | A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where:
* `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
* `s[i] == 'D'` if `perm[i] > perm[i + 1]`.
Given a string `s`, reconstruct the permutation `perm` and return it. If there are ... | null |
superpalidrome logic python3 | super-palindromes | 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 |
superpalidrome logic python3 | super-palindromes | 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 permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where:
* `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
* `s[i] == 'D'` if `perm[i] > perm[i + 1]`.
Given a string `s`, reconstruct the permutation `perm` and return it. If there are ... | null |
[Python3] Solution with explanation | super-palindromes | 0 | 1 | We separately iterate over palindromes with an even and odd number of digits, comparing the value with the square root of the resulting number including some conditions.\nTo form an **odd** digits palindrome we use left part with addition of reversed left part withiout last digit. \n```\npal = num_str + num_str[:-1][::... | 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 |
[Python3] Solution with explanation | super-palindromes | 0 | 1 | We separately iterate over palindromes with an even and odd number of digits, comparing the value with the square root of the resulting number including some conditions.\nTo form an **odd** digits palindrome we use left part with addition of reversed left part withiout last digit. \n```\npal = num_str + num_str[:-1][::... | 1 | A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where:
* `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
* `s[i] == 'D'` if `perm[i] > perm[i + 1]`.
Given a string `s`, reconstruct the permutation `perm` and return it. If there are ... | null |
python3 Solution | sum-of-subarray-minimums | 0 | 1 | \n```\nclass Solution:\n def sumSubarrayMins(self, arr: List[int]) -> int:\n n=len(arr)\n left=[1]*n\n dec_q=[(arr[0],1)]\n for i in range(1,n):\n while dec_q and arr[i]<=dec_q[-1][0]:\n left[i]+=dec_q.pop()[1]\n \n dec_q.append((arr[i],... | 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 |
python3 Solution | sum-of-subarray-minimums | 0 | 1 | \n```\nclass Solution:\n def sumSubarrayMins(self, arr: List[int]) -> int:\n n=len(arr)\n left=[1]*n\n dec_q=[(arr[0],1)]\n for i in range(1,n):\n while dec_q and arr[i]<=dec_q[-1][0]:\n left[i]+=dec_q.pop()[1]\n \n dec_q.append((arr[i],... | 1 | Given an array of strings `words`, return _the smallest string that contains each string in_ `words` _as a substring_. If there are multiple valid strings of the smallest length, return **any of them**.
You may assume that no string in `words` is a substring of another string in `words`.
**Example 1:**
**Input:** wo... | null |
[Python 3]Monotonic stack boundry | sum-of-subarray-minimums | 0 | 1 | ```\nclass Solution:\n def sumSubarrayMins(self, arr: List[int]) -> int:\n M = 10 ** 9 + 7\n\n # right bound for current number as minimum\n\t\tq = []\n n = len(arr)\n right = [n-1] * n \n \n for i in range(n):\n # must put the equal sign to one of the boun... | 11 | 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 |
[Python 3]Monotonic stack boundry | sum-of-subarray-minimums | 0 | 1 | ```\nclass Solution:\n def sumSubarrayMins(self, arr: List[int]) -> int:\n M = 10 ** 9 + 7\n\n # right bound for current number as minimum\n\t\tq = []\n n = len(arr)\n right = [n-1] * n \n \n for i in range(n):\n # must put the equal sign to one of the boun... | 11 | Given an array of strings `words`, return _the smallest string that contains each string in_ `words` _as a substring_. If there are multiple valid strings of the smallest length, return **any of them**.
You may assume that no string in `words` is a substring of another string in `words`.
**Example 1:**
**Input:** wo... | null |
Python3 | Short Solution | Min Stack | sum-of-subarray-minimums | 0 | 1 | # Complexity\n- Time complexity: $$O(N)$$\n\n- Space complexity: $$O(N)$$\n\n# Code\n```\nclass Solution:\n def sumSubarrayMins(self, arr: List[int]) -> int:\n MOD = 10**9+7\n ans = 0\n minstack = [(-inf, -1, 0)]\n for i, x in enumerate(arr):\n while minstack[-1][0] >= x:\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 |
Python3 | Short Solution | Min Stack | sum-of-subarray-minimums | 0 | 1 | # Complexity\n- Time complexity: $$O(N)$$\n\n- Space complexity: $$O(N)$$\n\n# Code\n```\nclass Solution:\n def sumSubarrayMins(self, arr: List[int]) -> int:\n MOD = 10**9+7\n ans = 0\n minstack = [(-inf, -1, 0)]\n for i, x in enumerate(arr):\n while minstack[-1][0] >= x:\n ... | 1 | Given an array of strings `words`, return _the smallest string that contains each string in_ `words` _as a substring_. If there are multiple valid strings of the smallest length, return **any of them**.
You may assume that no string in `words` is a substring of another string in `words`.
**Example 1:**
**Input:** wo... | null |
Simple solution in Python3 | smallest-range-i | 0 | 1 | # Intuition\nHere we have:\n- `nums` list of integers and `k`\n- we should apply `[x - k, x + k]` operation for each `nums[i]` **optimally** to extract **smallest range** between two integers in `nums`\n\nLet\'s act greedily:\n- define `left` and `right`, then find **difference** between them in range above.\n\nNote th... | 2 | 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 in Python3 | smallest-range-i | 0 | 1 | # Intuition\nHere we have:\n- `nums` list of integers and `k`\n- we should apply `[x - k, x + k]` operation for each `nums[i]` **optimally** to extract **smallest range** between two integers in `nums`\n\nLet\'s act greedily:\n- define `left` and `right`, then find **difference** between them in range above.\n\nNote th... | 2 | You are given an array of `n` strings `strs`, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
* For example, `strs = [ "abc ", "bce ", "cae "]` can be arranged as follows:
abc
bce
cae
You want to **delete** the columns that are **not sorted lexicographically... | null |
SIMPLE PYTHON | smallest-range-i | 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`.
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 PYTHON | smallest-range-i | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given an array of `n` strings `strs`, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
* For example, `strs = [ "abc ", "bce ", "cae "]` can be arranged as follows:
abc
bce
cae
You want to **delete** the columns that are **not sorted lexicographically... | null |
1 line Python | smallest-range-i | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def smallestRangeI(self, A: List[int], K: int) -> int:\n a=max((max(A) - min(A) - 2*K), 0)\n return a\n``` | 1 | 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 |
1 line Python | smallest-range-i | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def smallestRangeI(self, A: List[int], K: int) -> int:\n a=max((max(A) - min(A) - 2*K), 0)\n return a\n``` | 1 | You are given an array of `n` strings `strs`, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
* For example, `strs = [ "abc ", "bce ", "cae "]` can be arranged as follows:
abc
bce
cae
You want to **delete** the columns that are **not sorted lexicographically... | null |
Python O(n) by min & Max. 85%+ [w/ Visualization ] | smallest-range-i | 0 | 1 | Python O(n) by min & Max.\n\n---\n\n**Hint**:\n\nThe key factor to determine the gap size is the value of minimum of A (i.e., min(A) ) as well as maximum of A (i.e., max(A) ).\n\nFor B, every point value can go from A[i]-k to A[i]+k\nWhat we want is the smallest gap size between maximum and minimum.\n\n---\n\n**Diagram... | 24 | 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 |
Python O(n) by min & Max. 85%+ [w/ Visualization ] | smallest-range-i | 0 | 1 | Python O(n) by min & Max.\n\n---\n\n**Hint**:\n\nThe key factor to determine the gap size is the value of minimum of A (i.e., min(A) ) as well as maximum of A (i.e., max(A) ).\n\nFor B, every point value can go from A[i]-k to A[i]+k\nWhat we want is the smallest gap size between maximum and minimum.\n\n---\n\n**Diagram... | 24 | You are given an array of `n` strings `strs`, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
* For example, `strs = [ "abc ", "bce ", "cae "]` can be arranged as follows:
abc
bce
cae
You want to **delete** the columns that are **not sorted lexicographically... | null |
Solution | smallest-range-i | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int smallestRangeI(vector<int>& nums, int k) {\n int nsize = nums.size();\n int max=nums[0];\n int min=nums[0];\n for(int i=1; i<nsize; i++)\n {\n if(nums[i]>max)\n {\n max=nums[i];\n }\n ... | 2 | 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 |
Solution | smallest-range-i | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int smallestRangeI(vector<int>& nums, int k) {\n int nsize = nums.size();\n int max=nums[0];\n int min=nums[0];\n for(int i=1; i<nsize; i++)\n {\n if(nums[i]>max)\n {\n max=nums[i];\n }\n ... | 2 | You are given an array of `n` strings `strs`, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
* For example, `strs = [ "abc ", "bce ", "cae "]` can be arranged as follows:
abc
bce
cae
You want to **delete** the columns that are **not sorted lexicographically... | null |
Very Easy to Understand 💯💯 | C++, Java, Python, Python, C#, JavaScript, Kotlin | smallest-range-i | 1 | 1 | # C++\n```\nclass Solution {\npublic:\n int smallestRangeI(vector<int>& nums, int k) {\n int maxNum=nums[0],minNum=nums[0];\n for(auto pr:nums){\n maxNum=max(maxNum,pr);\n minNum=min(minNum,pr);\n }\n return max(0,(maxNum-minNum)-2*k);\n }\n};\n```\n\n# Java\n```\... | 1 | 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 |
Very Easy to Understand 💯💯 | C++, Java, Python, Python, C#, JavaScript, Kotlin | smallest-range-i | 1 | 1 | # C++\n```\nclass Solution {\npublic:\n int smallestRangeI(vector<int>& nums, int k) {\n int maxNum=nums[0],minNum=nums[0];\n for(auto pr:nums){\n maxNum=max(maxNum,pr);\n minNum=min(minNum,pr);\n }\n return max(0,(maxNum-minNum)-2*k);\n }\n};\n```\n\n# Java\n```\... | 1 | You are given an array of `n` strings `strs`, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
* For example, `strs = [ "abc ", "bce ", "cae "]` can be arranged as follows:
abc
bce
cae
You want to **delete** the columns that are **not sorted lexicographically... | null |
simple and fast solution in python | smallest-range-i | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- 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 and fast solution in python | smallest-range-i | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- 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 `n` strings `strs`, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
* For example, `strs = [ "abc ", "bce ", "cae "]` can be arranged as follows:
abc
bce
cae
You want to **delete** the columns that are **not sorted lexicographically... | null |
Solution | smallest-range-i | 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. -->\nsimple mathematical approch and logical thinking \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\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 |
Solution | smallest-range-i | 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. -->\nsimple mathematical approch and logical thinking \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!... | 0 | You are given an array of `n` strings `strs`, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
* For example, `strs = [ "abc ", "bce ", "cae "]` can be arranged as follows:
abc
bce
cae
You want to **delete** the columns that are **not sorted lexicographically... | null |
Very Detailed Python O(n) Solution - Intuition and Approach Explained | smallest-range-i | 0 | 1 | # Intuition\n- The goal is to minimize the difference between the maximum and minimum elements in the array after applying the operation at most once for each index.\n- To minimize this difference, you want to decrease the maximum value and increase the minimum value.\n- You are allowed to add any integer in the range ... | 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 |
Very Detailed Python O(n) Solution - Intuition and Approach Explained | smallest-range-i | 0 | 1 | # Intuition\n- The goal is to minimize the difference between the maximum and minimum elements in the array after applying the operation at most once for each index.\n- To minimize this difference, you want to decrease the maximum value and increase the minimum value.\n- You are allowed to add any integer in the range ... | 0 | You are given an array of `n` strings `strs`, all of the same length.
The strings can be arranged such that there is one on each line, making a grid.
* For example, `strs = [ "abc ", "bce ", "cae "]` can be arranged as follows:
abc
bce
cae
You want to **delete** the columns that are **not sorted lexicographically... | null |
BFS solution | snakes-and-ladders | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince BFS can be used to find the shortest part, hence it can be used to find the solution\n# Approach\n<!-- Describe your approach to solving the problem. -->\nAssume the board is a directed graph, with the ladders pointing to an higher ... | 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 |
BFS solution | snakes-and-ladders | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince BFS can be used to find the shortest part, hence it can be used to find the solution\n# Approach\n<!-- Describe your approach to solving the problem. -->\nAssume the board is a directed graph, with the ladders pointing to an higher ... | 1 | You are given an integer array `nums`. In one move, you can pick an index `i` where `0 <= i < nums.length` and increment `nums[i]` by `1`.
Return _the minimum number of moves to make every value in_ `nums` _**unique**_.
The test cases are generated so that the answer fits in a 32-bit integer.
**Example 1:**
**Input... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.