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 |
|---|---|---|---|---|---|---|---|
398: Solution with step by step explanation | random-pick-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTo solve this problem, we can create a dictionary to store the indices of each target number. Then, when we need to pick a random index, we can use the random module to generate a random index from the list of indices associ... | 3 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Python simplest 4-line solution | random-pick-index | 0 | 1 | **Like it? please upvote...**\n```\nclass Solution:\n def __init__(self, nums: List[int]):\n self.index_dict = defaultdict(list)\n for i in range(len(nums)):\n self.index_dict[nums[i]].append(i)\n\n def pick(self, target: int) -> int:\n return random.choice(self.index_dict[target])... | 8 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Solution | random-pick-index | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<int> arr;\n Solution(vector<int>& nums) {\n arr = nums; \n }\n int pick(int target) {\n int len = arr.size();\n int random = 0 + (rand() % len);\n while(arr[random] != target)\n random = 0 + (rand() % len);\n return ... | 1 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Succinct solution | random-pick-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Python: simple and clear solution (Hashmap) | random-pick-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Python | Combining dictionary and reservoir sampling to beat TLE | random-pick-index | 0 | 1 | I wasn\'t able to avoid TLE with either reservoir sampling or the simple hash map strategy (even copying other listed solutions).\n\nIn the end, I was only able to avoid TLE by combining the two approaches. Instead of building up the dictionary of index lists (from which we randomly sample) in the initializer, we inste... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Python3 || Simple || Hash Table || Random.randrange | random-pick-index | 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\nGroup all numbers in the nums array and save all of their indexes. Generate a random number in the range of the size of the index list that has already been calculat... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Python3 Solution without using random module | random-pick-index | 0 | 1 | \n# Code\n```\nclass Solution:\n\n def __init__(self, nums: List[int]):\n \n self.nums=nums\n self.d=defaultdict(list)\n for i,val in enumerate(nums):\n self.d[val].append(i)\n \n\n def pick(self, target: int) -> int:\n indices=self.d[target]\n ans=indic... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Python | Simple intuitive Solution using hash_map | random-pick-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Easy Python Solution | random-pick-index | 0 | 1 | # Code\n```\nfrom collections import Counter\nimport random\n\nclass Solution:\n def __init__(self, nums: List[int]):\n self.dict = {}\n for i in range(len(nums)):\n if nums[i] in self.dict: self.dict[nums[i]].append(i)\n else: self.dict[nums[i]] = [i]\n\n def pick(self, target... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
super short python solution using random.choice | random-pick-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Easy Python Solution ✅ | random-pick-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Store all the values of nums as HASHMAP Keys\n- All the corresponding indices as HASHMAP Values\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 s... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Preprocess the data and make the pick fast | random-pick-index | 0 | 1 | # Intuition\nFirst pre-process the data to make the read faster.\n# Approach\n1. Compute indices for each number\n2. Randomly choose indices\n\nStep 1. is going to take more space.\n# Complexity\n- Time complexity:\nO(1) to generate random index and fetch from list.\n\n- Space complexity:\nO(N) - Each entry can be uniq... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Pythonic - easy and quick | random-pick-index | 0 | 1 | # Intuition\n\nCreate a dictionary of all the indices then sample the indices\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)$$ -->\n... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Simple using map | random-pick-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
python | random-pick-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Python | random-pick-index | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n Hash Table\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(n)\n# Code\n```\nclass Solution:\n\n # O(n)\n d... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
[Python3] Beats 100% runtime and 100% memory | random-pick-index | 0 | 1 | # Code\n```\nclass Solution:\n def __init__(self, nums: List[int]):\n self.nums = nums\n self.map = {}\n\n def pick(self, target: int) -> int:\n if target not in self.map:\n temp = [i for i, n in enumerate(self.nums) if n == target]\n self.map[target] = temp\n ... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Python3 Dictionary+Dictionary | random-pick-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse two dictionaries one for storing all the indexes and for storing last used index.\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... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
3 liner python solution | random-pick-index | 0 | 1 | \n# Code\n```\nclass Solution:\n\n def __init__(self, nums: List[int]):\n self.a = nums\n\n def pick(self, target: int) -> int:\n self.i = [i for i, num in enumerate(self.a) if num == target]\n return random.choice(self.i)\n``` | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Hash map based approach | random-pick-index | 0 | 1 | ```\nclass Solution:\n\n def __init__(self, nums: List[int]):\n self.hash_map = defaultdict(list)\n for i, num in enumerate(nums):\n self.hash_map[num].append(i)\n\n def pick(self, target: int) -> int:\n s = self.hash_map[target]\n index = random.randint(0, len(s)-1)\n ... | 0 | Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
Implement the `Solution` class:
* `Solution(int[] nums)` Initializes the object with the array `nums`.
* `int pick(int target)` Pic... | null |
Image Explanation🏆- [Easiest, Concise & Complete Intuition] - C++/Java/Python | evaluate-division | 1 | 1 | # Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`Evaluate Division` by `Aryan Mittal`\n\n\n\n# Approach & Intution\n$$\n- Space complexity: $$O(n^2)$$\n\n# Code\n```\nclass Solution:\n def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:\n graph = defaultdict(dict)\n\n for (u, v), val in zip(equations, values):\n ... | 18 | You are given an array of variable pairs `equations` and an array of real numbers `values`, where `equations[i] = [Ai, Bi]` and `values[i]` represent the equation `Ai / Bi = values[i]`. Each `Ai` or `Bi` is a string that represents a single variable.
You are also given some `queries`, where `queries[j] = [Cj, Dj]` rep... | Do you recognize this as a graph problem? |
EASY PYTHON SOLUTION USING BFS | evaluate-division | 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 * E)\n- N is number of queries\n- E is number of edges or simple number of equations\n<!-- Add your time complexity here, e.g. ... | 1 | You are given an array of variable pairs `equations` and an array of real numbers `values`, where `equations[i] = [Ai, Bi]` and `values[i]` represent the equation `Ai / Bi = values[i]`. Each `Ai` or `Bi` is a string that represents a single variable.
You are also given some `queries`, where `queries[j] = [Cj, Dj]` rep... | Do you recognize this as a graph problem? |
DFS Approach | Python | Golang | C++ | evaluate-division | 0 | 1 | # Code\n``` Go []\nfunc calcEquation(equations [][]string, values []float64, queries [][]string) []float64 {\n graph := make(map[string]map[string]float64)\n\tseen := make(map[string]bool)\n\n\tfor i := 0; i < len(equations); i++ {\n\t\ta, b := equations[i][0], equations[i][1]\n\n\t\tif _, ok := graph[a]; !ok {\n\t\... | 4 | You are given an array of variable pairs `equations` and an array of real numbers `values`, where `equations[i] = [Ai, Bi]` and `values[i]` represent the equation `Ai / Bi = values[i]`. Each `Ai` or `Bi` is a string that represents a single variable.
You are also given some `queries`, where `queries[j] = [Cj, Dj]` rep... | Do you recognize this as a graph problem? |
399: Solution with step by step explanation | evaluate-division | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe solution uses a graph data structure to represent the equations, with each variable as a node and the value of the edge between two nodes representing the ratio of their values. The graph is built by iterating through th... | 8 | You are given an array of variable pairs `equations` and an array of real numbers `values`, where `equations[i] = [Ai, Bi]` and `values[i]` represent the equation `Ai / Bi = values[i]`. Each `Ai` or `Bi` is a string that represents a single variable.
You are also given some `queries`, where `queries[j] = [Cj, Dj]` rep... | Do you recognize this as a graph problem? |
Python DFS solution Explained (video + code) | evaluate-division | 0 | 1 | [](https://www.youtube.com/watch?v=EfkvVigVou0)\nhttps://www.youtube.com/watch?v=EfkvVigVou0\n```\nclass Solution:\n def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:\n # Step 1. Build the Graph\n graph = collections.defaultdict(dict)\n ... | 40 | You are given an array of variable pairs `equations` and an array of real numbers `values`, where `equations[i] = [Ai, Bi]` and `values[i]` represent the equation `Ai / Bi = values[i]`. Each `Ai` or `Bi` is a string that represents a single variable.
You are also given some `queries`, where `queries[j] = [Cj, Dj]` rep... | Do you recognize this as a graph problem? |
5 liner code | nth-digit | 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)$$ --... | 6 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
[Python3] O(logN) solution | nth-digit | 0 | 1 | Observe that there are 9 numbers with 1 digit, 90 numbers with 2 digits, 900 numbers with 3 digits, ... A `O(logN)` solution can be derived from it. \n\n```\n 1-9 | 9\n 10-99 | 90\n 100-999 | 900 \n 1000-9999 | 9000 \n10000-99999 | 90000\n```\n\n```\nclass Solution:\n def findNthDigit(self, n: int) -... | 36 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
400: Solution with step by step explanation | nth-digit | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Check if n is a single digit number, return n if true.\n2. Set the base value for the digit count to 9 and the initial number of digits to 1.\n3. Use a while loop to iterate as long as n is greater than the product of the... | 7 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
python clean & simple solution ✅ | | beats 94% in runtime | nth-digit | 0 | 1 | *Straight forward way to solve the problem in 3 steps:*\n\n*1. find the length of the number where the nth digit is from*\n*2. find the actual number where the nth digit is from*\n*3. find the nth digit and return*\n# *Code*\n```\nclass Solution:\n def findNthDigit(self, n: int) -> int:\n digit = 1\n c... | 4 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Python3 with math | nth-digit | 0 | 1 | ```\nclass Solution:\n def findNthDigit(self, n: int) -> int:\n """\n imagine the number you need to find have 4 digit\n so you need to go throught all num have 1 digit, 2 digit, 3 digit\n number have 1 digit: 10 ** 1 - 1 = 9 => 9 * 1 = 9 digit\n number have 2 digit: 10 ** 2 - 1 ... | 3 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
80% TC and 89% SC easy python solution | nth-digit | 0 | 1 | ```\ndef findNthDigit(self, n: int) -> int:\n\ttemp = 0\n\ti = 1\n\twhile(temp + i*(10**i - 10**(i-1)) < n):\n\t\ttemp += i*(10**i - 10**(i-1))\n\t\ti += 1\n\td = i\n\tn -= temp\n\tnum = n//d\n\tdone = str(10**(d-1)-1 + num + int(n%d > 0))\n\tif(n % d):\n\t\treturn done[n%d - 1]\n\treturn done[-1]\n``` | 1 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Full math solution | nth-digit | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Time complexity: O(log(N)) , Space complexity: O(log(N)) | nth-digit | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nCalculate Interval (inte): Divide n by 9 to determine the interval of numbers with a certain length.\n\nFind Length (indes): Convert inte to a string (le) and iterate through possible lengths (j). Find the length indes such that the cumulative count o... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
[Python] Intution, Drawing Explanation and Code for my Math Solution. | nth-digit | 0 | 1 | ## Intuition and Drawing Explanation\n\n\nWe can simply keep subtracting from n and get the range. Code is as follows\n\n```\nnumbers_with_i_digits = 9\npower = 1\ndigits ... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Beginner Friendly Solution | nth-digit | 0 | 1 | # Complexity\n- Time complexity:$$O(logn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def findNthDigit(self, n: int) -> int:\n num,digit = 1,1\n count = 9\n\n whi... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Python solution | runtime - 32 ms beats - 90% ✅✅ | nth-digit | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Python O(log(n)) | nth-digit | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nLook at the number of digits:\n1. 1*9: 1~9;\n2. 2*90: 10~99;\n3. 3*900: 100~999;\n4. 4*9000:1000~9999\n5. ....\n\nEach step can take 9, 90, 900, 9000...\nSo the intuition is to subtract number of digits denoted by n_digits * step and when... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Simple brute force that beats 100% in o(logn) | nth-digit | 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# **O**(logn)\n- Space complexity:\n<!-- Add your space complexity here, e.g... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Binary search | nth-digit | 0 | 1 | let f(x) = how many digits in sequence [1,x]\n\nbinary search with this f \n\n# Code\n```\nclass Solution:\n def findNthDigit(self, n: int) -> int:\n def f(bound):\n if (bound <=0):return 0\n digits = int(math.log10(bound))+1\n result =0 \n c = 9\n\n for ... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Python || Binary Search | nth-digit | 0 | 1 | # Intuition\n- We have a search space that is from 1 to n and we will need to find where is the number that we are looking for. Hence binary search\n\n# Approach\n- Binary search\n\n# Complexity\n- Time complexity:\n- O(logN)\n\n- Space complexity:\n- O(1)\n\n# Code\n```\nclass Solution:\n def findNthDigit(self, n: ... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
[Python3] Straightforward Solution | nth-digit | 0 | 1 | # Code\n```\nclass Solution:\n def findNthDigit(self, n: int) -> int:\n lst = []\n cnt = 1\n cur = 9\n for i in range(30):\n lst.append(cnt*cur)\n cnt+=1\n cur*=10\n if cnt*cur > pow(2, 31)-1:\n break\n \n cur = 0\n ... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Python Solution | nth-digit | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Pattern of Number of Values | Explained and Commented | nth-digit | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst thing to notice is the pattern of the number of digits in set ranges \nIn 1 -> 10 : 9 values \nIn 10 -> 100 : 90 values \nIn 100 -> 1000 : 900 values \n\nThis shows that the difference of the current and prior power of 10 is the num... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Python Solution | nth-digit | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
Solution | nth-digit | 1 | 1 | ```C++ []\nclass Solution {\n public:\n int findNthDigit(int n) {\n int digitSize = 1;\n int startNum = 1;\n long count = 9;\n\n while (digitSize * count < n) {\n n -= digitSize * count;\n ++digitSize;\n startNum *= 10;\n count *= 10;\n }\n const int targetNum = startNum + (n - 1)... | 0 | Given an integer `n`, return the `nth` digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
**Example 1:**
**Input:** n = 3
**Output:** 3
**Example 2:**
**Input:** n = 11
**Output:** 0
**Explanation:** The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which ... | null |
401: Solution with step by step explanation | binary-watch | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize an empty list to store the output.\n2. Loop through all possible combinations of hours and minutes from 0 to 11 and 0 to 59 respectively.\n3. For each combination, count the number of set bits in the binary rep... | 14 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Decode the Binary Watch: Find All Possible Times with a Given Number of LEDs On | binary-watch | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOne way to solve this problem is to generate all possible times that can be represented by a binary watch and check how many LEDs are turned on. If the number of turned-on LEDs matches the given input "turnedOn", we add the time to the re... | 7 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Very simple python solution | binary-watch | 0 | 1 | ```python\nclass Solution:\n def readBinaryWatch(self, turnedOn: int) -> List[str]:\n # for example:\n # 2 1 (3 min) - two leds on, bin: 11 \n # 2 (2 min) - one led on, bin: 10\n # 1 (1 min) - one led on, bin: 1\n \n def bit_counter(n):\n s = bin(n)[2:]\n ... | 1 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Python3 simple solution "One-liner" | binary-watch | 0 | 1 | ```\nclass Solution:\n def readBinaryWatch(self, turnedOn):\n return [\'{}:{}\'.format(i,str(j).zfill(2)) for i in range(12) for j in range(60) if bin(i)[2:].count(\'1\') + bin(j)[2:].count(\'1\') == turnedOn]\n```\n**If you like this solution, please upvote for this** | 5 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Solution in Python 3 (beats ~98%) (one line) | binary-watch | 0 | 1 | _Check All Possible Times:_\n```\nclass Solution:\n def readBinaryWatch(self, n: int) -> List[str]:\n \treturn [str(h)+\':\'+\'0\'*(m<10)+str(m) for h in range(12) for m in range(60) if (bin(m)+bin(h)).count(\'1\') == n]\n\n\n\n```\n_Check All Possible Combinations of LEDs:_\n```\nfrom itertools import combinatio... | 10 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Easy solution with full explanation : ) | binary-watch | 1 | 1 | Let\'s break down the problem and the code using the first example:\n\n**Example:**\n```plaintext\nInput: turnedOn = 1\nOutput: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]\n```\n\n**Problem Explanation:**\nYou are given an integer `turnedOn` representing the number of LEDs that are currently... | 0 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Easy to understand, but terrible performance (beats 5%) | binary-watch | 0 | 1 | \n```\nclass Solution:\n def convertBitsToTime(self, bits):\n hour_bits = bits[:4]\n minute_bits = bits[4:]\n hour = 0\n minutes = 0\n for i in range(len(hour_bits)):\n hour += hour_bits[len(hour_bits) - 1 - i] * (2 ** i)\n for i in range(len(minute_bits)):\n ... | 0 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
This problem is really hard. used itertools.combinations. beats 88% | binary-watch | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
[Python] Beats 100% in time, Combination problem | binary-watch | 0 | 1 | # Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nA simple and tuitive combination problem.\r\n\r\n# Code\r\n```\r\nHOURS = [8, 4, 2, 1]\r\nMINUTES = [32, 16, 8, 4, 2, 1]\r\nclass Solution:\r\n def readBinaryWatch(self, turnedOn: int) -> List[str]:\r\n res = []\r\n f... | 0 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
python3 enumerate and backtracking solution | binary-watch | 0 | 1 | \n# Code\nenumerate solution \n```\nclass Solution:\n def hanming_distance(self, num):\n count = 0\n while num:\n num &= num - 1\n count += 1\n return count\n\n def get_num_map(self):\n num_map = [0] * 60\n for i in range(60):\n num_map[i] = self... | 0 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Python Medium | binary-watch | 0 | 1 | ```\nclass Solution:\n def readBinaryWatch(self, turnedOn: int) -> List[str]:\n \'\'\'\n hours 0 - 11\n 1111\n\n\n 6 leds for minutes\n 111111\n\n\n \'\'\'\n\n self.ans = []\n\n def backTrack(curHours, curMinutes, turnedOn):\n if len(curHours) == 4 a... | 0 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Using recursion. Real problem solving ;) | binary-watch | 0 | 1 | # Code\n```\nclass Solution:\n def readBinaryWatch(self, turnedOn: int) -> List[str]:\n\n # UNDERSTANDING\n # 4 LEDs to show hours\n # 6 LEDs to show minutes\n\n # turnedOn give us the count of LED(s) turnedOn as the name suggestes\n\n # need to return all the possible times the LE... | 0 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Time Traveler's Guide: Finding All Possible Binary Watch Times | binary-watch | 0 | 1 | # Intuition\nThe problem involves finding all valid times on a binary watch given the number of "1" bits (turnedOn). To tackle this problem, we can systematically iterate through all possible combinations of hours (0-11) and minutes (0-59). For each combination, we check if the total count of "1" bits in the binary rep... | 0 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
Easy to Understand Brute Force + Recursion Solution | 32ms Runtime | binary-watch | 0 | 1 | Brute force solution, 32ms runtime 16.1MB memory utilization, not sure why but runtime is inconsistent, this is the fastest I got.\n\n# Code\n```\nclass Solution:\n def readBinaryWatch(self, turnedOn: int) -> List[str]:\n def solve(i,index, s, r, ti, lis, t):\n if i>=ti:\n r.append(s... | 0 | A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
* For example, the below binary watch reads `"4:51 "`.
Given an integer `turnedOn` which represents the number... | Simplify by seeking for solutions that involve comparing bit counts. Consider calculating all possible times for comparison purposes. |
✔️ [Python3] MONOTONIC STACK (o^^o)♪, Explained | remove-k-digits | 0 | 1 | **UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nIn order to get the smallest possible number, we have to get rid of as many as possible big digits in the most significant places on the left. We can use a monotonically increasing stack to help us remove those big d... | 154 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
[Python] Very detail explanation with examples using stack | remove-k-digits | 0 | 1 | ```\nclass Solution:\n def removeKdigits(self, num: str, k: int) -> str:\n ## RC ##\n\t\t## APPROACH : STACK ##\n ## IDEA : 1234, k= 2 => when numbers are in increasing order we need to delete last digits \n ## 4321 , k = 2 ==> when numbers are in decreasing order, we need to delete first digits... | 90 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
Monostonic Stack Logic Python3 | remove-k-digits | 0 | 1 | # 1. Monostonic Stack concept:\n```\nclass Solution:\n def removeKdigits(self, num: str, k: int) -> str:\n stack = []\n for n in num:\n while stack and k>0 and stack[-1] > n:\n stack.pop()\n k -= 1\n if stack or n is not \'0\': \n stack... | 5 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
402: Solution with step by step explanation | remove-k-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe algorithm uses a stack to keep track of the digits. It loops through each digit in num, and for each digit, it pops digits from the stack until either the top digit is smaller than the current digit or k becomes 0. Then ... | 4 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
Python Solution || Monotonic Stack || O(n) time | remove-k-digits | 0 | 1 | \n\n```\nclass Solution:\n def removeKdigits(self, num: str, k: int) -> str:\n st = []\n for i in num:\n while k and len(st) > 0 and st[-1] > i:\n k -= 1\n ... | 11 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
📍 ✔Python3 || Using Stack || faster solution | remove-k-digits | 0 | 1 | ```\nclass Solution:\n def removeKdigits(self, num: str, k: int) -> str:\n stack = ["0"]\n if len(num)==k:\n return "0"\n for i in range(len(num)):\n while stack[-1] > num[i] and k > 0:\n stack.pop()\n k=k-1\n stack.append(num[i])\n ... | 6 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
Explanation for beginners like me | remove-k-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwhen the input str is monotonically increasing like "**12345**" k=2 , we remove "45" turn it into "**123**".\nwhen the input array is monotonically decreasing like "**54321**", k=2, we remove "45" turn it into "**321**".\nDoes this mean w... | 2 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
Python3 "ValueError: Exceeds the limit (4300)..." error | remove-k-digits | 0 | 1 | Python 3.11.4 released on June 14, 2023.\n\nCPython introduced limiting conversion size up to 4300 to mitigate DOS attack (CVE-2020-10735)\n\nHowever, since the test case exceeds 4300 characters, an error occurs.\n\nWorkaround: Add below line on your solution\n```\nimport sys\nsys.set_int_max_str_digits(0) \n``` | 2 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
I bet you will Understand!!! Peak and Valley Python 3 | remove-k-digits | 0 | 1 | \nThis question can be analyzed as a series of peaks and valleys\nwe have to delete all **starting point of valleys** till attempts(k) are left!!\n\nfor ex - test case [1,4,3,2,2,1,9]\n\n**Elements inside [ ] are starting point of valleys which we have to delete**\n\n```\n 9\n ... | 6 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
Standard Greedy Approach | remove-k-digits | 0 | 1 | Our appraoch is to remove the decreasing sequence and how can we remove the decreasing sequence, well it is by removing the first digit of the decreasing sequence and we will continue to do so till the time our k becomes zero\n\nif there is no decreasing sequence that means it has increasing sequence, in this case we w... | 1 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
65% Tc and 55% Sc easy python solution | remove-k-digits | 0 | 1 | ```\ndef removeKdigits(self, num: str, k: int) -> str:\n l = len(num)\n if(l == k): return \'0\'\n ans = []\n for i in num:\n while(len(ans) and k > 0 and ans[-1] > i):\n ans.pop()\n k -= 1\n ans.append(i)\n if(k > 0): ans = ans[:-k]... | 1 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
Faster than 98% and less space than 100 % with just a little change | remove-k-digits | 0 | 1 | \nclass Solution:\n def removeKdigits(self, num: str, k: int) -> str:\n \n res = []\n counter = 0\n n = len(num)\n \n if n == k: return "0"\n \n for i in range(n):\n while k and res and res[-1] > num[i]:\n res.pop()\n k ... | 11 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
Python solution with explanation | Stack | Similar to problem 1673 | remove-k-digits | 0 | 1 | The question asks us to remove k digits so that the resulting number is the smallest. Instead of approaching the problem with the mantality of removing k digits, think of the it from another perspective: if the len of the string is N, the question can be re-phrased as:\n\nFind the smallest number with length of N - k. ... | 6 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
python | beats 98% | simple | remove-k-digits | 0 | 1 | ```\nif len(nums)==k:\n return "0"\n stack=[]\n count=0\n stack.append(nums[0])\n for i in range(1,len(nums)):\n while(stack and stack[-1]>nums[i]):\n stack.pop()\n count+=1\n if count==k:\n if stack:\n ... | 5 | Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
**Example 1:**
**Input:** num = "1432219 ", k = 3
**Output:** "1219 "
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which ... | null |
Simple brute force without dp that beats 100% | frog-jump | 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)$$ -->\nO(n**2)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(... | 3 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Bottom-up Dynamic Programming (memoization), beats 97% / 29% | frog-jump | 0 | 1 | # Intuition\nThe problem description tell us about the steps - **HOW** we can choose the **NEXT** stone for the frog jump.\n\n```\n# The frog can jump at each new step with K-previous step.\n# Following to the description there\'re 3 possible movements\nfirstStep = prevStep - 1\nmiddleStep = prevStep\nlastStep = prevSt... | 1 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Python BS + DP. Beats 100%. Explained | frog-jump | 0 | 1 | # Intuition\nThe problem is a DP problem where each state can be represented as (index, prev_jump). Since we are using dfs any state that we encounter again means its false. So use a simple hashmap to store the states. For traversal make use of a for loop to search all positions from prev-1, prev, prev+1, check if ston... | 1 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Python 9 lines 96.65% | frog-jump | 0 | 1 | # Code\n```\nclass Solution:\n def canCross(self, stones: List[int]) -> bool:\n t = stones[-1]\n stones = set(stones)\n @cache\n def rec(loc, j):\n nonlocal stones, t\n if loc not in stones or j == 0:\n return False\n return (loc == t) or re... | 1 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Simple Solution in Python: Just Ask the Right Questions | frog-jump | 0 | 1 | # Code (See below for line-by-line breakdown)\n```\nclass Solution:\n def canCross(self, stones: List[int]) -> bool:\n s = set(stones)\n end = stones[-1]\n\n @cache\n def jump(current: int, k: int):\n if k <= 0:\n # Jumping backwards or in-place.\n ... | 1 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Python3 | DFS | Memoization | frog-jump | 0 | 1 | # Complexity\n- Time complexity:\nO($$n.k$$)\n- Space complexity:\nO($$n.k$$)\n\nhere k can go upto n so O($$n^2$$)\n\n# Code\n```\nclass Solution:\n def canCross(self, stones: List[int]) -> bool:\n\n if stones[1] != 1:\n return False\n\n def dfs(i, k):\n if i == len(stones) - 1:\... | 1 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Graph || BFS || Commented line by line | frog-jump | 0 | 1 | # Code\n```\nclass Solution:\n def canCross(self, stones: List[int]) -> bool:\n #destination is a variable which stores the \n #position of last stone where frog need to reach\n destination=stones[len(stones)-1]\n #it may happen that elements can be repeated so to avoid that\n #ass... | 1 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Top Down DP with Memoization Solution | frog-jump | 0 | 1 | # Code\n```\nclass Solution:\n def canCross(self, stones: List[int]) -> bool:\n if stones[1] - stones[0] != 1:\n return False\n \n stone_ind = {s: i for i, s in enumerate(stones)}\n\n @cache\n def dp(i, lastj):\n if i == len(stones) - 1:\n retur... | 2 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
【Video】Ex-Amazon explains a solution with Python, JavaScript, Java and C++ | frog-jump | 1 | 1 | # Intuition\nUse dynamic programming to keep the last jump and I thought why the frog can\'t jump into the water.\n\n# Solution Video\n\n### Please subscribe to my channel from here. I have 249 videos as of August 28th, 2023.\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confi... | 21 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Python3 Solution | frog-jump | 0 | 1 | \n```\nclass Solution:\n def canCross(self, stones: List[int]) -> bool:\n n=len(stones)\n dp={stone:set() for stone in stones}\n dp[0].add(0)\n for i in range(n):\n for k in dp[stones[i]]:\n for step in range(k-1,k+2):\n if step and stones[i]+s... | 7 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
C++/Python 2d DP||binary Search vs Hash table||Beats 98.48% | frog-jump | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse recursion with meomory to solve. That is DP. Since stones is in ascending order, one approach is using binary search to find the next stone for frog jump. 2nd approach a hash table is established without BS.\n# Approach\n<!-- Describe... | 6 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Python short and clean. Functional programming. | frog-jump | 0 | 1 | # Approach\nTL;DR, Similar to [Editorial solution. Approach 1](https://leetcode.com/problems/frog-jump/editorial/) but shorter and functional.\n\n# Complexity\n- Time complexity: $$O(n^2)$$\n\n- Space complexity: $$O(n)$$\n\nwhere, `n is number of stones`.\n\n# Code\n```python\nclass Solution:\n def canCross(self, s... | 1 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Python | Depth-First-Search | Easy to Understand | Fastest | frog-jump | 0 | 1 | # Python | Depth-First-Search | Easy to Understand | Fastest\n```\nclass Solution(object):\n def canCross(self, stones):\n self.memo = set()\n target = stones[-1]\n stones = set(stones)\n\n res = self.bt(stones, 1, 1, target)\n return res\n\n def bt(self, stones, cur, speed, tar... | 2 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Python solution with Top Down Dynamic programming | frog-jump | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nAs the frist jump can only be 1 unit. If `stones[1] != 1`, we know the frog cannot jump across. Then we start from `stones[1]` and treat the following postions. We define a function `dp(i, k)`, which means whether the frog can jump to p... | 1 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
🔥🔥🔥🔥🔥Beats 100% | JS | TS | Java | C++ | C# | Python | python3 | Kotlin | 🔥🔥🔥🔥🔥 | frog-jump | 1 | 1 | ---\n\n\n---\n\n```C++ []\nclass Solution {\npublic:\n bool canCross(vector<int>& stones) {\n // Create a hashmap to store possible jump sizes for each stone position\n // Key: Stone posi... | 3 | A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by... | null |
Python DFS/Recursion Easy + Intuitive | sum-of-left-leaves | 0 | 1 | At any node, check if the left child is a leaf.\nif yes -> return the left leaf child\'s value + any left leaf children in the right sub-tree\nif no -> bummer. gotta check in the right sub-tree for any left leaf children\n\nAlso, we cannot enter a node and then determine whether it was a left child. For this, one may w... | 50 | Given the `root` of a binary tree, return _the sum of all left leaves._
A **leaf** is a node with no children. A **left leaf** is a leaf that is the left child of another node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 24
**Explanation:** There are two left leaves in the binary tree, wit... | null |
C++ / Python simple and short solutions | sum-of-left-leaves | 0 | 1 | **C++ :**\n\n```\nint sumOfLeftLeaves(TreeNode* root) {\n\tif(!root) return 0;\n\n\tif(root -> left && !root -> left -> left && !root -> left -> right)\n\t\treturn root -> left -> val + sumOfLeftLeaves(root -> right);\n\n\treturn sumOfLeftLeaves(root -> left) + sumOfLeftLeaves(root -> right); \n}\n```\n\n**Python :**\... | 16 | Given the `root` of a binary tree, return _the sum of all left leaves._
A **leaf** is a node with no children. A **left leaf** is a leaf that is the left child of another node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 24
**Explanation:** There are two left leaves in the binary tree, wit... | null |
404: Time 98.99%, Solution with step by step explanation | sum-of-left-leaves | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe algorithm works by recursively traversing the binary tree and checking if each node is a left child and a leaf node. If so, it adds the value of the node to the sum of left leaves. Otherwise, it recursively traverses the... | 8 | Given the `root` of a binary tree, return _the sum of all left leaves._
A **leaf** is a node with no children. A **left leaf** is a leaf that is the left child of another node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 24
**Explanation:** There are two left leaves in the binary tree, wit... | null |
Python iterative solution, beats 100% | sum-of-left-leaves | 0 | 1 | For those who were also confused if root node with no leaves count as left leaf - it is not.\nJust go through all the elements using DFS with stack and remember if it is left or right child.\n\n```\nclass Solution:\n def sumOfLeftLeaves(self, root: TreeNode) -> int:\n result = 0\n stack = [(root, False... | 36 | Given the `root` of a binary tree, return _the sum of all left leaves._
A **leaf** is a node with no children. A **left leaf** is a leaf that is the left child of another node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 24
**Explanation:** There are two left leaves in the binary tree, wit... | null |
✅|| Easiest Way|| Python|| 38ms | sum-of-left-leaves | 0 | 1 | We can easily the reach the leaf nodes, the problem to tackle here is how to know wheather the given node is a left leaf node or not. \n\nTo solve this what I have done is on every recursive call I sent one extra Boolean Variable which was ```True``` when ever I was sending a left node and ```False``` everytime the nod... | 3 | Given the `root` of a binary tree, return _the sum of all left leaves._
A **leaf** is a node with no children. A **left leaf** is a leaf that is the left child of another node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 24
**Explanation:** There are two left leaves in the binary tree, wit... | null |
405: Solution with step by step explanation | convert-a-number-to-hexadecimal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the function toHex that takes an integer num as input and returns a string representing its hexadecimal representation.\n2. Check if the input number is zero. If so, return \'0\' as its hexadecimal representation.\... | 15 | Given an integer `num`, return _a string representing its hexadecimal representation_. For negative integers, [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) method is used.
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer... | null |
using binary code logic || Easily Understandable || | convert-a-number-to-hexadecimal | 0 | 1 | - if you find it usefull please vote it so it can reach maximum people.\n# Code\n```\nclass Solution:\n def toHex(self, num: int) -> str:\n d = {10:\'a\',11:\'b\',12:\'c\',13:\'d\',14:\'e\',15:\'f\'}\n res = ""\n if num == 0:\n return "0"\n if num<0:\n num = (1<<32)+... | 6 | Given an integer `num`, return _a string representing its hexadecimal representation_. For negative integers, [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) method is used.
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer... | null |
convert to hexadecimal number | convert-a-number-to-hexadecimal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | Given an integer `num`, return _a string representing its hexadecimal representation_. For negative integers, [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) method is used.
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer... | null |
easy solution with explanation | convert-a-number-to-hexadecimal | 0 | 1 | * if you find it usefull please vote it so it can reach maximum people.\n```\nclass Solution:\n def toHex(self, num: int) -> str:\n hex="0123456789abcdef" #created string for reference\n ot="" # created a string variable to store and update output string\n if num==0:\n return "0"\n ... | 12 | Given an integer `num`, return _a string representing its hexadecimal representation_. For negative integers, [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement) method is used.
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.