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 |
|---|---|---|---|---|---|---|---|
Easy Python Solution | Sorting | h-index | 0 | 1 | \n# Code\n```\nclass Solution:\n def hIndex(self, citations: List[int]) -> int:\n citations.sort()\n citations=citations[::-1]\n for i in range(len(citations)):\n if citations[i]<=i:\n return i\n return len(citations) \n``` | 5 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th... | An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space. |
✅ Clean and Simple Python3 Code, clear explanations with easy to understand variable naming ✅ | h-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe want to find the largest h-index possible.\nAn h-index is a number `h` such that there are at least `h` papers with `h` citations.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort the citations array in a... | 2 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th... | An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space. |
Frequency Array | O(n) time and O(n) space | h-index | 0 | 1 | # Intuition\nCounting the number of occurences would allow us to determine the max h-index. But the range for the possible number of citations is quite large. Since the maximum h-index can only be the length of the citation array all occurences of papers with more than length many citations can be counted together.\n<!... | 5 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th... | An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space. |
PYTHON O(N) SOLUTION | h-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$$O(N)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $... | 1 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th... | An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space. |
Python | Single line solution | h-index | 0 | 1 | # Intuition\nTop `i+1` papers have a h-index of `min(i+1, citations[i])` since they have at least `i+1` papers with h-index of `citations[i]`.\n\n# Code\n```\nfrom sortedcontainers import SortedList\n\nclass Solution:\n def hIndex(self, citations: List[int]) -> int:\n return max(min(i+1, c) for i, c in enumer... | 2 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th... | An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space. |
Beats 98.78% using binary search | h-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$$O(log(len(citations)))$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution:\n def hIndex(self, citations: List[i... | 2 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th... | An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space. |
Python3 using heaps | h-index | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nUse heaps\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity h... | 2 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th... | An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space. |
Easy to Understand Python Solution with Explanation | h-index | 0 | 1 | I know the question could get really confusing while trying to come up with a solution, but it is very simple.\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n{Case 1: If number of citations of any papers is less or equal than the count of papers (with greater no. of citations)}, it m... | 1 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th... | An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space. |
Python sol by sorting. [w/ Comment] | h-index | 0 | 1 | Python sol by sorting.\n\n\n---\n\n**Implementation** by sorting:\n\n```\nclass Solution:\n def hIndex(self, citations: List[int]) -> int:\n \n citations.sort( reverse = True )\n \n for idx, citation in enumerate(citations):\n\n # find the first index where citation is smaller ... | 13 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th... | An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space. |
275: Time 98.1%, Solution with step by step explanation | h-index-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe problem is a follow-up to the H-Index problem with the added constraint that the input array is sorted in ascending order. As a result, we can take advantage of this property to optimize the solution.\n\nThe basic idea i... | 7 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
Easy Python solution | h-index-ii | 0 | 1 | # Code\n```\nclass Solution:\n def hIndex(self, citations: List[int]) -> int:\n citations=citations[::-1]\n for i in range(len(citations)):\n if citations[i]<=i:\n return i\n return len(citations) \n``` | 4 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
✔ Python3 Solution | Binary Search | O(logn) | h-index-ii | 0 | 1 | `Time Complexity` : `O(logn)`\n`Space Complexity` : `O(1)`\n```\nclass Solution:\n def hIndex(self, A):\n n = len(A)\n l, r = 0, n - 1\n while l < r:\n m = (l + r + 1) // 2\n if A[m] > n - m: r = m - 1\n else: l = m\n return n - l - (A[l] < n - l)\n``` | 3 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
Binary Search Intuative | h-index-ii | 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 array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
BinarySearch two templates | h-index-ii | 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 array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
h index O(n log n) | h-index-ii | 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 array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
Python3 O(logN) solution with binary search (93.51% Runtime) | h-index-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nApply binary search on the array based on citations[m] and length-m\n\n# Approach\n<!-- Describe your approach to solving ... | 0 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
Python binary search | h-index-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse left and right to locate the range of the index of the "starting paper". Let n = len(citations).\n1. The number of papers with equal or more citations: n - idx;\n2. The number of the bottom citations: citations[idx].\n\n\n# Code\n```\... | 0 | Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
o(N) | h-index-ii | 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 array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
optimal solution || Beats 72.64% || python3 | h-index-ii | 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 array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
optimal solution | h-index-ii | 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 array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
Python easy solution | h-index-ii | 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 array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
BinarySearch Solution 95%+ for time and memory | h-index-ii | 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 array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper and `citations` is sorted in **ascending order**, return _the researcher's h-index_.
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-ind... | Expected runtime complexity is in O(log n) and the input is sorted. |
Python3 easy understanding | first-bad-version | 0 | 1 | \n\n# Complexity\n- Time complexity: O(log n)\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```java []\npublic class Solution extends VersionControl {\n public int firstBadVersion(int n) {\n int left = 0;\n... | 2 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
Java and Python Solution | first-bad-version | 1 | 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 log n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g.... | 2 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
Python || Very Easy Solution || Super Fast | first-bad-version | 0 | 1 | \n\n# Code\n```\n# The isBadVersion API is already defined for you.\n# def isBadVersion(version: int) -> bool:\n\nclass Solution:\n def firstBadVersion(self, n: int) -> int:\n\n low = 1\n high = n\n\n while(low<=high):\n mid = (low+high)//2\n if(isBadVersion(mid)):\n ... | 1 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
✔️ Simple Python Solution Using Binary Search 🔥 | first-bad-version | 0 | 1 | **\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n\nVisit this blog to learn Python tips and techniques and to find a Leetcode solution with an explanation: https://www.python-techs.com/\n\n**Solution:**\n```\nclass Solution:\n def firstBadVersion(self, n: int) -> int:\n left = 1\n ... | 36 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
Python Solution Using Binary Search || Easy to Understand | first-bad-version | 0 | 1 | # Intuition\n\n---\n\n\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve this problem, you can use a **binary search approach** to find the **first bad version**.\n# Approach\n\n---\n\n\n<!-- Describe your approach to solving the problem. -->\nWe use two pointers, **left** and **right**, to... | 1 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
Simple binary search approach to solve bad version | first-bad-version | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
Awesome Logic With binary search | first-bad-version | 0 | 1 | \n# Binary Search\n```\n# The isBadVersion API is already defined for you.\n# def isBadVersion(version: int) -> bool:\n\nclass Solution:\n def firstBadVersion(self, n: int) -> int:\n left,right=0,n-1\n while left<=right:\n mid=(left+right)//2\n if isBadVersion(mid)==False:\n ... | 26 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
278: Solution with step by step explanation | first-bad-version | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIn this solution, we maintain two pointers left and right that represent the range of versions we are searching. We start with left = 1 and right = n. At each iteration of the while loop, we compute the midpoint mid of the r... | 22 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
EASY TO UNDERSTAND BINARY SEARCH IMPLEMENTATION | first-bad-version | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nO(n) too slow for big data (do not go for linear method)\ninstead go with binary searching method to reduce api calling\n# Approach\n<!-- Describe your approach to solving the problem. -->\nmodify and implement binary search model to fin... | 3 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
Python Binary Search Beats 92% | first-bad-version | 0 | 1 | # :: IF YOU LIKE THE SOLUTION PLEASE UP-VOTE ::\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. --> We can exploit the fact that our search space is linearly increasing so we can use Binary-Search. \n\n# Approach\n<!-- Describe your approach to solving the problem. --> \n- We find our solu... | 3 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
Python solution, Easy to understand (Binary search) with detailed explanation | first-bad-version | 0 | 1 | ### Solution\n```\ndef firstBadVersion(self, n):\n i = 1\n j = n\n while (i < j):\n pivot = (i+j) // 2\n if (isBadVersion(pivot)):\n j = pivot # keep track of the leftmost bad version\n else:\n i = pivot + 1 # the one after the ... | 58 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
C++/Python/Java Best Optimized Approach using Binary Search | first-bad-version | 1 | 1 | #### C++\nRuntime: 0 ms, faster than 100.00% of C++ online submissions for First Bad Version.\nMemory Usage: 6 MB, less than 22.67% of C++ online submissions for First Bad Version.\n\n```\n// The API isBadVersion is defined for you.\n// bool isBadVersion(int version);\n\nclass Solution {\npublic:\n int firstBadVersi... | 4 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
CPP || PYTHON || Binary Search || 3ms | first-bad-version | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- This problem is a version of binary search much like findind the first occurrence of element in sorted array containing duplicate elements.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Applying binary search, ... | 4 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
✅ 🔥 Python3 || ⚡easy solution | first-bad-version | 0 | 1 | \n```\nclass Solution:\n def firstBadVersion(self, n: int) -> int:\n left, right = 1, n\n \n while left < right:\n mid = (left + right) // 2\n \n if isBadVersion(mid):\n right = mid\n else:\n left = mid + 1\n \n ... | 1 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have `n` versions `[1, 2, ..., n]` an... | null |
Python3 Dynamic Programming | perfect-squares | 0 | 1 | # Code\n```\nclass Solution:\n def numSquares(self, n: int) -> int:\n dp = [20000 for _ in range(n + 1)]\n dp[0] = 0\n ps = []\n\n for i in range(1,n + 1):\n if pow(i, 2) > n:\n break\n ps.append(i ** 2)\n\n for i in range(n + 1):\n f... | 3 | Given an integer `n`, return _the least number of perfect square numbers that sum to_ `n`.
A **perfect square** is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, `1`, `4`, `9`, and `16` are perfect squares while `3` and `11` are not.
**Example ... | null |
VERY EASY TO UNDERSTAND WITH PICTURE PYTHON RECURSION + MEMOIZATION | perfect-squares | 0 | 1 | **279. Perfect Squares**\n\nHey Everyone I will try to explain the solution through some pictures. **How each piece of code is working**!!!\nWas going through DISCUSS Section but coudn\'t wrap my head around why certain lines were written, so after figuring out I tried to share it out here to save someone\'s else time.... | 59 | Given an integer `n`, return _the least number of perfect square numbers that sum to_ `n`.
A **perfect square** is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, `1`, `4`, `9`, and `16` are perfect squares while `3` and `11` are not.
**Example ... | null |
Python 4 lines - Can it be any more concise? | perfect-squares | 0 | 1 | ```\n@lru_cache(None)\ndef dp(n: int) -> int:\n return 0 if n == 0 else min(dp(n-(x+1)**2) for x in range(floor(sqrt(n)))) + 1\nclass Solution:\n def numSquares(self, n: int) -> int:\n return dp(n)\n``` | 3 | Given an integer `n`, return _the least number of perfect square numbers that sum to_ `n`.
A **perfect square** is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, `1`, `4`, `9`, and `16` are perfect squares while `3` and `11` are not.
**Example ... | null |
279: Solution with step by step explanation | perfect-squares | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. We first initialize a list dp with n+1 elements, where dp[i] represents the least number of perfect square numbers that sum to the index i. We set dp[0] = 0, since zero is not a perfect square and does not require any per... | 7 | Given an integer `n`, return _the least number of perfect square numbers that sum to_ `n`.
A **perfect square** is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, `1`, `4`, `9`, and `16` are perfect squares while `3` and `11` are not.
**Example ... | null |
maths solution beats 98.88 % in runtime 🔥🔥🔥| | clean & simple✅ | | Python | perfect-squares | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def numSquares(self, n: int) -> int:\n # Nice Approach 2nd fastest\n # if n <= 0:\n # return 0\n #\n # cnt_perfect_square = [0]\n #\n # while len(cnt_perfect_square) <= n:\n # m = len(cnt_perfect_square)\n # ... | 7 | Given an integer `n`, return _the least number of perfect square numbers that sum to_ `n`.
A **perfect square** is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, `1`, `4`, `9`, and `16` are perfect squares while `3` and `11` are not.
**Example ... | null |
Python Neat Recursive Solution|Does not give TLE but extremely slow | expression-add-operators | 0 | 1 | ```\nimport re\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n operators=[\'+\',\'-\',\'*\']\n N=len(num)\n ans=[]\n def recursive(sp,curr):\n pattern = r\'\\b0\\d+\\b\'\n if sp==len(curr)-1 :\n if (not re.search(patter... | 1 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python Neat Recursive Solution|Does not give TLE but extremely slow | expression-add-operators | 0 | 1 | ```\nimport re\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n operators=[\'+\',\'-\',\'*\']\n N=len(num)\n ans=[]\n def recursive(sp,curr):\n pattern = r\'\\b0\\d+\\b\'\n if sp==len(curr)-1 :\n if (not re.search(patter... | 1 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
282: Solution with step by step explanation | expression-add-operators | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nStep-by-step explanation:\n\n1. The input to the function is a string num and an integer target, representing the number to be expressed and the target value to be achieved, respectively.\n2. The function initializes an empt... | 5 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
282: Solution with step by step explanation | expression-add-operators | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nStep-by-step explanation:\n\n1. The input to the function is a string num and an integer target, representing the number to be expressed and the target value to be achieved, respectively.\n2. The function initializes an empt... | 5 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Well explained Python code | expression-add-operators | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def addOperators(self, s: str, target: int) -> List[str]:\n # need to traverse through s\n # backtrack each case of start index and then +,*,-\n # need empty array ofcourse\n # need curidx = i\n # need the str path to append to the arr if my --> ... | 4 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Well explained Python code | expression-add-operators | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def addOperators(self, s: str, target: int) -> List[str]:\n # need to traverse through s\n # backtrack each case of start index and then +,*,-\n # need empty array ofcourse\n # need curidx = i\n # need the str path to append to the arr if my --> ... | 4 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python recursive solution | expression-add-operators | 0 | 1 | ```\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n answer = set()\n \n def dp(idx, total, path, last_number):\n if idx == len(num) and total == target:\n answer.add(path)\n \n if idx >= len(num):\n ... | 1 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python recursive solution | expression-add-operators | 0 | 1 | ```\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n answer = set()\n \n def dp(idx, total, path, last_number):\n if idx == len(num) and total == target:\n answer.add(path)\n \n if idx >= len(num):\n ... | 1 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Clean Python3 Backtracking Solution | expression-add-operators | 0 | 1 | ```\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n L = len(num)\n ans = set()\n \n def backtrack(i, total, last, expr):\n if i == L:\n if total == target:\n ans.add(expr)\n return\n \n... | 13 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Clean Python3 Backtracking Solution | expression-add-operators | 0 | 1 | ```\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n L = len(num)\n ans = set()\n \n def backtrack(i, total, last, expr):\n if i == L:\n if total == target:\n ans.add(expr)\n return\n \n... | 13 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
brute force with eval and itertools | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
brute force with eval and itertools | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Backtracking python3 | expression-add-operators | 0 | 1 | # Intuition\nBacktracking. The only difference for this one is when making a choice at each step, we either choose to add a number to the expression or add an operator to the expression.\n\n# Approach\nWhen the expression is empty or ending with an operator, we need to choose a number. It can be any substring starting ... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Backtracking python3 | expression-add-operators | 0 | 1 | # Intuition\nBacktracking. The only difference for this one is when making a choice at each step, we either choose to add a number to the expression or add an operator to the expression.\n\n# Approach\nWhen the expression is empty or ending with an operator, we need to choose a number. It can be any substring starting ... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python3 Solution | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python3 Solution | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python Solution | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python Solution | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Recursive Python Solution | expression-add-operators | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def addOperatorsRec(self, num, value, last_mul, target):\n n = len(num)\n ans = []\n if num[0] != \'0\' or len(num) == 1:\n next_num = int(num)\n if max(last_mul * next_num, last_mul + next_num) < target - value:\n return []... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Recursive Python Solution | expression-add-operators | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def addOperatorsRec(self, num, value, last_mul, target):\n n = len(num)\n ans = []\n if num[0] != \'0\' or len(num) == 1:\n next_num = int(num)\n if max(last_mul * next_num, last_mul + next_num) < target - value:\n return []... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python (Simple Backtracking) | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python (Simple Backtracking) | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python Solution | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python Solution | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python3: Backtrack solution | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python3: Backtrack solution | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python backtracking using eval | expression-add-operators | 0 | 1 | # Intuition\nTo give all posibilities of reaching the `target` value using combinations of numbers from `num`, we must iterate through every possible expressions that could be generated out of the original `num` string. Each insert location between two digits faces two decisions: 1) either an operator is inserted, or 2... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python backtracking using eval | expression-add-operators | 0 | 1 | # Intuition\nTo give all posibilities of reaching the `target` value using combinations of numbers from `num`, we must iterate through every possible expressions that could be generated out of the original `num` string. Each insert location between two digits faces two decisions: 1) either an operator is inserted, or 2... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python Easy Solution Runtime 772 ms | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python Easy Solution Runtime 772 ms | expression-add-operators | 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 string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Recursive + Backtrack | expression-add-operators | 0 | 1 | # Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(3^{n-1})$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n + 3^{n-1})$$\n# Code\n```\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n results = []\... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Recursive + Backtrack | expression-add-operators | 0 | 1 | # Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(3^{n-1})$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n + 3^{n-1})$$\n# Code\n```\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n results = []\... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python Solution Evaluate on Fly | expression-add-operators | 0 | 1 | # Code\n```\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n\n def recur(nums,val,prev,expr,res):\n if not nums and val == target:\n res.append(expr)\n return\n \n for i in range(1,len(nums) + 1):\n c... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python Solution Evaluate on Fly | expression-add-operators | 0 | 1 | # Code\n```\nclass Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n\n def recur(nums,val,prev,expr,res):\n if not nums and val == target:\n res.append(expr)\n return\n \n for i in range(1,len(nums) + 1):\n c... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python: brute force solution | expression-add-operators | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. use itertools.product to create a list for all the operators.\n2. calcualte the results as below:\n- calculate all the joint digits\n- calculate all the "*"\n- calc... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Python: brute force solution | expression-add-operators | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. use itertools.product to create a list for all the operators.\n2. calcualte the results as below:\n- calculate all the joint digits\n- calculate all the "*"\n- calc... | 0 | Given a string `num` that contains only digits and an integer `target`, return _**all possibilities** to insert the binary operators_ `'+'`_,_ `'-'`_, and/or_ `'*'` _between the digits of_ `num` _so that the resultant expression evaluates to the_ `target` _value_.
Note that operands in the returned expressions **shoul... | Note that a number can contain multiple digits. Since the question asks us to find all of the valid expressions, we need a way to iterate over all of them. (Hint: Recursion!) We can keep track of the expression string and evaluate it at the very end. But that would take a lot of time. Can we keep track of the expressio... |
Two pointers technique (Python, O(n) time / O(1) space) | move-zeroes | 0 | 1 | Hi there! Here is my solution to this problem that uses two pointers technique.\n\n**Code:**\n```\nclass Solution:\n def moveZeroes(self, nums: list) -> None:\n slow = 0\n for fast in range(len(nums)):\n if nums[fast] != 0 and nums[slow] == 0:\n nums[slow], nums[fast] = nums[f... | 766 | Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
simple approach | move-zeroes | 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`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
Python3 | Beats 99% | Optimized In-Place Rearrangement | move-zeroes | 0 | 1 | # Solution no. 01\n\n---\n\n\n# Intuition\nWhen approaching this problem, the idea is to perform an in-place modification of the given list of numbers. We can achieve this by first iterating through the list and moving all non-zero elements to the beginning of the list. Once we have moved all non-zero elements, we can ... | 27 | Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
Python solution, super simple, clear explanation | move-zeroes | 0 | 1 | # 1. Solution\n\n```\n def moveZeroes(self, nums):\n n = len(nums)\n i = 0\n for j in range(n):\n if (nums[j] != 0):\n nums[i], nums[j] = nums[j], nums[i]\n i += 1\n```\n\n\n# 2. Main idea\n* We use` i` to keep track of position of the first zero in the l... | 169 | Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
Easy solution with O(n) time complexity | move-zeroes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $... | 2 | Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
python O(n) no extra space solution | move-zeroes | 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`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
Python3 simple and clean code | move-zeroes | 0 | 1 | # Code\n```\nclass Solution:\n def moveZeroes(self, nums: List[int]) -> None:\n #Do not return anything, modify nums in-place instead.\n i=0\n for j in range(len(nums)):\n if(nums[i]==0 and nums[j]!=0):\n nums[i],nums[j]=nums[j],nums[i]\n if(nums[i]!=0):i+=1\... | 2 | Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
Simple Python solution | move-zeroes | 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`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
🚀unusual laconic✅ solution!🚀 | move-zeroes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSort in python is stable, so order of equal elements will be not changed\nhttps://wiki.python.org/moin/HowTo/Sorting#Sort_Stability_and_Complex_Sorts\n# Approach\n<!-- Describe your approach to solving the problem. -->\nJust sort them pre... | 11 | Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
Easy Solution. Two pointers Approach. Beats 98%. Python. | move-zeroes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Use two pointers and change elements in place.**\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Iterate through the array with two pointers.\n2. Left pointer points to the last replaced element.\n3. Right point... | 4 | Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
Beginner Friendly Solution | move-zeroes | 0 | 1 | \n# Approach\nTake a variable j, if the value of element in array is not zero, make value of nums[j] equal to i (non zero value of array) and count the total changes made by incrementing j.\nNext, make the value of all elements from range j to n equal to zero.\n<!-- Describe your approach to solving the problem. -->\n\... | 1 | Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
Pointers technique (Python) | move-zeroes | 0 | 1 | # Intuition\nHere is my solution to this problem that uses two pointers technique.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N)\n Our fast pointer does not visit the same spot twice.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complex... | 22 | Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
**Note** that you must do this in-place without making a copy of the array.
**Example 1:**
**Input:** nums = \[0,1,0,3,12\]
**Output:** \[1,3,12,0,0\]
**Example 2:**
**Input:** nums = \[0\]... | In-place means we should not be allocating any space for extra array. But we are allowed to modify the existing array. However, as a first step, try coming up with a solution that makes use of additional space. For this problem as well, first apply the idea discussed using an additional array and the in-place solution ... |
284: Time 89.38% and Space 100%, Solution with step by step explanation | peeking-iterator | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- In the constructor, the iterator is initialized and a buffer is set to the next element if it exists, otherwise, the buffer is set to None.\n- The peek method returns the element currently in the buffer without advancing t... | 3 | Design an iterator that supports the `peek` operation on an existing iterator in addition to the `hasNext` and the `next` operations.
Implement the `PeekingIterator` class:
* `PeekingIterator(Iterator nums)` Initializes the object with the given integer iterator `iterator`.
* `int next()` Returns the next element... | Think of "looking ahead". You want to cache the next element. Is one variable sufficient? Why or why not? Test your design with call order of peek() before next() vs next() before peek(). For a clean implementation, check out Google's guava library source code. |
python3 simple soln | peeking-iterator | 0 | 1 | ```\nclass PeekingIterator:\n def __init__(self, iterator):\n """\n Initialize your data structure here.\n :type iterator: Iterator\n """\n self.iterator = iterator\n self.current = self.iterator.next() if self.iterator.hasNext() else None \n\n def peek(self):\n ... | 23 | Design an iterator that supports the `peek` operation on an existing iterator in addition to the `hasNext` and the `next` operations.
Implement the `PeekingIterator` class:
* `PeekingIterator(Iterator nums)` Initializes the object with the given integer iterator `iterator`.
* `int next()` Returns the next element... | Think of "looking ahead". You want to cache the next element. Is one variable sufficient? Why or why not? Test your design with call order of peek() before next() vs next() before peek(). For a clean implementation, check out Google's guava library source code. |
Python | Easy Solution | O(1) | 89% beats | peeking-iterator | 0 | 1 | ```\nclass PeekingIterator:\n def __init__(self, iterator):\n """\n Initialize your data structure here.\n :type iterator: Iterator\n """\n self.iterator = iterator\n self.to_peek = self.iterator.next() if self.iterator.hasNext() else None\n\n def peek(self):\n """... | 13 | Design an iterator that supports the `peek` operation on an existing iterator in addition to the `hasNext` and the `next` operations.
Implement the `PeekingIterator` class:
* `PeekingIterator(Iterator nums)` Initializes the object with the given integer iterator `iterator`.
* `int next()` Returns the next element... | Think of "looking ahead". You want to cache the next element. Is one variable sufficient? Why or why not? Test your design with call order of peek() before next() vs next() before peek(). For a clean implementation, check out Google's guava library source code. |
[ Python ] Easy Solution | Double beats 100% submit | peeking-iterator | 0 | 1 | ```\nclass PeekingIterator:\n def __init__(self, iterator):\n """\n Initialize your data structure here.\n :type iterator: Iterator\n """\n self.data = iterator.v\n self.index = -1\n \n\n def peek(self):\n """\n Returns the next element in the iterati... | 2 | Design an iterator that supports the `peek` operation on an existing iterator in addition to the `hasNext` and the `next` operations.
Implement the `PeekingIterator` class:
* `PeekingIterator(Iterator nums)` Initializes the object with the given integer iterator `iterator`.
* `int next()` Returns the next element... | Think of "looking ahead". You want to cache the next element. Is one variable sufficient? Why or why not? Test your design with call order of peek() before next() vs next() before peek(). For a clean implementation, check out Google's guava library source code. |
Python3 O(N) solution with a pointer (95.35% Runtime) | peeking-iterator | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nDefine a pointer which stores next value\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Pre-stor... | 0 | Design an iterator that supports the `peek` operation on an existing iterator in addition to the `hasNext` and the `next` operations.
Implement the `PeekingIterator` class:
* `PeekingIterator(Iterator nums)` Initializes the object with the given integer iterator `iterator`.
* `int next()` Returns the next element... | Think of "looking ahead". You want to cache the next element. Is one variable sufficient? Why or why not? Test your design with call order of peek() before next() vs next() before peek(). For a clean implementation, check out Google's guava library source code. |
Python Solution | peeking-iterator | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Design an iterator that supports the `peek` operation on an existing iterator in addition to the `hasNext` and the `next` operations.
Implement the `PeekingIterator` class:
* `PeekingIterator(Iterator nums)` Initializes the object with the given integer iterator `iterator`.
* `int next()` Returns the next element... | Think of "looking ahead". You want to cache the next element. Is one variable sufficient? Why or why not? Test your design with call order of peek() before next() vs next() before peek(). For a clean implementation, check out Google's guava library source code. |
Python solution easy | peeking-iterator | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe iterator class only has two methods: hasNext() and next(). next() would inevitably remove the next element, so we need to cache this result and return it in future.\n\n# Approach\n<!-- Describe your approach to solving the problem. --... | 0 | Design an iterator that supports the `peek` operation on an existing iterator in addition to the `hasNext` and the `next` operations.
Implement the `PeekingIterator` class:
* `PeekingIterator(Iterator nums)` Initializes the object with the given integer iterator `iterator`.
* `int next()` Returns the next element... | Think of "looking ahead". You want to cache the next element. Is one variable sufficient? Why or why not? Test your design with call order of peek() before next() vs next() before peek(). For a clean implementation, check out Google's guava library source code. |
Python3 Solution | find-the-duplicate-number | 0 | 1 | \n```\nclass Solution:\n def findDuplicate(self, nums: List[int]) -> int:\n slow,fast=0,0\n while True:\n slow=nums[slow]\n fast=nums[nums[fast]]\n if slow==fast:\n break\n\n slow2=0\n while True:\n slow=nums[slow]\n sl... | 2 | Given an array of integers `nums` containing `n + 1` integers where each integer is in the range `[1, n]` inclusive.
There is only **one repeated number** in `nums`, return _this repeated number_.
You must solve the problem **without** modifying the array `nums` and uses only constant extra space.
**Example 1:**
**... | null |
✅ 97.77% 6 Approaches Set, Count, Binary Search, Fast Slow, Mark, Sort | find-the-duplicate-number | 1 | 1 | # Comprehensive Guide to Solving "Find the Duplicate Number"\n\n## Introduction & Problem Statement\n\nGiven an array of integers, $$ \\text{nums} $$, containing $$ n + 1 $$ integers where each integer is in the range $$[1, n]$$ inclusive, find the one repeated number in the array. The challenge lies in solving the pro... | 140 | Given an array of integers `nums` containing `n + 1` integers where each integer is in the range `[1, n]` inclusive.
There is only **one repeated number** in `nums`, return _this repeated number_.
You must solve the problem **without** modifying the array `nums` and uses only constant extra space.
**Example 1:**
**... | null |
Floyd's Tortoise and Hare (Cycle Detection)✅ | O( n )✅ | (Step by step explanation)✅ | find-the-duplicate-number | 0 | 1 | # Intuition\nThe problem is asking us to find a duplicate number in an array of integers. One approach is to use Floyd\'s Tortoise and Hare (Cycle Detection) algorithm. The intuition behind this approach is that if we consider the array as a linked list where the value at each index represents the next index, and there... | 1 | Given an array of integers `nums` containing `n + 1` integers where each integer is in the range `[1, n]` inclusive.
There is only **one repeated number** in `nums`, return _this repeated number_.
You must solve the problem **without** modifying the array `nums` and uses only constant extra space.
**Example 1:**
**... | null |
Two Pointer solution with Floyd's Tortoise and Hare algorithm - Python, JavaScript, Java and C++ | find-the-duplicate-number | 1 | 1 | Before starting my article, why do I have to get multiple downvotes in a minute? Please show your respect to others. Thanks. \n\n# Intuition\nslow pointer moves once and fast pointer move twice.\n\n---\n\n# Solution Video\n\nIn the video, the steps of approach below are visualized using diagrams and drawings. I\'m sure... | 71 | Given an array of integers `nums` containing `n + 1` integers where each integer is in the range `[1, n]` inclusive.
There is only **one repeated number** in `nums`, return _this repeated number_.
You must solve the problem **without** modifying the array `nums` and uses only constant extra space.
**Example 1:**
**... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.