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
Very Easy || 100% || Fully Explained || Java, C++, Python, JavaScript, Python3 || DFS || Recursion
flood-fill
1
1
**Problem Statement:**\n\nAn image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.\n\nYou are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc].\n\nTo perform a flood fill, consider the...
93
An image is represented by an `m x n` integer grid `image` where `image[i][j]` represents the pixel value of the image. You are also given three integers `sr`, `sc`, and `color`. You should perform a **flood fill** on the image starting from the pixel `image[sr][sc]`. To perform a **flood fill**, consider the startin...
Write a recursive function that paints the pixel if it's the correct color, then recurses on neighboring pixels.
Solution
flood-fill
1
1
```C++ []\nclass Solution {\npublic:\n vector<pair<int,int>> dirs = {{0,-1}, {0,1}, {1,0}, {-1,0}};\n void dfs(int i, int j, vector<vector<int>>& image, int oldColor, int newColor){\n int n = image.size();\n int m = image[0].size();\n if( i < 0 || i>=n || j < 0 || j >= m || image[i][j] != old...
2
An image is represented by an `m x n` integer grid `image` where `image[i][j]` represents the pixel value of the image. You are also given three integers `sr`, `sc`, and `color`. You should perform a **flood fill** on the image starting from the pixel `image[sr][sc]`. To perform a **flood fill**, consider the startin...
Write a recursive function that paints the pixel if it's the correct color, then recurses on neighboring pixels.
Simple dfs python
flood-fill
0
1
\n\n# Code\n```\nclass Solution:\n def floodFill(self, grid: List[List[int]], x: int, y: int, color: int) -> List[List[int]]:\n curr = grid[x][y]\n n = len(grid)\n m = len(grid[0])\n def dfs(i, j):\n if 0 <= i < n and 0 <= j < m and grid[i][j] == curr and grid[i][j] != color:\n...
3
An image is represented by an `m x n` integer grid `image` where `image[i][j]` represents the pixel value of the image. You are also given three integers `sr`, `sc`, and `color`. You should perform a **flood fill** on the image starting from the pixel `image[sr][sc]`. To perform a **flood fill**, consider the startin...
Write a recursive function that paints the pixel if it's the correct color, then recurses on neighboring pixels.
✔️ Python Flood Fill Algorithm | 98% Faster 🔥
flood-fill
0
1
**\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n\nVisit this blog to learn Python tips and techniques and to find a Leetcode solution with an explanation: https://www.python-techs.com/\n\n**Solution:**\n```\nclass Solution:\n def floodFill(self, image: List[List[int]], sr: int, sc: int, co...
52
An image is represented by an `m x n` integer grid `image` where `image[i][j]` represents the pixel value of the image. You are also given three integers `sr`, `sc`, and `color`. You should perform a **flood fill** on the image starting from the pixel `image[sr][sc]`. To perform a **flood fill**, consider the startin...
Write a recursive function that paints the pixel if it's the correct color, then recurses on neighboring pixels.
C++ stack vs vector/Python list
asteroid-collision
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThat is a stack problem. One approach is using C++ STL stack. In the second version, Python code uses list and C++ uses vector!\n\nThe programming becomes easier!\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# C...
2
We are given an array `asteroids` of integers representing asteroids in a row. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed. Find out the state of the asteroids after all collisio...
Say a row of asteroids is stable. What happens when a new asteroid is added on the right?
🤩 Simple Iteration | No Stack needed | Beats 100% | Runtime 1ms
asteroid-collision
1
1
# Intuition\nThe intuition behind the optimized approach is to simulate the asteroid collisions while using an in-place modification of the original array. By iterating through the array and updating it in a way that ensures surviving asteroids are placed in the front of the array, we can avoid using an additional stac...
46
We are given an array `asteroids` of integers representing asteroids in a row. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed. Find out the state of the asteroids after all collisio...
Say a row of asteroids is stable. What happens when a new asteroid is added on the right?
[Python 3] Simple stack solution
asteroid-collision
0
1
```python3 []\nclass Solution:\n def asteroidCollision(self, asteroids: List[int]) -> List[int]:\n stack = []\n for a in asteroids:\n while stack and stack[-1] > 0 > a:\n if stack[-1] < abs(a):\n stack.pop()\n continue\n eli...
16
We are given an array `asteroids` of integers representing asteroids in a row. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed. Find out the state of the asteroids after all collisio...
Say a row of asteroids is stable. What happens when a new asteroid is added on the right?
Solution
parse-lisp-expression
1
1
```C++ []\nclass Solution {\npublic:\n int evaluate(string expression) {\n unordered_map<string, int> map;\n int idx = 0;\n return eval(expression, idx, map);\n }\n int eval(const string& exp, int& idx, const unordered_map<string, int>& map){\n int result;\n idx++;\n i...
1
You are given a string expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the o...
Solution
parse-lisp-expression
1
1
```C++ []\nclass Solution {\npublic:\n int evaluate(string expression) {\n unordered_map<string, int> map;\n int idx = 0;\n return eval(expression, idx, map);\n }\n int eval(const string& exp, int& idx, const unordered_map<string, int>& map){\n int result;\n idx++;\n i...
1
You are given a string expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the occur...
736: Solution with step by step explanation
parse-lisp-expression
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe solution uses a recursive approach to evaluate the expression. Here\'s how it\'s structured:\n\nParsing the expression: Split the given expression into its constituent parts or tokens.\nEvaluating the expression: Recursi...
0
You are given a string expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the o...
736: Solution with step by step explanation
parse-lisp-expression
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe solution uses a recursive approach to evaluate the expression. Here\'s how it\'s structured:\n\nParsing the expression: Split the given expression into its constituent parts or tokens.\nEvaluating the expression: Recursi...
0
You are given a string expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the occur...
Python (Simple Stack)
parse-lisp-expression
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 expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the o...
Python (Simple Stack)
parse-lisp-expression
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 expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the occur...
solve it with python
parse-lisp-expression
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 expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the o...
solve it with python
parse-lisp-expression
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 expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the occur...
Python3 Solution
parse-lisp-expression
0
1
```\nclass Solution:\n def evaluate(self, expression: str) -> int:\n stack = []\n parenEnd = {}\n \n # Get the end parenthesis location \n for idx, ch in enumerate(expression):\n if ch == \'(\':\n stack.append(idx)\n if ch == \')\':\n ...
3
You are given a string expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the o...
Python3 Solution
parse-lisp-expression
0
1
```\nclass Solution:\n def evaluate(self, expression: str) -> int:\n stack = []\n parenEnd = {}\n \n # Get the end parenthesis location \n for idx, ch in enumerate(expression):\n if ch == \'(\':\n stack.append(idx)\n if ch == \')\':\n ...
3
You are given a string expression representing a Lisp-like expression to return the integer value of. The syntax for these expressions is given as follows. * An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer....
* If the expression starts with a digit or '-', it's an integer: return it. * If the expression starts with a letter, it's a variable. Recall it by checking the current scope in reverse order. * Otherwise, group the tokens (variables or expressions) within this expression by counting the "balance" `bal` of the occur...
Solution
monotone-increasing-digits
1
1
```C++ []\nclass Solution {\npublic:\n int monotoneIncreasingDigits(int n) {\n string s = to_string(n);\n while(1){\n bool flag = false;\n for(int i = 1; i < s.length(); i++){\n if(s[i] >= s[i-1]) continue;\n else{\n s[i-1]--;\n ...
1
An integer has **monotone increasing digits** if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`. Given an integer `n`, return _the largest number that is less than or equal to_ `n` _with **monotone increasing digits**_. **Example 1:** **Input:** n = 10 **Output:** 9 **Example 2:** **Input:**...
Build the answer digit by digit, adding the largest possible one that would make the number still less than or equal to N.
Python Solution Using Backtracking || Easy to Understand
monotone-increasing-digits
0
1
```\nclass Solution:\n def monotoneIncreasingDigits(self, n: int) -> int:\n\t\t# max size of string\n self.maxlen = len(str(n))\n\t\t# maximum ans store here\n self.maxans = 0\n self.find(str(n))\n return self.maxans\n \n def find(self,num,idx=0,curnum=\'\',pb=None):\n\t\t# idx is s...
1
An integer has **monotone increasing digits** if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`. Given an integer `n`, return _the largest number that is less than or equal to_ `n` _with **monotone increasing digits**_. **Example 1:** **Input:** n = 10 **Output:** 9 **Example 2:** **Input:**...
Build the answer digit by digit, adding the largest possible one that would make the number still less than or equal to N.
738: Solution with step by step explanation
monotone-increasing-digits
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ndigits = list(str(n))\n```\nWe first convert the integer n to its string representation and then create a list of its characters.\nThis step allows us to easily modify individual digits if needed.\n\n```\nn = len(digits...
1
An integer has **monotone increasing digits** if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`. Given an integer `n`, return _the largest number that is less than or equal to_ `n` _with **monotone increasing digits**_. **Example 1:** **Input:** n = 10 **Output:** 9 **Example 2:** **Input:**...
Build the answer digit by digit, adding the largest possible one that would make the number still less than or equal to N.
Log(n) Time Complexity? | Beats 99% ! | Intuitive Solution ?!
monotone-increasing-digits
0
1
## First, the Intuition ...\nFor any arbitrary integer *n*, if its digits are in non-decreasing order from left to right, then it satisfies the monotonic increasing condition. \n\nHowever, if we encounter a digit that\'s smaller than the preceding one, we can decrement the previous digit by one and **replace all digits...
0
An integer has **monotone increasing digits** if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`. Given an integer `n`, return _the largest number that is less than or equal to_ `n` _with **monotone increasing digits**_. **Example 1:** **Input:** n = 10 **Output:** 9 **Example 2:** **Input:**...
Build the answer digit by digit, adding the largest possible one that would make the number still less than or equal to N.
[Python3] Good enough
monotone-increasing-digits
0
1
``` Python3 []\nclass Solution:\n def monotoneIncreasingDigits(self, n: int) -> int:\n digits = []\n final = []\n\n while n:\n digits.append(n%10)\n n //= 10\n \n for i in range(len(digits)):\n if digits[i]<=0 or (i!=len(digits)-1 and digits[i]<digi...
0
An integer has **monotone increasing digits** if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`. Given an integer `n`, return _the largest number that is less than or equal to_ `n` _with **monotone increasing digits**_. **Example 1:** **Input:** n = 10 **Output:** 9 **Example 2:** **Input:**...
Build the answer digit by digit, adding the largest possible one that would make the number still less than or equal to N.
easy||digit dp||intutive aproach
monotone-increasing-digits
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:O(N)\n<!-- Add your space complexity here, e.g. $$O...
0
An integer has **monotone increasing digits** if and only if each pair of adjacent digits `x` and `y` satisfy `x <= y`. Given an integer `n`, return _the largest number that is less than or equal to_ `n` _with **monotone increasing digits**_. **Example 1:** **Input:** n = 10 **Output:** 9 **Example 2:** **Input:**...
Build the answer digit by digit, adding the largest possible one that would make the number still less than or equal to N.
Python Code (Stack approach) | O(n)
daily-temperatures
0
1
# Intuition\nThe problem requires finding the number of days you would have to wait until a warmer temperature. This suggests using a stack to keep track of temperatures and their indices.\n\n# Approach\n1. Initialize an empty stack to store temperatures and their corresponding indices.\n\n2. Initialize a list ans of t...
6
Given an array of integers `temperatures` represents the daily temperatures, return _an array_ `answer` _such that_ `answer[i]` _is the number of days you have to wait after the_ `ith` _day to get a warmer temperature_. If there is no future day for which this is possible, keep `answer[i] == 0` instead. **Example 1:**...
If the temperature is say, 70 today, then in the future a warmer temperature must be either 71, 72, 73, ..., 99, or 100. We could remember when all of them occur next.
740: Beats 97.58%, Solution with step by step explanation
delete-and-earn
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nfrom collections import defaultdict\n\nclass Solution:\n def deleteAndEarn(self, nums: list[int]) -> int:\n # Step 1: Convert nums to a dictionary of points\n points = defaultdict(int)\n for num ...
1
You are given an integer array `nums`. You want to maximize the number of points you get by performing the following operation any number of times: * Pick any `nums[i]` and delete it to earn `nums[i]` points. Afterwards, you must delete **every** element equal to `nums[i] - 1` and **every** element equal to `nums[i]...
If you take a number, you might as well take them all. Keep track of what the value is of the subset of the input with maximum M when you either take or don't take M.
Dynamic Programming : Just Duplicates Together -- House Robber Question
delete-and-earn
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)$$ --...
23
You are given an integer array `nums`. You want to maximize the number of points you get by performing the following operation any number of times: * Pick any `nums[i]` and delete it to earn `nums[i]` points. Afterwards, you must delete **every** element equal to `nums[i] - 1` and **every** element equal to `nums[i]...
If you take a number, you might as well take them all. Keep track of what the value is of the subset of the input with maximum M when you either take or don't take M.
Python Solution O(n) similar to House Robber Faster than 89.94% solutions
delete-and-earn
0
1
# Please upvote if you liked or it helped. feel free to comments suggestions or any other change and update in code. **\n\n# EXPLAINDED SOLUTION \n\n```\nclass Solution:\n def deleteAndEarn(self, nums: List[int]) -> int:\n# createed a dic to store value of a number i.e the dic[n] = n*(number of times it occurs)\n ...
4
You are given an integer array `nums`. You want to maximize the number of points you get by performing the following operation any number of times: * Pick any `nums[i]` and delete it to earn `nums[i]` points. Afterwards, you must delete **every** element equal to `nums[i] - 1` and **every** element equal to `nums[i]...
If you take a number, you might as well take them all. Keep track of what the value is of the subset of the input with maximum M when you either take or don't take M.
Solution
delete-and-earn
1
1
```C++ []\nclass Solution {\npublic:\n int deleteAndEarn(vector<int>& nums) {\n int n=nums.size();\n int maxi=0;\n for(int i=0;i<n;i++){\n maxi=max(maxi,nums[i]);\n }\n vector<int> hash(maxi+1,0);\n vector<int> dp(maxi+2);\n for(int i=0;i<n;i++){\n ...
5
You are given an integer array `nums`. You want to maximize the number of points you get by performing the following operation any number of times: * Pick any `nums[i]` and delete it to earn `nums[i]` points. Afterwards, you must delete **every** element equal to `nums[i] - 1` and **every** element equal to `nums[i]...
If you take a number, you might as well take them all. Keep track of what the value is of the subset of the input with maximum M when you either take or don't take M.
python DP : reduced to house robber
delete-and-earn
0
1
Once you store the frequency of each number, you can easily see that it is like the House Robber problem:\n\n```\nclass Solution:\n def deleteAndEarn(self, nums: List[int]) -> int:\n if not nums:\n return 0\n\n freq = [0] * (max(nums)+1)\n for n in nums:\n freq[n] += n\n\n...
71
You are given an integer array `nums`. You want to maximize the number of points you get by performing the following operation any number of times: * Pick any `nums[i]` and delete it to earn `nums[i]` points. Afterwards, you must delete **every** element equal to `nums[i] - 1` and **every** element equal to `nums[i]...
If you take a number, you might as well take them all. Keep track of what the value is of the subset of the input with maximum M when you either take or don't take M.
Simplest Python dp solution
delete-and-earn
0
1
\n\n# Code\n```\nclass Solution:\n def deleteAndEarn(self, nums: List[int]) -> int:\n n = 10001\n sm = [0] * n\n dp = [0] * n\n \n for num in nums:\n sm[num] += num\n \n dp[0] = 0\n dp[1] = sm[1]\n\n for i in range(2, n):\n dp[i] = ...
2
You are given an integer array `nums`. You want to maximize the number of points you get by performing the following operation any number of times: * Pick any `nums[i]` and delete it to earn `nums[i]` points. Afterwards, you must delete **every** element equal to `nums[i] - 1` and **every** element equal to `nums[i]...
If you take a number, you might as well take them all. Keep track of what the value is of the subset of the input with maximum M when you either take or don't take M.
easy solution python3 beats 90% in time and memory
delete-and-earn
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ninitialize the array going from 0 to max_value, loop through the input list and increment the count of each value. Then treat this like the robber problem and solve using dynamic programming\n\n# Complexity\n- Time complexity:\n<!-- Add y...
6
You are given an integer array `nums`. You want to maximize the number of points you get by performing the following operation any number of times: * Pick any `nums[i]` and delete it to earn `nums[i]` points. Afterwards, you must delete **every** element equal to `nums[i] - 1` and **every** element equal to `nums[i]...
If you take a number, you might as well take them all. Keep track of what the value is of the subset of the input with maximum M when you either take or don't take M.
741: Solution with step by step explanation
cherry-pickup
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIn this problem, two people are simultaneously traveling on a grid from the top-left corner to the bottom-right corner. These two travelers pick cherries from the cells they visit. Since they both move towards the bottom-rig...
1
You are given an `n x n` `grid` representing a field of cherries, each cell is one of three possible integers. * `0` means the cell is empty, so you can pass through, * `1` means the cell contains a cherry that you can pick up and pass through, or * `-1` means the cell contains a thorn that blocks your way. Ret...
null
DP and DFS Solutions, With a Different and Hopefully Simpler Explanations, FT 100%!!
cherry-pickup
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nI found this problem to be REALLY challenging. Even after reading some of the top solutions I was pretty confused. So to lead off, the solution is to use dynamic programming or DFS to solve for both paths at the same time.\n\nThe path w...
2
You are given an `n x n` `grid` representing a field of cherries, each cell is one of three possible integers. * `0` means the cell is empty, so you can pass through, * `1` means the cell contains a cherry that you can pick up and pass through, or * `-1` means the cell contains a thorn that blocks your way. Ret...
null
Solution
network-delay-time
1
1
```C++ []\nclass Solution {\npublic:\n int networkDelayTime(vector<vector<int>>& times, int n, int k) {\n vector<pair<int,int>>adj[n+1];\n for(int i=0;i<times.size();i++){\n adj[times[i][0]].push_back({times[i][1],times[i][2]});\n }\n vector<int>time(n+1,1e9);\n set<pair...
1
You are given a network of `n` nodes, labeled from `1` to `n`. You are also given `times`, a list of travel times as directed edges `times[i] = (ui, vi, wi)`, where `ui` is the source node, `vi` is the target node, and `wi` is the time it takes for a signal to travel from source to target. We will send a signal from a...
Convert the tree to a general graph, and do a breadth-first search. Alternatively, find the closest leaf for every node on the path from root to target.
Solution
network-delay-time
1
1
```C++ []\nclass Solution {\npublic:\n int networkDelayTime(vector<vector<int>>& times, int n, int k) {\n vector<pair<int,int>>adj[n+1];\n for(int i=0;i<times.size();i++){\n adj[times[i][0]].push_back({times[i][1],times[i][2]});\n }\n vector<int>time(n+1,1e9);\n set<pair...
1
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
FASTEST PYTHON SOLUTION
network-delay-time
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
You are given a network of `n` nodes, labeled from `1` to `n`. You are also given `times`, a list of travel times as directed edges `times[i] = (ui, vi, wi)`, where `ui` is the source node, `vi` is the target node, and `wi` is the time it takes for a signal to travel from source to target. We will send a signal from a...
Convert the tree to a general graph, and do a breadth-first search. Alternatively, find the closest leaf for every node on the path from root to target.
FASTEST PYTHON SOLUTION
network-delay-time
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 characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Python Simple Dijkstra Beats ~90%
network-delay-time
0
1
**Time - O(N + ELogN)** -Standard Time complexity of [Dijkstra\'s algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Running_time)\n\n**Space - O(N + E)** - for adjacency list and maintaining Heap.\n\n```\nclass Solution:\n def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: ...
54
You are given a network of `n` nodes, labeled from `1` to `n`. You are also given `times`, a list of travel times as directed edges `times[i] = (ui, vi, wi)`, where `ui` is the source node, `vi` is the target node, and `wi` is the time it takes for a signal to travel from source to target. We will send a signal from a...
Convert the tree to a general graph, and do a breadth-first search. Alternatively, find the closest leaf for every node on the path from root to target.
Python Simple Dijkstra Beats ~90%
network-delay-time
0
1
**Time - O(N + ELogN)** -Standard Time complexity of [Dijkstra\'s algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Running_time)\n\n**Space - O(N + E)** - for adjacency list and maintaining Heap.\n\n```\nclass Solution:\n def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int: ...
54
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Dijkstra's Algorithms
network-delay-time
0
1
# Dijkstra\'s Algorithm\n```\nclass Solution:\n def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:\n edges=defaultdict(list)\n for u, v, w in times:\n #graph[u-1].append((v-1, w))\n edges[u].append((v,w))\n minheap=[(0,k)]\n visit=set()\n ...
4
You are given a network of `n` nodes, labeled from `1` to `n`. You are also given `times`, a list of travel times as directed edges `times[i] = (ui, vi, wi)`, where `ui` is the source node, `vi` is the target node, and `wi` is the time it takes for a signal to travel from source to target. We will send a signal from a...
Convert the tree to a general graph, and do a breadth-first search. Alternatively, find the closest leaf for every node on the path from root to target.
Dijkstra's Algorithms
network-delay-time
0
1
# Dijkstra\'s Algorithm\n```\nclass Solution:\n def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:\n edges=defaultdict(list)\n for u, v, w in times:\n #graph[u-1].append((v-1, w))\n edges[u].append((v,w))\n minheap=[(0,k)]\n visit=set()\n ...
4
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
743: Solution with step by step explanation
network-delay-time
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe input is given in the form of edge lists. We want to convert this representation into an adjacency list.\n\n```\ngraph = collections.defaultdict(list)\n\nfor u, v, w in times:\n if u != v:\n graph[u].append((v,...
1
You are given a network of `n` nodes, labeled from `1` to `n`. You are also given `times`, a list of travel times as directed edges `times[i] = (ui, vi, wi)`, where `ui` is the source node, `vi` is the target node, and `wi` is the time it takes for a signal to travel from source to target. We will send a signal from a...
Convert the tree to a general graph, and do a breadth-first search. Alternatively, find the closest leaf for every node on the path from root to target.
743: Solution with step by step explanation
network-delay-time
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe input is given in the form of edge lists. We want to convert this representation into an adjacency list.\n\n```\ngraph = collections.defaultdict(list)\n\nfor u, v, w in times:\n if u != v:\n graph[u].append((v,...
1
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Python Elegant & Short | O(log(n)) | Bisect
find-smallest-letter-greater-than-target
0
1
# Complexity\n- Time complexity: $$O(\\log_2 {n})$$\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n return letters[bisect(letters, target) % len(letters)]\n```
2
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Python Elegant & Short | O(log(n)) | Bisect
find-smallest-letter-greater-than-target
0
1
# Complexity\n- Time complexity: $$O(\\log_2 {n})$$\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n return letters[bisect(letters, target) % len(letters)]\n```
2
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
SImple Python one-liner using bisect_right (Binary search)
find-smallest-letter-greater-than-target
0
1
\n# Code\n```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n idx = bisect_right(letters, target)\n return letters[idx] if idx < len(letters) else letters[0]\n```\njust converted it into 1 line for fun heh\n```\nclass Solution:\n def nextGreatestLetter(self...
2
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
SImple Python one-liner using bisect_right (Binary search)
find-smallest-letter-greater-than-target
0
1
\n# Code\n```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n idx = bisect_right(letters, target)\n return letters[idx] if idx < len(letters) else letters[0]\n```\njust converted it into 1 line for fun heh\n```\nclass Solution:\n def nextGreatestLetter(self...
2
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
python python python job job job job need solution
find-smallest-letter-greater-than-target
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 characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
python python python job job job job need solution
find-smallest-letter-greater-than-target
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 a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Find Smallest Letter Greater Than Target solution
find-smallest-letter-greater-than-target
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 characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Find Smallest Letter Greater Than Target solution
find-smallest-letter-greater-than-target
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 a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
python sorted+index solution, no loop
find-smallest-letter-greater-than-target
0
1
# Intuition\nCharacters in python are equal to numeric values so sort by them and looking for the indexes should find the next one\n\n# Approach\nfirst we create a set with the target added, this will garantee that its in order and no repetition in found, then we get the index for the target and add one more to get the...
1
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
python sorted+index solution, no loop
find-smallest-letter-greater-than-target
0
1
# Intuition\nCharacters in python are equal to numeric values so sort by them and looking for the indexes should find the next one\n\n# Approach\nfirst we create a set with the target added, this will garantee that its in order and no repetition in found, then we get the index for the target and add one more to get the...
1
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Intuitive, Easy Python3 Solution 🔥
find-smallest-letter-greater-than-target
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe needed to find the next greater element than the one given as target. If we find such an element in the list letters then return it, else return the first element in letters list.\n\n# Approach\n<!-- Describe your approach to solving t...
1
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Intuitive, Easy Python3 Solution 🔥
find-smallest-letter-greater-than-target
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe needed to find the next greater element than the one given as target. If we find such an element in the list letters then return it, else return the first element in letters list.\n\n# Approach\n<!-- Describe your approach to solving t...
1
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Binary Search Variation
find-smallest-letter-greater-than-target
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo optimize brute force, binary search is an straightforward approach.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThis is a variation of binary search. Since you have to get the smallest character which is lar...
1
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Binary Search Variation
find-smallest-letter-greater-than-target
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo optimize brute force, binary search is an straightforward approach.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThis is a variation of binary search. Since you have to get the smallest character which is lar...
1
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Python | One liner
find-smallest-letter-greater-than-target
0
1
1. Linear solution `O(N)`\n```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n return min(a) if len(a := list(filter(lambda x: x>target, letters))) > 0 else letters[0]\n```\n2. Binary search solution `O(log N)`\n```\nclass Solution:\n def nextGreatestLetter(self, ...
1
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Python | One liner
find-smallest-letter-greater-than-target
0
1
1. Linear solution `O(N)`\n```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n return min(a) if len(a := list(filter(lambda x: x>target, letters))) > 0 else letters[0]\n```\n2. Binary search solution `O(log N)`\n```\nclass Solution:\n def nextGreatestLetter(self, ...
1
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Python3 Solution
find-smallest-letter-greater-than-target
0
1
\n```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n ans=[]\n c=0\n n=len(letters)\n for i in range(0,n):\n if letters[i]>target:\n ans.append(letters[i])\n\n break\n\n else:\n c...
1
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Python3 Solution
find-smallest-letter-greater-than-target
0
1
\n```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n ans=[]\n c=0\n n=len(letters)\n for i in range(0,n):\n if letters[i]>target:\n ans.append(letters[i])\n\n break\n\n else:\n c...
1
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Solution
find-smallest-letter-greater-than-target
1
1
```C++ []\nclass Solution {\npublic:\n char nextGreatestLetter(vector<char>& letters, char target) {\n auto it = upper_bound(letters.begin(), letters.end(), target);\n return it == letters.end() ? letters[0] : *it;\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def nextGreatestLetter(self, lett...
1
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Solution
find-smallest-letter-greater-than-target
1
1
```C++ []\nclass Solution {\npublic:\n char nextGreatestLetter(vector<char>& letters, char target) {\n auto it = upper_bound(letters.begin(), letters.end(), target);\n return it == letters.end() ? letters[0] : *it;\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def nextGreatestLetter(self, lett...
1
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Python Linear Scan Easy Solution
find-smallest-letter-greater-than-target
0
1
```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n for i in letters:\n if i>target:\n return i\n return letters[0]\n```
1
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`. There are **at least two different** characters in `letters`. Return _the smallest character in_ `letters` _that is lexicographically greater than_ `target`. If such a character does not exist, return t...
We visit each node at some time, and if that time is better than the fastest time we've reached this node, we travel along outgoing edges in sorted order. Alternatively, we could use Dijkstra's algorithm.
Python Linear Scan Easy Solution
find-smallest-letter-greater-than-target
0
1
```\nclass Solution:\n def nextGreatestLetter(self, letters: List[str], target: str) -> str:\n for i in letters:\n if i>target:\n return i\n return letters[0]\n```
1
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Python Easy Trie Solution
prefix-and-suffix-search
0
1
The Data structure more suitable in such cases is a [Trie](https://en.wikipedia.org/wiki/Trie). Please read and understand Trie before you move onto the solution.\n\nIn this approach, I will be storing all the following combinations of a word in the trie.\n>eg: word = **\'leet\'**\n**>leet#leet\n>eet#leet\n>et#leet\...
32
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Python Easy Trie Solution
prefix-and-suffix-search
0
1
The Data structure more suitable in such cases is a [Trie](https://en.wikipedia.org/wiki/Trie). Please read and understand Trie before you move onto the solution.\n\nIn this approach, I will be storing all the following combinations of a word in the trie.\n>eg: word = **\'leet\'**\n**>leet#leet\n>eet#leet\n>et#leet\...
32
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
Python Sets + Optimization beats 99%
prefix-and-suffix-search
0
1
\n# Code\n```\nfrom collections import defaultdict\n\nclass WordFilter:\n\n def __init__(self, words: List[str]):\n self.prefixes = defaultdict(set)\n self.suffixes = defaultdict(set)\n seen = set()\n for idx in range(len(words) - 1, -1, -1):\n word = words[idx]\n if...
1
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Python Sets + Optimization beats 99%
prefix-and-suffix-search
0
1
\n# Code\n```\nfrom collections import defaultdict\n\nclass WordFilter:\n\n def __init__(self, words: List[str]):\n self.prefixes = defaultdict(set)\n self.suffixes = defaultdict(set)\n seen = set()\n for idx in range(len(words) - 1, -1, -1):\n word = words[idx]\n if...
1
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
745: Solution with step by step explanation
prefix-and-suffix-search
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n__init__ method\nThis method initializes the WordFilter object and pre-processes the given list of words.\n\n```\ndef __init__(self, words):\n self.prefixes = defaultdict(list)\n self.suffixes = defaultdict(list)\n```\...
1
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
745: Solution with step by step explanation
prefix-and-suffix-search
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n__init__ method\nThis method initializes the WordFilter object and pre-processes the given list of words.\n\n```\ndef __init__(self, words):\n self.prefixes = defaultdict(list)\n self.suffixes = defaultdict(list)\n```\...
1
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
Python3 | very easy to understand | basic approach | dictionary
prefix-and-suffix-search
0
1
```\nclass WordFilter:\n\n def __init__(self, words: List[str]):\n self.pref_suf = {}\n for k, word in enumerate(words):\n for i in range(len(word)):\n for j in range(len(word)):\n self.pref_suf[(word[:i+1], word[j:])] = k+1\n\n def f(self, prefix: str, s...
2
Design a special dictionary that searches the words in it by a prefix and a suffix. Implement the `WordFilter` class: * `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary. * `f(string pref, string suff)` Returns _the index of the word in the dictionary,_ which has the prefix `p...
Try to find whether each of 26 next letters are in the given string array.
Python3 | very easy to understand | basic approach | dictionary
prefix-and-suffix-search
0
1
```\nclass WordFilter:\n\n def __init__(self, words: List[str]):\n self.pref_suf = {}\n for k, word in enumerate(words):\n for i in range(len(word)):\n for j in range(len(word)):\n self.pref_suf[(word[:i+1], word[j:])] = k+1\n\n def f(self, prefix: str, s...
2
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
python3 Solution
min-cost-climbing-stairs
0
1
\n```\nclass Solution:\n def minCostClimbingStairs(self, cost: List[int]) -> int:\n n=len(cost)\n prev2=cost[0]\n prev1=cost[1]\n for i in range(2,n):\n temp=cost[i]+min(prev1,prev2)\n prev2=prev1\n prev1=temp\n return min(prev1,prev2) \n ...
2
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
python3 Solution
min-cost-climbing-stairs
0
1
\n```\nclass Solution:\n def minCostClimbingStairs(self, cost: List[int]) -> int:\n n=len(cost)\n prev2=cost[0]\n prev1=cost[1]\n for i in range(2,n):\n temp=cost[i]+min(prev1,prev2)\n prev2=prev1\n prev1=temp\n return min(prev1,prev2) \n ...
2
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
Video Solution | Explanation With Drawings | In Depth | Java | C++ | Python 3
min-cost-climbing-stairs
1
1
# Intuition and approach dicussed in detail in video solution\nhttps://youtu.be/mfKoiEdfMkQ\n\n# Code\nC++\n```\nclass Solution {\npublic:\n int minCostClimbingStairs(vector<int>& cost) {\n int sz = cost.size();\n vector<int> minCost(sz);\n minCost[0] = cost[0];\n minCost[1] = cost[1];\n ...
1
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
Video Solution | Explanation With Drawings | In Depth | Java | C++ | Python 3
min-cost-climbing-stairs
1
1
# Intuition and approach dicussed in detail in video solution\nhttps://youtu.be/mfKoiEdfMkQ\n\n# Code\nC++\n```\nclass Solution {\npublic:\n int minCostClimbingStairs(vector<int>& cost) {\n int sz = cost.size();\n vector<int> minCost(sz);\n minCost[0] = cost[0];\n minCost[1] = cost[1];\n ...
1
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
Beats 100% | O(n) Solution using only one loop | Dynamic Programming Easy Code
min-cost-climbing-stairs
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem can be approached as a dynamic programming task where we need to find the minimum cost to reach the top of the staircase. You can start either from the first step (index 0) or the second step (index 1). You can climb one or tw...
6
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
Beats 100% | O(n) Solution using only one loop | Dynamic Programming Easy Code
min-cost-climbing-stairs
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem can be approached as a dynamic programming task where we need to find the minimum cost to reach the top of the staircase. You can start either from the first step (index 0) or the second step (index 1). You can climb one or tw...
6
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
Easy python code
min-cost-climbing-stairs
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 `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
Easy python code
min-cost-climbing-stairs
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` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
🔥 Easy solution | JAVA | Python 3 🔥|
min-cost-climbing-stairs
1
1
# Intuition\nDynamic programming to build up the solution incrementally, considering the minimum cost to reach each step. By the end of the loop, the dp array will contain the minimum cost to reach each step, and the minimum cost to reach the top of the staircase is either in dp[n-1] or dp[n-2]\n<!-- Describe your firs...
1
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
🔥 Easy solution | JAVA | Python 3 🔥|
min-cost-climbing-stairs
1
1
# Intuition\nDynamic programming to build up the solution incrementally, considering the minimum cost to reach each step. By the end of the loop, the dp array will contain the minimum cost to reach each step, and the minimum cost to reach the top of the staircase is either in dp[n-1] or dp[n-2]\n<!-- Describe your firs...
1
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
python solution with easy understanding of dynamic approach for beginners to learn
min-cost-climbing-stairs
0
1
You if like and understand the solution and found helpful so please like my solution and give the star please \uD83D\uDE4F\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.The code uses dynamic programming to find the minimum cost to reach the top of the stairs.\n\n2.It creates an array dp to sto...
1
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
python solution with easy understanding of dynamic approach for beginners to learn
min-cost-climbing-stairs
0
1
You if like and understand the solution and found helpful so please like my solution and give the star please \uD83D\uDE4F\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.The code uses dynamic programming to find the minimum cost to reach the top of the stairs.\n\n2.It creates an array dp to sto...
1
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
【Video】Give me 10 minutes - How we think about a solution - Python, JavaScript, Java, C++
min-cost-climbing-stairs
1
1
Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope it is helpful for someone.\n\n# Intuition\nAppend 0 to the las...
43
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
【Video】Give me 10 minutes - How we think about a solution - Python, JavaScript, Java, C++
min-cost-climbing-stairs
1
1
Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope it is helpful for someone.\n\n# Intuition\nAppend 0 to the las...
43
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
Simple Code || 100 % beats
min-cost-climbing-stairs
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis problem can be solved using dynamic programming. The idea is to calculate the minimum cost to reach each step in the staircase. You can do this by iterating through the cost list and updating the cost at each step. Spec...
9
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
Simple Code || 100 % beats
min-cost-climbing-stairs
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis problem can be solved using dynamic programming. The idea is to calculate the minimum cost to reach each step in the staircase. You can do this by iterating through the cost list and updating the cost at each step. Spec...
9
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
A Beginner's Guide on DP validation & How to come up with a Recursive solution [Python 3]
min-cost-climbing-stairs
0
1
### Section 1: DP Validation \nOur first question should be: **Is Dynamic Programming (DP) suitable for this question?**\n\nIn its essence, DP is just **doing brute force recursion smartly**, so we should first check if the question can be done **recursively** (whether we can break it down into **smaller subproblems**)...
402
You are given an integer array `cost` where `cost[i]` is the cost of `ith` step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index `0`, or the step with index `1`. Return _the minimum cost to reach the top of the floor_. **Example 1:** **Input...
For a word like "test", consider "#test", "t#test", "st#test", "est#test", "test#test". Then if we have a query like prefix = "te", suffix = "t", we can find it by searching for something we've inserted starting with "t#te".
A Beginner's Guide on DP validation & How to come up with a Recursive solution [Python 3]
min-cost-climbing-stairs
0
1
### Section 1: DP Validation \nOur first question should be: **Is Dynamic Programming (DP) suitable for this question?**\n\nIn its essence, DP is just **doing brute force recursion smartly**, so we should first check if the question can be done **recursively** (whether we can break it down into **smaller subproblems**)...
402
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
Easy Python Solution
largest-number-at-least-twice-of-others
0
1
# Code\n```\nclass Solution:\n def dominantIndex(self, nums: List[int]) -> int:\n maxx,index=nums[0],0\n for i,v in enumerate(nums):\n maxx=max(maxx,v)\n if maxx==v:\n index=i\n for i in range(len(nums)):\n if nums[i]!=maxx and maxx<2*nums[i]:\n ...
1
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
Easy Python Solution
largest-number-at-least-twice-of-others
0
1
# Code\n```\nclass Solution:\n def dominantIndex(self, nums: List[int]) -> int:\n maxx,index=nums[0],0\n for i,v in enumerate(nums):\n maxx=max(maxx,v)\n if maxx==v:\n index=i\n for i in range(len(nums)):\n if nums[i]!=maxx and maxx<2*nums[i]:\n ...
1
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Python Elegant & Short | O(n) time one pass | O(1) space | 98.3% faster | 99.7% memory
largest-number-at-least-twice-of-others
0
1
![image](https://assets.leetcode.com/users/images/c25ff72b-bf86-419e-ac31-2346da697e7d_1659708965.3972194.png)\n\n```\ndef dominantIndex(self, nums: List[int]) -> int:\n\tfirst_max = second_max = -1\n\tmax_ind = 0\n\n\tfor i, num in enumerate(nums):\n\t\tif num >= first_max:\n\t\t\tfirst_max, second_max = num, first_ma...
24
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
Python Elegant & Short | O(n) time one pass | O(1) space | 98.3% faster | 99.7% memory
largest-number-at-least-twice-of-others
0
1
![image](https://assets.leetcode.com/users/images/c25ff72b-bf86-419e-ac31-2346da697e7d_1659708965.3972194.png)\n\n```\ndef dominantIndex(self, nums: List[int]) -> int:\n\tfirst_max = second_max = -1\n\tmax_ind = 0\n\n\tfor i, num in enumerate(nums):\n\t\tif num >= first_max:\n\t\t\tfirst_max, second_max = num, first_ma...
24
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
747: Solution with step by step explanation
largest-number-at-least-twice-of-others
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nclass Solution:\n def dominantIndex(self, nums: List[int]) -> int:\n \n # Step 1: Initialization\n # We will start by initializing two indices, `max_idx` for the largest number and \n # `s...
1
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
747: Solution with step by step explanation
largest-number-at-least-twice-of-others
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nclass Solution:\n def dominantIndex(self, nums: List[int]) -> int:\n \n # Step 1: Initialization\n # We will start by initializing two indices, `max_idx` for the largest number and \n # `s...
1
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Beats 100% - Easy Java, C++ and Python Solution
largest-number-at-least-twice-of-others
1
1
# Intuition\nWe will find the highest and second highest number in nums. Then finally, check if:\nhighest >= (second highest * 2)\nIf it is, we return the index of the highest element.\nIf not, then we return -1.\n# Approach\nSince all the numbers in nums are positive, we set highest and second highest as both -1 at th...
3
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).
Beats 100% - Easy Java, C++ and Python Solution
largest-number-at-least-twice-of-others
1
1
# Intuition\nWe will find the highest and second highest number in nums. Then finally, check if:\nhighest >= (second highest * 2)\nIf it is, we return the index of the highest element.\nIf not, then we return -1.\n# Approach\nSince all the numbers in nums are positive, we set highest and second highest as both -1 at th...
3
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Python3||Beats 95.1% || Easy beginner solution
largest-number-at-least-twice-of-others
0
1
![image.png](https://assets.leetcode.com/users/images/4e46ce2a-f10a-4a3e-ac7c-45eb2d14e1f4_1677867186.9021106.png)\n\n# Code\n```\nclass Solution:\n def dominantIndex(self, nums: List[int]) -> int:\n nums1 = sorted(nums)\n if nums1[len(nums1)-1]>= 2*nums1[len(nums1)-2]:\n for i in range(len(...
4
You are given an integer array `nums` where the largest integer is **unique**. Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return _the **index** of the largest element, or return_ `-1` _otherwise_. **Example 1:** **Input:** nums = \[3...
Say f[i] is the final cost to climb to the top from step i. Then f[i] = cost[i] + min(f[i+1], f[i+2]).