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 |
|---|---|---|---|---|---|---|---|
Solution | sliding-puzzle | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n string target = "123450";\n vector<vector<int>> dir{{1,3},{0,2,4},{1,5},{0,4},{1,3,5},{2,4}};\n int slidingPuzzle(vector<vector<int>>& board) \n {\n int m = board.size(),n = board[0].size();\n string first = "";\n for(int i=0; i<m; i++)\n {\... | 1 | On an `2 x 3` board, there are five tiles labeled from `1` to `5`, and an empty square represented by `0`. A **move** consists of choosing `0` and a 4-directionally adjacent number and swapping it.
The state of the board is solved if and only if the board is `[[1,2,3],[4,5,0]]`.
Given the puzzle board `board`, return... | null |
Solution | sliding-puzzle | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n string target = "123450";\n vector<vector<int>> dir{{1,3},{0,2,4},{1,5},{0,4},{1,3,5},{2,4}};\n int slidingPuzzle(vector<vector<int>>& board) \n {\n int m = board.size(),n = board[0].size();\n string first = "";\n for(int i=0; i<m; i++)\n {\... | 1 | There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [fromi, toi, pricei]` indicates that there is a flight from city `fromi` to city `toi` with cost `pricei`.
You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from_ `src` _... | Perform a breadth-first-search, where the nodes are the puzzle boards and edges are if two puzzle boards can be transformed into one another with one move. |
BFS APPROACH | sliding-puzzle | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy first thought is that this problem can be solved using a breadth-first search (BFS) algorithm, where we start with the initial board state and explore all possible moves (by swapping the empty space with its neighboring tiles) until we... | 2 | On an `2 x 3` board, there are five tiles labeled from `1` to `5`, and an empty square represented by `0`. A **move** consists of choosing `0` and a 4-directionally adjacent number and swapping it.
The state of the board is solved if and only if the board is `[[1,2,3],[4,5,0]]`.
Given the puzzle board `board`, return... | null |
BFS APPROACH | sliding-puzzle | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy first thought is that this problem can be solved using a breadth-first search (BFS) algorithm, where we start with the initial board state and explore all possible moves (by swapping the empty space with its neighboring tiles) until we... | 2 | There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [fromi, toi, pricei]` indicates that there is a flight from city `fromi` to city `toi` with cost `pricei`.
You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from_ `src` _... | Perform a breadth-first-search, where the nodes are the puzzle boards and edges are if two puzzle boards can be transformed into one another with one move. |
Cool!!! | sliding-puzzle | 0 | 1 | \n# Code\n```\nclass Solution:\n\n \n def slidingPuzzle(self, board: List[List[int]]) -> int:\n board = tuple(board[0]+board[1])\n graph_step_history = { (1, 4, 3, 5, 2, 0): 14,\n (4, 0, 2, 5, 1, 3): 6,\n (5, 4, 1, 3, 0, 2): 11,\n ... | 1 | On an `2 x 3` board, there are five tiles labeled from `1` to `5`, and an empty square represented by `0`. A **move** consists of choosing `0` and a 4-directionally adjacent number and swapping it.
The state of the board is solved if and only if the board is `[[1,2,3],[4,5,0]]`.
Given the puzzle board `board`, return... | null |
Cool!!! | sliding-puzzle | 0 | 1 | \n# Code\n```\nclass Solution:\n\n \n def slidingPuzzle(self, board: List[List[int]]) -> int:\n board = tuple(board[0]+board[1])\n graph_step_history = { (1, 4, 3, 5, 2, 0): 14,\n (4, 0, 2, 5, 1, 3): 6,\n (5, 4, 1, 3, 0, 2): 11,\n ... | 1 | There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [fromi, toi, pricei]` indicates that there is a flight from city `fromi` to city `toi` with cost `pricei`.
You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from_ `src` _... | Perform a breadth-first-search, where the nodes are the puzzle boards and edges are if two puzzle boards can be transformed into one another with one move. |
775: Beats 98.25%, Solution with step by step explanation | global-and-local-inversions | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nfor i, num in enumerate(A):\n```\nWe use the enumerate function, which allows us to loop over the list A and get both the index (i) and the value (num) of each element.\n\n```\nif abs(i - num) > 1:\n return False\n``... | 0 | You are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
The number of **global inversions** is the number of the different pairs `(i, j)` where:
* `0 <= i < j < n`
* `nums[i] > nums[j]`
The number of **local inversions** is the number of i... | null |
775: Beats 98.25%, Solution with step by step explanation | global-and-local-inversions | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nfor i, num in enumerate(A):\n```\nWe use the enumerate function, which allows us to loop over the list A and get both the index (i) and the value (num) of each element.\n\n```\nif abs(i - num) > 1:\n return False\n``... | 0 | You have two types of tiles: a `2 x 1` domino shape and a tromino shape. You may rotate these shapes.
Given an integer n, return _the number of ways to tile an_ `2 x n` _board_. Since the answer may be very large, return it **modulo** `109 + 7`.
In a tiling, every square must be covered by a tile. Two tilings are dif... | Where can the 0 be placed in an ideal permutation? What about the 1? |
Solution | global-and-local-inversions | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool isIdealPermutation(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n for(int i=0;i<nums.size();i++){\n if(abs(nums[i]-i)>1) return false;\n }\n return true;\n }\n};\n```\n\n... | 2 | You are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
The number of **global inversions** is the number of the different pairs `(i, j)` where:
* `0 <= i < j < n`
* `nums[i] > nums[j]`
The number of **local inversions** is the number of i... | null |
Solution | global-and-local-inversions | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool isIdealPermutation(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n for(int i=0;i<nums.size();i++){\n if(abs(nums[i]-i)>1) return false;\n }\n return true;\n }\n};\n```\n\n... | 2 | You have two types of tiles: a `2 x 1` domino shape and a tromino shape. You may rotate these shapes.
Given an integer n, return _the number of ways to tile an_ `2 x n` _board_. Since the answer may be very large, return it **modulo** `109 + 7`.
In a tiling, every square must be covered by a tile. Two tilings are dif... | Where can the 0 be placed in an ideal permutation? What about the 1? |
Python || 100% Faster || Two Approaches | global-and-local-inversions | 0 | 1 | **Brute Forece Appoach: O(nlogn) Time Complexity It will give TLE**\n\n```\nclass Solution:\n def isIdealPermutation(self, arr: List[int]) -> bool:\n n=len(arr)\n l=0\n for i in range(n-1):\n if arr[i]>arr[i+1]:\n l+=1\n if l>1:\n retur... | 2 | You are given an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
The number of **global inversions** is the number of the different pairs `(i, j)` where:
* `0 <= i < j < n`
* `nums[i] > nums[j]`
The number of **local inversions** is the number of i... | null |
Python || 100% Faster || Two Approaches | global-and-local-inversions | 0 | 1 | **Brute Forece Appoach: O(nlogn) Time Complexity It will give TLE**\n\n```\nclass Solution:\n def isIdealPermutation(self, arr: List[int]) -> bool:\n n=len(arr)\n l=0\n for i in range(n-1):\n if arr[i]>arr[i+1]:\n l+=1\n if l>1:\n retur... | 2 | You have two types of tiles: a `2 x 1` domino shape and a tromino shape. You may rotate these shapes.
Given an integer n, return _the number of ways to tile an_ `2 x n` _board_. Since the answer may be very large, return it **modulo** `109 + 7`.
In a tiling, every square must be covered by a tile. Two tilings are dif... | Where can the 0 be placed in an ideal permutation? What about the 1? |
777: Beats 95.06%, Solution with step by step explanation | swap-adjacent-in-lr-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ni, j = 0, 0\nn, m = len(start), len(end)\n```\nWe create two pointers, i and j, to iterate through start and end, respectively. We also store the lengths of start and end in n and m.\n\nWe continue while neither i nor j... | 1 | In a string composed of `'L'`, `'R'`, and `'X'` characters, like `"RXXLRXRXL "`, a move consists of either replacing one occurrence of `"XL "` with `"LX "`, or replacing one occurrence of `"RX "` with `"XR "`. Given the starting string `start` and the ending string `end`, return `True` if and only if there exists a seq... | Check whether each value is equal to the value of it's top-left neighbor. |
777: Beats 95.06%, Solution with step by step explanation | swap-adjacent-in-lr-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ni, j = 0, 0\nn, m = len(start), len(end)\n```\nWe create two pointers, i and j, to iterate through start and end, respectively. We also store the lengths of start and end in n and m.\n\nWe continue while neither i nor j... | 1 | Let `f(x)` be the number of zeroes at the end of `x!`. Recall that `x! = 1 * 2 * 3 * ... * x` and by convention, `0! = 1`.
* For example, `f(3) = 0` because `3! = 6` has no zeroes at the end, while `f(11) = 2` because `11! = 39916800` has two zeroes at the end.
Given an integer `k`, return the number of non-negativ... | Think of the L and R's as people on a horizontal line, where X is a space. The people can't cross each other, and also you can't go from XRX to RXX. |
4 Solutions: TLE -> O(n) Space Optimised - Python/Java | swap-adjacent-in-lr-string | 1 | 1 | > # Intuition\n\nIn spite of the downvotes I actually thought this was a pretty good question, even though only the O(n) passes. \n\nI\'ve heavily commented the code with explanations, and provided the brute force alternatives that don\'t pass for people like me who find those helpful:\n\n---\n\n\n> # DFS (TLE)\n\n```p... | 1 | In a string composed of `'L'`, `'R'`, and `'X'` characters, like `"RXXLRXRXL "`, a move consists of either replacing one occurrence of `"XL "` with `"LX "`, or replacing one occurrence of `"RX "` with `"XR "`. Given the starting string `start` and the ending string `end`, return `True` if and only if there exists a seq... | Check whether each value is equal to the value of it's top-left neighbor. |
4 Solutions: TLE -> O(n) Space Optimised - Python/Java | swap-adjacent-in-lr-string | 1 | 1 | > # Intuition\n\nIn spite of the downvotes I actually thought this was a pretty good question, even though only the O(n) passes. \n\nI\'ve heavily commented the code with explanations, and provided the brute force alternatives that don\'t pass for people like me who find those helpful:\n\n---\n\n\n> # DFS (TLE)\n\n```p... | 1 | Let `f(x)` be the number of zeroes at the end of `x!`. Recall that `x! = 1 * 2 * 3 * ... * x` and by convention, `0! = 1`.
* For example, `f(3) = 0` because `3! = 6` has no zeroes at the end, while `f(11) = 2` because `11! = 39916800` has two zeroes at the end.
Given an integer `k`, return the number of non-negativ... | Think of the L and R's as people on a horizontal line, where X is a space. The people can't cross each other, and also you can't go from XRX to RXX. |
Solution | swap-adjacent-in-lr-string | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool canTransform(string st, string tar) {\n int n=tar.length();\n int i=0,j=0;\n while(i<=n && j<=n){\n while(i<n && tar[i]==\'X\') i++;\n while(j<n && st[j]==\'X\') j++;\n if(i==n || j==n)\n return i==n && j... | 2 | In a string composed of `'L'`, `'R'`, and `'X'` characters, like `"RXXLRXRXL "`, a move consists of either replacing one occurrence of `"XL "` with `"LX "`, or replacing one occurrence of `"RX "` with `"XR "`. Given the starting string `start` and the ending string `end`, return `True` if and only if there exists a seq... | Check whether each value is equal to the value of it's top-left neighbor. |
Solution | swap-adjacent-in-lr-string | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool canTransform(string st, string tar) {\n int n=tar.length();\n int i=0,j=0;\n while(i<=n && j<=n){\n while(i<n && tar[i]==\'X\') i++;\n while(j<n && st[j]==\'X\') j++;\n if(i==n || j==n)\n return i==n && j... | 2 | Let `f(x)` be the number of zeroes at the end of `x!`. Recall that `x! = 1 * 2 * 3 * ... * x` and by convention, `0! = 1`.
* For example, `f(3) = 0` because `3! = 6` has no zeroes at the end, while `f(11) = 2` because `11! = 39916800` has two zeroes at the end.
Given an integer `k`, return the number of non-negativ... | Think of the L and R's as people on a horizontal line, where X is a space. The people can't cross each other, and also you can't go from XRX to RXX. |
Python 3 || Priority Queue | swim-in-rising-water | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | You are given an `n x n` integer matrix `grid` where each value `grid[i][j]` represents the elevation at that point `(i, j)`.
The rain starts to fall. At time `t`, the depth of the water everywhere is `t`. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both square... | Alternate placing the most common letters. |
Python3 | Min Heap | Simple Solution | swim-in-rising-water | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe Brute force approach is to start from (0,0) traverse through every item in grid and return the smallest elevation value when we reach n-1,n-1. However this approach could possibly take O(N^N). \n\nhence we need a way to take smallest ... | 1 | You are given an `n x n` integer matrix `grid` where each value `grid[i][j]` represents the elevation at that point `(i, j)`.
The rain starts to fall. At time `t`, the depth of the water everywhere is `t`. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both square... | Alternate placing the most common letters. |
EASY PYTHON SOLUTION USING HEAPSORT AND BFS | swim-in-rising-water | 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(m*n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(m*n)$$\n<!-- Add your space complexity he... | 2 | You are given an `n x n` integer matrix `grid` where each value `grid[i][j]` represents the elevation at that point `(i, j)`.
The rain starts to fall. At time `t`, the depth of the water everywhere is `t`. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both square... | Alternate placing the most common letters. |
Easy to follow python3 solutoon | swim-in-rising-water | 0 | 1 | Do please comment if the complexity analysis needs correction, I am more than happy to hear it.\n\n```\nclass Solution:\n # O(max(n^2, m)) time, h --> the highest elevation in the grid\n # O(n^2) space,\n # Approach: BFS, Priority queue\n # I wld advise to do task scheduler question, it\'s pretty similar\n ... | 1 | You are given an `n x n` integer matrix `grid` where each value `grid[i][j]` represents the elevation at that point `(i, j)`.
The rain starts to fall. At time `t`, the depth of the water everywhere is `t`. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both square... | Alternate placing the most common letters. |
C/C++/Python recursion independent of n->one-line||0ms beats 100% | k-th-symbol-in-grammar | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhat is the meaning for the contraint for `1 <= n <= 30`? There are exactly $2^{n-1}$ elements in n-th row! Let\'s try write out the first some rows to find the possible pattern\n```\n0\n01\n0110\n01101001\n.....\n```\nNow could you find ... | 9 | We build a table of `n` rows (**1-indexed**). We start by writing `0` in the `1st` row. Now in every subsequent row, we look at the previous row and replace each occurrence of `0` with `01`, and each occurrence of `1` with `10`.
* For example, for `n = 3`, the `1st` row is `0`, the `2nd` row is `01`, and the `3rd` r... | Each k for which some permutation of arr[:k] is equal to sorted(arr)[:k] is where we should cut each chunk. |
C/C++/Python recursion independent of n->one-line||0ms beats 100% | k-th-symbol-in-grammar | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhat is the meaning for the contraint for `1 <= n <= 30`? There are exactly $2^{n-1}$ elements in n-th row! Let\'s try write out the first some rows to find the possible pattern\n```\n0\n01\n0110\n01101001\n.....\n```\nNow could you find ... | 9 | Given an integer array `nums` and two integers `left` and `right`, return _the number of contiguous non-empty **subarrays** such that the value of the maximum array element in that subarray is in the range_ `[left, right]`.
The test cases are generated so that the answer will fit in a **32-bit** integer.
**Example 1:... | Try to represent the current (N, K) in terms of some (N-1, prevK). What is prevK ? |
【Video】Give me 8 minutes - How we think about solution - Python, JavaScript, Java, C++ | k-th-symbol-in-grammar | 1 | 1 | # Intuition\nCheck parent and position of a target pair.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/cCN38sMai-c\n\n\u25A0 Timeline of the video\n`0:04` Important Rules\n`0:17` How can you find the answer\n`1:13` How can you find the parent of the target pair\n`3:05` How can you know 0 or 1 in the target pair\n`3:40... | 29 | We build a table of `n` rows (**1-indexed**). We start by writing `0` in the `1st` row. Now in every subsequent row, we look at the previous row and replace each occurrence of `0` with `01`, and each occurrence of `1` with `10`.
* For example, for `n = 3`, the `1st` row is `0`, the `2nd` row is `01`, and the `3rd` r... | Each k for which some permutation of arr[:k] is equal to sorted(arr)[:k] is where we should cut each chunk. |
【Video】Give me 8 minutes - How we think about solution - Python, JavaScript, Java, C++ | k-th-symbol-in-grammar | 1 | 1 | # Intuition\nCheck parent and position of a target pair.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/cCN38sMai-c\n\n\u25A0 Timeline of the video\n`0:04` Important Rules\n`0:17` How can you find the answer\n`1:13` How can you find the parent of the target pair\n`3:05` How can you know 0 or 1 in the target pair\n`3:40... | 29 | Given an integer array `nums` and two integers `left` and `right`, return _the number of contiguous non-empty **subarrays** such that the value of the maximum array element in that subarray is in the range_ `[left, right]`.
The test cases are generated so that the answer will fit in a **32-bit** integer.
**Example 1:... | Try to represent the current (N, K) in terms of some (N-1, prevK). What is prevK ? |
🚀 100% || Easy Iterative Approach || Explained Intuition 🚀 | k-th-symbol-in-grammar | 1 | 1 | # Porblem Description\n\nThe task is **constructing** a table consisting of `n` rows, with each row generated from the row immediately above it according to a specific **rule**. \nThe rule involves **replacing** each `0` with `01` and each `1` with `10` in the **previous** row. \nThe task is to **determine** the value ... | 172 | We build a table of `n` rows (**1-indexed**). We start by writing `0` in the `1st` row. Now in every subsequent row, we look at the previous row and replace each occurrence of `0` with `01`, and each occurrence of `1` with `10`.
* For example, for `n = 3`, the `1st` row is `0`, the `2nd` row is `01`, and the `3rd` r... | Each k for which some permutation of arr[:k] is equal to sorted(arr)[:k] is where we should cut each chunk. |
🚀 100% || Easy Iterative Approach || Explained Intuition 🚀 | k-th-symbol-in-grammar | 1 | 1 | # Porblem Description\n\nThe task is **constructing** a table consisting of `n` rows, with each row generated from the row immediately above it according to a specific **rule**. \nThe rule involves **replacing** each `0` with `01` and each `1` with `10` in the **previous** row. \nThe task is to **determine** the value ... | 172 | Given an integer array `nums` and two integers `left` and `right`, return _the number of contiguous non-empty **subarrays** such that the value of the maximum array element in that subarray is in the range_ `[left, right]`.
The test cases are generated so that the answer will fit in a **32-bit** integer.
**Example 1:... | Try to represent the current (N, K) in terms of some (N-1, prevK). What is prevK ? |
✅ 100% Recursive & Bit Count | k-th-symbol-in-grammar | 1 | 1 | # Solution 1: Recursive Approach\n\n## Intuition\nThe initial thought on solving the problem revolves around understanding the pattern of sequence generation as described in the problem statement. As the sequence grows by replacing 0 with 01 and 1 with 10, we can observe a recursive relationship between the nth row and... | 67 | We build a table of `n` rows (**1-indexed**). We start by writing `0` in the `1st` row. Now in every subsequent row, we look at the previous row and replace each occurrence of `0` with `01`, and each occurrence of `1` with `10`.
* For example, for `n = 3`, the `1st` row is `0`, the `2nd` row is `01`, and the `3rd` r... | Each k for which some permutation of arr[:k] is equal to sorted(arr)[:k] is where we should cut each chunk. |
✅ 100% Recursive & Bit Count | k-th-symbol-in-grammar | 1 | 1 | # Solution 1: Recursive Approach\n\n## Intuition\nThe initial thought on solving the problem revolves around understanding the pattern of sequence generation as described in the problem statement. As the sequence grows by replacing 0 with 01 and 1 with 10, we can observe a recursive relationship between the nth row and... | 67 | Given an integer array `nums` and two integers `left` and `right`, return _the number of contiguous non-empty **subarrays** such that the value of the maximum array element in that subarray is in the range_ `[left, right]`.
The test cases are generated so that the answer will fit in a **32-bit** integer.
**Example 1:... | Try to represent the current (N, K) in terms of some (N-1, prevK). What is prevK ? |
✅☑[C++/Java/Python/JavaScript] || Beats 100% || 2 Approaches || EXPLAINED🔥 | k-th-symbol-in-grammar | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(Binary Tree)***\n1. **depthFirstSearch(int n, int k, int rootVal):** This is a recursive function that calculates the kth symbol in the nth row of the sequence. It takes three parameters:\n\n - **n:**The cur... | 2 | We build a table of `n` rows (**1-indexed**). We start by writing `0` in the `1st` row. Now in every subsequent row, we look at the previous row and replace each occurrence of `0` with `01`, and each occurrence of `1` with `10`.
* For example, for `n = 3`, the `1st` row is `0`, the `2nd` row is `01`, and the `3rd` r... | Each k for which some permutation of arr[:k] is equal to sorted(arr)[:k] is where we should cut each chunk. |
✅☑[C++/Java/Python/JavaScript] || Beats 100% || 2 Approaches || EXPLAINED🔥 | k-th-symbol-in-grammar | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(Binary Tree)***\n1. **depthFirstSearch(int n, int k, int rootVal):** This is a recursive function that calculates the kth symbol in the nth row of the sequence. It takes three parameters:\n\n - **n:**The cur... | 2 | Given an integer array `nums` and two integers `left` and `right`, return _the number of contiguous non-empty **subarrays** such that the value of the maximum array element in that subarray is in the range_ `[left, right]`.
The test cases are generated so that the answer will fit in a **32-bit** integer.
**Example 1:... | Try to represent the current (N, K) in terms of some (N-1, prevK). What is prevK ? |
🐱 Find parity of bits in k - 1. One Liner ! | k-th-symbol-in-grammar | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nImagine that we are building a binary tree starting from the root node with key `0`. \n\n1. **The key of the left child is same as the parent whereas the key of the right child is different than the parent.**\n\nIn mathematical terms, $le... | 3 | We build a table of `n` rows (**1-indexed**). We start by writing `0` in the `1st` row. Now in every subsequent row, we look at the previous row and replace each occurrence of `0` with `01`, and each occurrence of `1` with `10`.
* For example, for `n = 3`, the `1st` row is `0`, the `2nd` row is `01`, and the `3rd` r... | Each k for which some permutation of arr[:k] is equal to sorted(arr)[:k] is where we should cut each chunk. |
🐱 Find parity of bits in k - 1. One Liner ! | k-th-symbol-in-grammar | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nImagine that we are building a binary tree starting from the root node with key `0`. \n\n1. **The key of the left child is same as the parent whereas the key of the right child is different than the parent.**\n\nIn mathematical terms, $le... | 3 | Given an integer array `nums` and two integers `left` and `right`, return _the number of contiguous non-empty **subarrays** such that the value of the maximum array element in that subarray is in the range_ `[left, right]`.
The test cases are generated so that the answer will fit in a **32-bit** integer.
**Example 1:... | Try to represent the current (N, K) in terms of some (N-1, prevK). What is prevK ? |
Recursion | k-th-symbol-in-grammar | 0 | 1 | \n\n# Complexity\n- Time complexity:\nO(2^n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def kthGrammar(self, n: int, k: int) -> int:\n if n == 1:\n return 0\n midpoint = 2 ** (n - 2)\n if k <= midpoint:\n return self.kthGrammar(n - 1, k)\n else:\n... | 5 | We build a table of `n` rows (**1-indexed**). We start by writing `0` in the `1st` row. Now in every subsequent row, we look at the previous row and replace each occurrence of `0` with `01`, and each occurrence of `1` with `10`.
* For example, for `n = 3`, the `1st` row is `0`, the `2nd` row is `01`, and the `3rd` r... | Each k for which some permutation of arr[:k] is equal to sorted(arr)[:k] is where we should cut each chunk. |
Recursion | k-th-symbol-in-grammar | 0 | 1 | \n\n# Complexity\n- Time complexity:\nO(2^n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def kthGrammar(self, n: int, k: int) -> int:\n if n == 1:\n return 0\n midpoint = 2 ** (n - 2)\n if k <= midpoint:\n return self.kthGrammar(n - 1, k)\n else:\n... | 5 | Given an integer array `nums` and two integers `left` and `right`, return _the number of contiguous non-empty **subarrays** such that the value of the maximum array element in that subarray is in the range_ `[left, right]`.
The test cases are generated so that the answer will fit in a **32-bit** integer.
**Example 1:... | Try to represent the current (N, K) in terms of some (N-1, prevK). What is prevK ? |
Python 3: Simple Iterative Solution | k-th-symbol-in-grammar | 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 | We build a table of `n` rows (**1-indexed**). We start by writing `0` in the `1st` row. Now in every subsequent row, we look at the previous row and replace each occurrence of `0` with `01`, and each occurrence of `1` with `10`.
* For example, for `n = 3`, the `1st` row is `0`, the `2nd` row is `01`, and the `3rd` r... | Each k for which some permutation of arr[:k] is equal to sorted(arr)[:k] is where we should cut each chunk. |
Python 3: Simple Iterative Solution | k-th-symbol-in-grammar | 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 | Given an integer array `nums` and two integers `left` and `right`, return _the number of contiguous non-empty **subarrays** such that the value of the maximum array element in that subarray is in the range_ `[left, right]`.
The test cases are generated so that the answer will fit in a **32-bit** integer.
**Example 1:... | Try to represent the current (N, K) in terms of some (N-1, prevK). What is prevK ? |
Solution | reaching-points | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool reachingPoints(int sx, int sy, int tx, int ty) {\n while(sx < tx && sy < ty){\n if(tx > ty) tx = tx%ty;\n else ty = ty%tx;\n }\n if(sx == tx && sy<= ty && (ty-sy)%sx == 0) return true;\n if(sy == ty && sx <= tx && (tx-sx)%s... | 2 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Solution | reaching-points | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool reachingPoints(int sx, int sy, int tx, int ty) {\n while(sx < tx && sy < ty){\n if(tx > ty) tx = tx%ty;\n else ty = ty%tx;\n }\n if(sx == tx && sy<= ty && (ty-sy)%sx == 0) return true;\n if(sy == ty && sx <= tx && (tx-sx)%s... | 2 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Python 3 || 4 lines, GCD, iteration || T/M: 95% / 97% | reaching-points | 0 | 1 | Pretty much the same plan as many others posted, but with one addtional twist. If `True`, then `tx` and `ty` must each be linear combinations of `sx` and `sy`, which in turn implies that `gcd(sx,sy)` and `gcd(tx,ty)` must be equal.\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) ... | 5 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Python 3 || 4 lines, GCD, iteration || T/M: 95% / 97% | reaching-points | 0 | 1 | Pretty much the same plan as many others posted, but with one addtional twist. If `True`, then `tx` and `ty` must each be linear combinations of `sx` and `sy`, which in turn implies that `gcd(sx,sy)` and `gcd(tx,ty)` must be equal.\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) ... | 5 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
[Python3] [Impossible not to understand] Intuitive Method with Detailed Explanation | reaching-points | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst, we want to understand that it\'s faster to go from the target to the source because we can only subtract the smaller number from the larger to get a non-negative number.\n\nSecond, we want to optimize this "subtraction loop" becaus... | 9 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
[Python3] [Impossible not to understand] Intuitive Method with Detailed Explanation | reaching-points | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst, we want to understand that it\'s faster to go from the target to the source because we can only subtract the smaller number from the larger to get a non-negative number.\n\nSecond, we want to optimize this "subtraction loop" becaus... | 9 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
[Python3] Only Solution You'll Need || 30ms, 80% faster | reaching-points | 0 | 1 | Trust me when I say you should really just read this solution and you would understand it:\n\n**Optimized: Bottom Up Solution**:\n\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n\t\n while (tx >= sx and ty >= sy):\n # check if we have reached ends\n ... | 2 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
[Python3] Only Solution You'll Need || 30ms, 80% faster | reaching-points | 0 | 1 | Trust me when I say you should really just read this solution and you would understand it:\n\n**Optimized: Bottom Up Solution**:\n\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n\t\n while (tx >= sx and ty >= sy):\n # check if we have reached ends\n ... | 2 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Python recursive solution runtime beats 98.91 % | reaching-points | 0 | 1 | ```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n if sx > tx or sy > ty: return False\n if sx == tx: return (ty-sy)%sx == 0 # only change y\n if sy == ty: return (tx-sx)%sy == 0\n if tx > ty: \n return self.reachingPoints(sx, sy, tx%... | 20 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Python recursive solution runtime beats 98.91 % | reaching-points | 0 | 1 | ```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n if sx > tx or sy > ty: return False\n if sx == tx: return (ty-sy)%sx == 0 # only change y\n if sy == ty: return (tx-sx)%sy == 0\n if tx > ty: \n return self.reachingPoints(sx, sy, tx%... | 20 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Python Modulo from the End 38ms | reaching-points | 0 | 1 | ```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n while sx <= tx and sy <= ty:\n if tx == sx and ty == sy: return True\n if tx == ty: break\n if tx > ty:\n tx %= ty\n elif ty > tx:\n ty %= tx... | 3 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Python Modulo from the End 38ms | reaching-points | 0 | 1 | ```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n while sx <= tx and sy <= ty:\n if tx == sx and ty == sy: return True\n if tx == ty: break\n if tx > ty:\n tx %= ty\n elif ty > tx:\n ty %= tx... | 3 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Python3 Beats 99% | reaching-points | 0 | 1 | # Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(log(max(tx, ty)))$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n# Code\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n wh... | 0 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Python3 Beats 99% | reaching-points | 0 | 1 | # Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(log(max(tx, ty)))$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n# Code\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n wh... | 0 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Python Simple Solution | reaching-points | 0 | 1 | # Approach\nBacktracking\n\n# Code\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n while sx < tx and sy < ty:\n tx, ty = tx % ty, ty % tx\n\n return (\n sx <= tx\n and sy <= ty\n and (ty - sy) % tx == 0\n ... | 0 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Python Simple Solution | reaching-points | 0 | 1 | # Approach\nBacktracking\n\n# Code\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n while sx < tx and sy < ty:\n tx, ty = tx % ty, ty % tx\n\n return (\n sx <= tx\n and sy <= ty\n and (ty - sy) % tx == 0\n ... | 0 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
[Python] O(log(n)) solution using iteration and modulo operator | reaching-points | 0 | 1 | I\'m just doing a quick-submit this time, so I\'m leaving the code below without any commentary up here. If you have any questions, please ask me in the comments and I\'ll fill out more information over time. Thanks!\n\n# Complexity\n- Time complexity: O(log(n)) where n is max(tx,ty)\n- Space complexity: O(1)\n\n# Code... | 0 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
[Python] O(log(n)) solution using iteration and modulo operator | reaching-points | 0 | 1 | I\'m just doing a quick-submit this time, so I\'m leaving the code below without any commentary up here. If you have any questions, please ask me in the comments and I\'ll fill out more information over time. Thanks!\n\n# Complexity\n- Time complexity: O(log(n)) where n is max(tx,ty)\n- Space complexity: O(1)\n\n# Code... | 0 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Solution of leetcode 780 | reaching-points | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Solution of leetcode 780 | reaching-points | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Python3 easy solution | reaching-points | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n # in such questions start from the target and move to the source, as \n #that contains only one path\n #going from source to target instead has many paths\n #visualize this in the fo... | 0 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Python3 easy solution | reaching-points | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def reachingPoints(self, sx: int, sy: int, tx: int, ty: int) -> bool:\n # in such questions start from the target and move to the source, as \n #that contains only one path\n #going from source to target instead has many paths\n #visualize this in the fo... | 0 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
780: Solution with step by step explanation | reaching-points | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n while sx < tx and sy < ty:\n tx, ty = tx % ty, ty % tx\n```\n\nHere, we are using a while loop to reduce the tx and ty values. If both tx and ty are greater than their starting counterparts, then the ... | 0 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
780: Solution with step by step explanation | reaching-points | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n while sx < tx and sy < ty:\n tx, ty = tx % ty, ty % tx\n```\n\nHere, we are using a while loop to reduce the tx and ty values. If both tx and ty are greater than their starting counterparts, then the ... | 0 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Magic solution with pure maths | reaching-points | 0 | 1 | # Intuition\nThe problem involves reaching a target point `(tx, ty)` starting from an initial point `(sx, sy)` while subtracting one coordinate from the other until one of them matches. We need to determine if it\'s possible to reach the target point using this method.\n\n# Approach\nWe use a while loop to iteratively ... | 0 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Magic solution with pure maths | reaching-points | 0 | 1 | # Intuition\nThe problem involves reaching a target point `(tx, ty)` starting from an initial point `(sx, sy)` while subtracting one coordinate from the other until one of them matches. We need to determine if it\'s possible to reach the target point using this method.\n\n# Approach\nWe use a while loop to iteratively ... | 0 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Solution with explanation | reaching-points | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
**Example 1:**
**Input... | The first chunk can be found as the smallest k for which A[:k+1] == [0, 1, 2, ...k]; then we repeat this process. |
Solution with explanation | reaching-points | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given two strings `s` and `goal`, return `true` _if and only if_ `s` _can become_ `goal` _after some number of **shifts** on_ `s`.
A **shift** on `s` consists of moving the leftmost character of `s` to the rightmost position.
* For example, if `s = "abcde "`, then it will be `"bcdea "` after one shift.
**Example 1... | null |
Clear solution with comments (Java, Python) | rabbits-in-forest | 1 | 1 | # Intuition\n* If a rabbit says there are $n$ other rabbits of the same color, we can infer there is a group of $n+1$ rabbits that are the same color.\n* If $n+2$ rabbits say they are in a group with $n$ other rabbits of the same color, we need to add another $n+1$ rabbits to our total count.\n\n# Plan\n1. Create a has... | 2 | There is a forest with an unknown number of rabbits. We asked n rabbits **"How many rabbits have the same color as you? "** and collected the answers in an integer array `answers` where `answers[i]` is the answer of the `ith` rabbit.
Given the array `answers`, return _the minimum number of rabbits that could be in the... | One way is with a Polynomial class. For example,
* `Poly:add(this, that)` returns the result of `this + that`.
* `Poly:sub(this, that)` returns the result of `this - that`.
* `Poly:mul(this, that)` returns the result of `this * that`.
* `Poly:evaluate(this, evalmap)` returns the polynomial after replacing all fr... |
Clear solution with comments (Java, Python) | rabbits-in-forest | 1 | 1 | # Intuition\n* If a rabbit says there are $n$ other rabbits of the same color, we can infer there is a group of $n+1$ rabbits that are the same color.\n* If $n+2$ rabbits say they are in a group with $n$ other rabbits of the same color, we need to add another $n+1$ rabbits to our total count.\n\n# Plan\n1. Create a has... | 2 | Given a directed acyclic graph (**DAG**) of `n` nodes labeled from `0` to `n - 1`, find all possible paths from node `0` to node `n - 1` and return them in **any order**.
The graph is given as follows: `graph[i]` is a list of all nodes you can visit from node `i` (i.e., there is a directed edge from node `i` to node `... | null |
Python3 intuitive solution (98.43% Runtime, 100% Memory) | rabbits-in-forest | 0 | 1 | ```\nclass Solution:\n def numRabbits(self, answers: List[int]) -> int:\n d = {}\n count = 0\n for i in answers:\n\t\t# add 1 if rabbit says 0 other rabbits have same color\n if i == 0:\n count+=1\n else:\n\t\t\t# check if i is present in dictionary or not \n... | 21 | There is a forest with an unknown number of rabbits. We asked n rabbits **"How many rabbits have the same color as you? "** and collected the answers in an integer array `answers` where `answers[i]` is the answer of the `ith` rabbit.
Given the array `answers`, return _the minimum number of rabbits that could be in the... | One way is with a Polynomial class. For example,
* `Poly:add(this, that)` returns the result of `this + that`.
* `Poly:sub(this, that)` returns the result of `this - that`.
* `Poly:mul(this, that)` returns the result of `this * that`.
* `Poly:evaluate(this, evalmap)` returns the polynomial after replacing all fr... |
Python3 intuitive solution (98.43% Runtime, 100% Memory) | rabbits-in-forest | 0 | 1 | ```\nclass Solution:\n def numRabbits(self, answers: List[int]) -> int:\n d = {}\n count = 0\n for i in answers:\n\t\t# add 1 if rabbit says 0 other rabbits have same color\n if i == 0:\n count+=1\n else:\n\t\t\t# check if i is present in dictionary or not \n... | 21 | Given a directed acyclic graph (**DAG**) of `n` nodes labeled from `0` to `n - 1`, find all possible paths from node `0` to node `n - 1` and return them in **any order**.
The graph is given as follows: `graph[i]` is a list of all nodes you can visit from node `i` (i.e., there is a directed edge from node `i` to node `... | null |
W | rabbits-in-forest | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | There is a forest with an unknown number of rabbits. We asked n rabbits **"How many rabbits have the same color as you? "** and collected the answers in an integer array `answers` where `answers[i]` is the answer of the `ith` rabbit.
Given the array `answers`, return _the minimum number of rabbits that could be in the... | One way is with a Polynomial class. For example,
* `Poly:add(this, that)` returns the result of `this + that`.
* `Poly:sub(this, that)` returns the result of `this - that`.
* `Poly:mul(this, that)` returns the result of `this * that`.
* `Poly:evaluate(this, evalmap)` returns the polynomial after replacing all fr... |
W | rabbits-in-forest | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given a directed acyclic graph (**DAG**) of `n` nodes labeled from `0` to `n - 1`, find all possible paths from node `0` to node `n - 1` and return them in **any order**.
The graph is given as follows: `graph[i]` is a list of all nodes you can visit from node `i` (i.e., there is a directed edge from node `i` to node `... | null |
Solution with explaination | rabbits-in-forest | 0 | 1 | # Intuition\nWe notice two things:\n1. two rabbits with different answers belong to two diffent groups.\n2. two rabbits with the same answer may or may not belong to the same group.\n\n# Approach\nUsing the first notice, with answers occuring once, the number of rabbits is the sum of these answers plus 1. For example: ... | 0 | There is a forest with an unknown number of rabbits. We asked n rabbits **"How many rabbits have the same color as you? "** and collected the answers in an integer array `answers` where `answers[i]` is the answer of the `ith` rabbit.
Given the array `answers`, return _the minimum number of rabbits that could be in the... | One way is with a Polynomial class. For example,
* `Poly:add(this, that)` returns the result of `this + that`.
* `Poly:sub(this, that)` returns the result of `this - that`.
* `Poly:mul(this, that)` returns the result of `this * that`.
* `Poly:evaluate(this, evalmap)` returns the polynomial after replacing all fr... |
Solution with explaination | rabbits-in-forest | 0 | 1 | # Intuition\nWe notice two things:\n1. two rabbits with different answers belong to two diffent groups.\n2. two rabbits with the same answer may or may not belong to the same group.\n\n# Approach\nUsing the first notice, with answers occuring once, the number of rabbits is the sum of these answers plus 1. For example: ... | 0 | Given a directed acyclic graph (**DAG**) of `n` nodes labeled from `0` to `n - 1`, find all possible paths from node `0` to node `n - 1` and return them in **any order**.
The graph is given as follows: `graph[i]` is a list of all nodes you can visit from node `i` (i.e., there is a directed edge from node `i` to node `... | null |
Solution | transform-to-chessboard | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int movesToChessboard(vector<vector<int>>& board) {\n int n = board.size();\n int row_counter = 0, col_counter = 0;\n for(int r = 0; r < n; r++){\n row_counter += board[r][0] ? 1 : -1;\n for(int c = 0; c < n; c++){\n ... | 1 | You are given an `n x n` binary grid `board`. In each move, you can swap any two rows with each other, or any two columns with each other.
Return _the minimum number of moves to transform the board into a **chessboard board**_. If the task is impossible, return `-1`.
A **chessboard board** is a board where no `0`'s a... | For each stone, check if it is a jewel. |
Solution | transform-to-chessboard | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int movesToChessboard(vector<vector<int>>& board) {\n int n = board.size();\n int row_counter = 0, col_counter = 0;\n for(int r = 0; r < n; r++){\n row_counter += board[r][0] ? 1 : -1;\n for(int c = 0; c < n; c++){\n ... | 1 | You are given an array `nums`. You can rotate it by a non-negative integer `k` so that the array becomes `[nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]`. Afterward, any entries that are less than or equal to their index are worth one point.
* For example, if we have `nums = [2,4... | null |
[Python3] alternating numbers | transform-to-chessboard | 0 | 1 | \n```\nclass Solution:\n def movesToChessboard(self, board: List[List[int]]) -> int:\n n = len(board)\n \n def fn(vals): \n """Return min moves to transform to chessboard."""\n total = odd = 0 \n for i, x in enumerate(vals): \n if vals[0] == x: \n ... | 8 | You are given an `n x n` binary grid `board`. In each move, you can swap any two rows with each other, or any two columns with each other.
Return _the minimum number of moves to transform the board into a **chessboard board**_. If the task is impossible, return `-1`.
A **chessboard board** is a board where no `0`'s a... | For each stone, check if it is a jewel. |
[Python3] alternating numbers | transform-to-chessboard | 0 | 1 | \n```\nclass Solution:\n def movesToChessboard(self, board: List[List[int]]) -> int:\n n = len(board)\n \n def fn(vals): \n """Return min moves to transform to chessboard."""\n total = odd = 0 \n for i, x in enumerate(vals): \n if vals[0] == x: \n ... | 8 | You are given an array `nums`. You can rotate it by a non-negative integer `k` so that the array becomes `[nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]`. Afterward, any entries that are less than or equal to their index are worth one point.
* For example, if we have `nums = [2,4... | null |
782: Beats 98.8%, Solution with step by step explanation | transform-to-chessboard | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n n = len(board)\n```\n\nGet the size of the board.\n\n```\n for i in range(n):\n for j in range(n):\n if board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j]:\n ... | 0 | You are given an `n x n` binary grid `board`. In each move, you can swap any two rows with each other, or any two columns with each other.
Return _the minimum number of moves to transform the board into a **chessboard board**_. If the task is impossible, return `-1`.
A **chessboard board** is a board where no `0`'s a... | For each stone, check if it is a jewel. |
782: Beats 98.8%, Solution with step by step explanation | transform-to-chessboard | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n n = len(board)\n```\n\nGet the size of the board.\n\n```\n for i in range(n):\n for j in range(n):\n if board[0][0] ^ board[i][0] ^ board[0][j] ^ board[i][j]:\n ... | 0 | You are given an array `nums`. You can rotate it by a non-negative integer `k` so that the array becomes `[nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]`. Afterward, any entries that are less than or equal to their index are worth one point.
* For example, if we have `nums = [2,4... | null |
Time: O(n2)O(n2) Space: O(1)O(1) | transform-to-chessboard | 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 `n x n` binary grid `board`. In each move, you can swap any two rows with each other, or any two columns with each other.
Return _the minimum number of moves to transform the board into a **chessboard board**_. If the task is impossible, return `-1`.
A **chessboard board** is a board where no `0`'s a... | For each stone, check if it is a jewel. |
Time: O(n2)O(n2) Space: O(1)O(1) | transform-to-chessboard | 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 `nums`. You can rotate it by a non-negative integer `k` so that the array becomes `[nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]`. Afterward, any entries that are less than or equal to their index are worth one point.
* For example, if we have `nums = [2,4... | null |
Solid Principles | Dimension Independence | Commented and Explained | transform-to-chessboard | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOur main insight into the problem is that there must be a balance of either exactly the same or alternating by 1 number of chess piece locations in two rows based on the idea that we have two rows next to each other (think of a 2 by 2 gri... | 0 | You are given an `n x n` binary grid `board`. In each move, you can swap any two rows with each other, or any two columns with each other.
Return _the minimum number of moves to transform the board into a **chessboard board**_. If the task is impossible, return `-1`.
A **chessboard board** is a board where no `0`'s a... | For each stone, check if it is a jewel. |
Solid Principles | Dimension Independence | Commented and Explained | transform-to-chessboard | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOur main insight into the problem is that there must be a balance of either exactly the same or alternating by 1 number of chess piece locations in two rows based on the idea that we have two rows next to each other (think of a 2 by 2 gri... | 0 | You are given an array `nums`. You can rotate it by a non-negative integer `k` so that the array becomes `[nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]`. Afterward, any entries that are less than or equal to their index are worth one point.
* For example, if we have `nums = [2,4... | null |
Easy Explanation | TC: O(N) and SC: O(H) | Python and C++ | minimum-distance-between-bst-nodes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this approach is that in a BST, the difference between adjacent values is always the smallest. Therefore, if we traverse the BST in a sorted order, we can compare adjacent values and find the smallest difference. By k... | 2 | Given the `root` of a Binary Search Tree (BST), return _the minimum difference between the values of any two different nodes in the tree_.
**Example 1:**
**Input:** root = \[4,2,6,1,3\]
**Output:** 1
**Example 2:**
**Input:** root = \[1,0,48,null,null,12,49\]
**Output:** 1
**Constraints:**
* The number of nodes... | null |
Easy Explanation | TC: O(N) and SC: O(H) | Python and C++ | minimum-distance-between-bst-nodes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this approach is that in a BST, the difference between adjacent values is always the smallest. Therefore, if we traverse the BST in a sorted order, we can compare adjacent values and find the smallest difference. By k... | 2 | We stack glasses in a pyramid, where the **first** row has `1` glass, the **second** row has `2` glasses, and so on until the 100th row. Each glass holds one cup of champagne.
Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to t... | null |
simple solution for beginners | minimum-distance-between-bst-nodes | 0 | 1 | \n\n# Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def minDiffInBST(self, root: Optional[TreeNode]) -> int:\n res=[]\n de... | 1 | Given the `root` of a Binary Search Tree (BST), return _the minimum difference between the values of any two different nodes in the tree_.
**Example 1:**
**Input:** root = \[4,2,6,1,3\]
**Output:** 1
**Example 2:**
**Input:** root = \[1,0,48,null,null,12,49\]
**Output:** 1
**Constraints:**
* The number of nodes... | null |
simple solution for beginners | minimum-distance-between-bst-nodes | 0 | 1 | \n\n# Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def minDiffInBST(self, root: Optional[TreeNode]) -> int:\n res=[]\n de... | 1 | We stack glasses in a pyramid, where the **first** row has `1` glass, the **second** row has `2` glasses, and so on until the 100th row. Each glass holds one cup of champagne.
Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to t... | null |
Python3 | DFS | Easy | minimum-distance-between-bst-nodes | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$ recursive stack space\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def minDiffInBST(self, root: Optional[TreeNode]) -> int:\n minSoFar =... | 1 | Given the `root` of a Binary Search Tree (BST), return _the minimum difference between the values of any two different nodes in the tree_.
**Example 1:**
**Input:** root = \[4,2,6,1,3\]
**Output:** 1
**Example 2:**
**Input:** root = \[1,0,48,null,null,12,49\]
**Output:** 1
**Constraints:**
* The number of nodes... | null |
Python3 | DFS | Easy | minimum-distance-between-bst-nodes | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$ recursive stack space\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def minDiffInBST(self, root: Optional[TreeNode]) -> int:\n minSoFar =... | 1 | We stack glasses in a pyramid, where the **first** row has `1` glass, the **second** row has `2` glasses, and so on until the 100th row. Each glass holds one cup of champagne.
Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to t... | null |
Beginner-friendly || Simple solution with Backracking in Python3 / TypeScript | letter-case-permutation | 0 | 1 | # Intuition\nHere\'s the brief explanation of a problem:\n- there\'s a string `s`, \n- this consists with **alphabetical characters** (English letters and digits)\n- our goal is to get **all** the possible variations of this string by swapping letters in different cases\n\nThere\'re **2 cases** - **lowerCase and upperC... | 1 | Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string.
Return _a list of all possible strings we could create_. Return the output in **any order**.
**Example 1:**
**Input:** s = "a1b2 "
**Output:** \[ "a1b2 ", "a1B2 ", "A1b2 ", "A1B2 "\]
**Example 2:*... | null |
Solution | letter-case-permutation | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<string> letterCasePermutation(string s) {\n vector<string> result;\n int length=s.length();\n letterCasePermutationRec(s,result,length,0);\n return result;\n }\n void letterCasePermutationRec(string& s,vector<string>& result,int length,i... | 1 | Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string.
Return _a list of all possible strings we could create_. Return the output in **any order**.
**Example 1:**
**Input:** s = "a1b2 "
**Output:** \[ "a1b2 ", "a1B2 ", "A1b2 ", "A1B2 "\]
**Example 2:*... | null |
Python clear solution | letter-case-permutation | 0 | 1 | ```\nclass Solution(object):\n def letterCasePermutation(self, S):\n """\n :type S: str\n :rtype: List[str]\n """\n def backtrack(sub="", i=0):\n if len(sub) == len(S):\n res.append(sub)\n else:\n if S[i].isalpha():\n ... | 191 | Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string.
Return _a list of all possible strings we could create_. Return the output in **any order**.
**Example 1:**
**Input:** s = "a1b2 "
**Output:** \[ "a1b2 ", "a1B2 ", "A1b2 ", "A1B2 "\]
**Example 2:*... | null |
「Python3」🧼Clean w/ explain || O(2^N) || Beats "94.11%" || ⏫Thanks for reading & upvt | letter-case-permutation | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe see a `number`, we add it to every string in the `permutation[]`.\n\nWe see a `letter`, we add it 1st time **(lowercase)** to every string `permutation[]` + 2nd time **(uppercase)** to every string `permutation[]`.\n\n---\n\n\n# Appro... | 3 | Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string.
Return _a list of all possible strings we could create_. Return the output in **any order**.
**Example 1:**
**Input:** s = "a1b2 "
**Output:** \[ "a1b2 ", "a1B2 ", "A1b2 ", "A1B2 "\]
**Example 2:*... | null |
✅ 🔥 Python3 || ⚡very easy solution uwu | letter-case-permutation | 0 | 1 | ```\nclass Solution:\n def letterCasePermutation(self, S: str) -> List[str]:\n res = []\n def backtrack(i, curr):\n if i == len(S):\n res.append(curr)\n return\n if S[i].isalpha():\n backtrack(i+1, curr+S[i].upper())\n ba... | 4 | Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string.
Return _a list of all possible strings we could create_. Return the output in **any order**.
**Example 1:**
**Input:** s = "a1b2 "
**Output:** \[ "a1b2 ", "a1B2 ", "A1b2 ", "A1B2 "\]
**Example 2:*... | null |
Python || 99.36% Faster || Recursion || Easy | letter-case-permutation | 0 | 1 | ```\nclass Solution:\n def letterCasePermutation(self, s: str) -> List[str]:\n ans=[]\n def solve(ip,op):\n if ip==\'\':\n ans.append(op)\n return \n if ip[0].isalpha():\n op1=op+ip[0].lower()\n op2=op+ip[0].upper()\n ... | 1 | Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string.
Return _a list of all possible strings we could create_. Return the output in **any order**.
**Example 1:**
**Input:** s = "a1b2 "
**Output:** \[ "a1b2 ", "a1B2 ", "A1b2 ", "A1B2 "\]
**Example 2:*... | null |
BFS Approach || Python || Easy to Understand | is-graph-bipartite | 0 | 1 | # Complexity\n- Time complexity:\nO(E+V)\n\n- Space complexity:\nO(V)\n\n# Code\n```\nfrom collections import deque\nclass Solution:\n def isBipartite(self, graph: List[List[int]]) -> bool:\n color=[-1]*len(graph)\n def bfs(start,graph,color):\n q=deque()\n color[start]=0\n ... | 1 | There is an **undirected** graph with `n` nodes, where each node is numbered between `0` and `n - 1`. You are given a 2D array `graph`, where `graph[u]` is an array of nodes that node `u` is adjacent to. More formally, for each `v` in `graph[u]`, there is an undirected edge between node `u` and node `v`. The graph has ... | null |
BFS Approach || Python || Easy to Understand | is-graph-bipartite | 0 | 1 | # Complexity\n- Time complexity:\nO(E+V)\n\n- Space complexity:\nO(V)\n\n# Code\n```\nfrom collections import deque\nclass Solution:\n def isBipartite(self, graph: List[List[int]]) -> bool:\n color=[-1]*len(graph)\n def bfs(start,graph,color):\n q=deque()\n color[start]=0\n ... | 1 | You are given two integer arrays of the same length `nums1` and `nums2`. In one operation, you are allowed to swap `nums1[i]` with `nums2[i]`.
* For example, if `nums1 = [1,2,3,8]`, and `nums2 = [5,6,7,4]`, you can swap the element at `i = 3` to obtain `nums1 = [1,2,3,4]` and `nums2 = [5,6,7,8]`.
Return _the minimu... | null |
Python3 Solution | is-graph-bipartite | 0 | 1 | \n```\nclass Solution:\n def isBipartite(self, graph: List[List[int]]) -> bool:\n n=len(graph)\n col=[-1]*n\n for i in range(n):\n if col[i]!=-1:\n continue\n\n q=deque()\n q.append((i,0))\n while q:\n node,color=q.popleft... | 1 | There is an **undirected** graph with `n` nodes, where each node is numbered between `0` and `n - 1`. You are given a 2D array `graph`, where `graph[u]` is an array of nodes that node `u` is adjacent to. More formally, for each `v` in `graph[u]`, there is an undirected edge between node `u` and node `v`. The graph has ... | null |
Python3 Solution | is-graph-bipartite | 0 | 1 | \n```\nclass Solution:\n def isBipartite(self, graph: List[List[int]]) -> bool:\n n=len(graph)\n col=[-1]*n\n for i in range(n):\n if col[i]!=-1:\n continue\n\n q=deque()\n q.append((i,0))\n while q:\n node,color=q.popleft... | 1 | You are given two integer arrays of the same length `nums1` and `nums2`. In one operation, you are allowed to swap `nums1[i]` with `nums2[i]`.
* For example, if `nums1 = [1,2,3,8]`, and `nums2 = [5,6,7,4]`, you can swap the element at `i = 3` to obtain `nums1 = [1,2,3,4]` and `nums2 = [5,6,7,8]`.
Return _the minimu... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.