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 DP & RECURSIVE BOTH SOL || Python | house-robber-ii | 0 | 1 | # Intuition\nIn recursive Apporoach u will get a TLE in this question so DP is an optimized acceptable solution her.\nBase Cases\n`1. If only 1 house present return it as max amount to be robbed`\n`2. If only 2 houses present return the max of them`\n\n\nHow this question is different from House Robbers I?\nBecause the... | 1 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
Best 100% || Dp || Space space optimization 🔥🔥🥇🥇✅✅🧠🧠 | house-robber-ii | 0 | 1 | # Intuition\n\n\n# Approach\n\n\nThe code defines a function called maxPrice that takes a list of integers as input and returns the maximum amount of money that can be robbed from that list of houses, subject to the adjacent house constraint. The maxPrice function uses dynamic programming to solve the problem by mainta... | 2 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
Only 1 for loop required! Pseudocode included! | house-robber-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)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$... | 2 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
Easy & Clear Python 3 Solution Recursive + memo (top-down). | house-robber-ii | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def rob(self, nums: List[int]) -> int:\n if len(nums)==1:\n return nums[0]\n memo=[-1 for _ in range(len(nums))]\n def dp(i):\n if i<0:\n return 0 \n elif memo[i]>=0:\n return memo[i]\n ... | 6 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
Python || 99.19% Faster || Dynamic Programming | house-robber-ii | 0 | 1 | ```\nclass Solution:\n def rob(self, nums: List[int]) -> int:\n def solve(a,n):\n n=len(a)\n prev=a[0]\n prev2=0\n curr=0\n for i in range(1,n):\n pick=a[i]\n if i>1:\n pick+=prev2\n non_pick... | 3 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
Python Very Simple and Clean DP Solution - faster than 97% | house-robber-ii | 0 | 1 | ```\nclass Solution:\n def rob(self, nums: List[int]) -> int:\n def houseRobber(nums):\n # dynamic programming - decide each problem by its sub-problems:\n dp = [0]*len(nums)\n dp[0] = nums[0]\n dp[1] = max(nums[0], nums[1])\n for i in range(2, len(nums))... | 64 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
O(N) Approach time Complexity | house-robber-ii | 0 | 1 | \n```\nclass Solution:\n def rob(self, nums: List[int]) -> int:\n #time complexity---->O(N)\n def house(nums):\n rob1,rob2=0,0\n for num in nums:\n temp=max(num+rob1,rob2)\n rob1=rob2\n rob2=temp\n return rob2\n return max... | 6 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
213: Solution with step by step explanation | house-robber-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the class Solution, which contains the rob() method that takes a list of integers as input and returns an integer.\n2. Check the edge case where the input list is empty. If this is true, return 0 because there is n... | 6 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
Dynamic Programming Logic | house-robber-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)$$ --... | 5 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
PYTHON || EASY TO UNDERSTAND || TOP DOWN RECUSRIVE W/ MEMOISATION || 2 SOLUTIONS | house-robber-ii | 0 | 1 | # Solution 1 - in each recursion, decide to skip or take:\nThis requires 2 initial recursive calls (one with first house removed, and one with last house removed)\n```\nclass Solution:\n def rob(self, nums: List[int]) -> int:\n cache1 = {}\n cache2 = {}\n\n def helper(i, nums, cache):\n ... | 1 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
📌 Do house robber twice | house-robber-ii | 0 | 1 | ```\nclass Solution:\n def rob(self, nums: List[int]) -> int:\n \n if len(nums) == 1:\n return nums[0]\n \n dp = {}\n def getResult(a,i):\n if i>=len(a):\n return 0\n if i in dp:\n return dp[i]\n \n ... | 4 | You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatical... | Since House[1] and House[n] are adjacent, they cannot be robbed together. Therefore, the problem becomes to rob either House[1]-House[n-1] or House[2]-House[n], depending on which choice offers more money. Now the problem has degenerated to the House Robber, which is already been solved. |
Brute force | shortest-palindrome | 0 | 1 | # Intuition\nFirst, we need to find the pre-existing palindrome in the string (if it is there). Then, we\'ll add characters to complete that palindrome.\n\n# Approach\nI created a reversed string \'p\'. Then, I iterated over the length of \'s\' and check if the inital \'n-i\' characters of \'s\' are same as the last \'... | 1 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
No brain Python solution | shortest-palindrome | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
Recursion | shortest-palindrome | 0 | 1 | # Approach\nFirst, found the longest prefix of the string that is a palindrome and stored the length. Then reversed the remaining substring. Recursively found the palindrome of the remaining substring and appended it to original string. For base case, if i equals to n (length of string), then s is returned.\n\n# Comple... | 2 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
214: 90.2%, Solution with step by step explanation | shortest-palindrome | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis is a simple and efficient solution that uses string slicing to reverse the original string and check prefixes of the reversed string to find the longest palindrome suffix in the original string. Then it prepends the rem... | 8 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
100% Faster Python Solution - using KMP Algorithm | shortest-palindrome | 1 | 1 | \tclass Solution:\n\t\tdef shortestPalindrome(self, s: str) -> str:\n\t\t\tdef kmp(txt, patt):\n\t\t\t\tnewString = patt + \'#\' + txt\n\t\t\t\tfreqArray = [0 for _ in range(len(newString))]\n\t\t\t\ti = 1\n\t\t\t\tlength = 0\n\t\t\t\twhile i < len(newString):\n\t\t\t\t\tif newString[i] == newString[length]:\n\t\t\t\t\... | 2 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
Rabin Karp and KMP | shortest-palindrome | 0 | 1 | # Code\n```\nclass Solution:\n\n # https://leetcode.com/problems/shortest-palindrome/solutions/60153/8-line-o-n-method-using-rabin-karp-rolling-hash/\n # There is a slight chance that ther could be a hash collision. But this is very \n # unlikely. And we can add check for this where we save all the i values wh... | 1 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
Python3 Epic 3 Liner | shortest-palindrome | 0 | 1 | :)\n```\nclass Solution:\n def shortestPalindrome(self, s: str) -> str:\n for i in range(len(s), -1, -1):\n if s[:i] == s[i-1::-1]:\n return s[:i-1:-1] + s | 1 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
Simple Python in 7 lines with comment | shortest-palindrome | 0 | 1 | \'\'\'\n\n def shortestPalindrome(self, s: str) -> str:\n if s=="": return ""\n if s==s[::-1]: return s\n\t\t\n\t\t# start removing chars from the end until we find a valid palindrome\n\t\t# then, reverse whatever was left at the end and append to the beginning\n for i in range(len(s)-1, -1, -1)... | 9 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
Simple Logic! Python3 Solution | shortest-palindrome | 0 | 1 | # Code\n```\nclass Solution:\n def shortestPalindrome(self, s: str) -> str:\n dd=""\n r=s[::-1]\n k=0\n pali=s\n while dd+s!=(dd+s)[::-1]:\n dd+=r[k]\n k+=1\n pali=dd+s\n return pali\n \n \n \n``` | 1 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
No DP; No DS; Intuitive with comments || Python | shortest-palindrome | 0 | 1 | This solution is very intuitive.\n\nCase 1: \nThe given string is a palindrome; return the string itself.\n\nCase 2:\nFind the longest palindromic substring that starts from the first character of the given string. \nThen reverse the remaining part of the string and add it to the front.\n\nTake a look at the code below... | 3 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
[Python] Stupidly Simple 5 lines solution | shortest-palindrome | 0 | 1 | ```\nclass Solution:\n def shortestPalindrome(self, s: str) -> str:\n if not s: return ""\n for i in reversed(range(len(s))):\n if s[0:i+1] == s[i::-1]:\n return s[:i:-1] + s\n \n``` | 1 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
1 line Python recursive solution | shortest-palindrome | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
Python | 2 methods | KMP + Brute Force | shortest-palindrome | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n- Brute Force\n- KMP\n\n# Code\n```\nclass Solution:\n \n # Brute Force\n # def shortestPalindrome(self, s: str) -> str:\n # r = s[::-1]\n # for i in range(len(r) + 1):\n # if s.startswith(r[i:]):\n ... | 0 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
KMP algorithm O(n) solution | shortest-palindrome | 0 | 1 | # Intuition\nFind the longest palindrome starting at index 0 so that we can append the remaining characters of string at the begining of the string s to get required shortest palindrome\n\n# Approach\nBut How to find the longest palindrome starting at index 0 so I used KMP algorithm to determine it\n\n(please do the qu... | 0 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
3 lines of code super Logic | shortest-palindrome | 0 | 1 | ```\nclass Solution:\n def shortestPalindrome(self, s: str) -> str:\n for i in range(n):\n if s[:n-i]==s[:n-i][::-1]:\n return s[n-i:][::-1]+s\n return ""\n```\n# please upvote me it would encourage me alot\n\n\n | 0 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
Solution O(n) using Manacher's algorythm | shortest-palindrome | 0 | 1 | # Intuition\nI\'ve already solved [Longest Palindromic Substring\n](https://leetcode.com/problems/longest-palindromic-substring/) using [Manacher\'s_algorithm](https://en.wikipedia.org/wiki/Longest_palindromic_substring#Manacher\'s_algorithm) with O(n) time complexity so I decided to reuse code in this problem.\n\n# Ap... | 0 | You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
Return _the shortest palindrome you can find by performing this transformation_.
**Example 1:**
**Input:** s = "aacecaaa"
**Output:** "aaacecaaa"
**Example 2:**
**Input:** s = "abcd"
**Output:** "dcbabcd"
**Constr... | null |
✅ 100% 3-Approaches [VIDEO] - Heap & QuickSelect & Sorting | kth-largest-element-in-an-array | 1 | 1 | # Problem Understanding\n\nIn the "Kth Largest Element in an Array" problem, we are provided with an array of integers `nums` and an integer `k`. The objective is to determine the `k`th largest element in this array.\n\nFor example, with the array `nums = [3,2,1,5,6,4]` and `k = 2`, the expected answer is `5` since `5`... | 236 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
Python randomized quicksort partioning and Reducing search space | kth-largest-element-in-an-array | 0 | 1 | \n# Code\n```\nclass Solution:\n def findKthLargest(self, nums: List[int], k: int) -> int:\n return self.quicksort(nums,len(nums)-k,0,len(nums)-1);\n def quicksort(self,nums,k,a,b):\n #partition\n r = randint(a,b)\n nums[r],nums[b] = nums[b],nums[r]\n p = b # set pivot to right... | 1 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
python3 Solution | kth-largest-element-in-an-array | 0 | 1 | \n```\nclass Solution:\n def findKthLargest(self, nums: List[int], k: int) -> int:\n pivot=random.choice(nums)\n left=[x for x in nums if x>pivot]\n mid=[x for x in nums if x==pivot]\n right=[x for x in nums if x<pivot]\n n=len(left)\n m=len(mid)\n if k<=n:\n ... | 3 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
Python3 | One line code |Beats 97.92%of users with Python3 | kth-largest-element-in-an-array | 0 | 1 | # Code\n```\nclass Solution:\n def findKthLargest(self, nums: List[int], k: int) -> int:\n nums.sort(); return nums[k*-1]\n``` | 0 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
Python Easy Solution || 100% || 2 line Solution || Heap || | kth-largest-element-in-an-array | 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 (logn))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, ... | 1 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
【Quickselect Video】Ex-Amazon explains a solution with Python, JavaScript, Java and C++ | kth-largest-element-in-an-array | 1 | 1 | # Intuition\nUsing quickselect to pick kth largetst number.\n\nNote that I took a video Jan 1st 2023. This question is a little bit changed now. But I still meet this condition "Can you solve it without sorting?". I think we can use the same idea to solve this question and at least, you will learn how quicksort works f... | 15 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
Python short and clean. QuickSelect. Functional programming. | kth-largest-element-in-an-array | 0 | 1 | # Approach: Quick-Select\nTL;DR, Similar to [Editorial solution Approach 3](https://leetcode.com/problems/kth-largest-element-in-an-array/editorial/) but cleaner and more generic.\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\nwhere, `n is length of nums`.\n\n# Code\nRecursive:\n```pyth... | 1 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
Comprehensive Solution Using a Priority Queue | kth-largest-element-in-an-array | 0 | 1 | # Intuition\nWe can convert each values in our input array to their negatives and store them in a min heap. We will then keep popping the smallest value until we get to the appropriate k value after which the value we are interested in will be in front of the min heap, we convert this value to the its original and retu... | 1 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
Python | Optimized Min Heap | O(n) | 99% T 85% S | kth-largest-element-in-an-array | 0 | 1 | **Optimized Min Heap**\n\nThe general idea is to heapify our array and pop k elements to find the k-th largest. There is an optimization we can implement to cut the time and space complexity down. \n\nThe idea is to ALWAYS maintain a MIN heap with only k elements\n * in this case, the k-th largest element will always... | 1 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
Best Python Solution | 1 Line | Using Heap | kth-largest-element-in-an-array | 0 | 1 | \n# Code\n```\nimport heapq\nclass Solution:\n def findKthLargest(self, nums: List[int], k: int) -> int:\n return heapq.nlargest(k,nums)[-1]\n``` | 1 | Given an integer array `nums` and an integer `k`, return _the_ `kth` _largest element in the array_.
Note that it is the `kth` largest element in the sorted order, not the `kth` distinct element.
You must solve it in `O(n)` time complexity.
**Example 1:**
**Input:** nums = \[3,2,1,5,6,4\], k = 2
**Output:** 5
**Ex... | null |
[Python3] Recursive, Beats 98.16% | combination-sum-iii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst thoughts are that we have to somehow not repeat combinations--so what if we pick only the acending ones?\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nDefine some end conditions for recursion: n is unattain... | 1 | Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:
* Only numbers `1` through `9` are used.
* Each number is used **at most once**.
Return _a list of all possible valid combinations_. The list must not contain the same combination twice, and the combinations... | null |
C++ and Python | 80% Faster Code using Backtracking | Easy Understanding | combination-sum-iii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n**Backtracking**\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... | 1 | Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:
* Only numbers `1` through `9` are used.
* Each number is used **at most once**.
Return _a list of all possible valid combinations_. The list must not contain the same combination twice, and the combinations... | null |
✅Easy Backtracking Code || Beats 100%🔥 || AMAZON SDE-1 Interview⚠️ | combination-sum-iii | 1 | 1 | \n\n### **It is one of the Pick only type of Backtracking problems.**\n\n### **Doing Combination Sum 1 and 2 is mandatory before solving this problem.**\n\n### **Also this problem has been asked f... | 7 | Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:
* Only numbers `1` through `9` are used.
* Each number is used **at most once**.
Return _a list of all possible valid combinations_. The list must not contain the same combination twice, and the combinations... | null |
[ Python ] | Simple & Clean Solution using combinations | combination-sum-iii | 0 | 1 | \n# Code\n\n## Using Recursion / DFS:\n```\nclass Solution:\n def combinationSum3(self, k: int, n: int) -> List[List[int]]:\n def dfs(nums, k, n, path, ans):\n if k < 0 or n < 0: return\n if k == 0 and n == 0: ans.append(path)\n\n for i in range(len(nums)):\n ... | 3 | Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:
* Only numbers `1` through `9` are used.
* Each number is used **at most once**.
Return _a list of all possible valid combinations_. The list must not contain the same combination twice, and the combinations... | null |
216: Solution with step by step explanation | combination-sum-iii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe backtrack() function returns a list of lists, where each inner list is a valid combination of numbers that sum up to n. The start parameter represents the smallest number that can be added to the combination, and it star... | 5 | Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:
* Only numbers `1` through `9` are used.
* Each number is used **at most once**.
Return _a list of all possible valid combinations_. The list must not contain the same combination twice, and the combinations... | null |
Python Simple Solution Explained (video + code) Backtracking | combination-sum-iii | 0 | 1 | [](https://www.youtube.com/watch?v=J2hcPZRpbMk)\nhttps://www.youtube.com/watch?v=J2hcPZRpbMk\n```\nclass Solution:\n def combinationSum3(self, k: int, n: int) -> List[List[int]]:\n res = []\n \n def backtrack(num, stack, target):\n if len(stack) == k:\n if target == 0:\... | 21 | Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:
* Only numbers `1` through `9` are used.
* Each number is used **at most once**.
Return _a list of all possible valid combinations_. The list must not contain the same combination twice, and the combinations... | null |
python3 - 3 lines of code | combination-sum-iii | 0 | 1 | \n def combinationSum3(self, k: int, n: int) -> List[List[int]]:\n nums = [i for i in range(1,10)]\n \n comb = itertools.combinations(nums,k)\n \n return [c for c in comb if sum(c) == n] | 4 | Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:
* Only numbers `1` through `9` are used.
* Each number is used **at most once**.
Return _a list of all possible valid combinations_. The list must not contain the same combination twice, and the combinations... | null |
✅4 Method's || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥 | contains-duplicate | 1 | 1 | # Approach 1: Brute Force\n\n# Intuition:\nThe brute force approach compares each element with every other element in the array to check for duplicates. If any duplicates are found, it returns `true`. This approach is straightforward but has a time complexity of O(n^2), making it less efficient for large arrays.\n\n# E... | 886 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
Very Easy || 100% || Fully Explained || C++, Java, Python, Javascript, Python3 (Creating Set) | contains-duplicate | 1 | 1 | # **C++ Solution (Using Set / Sort & Find Approach)**\n```\n/** Approach : Using Set **/\n// Time Complexity: O(n)\nclass Solution {\npublic:\n bool containsDuplicate(vector<int>& nums) {\n // Create a set...\n unordered_set<int> hset;\n // Traverse all the elements through the loop...\n ... | 694 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
One Line code Python3 | contains-duplicate | 0 | 1 | \n\n# One Line Of Code\n```\nclass Solution:\n def containsDuplicate(self, nums: List[int]) -> bool:\n return len(set(nums))!=len(nums)\n```\n# please upvote me it would encourage me alot | 200 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
[VIDEO] Step-by-Step Visualization of O(n) Solution | contains-duplicate | 0 | 1 | https://www.youtube.com/watch?v=JTIq3m8F4hw\n\nThe brute force approach would be to simply check each element with every other element in the array, and return `True` if a duplicate is found or `False` otherwise. Unfortunately, this runs in O(n<sup>2</sup>).\n\nWe can reduce this to O(n) by using a hash set, or simply... | 9 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
217. Contains Duplicate || Solution for Python3 | contains-duplicate | 0 | 1 | Upgraded!\n```\nclass Solution:\n def containsDuplicate(self, nums: List[int]) -> bool:\n if len(nums) != len(set(nums)):\n return True\n return False\n``` | 1 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
Python: set+ len() | contains-duplicate | 0 | 1 | # Intuition\nSince we want to avoid using loops, a good way of thinking about the problem is identifying what we want which is the number of times unique numbers appear in the list.\n\n# Approach\nWe are going to use set() + len() function to help us solve this.\n\n# Complexity\n- Time complexity:\n<!-- Add your time c... | 1 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
Python 1 liner | contains-duplicate | 0 | 1 | ```\ndef containsDuplicate(nums):\n\treturn len(set(nums)) != len(nums)\n``` | 72 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
easiest python3 solution | contains-duplicate | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
contains duplicate | contains-duplicate | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
Python3 code to check if num contains duplicate (TC&SC: O(n)) | contains-duplicate | 0 | 1 | \n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def containsDuplicate(self, nums: List[int]) -> bool:\n if len(set(nums)) != len(nums):\n return True\n \n\n``` | 1 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
One Line Code | contains-duplicate | 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)$$ --... | 5 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
Contains Duplicate || Simple Solution | contains-duplicate | 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)$$ --... | 18 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
5 Different Approaches w/ Explanations | contains-duplicate | 0 | 1 | **Approach #1**: Brute Force - *Time Limit Exceeded*\n\n```\ndef containsDuplicate(self, nums: List[int]) -> bool:\n n = len(nums)\n for i in range(n - 1):\n for j in range(i + 1, n):\n if nums[i] == nums[j]: return True\n return False\n```\n\n* **Explanation**: Use two for loops to compare p... | 98 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
Extremely Simplified ONE LINE PYTHON!!! | contains-duplicate | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nLooking for if the list contains duplicate.\nso we used the length of list and set to compare whether the len(set) is the same as len(list).\n# Solution SIUUUUUUU\n<!-- Describe your approach to solving the problem. -->\nAs we return bool... | 4 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
✅1 line simple in python | contains-duplicate | 0 | 1 | # Code\n```\nclass Solution:\n def containsDuplicate(self, nums: List[int]) -> bool:\n return len(set(nums)) != len(nums)\n # example:\n # nums = [1, 2, 2, 3]\n # set(nums) = [1, 2, 3] (order could be different)\n # if both lenght are the same, it has no duplicated item\n``` | 1 | Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
**Example 1:**
**Input:** nums = \[1,2,3,1\]
**Output:** true
**Example 2:**
**Input:** nums = \[1,2,3,4\]
**Output:** false
**Example 3:**
**Input:** nums = \[1,1,1,... | null |
Solution | the-skyline-problem | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {\n int edge_idx = 0;\n vector<pair<int, int>> edges;\n priority_queue<pair<int, int>> pq;\n vector<vector<int>> skyline;\n\n for (int i = 0; i < buildings.size(); ++i) {\n ... | 437 | A city's **skyline** is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Given the locations and heights of all the buildings, return _the **skyline** formed by these buildings collectively_.
The geometric information of each building is given in the array `buil... | null |
Faster than 93.17% | Single priority Queue | Easy to understand | Python3 | Max heap | the-skyline-problem | 0 | 1 | Please upvote this post if you find it helpful in anyway.\n\n\n\n\n```\nclass Solution(object):\n def getSkyline(self, buildings):\n """\n :type buildings: List[List[int]]\n :rtype: List... | 2 | A city's **skyline** is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Given the locations and heights of all the buildings, return _the **skyline** formed by these buildings collectively_.
The geometric information of each building is given in the array `buil... | null |
Python || 99 % runtime || Hash Table || Easy to understand | contains-duplicate-ii | 0 | 1 | **Plz Upvote ..if you got help from this.**\n# Code\n```\nclass Solution:\n def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:\n d = {}\n\n for i, n in enumerate(nums):\n if n in d and abs(i - d[n]) <= k:\n return True\n else:\n d[n] = i\n ... | 28 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
Very Easy || 100% || Fully Explained || Java, C++, Python, Javascript, Python3 (Using HashSet) | contains-duplicate-ii | 1 | 1 | # **Java Solution (Using HashSet):**\n```\n// Time Complexity : O(n)\nclass Solution {\n public boolean containsNearbyDuplicate(int[] nums, int k) {\n // Base case...\n if(nums == null || nums.length < 2 || k == 0)\n return false;\n int i = 0;\n // Create a Hash Set for storing... | 138 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
Python3 Beats 99.83% | contains-duplicate-ii | 0 | 1 | # Approach\nSimilar to the [Two Sum](https://leetcode.com/problems/two-sum)\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$ because there has been used support dictionary to store covered list\'s items\n\n# Code\n```\nclass Solution:\n def containsNearbyDuplicate(self, nums: List[int],... | 3 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
Easy Python3 Hashmap Solution Beats 55% | contains-duplicate-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:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O... | 1 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
Deadly simple code beats 90.46% in runtime & 87.12% in memory only 6 lines !!!!!!!!!! | contains-duplicate-ii | 0 | 1 | \n\nPlease UPVOTE !!!\n\n\n\n\n\n```\nclass Solution:\n def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:\n if k==0:return False\n set_=set()\n for r in range(len(n... | 11 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
Beats 99.97%, Linear Time Complexity | contains-duplicate-ii | 0 | 1 | # Simple to Understand a Solution\n# Code\n```\nclass Solution:\n def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:\n d1 = {}\n for key, val in enumerate(nums):\n if val not in d1:\n d1[val] = key\n else:\n if abs(d1.get(val) - key) ... | 13 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
219: 96.58%, Solution with step by step explanation | contains-duplicate-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nOne approach to solve this problem is by using a hash map. We will keep track of the indices of each element in the hash map. If we find a repeated element, we can check if the absolute difference between its current index a... | 12 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
Python simple solution | contains-duplicate-ii | 0 | 1 | **Python :**\n\n```\ndef containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:\n\tindex_dict = {}\n\n\n\tfor i, n in enumerate(nums):\n\t\tif n in index_dict and i - index_dict[n] <= k:\n\t\t\treturn True\n\n\t\tindex_dict[n] = i\n\n\treturn False\n```\n\n**Like it ? please upvote !** | 39 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
✔️ Python's Simple and Easy to Understand Solution| O(n) Solution | 99% Faster 🔥 | contains-duplicate-ii | 0 | 1 | **\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n```\nclass Solution:\n def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:\n # Create dictionary for Lookup\n lookup = {}\n \n for i in range(len(nums)):\n \n # If num is prese... | 15 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
Sliding Window Approach Python3 | contains-duplicate-ii | 0 | 1 | # Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:\n window = set()\n L = 0\n for R in range(len(nums)):\n if R - L > k:\n window.remove(nums[L])\n ... | 1 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
✅Python3 Easy to Understand | Dictionary | contains-duplicate-ii | 0 | 1 | ```\nclass Solution:\n def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:\n seen = {}\n \n for i in range(len(nums)):\n if nums[i] in seen and abs(i - seen[nums[i]]) <= k:\n return True\n seen[nums[i]] = i\n return False\n```\nPlease u... | 4 | Given an integer array `nums` and an integer `k`, return `true` _if there are two **distinct indices**_ `i` _and_ `j` _in the array such that_ `nums[i] == nums[j]` _and_ `abs(i - j) <= k`.
**Example 1:**
**Input:** nums = \[1,2,3,1\], k = 3
**Output:** true
**Example 2:**
**Input:** nums = \[1,0,1,1\], k = 1
**Outp... | null |
[Python3] BucketSort O(n) || beats 98% | contains-duplicate-iii | 0 | 1 | ```\nclass Solution:\n def containsNearbyAlmostDuplicate(self, nums: List[int], indexDiff: int, valueDiff: int) -> bool:\n buckets = {} #store values for diapason (i-indexDiff:i]\n valueDiff +=1 #if valueDiff = zero it\'s simplify proces edge-cases\n \n for idx, curVal in enumerate(nums):... | 11 | You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.
Find a pair of indices `(i, j)` such that:
* `i != j`,
* `abs(i - j) <= indexDiff`.
* `abs(nums[i] - nums[j]) <= valueDiff`, and
Return `true` _if such pair exists or_ `false` _otherwise_.
**Example 1:**
**Input:** nums = \[1... | Time complexity O(n logk) - This will give an indication that sorting is involved for k elements. Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is... |
220: Time 94.72% and Space 92.33%, Solution with step by step explanation | contains-duplicate-iii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe solution uses bucketing technique to solve the problem.\n\n1. First, we check if the input list nums is not empty and if indexDiff is greater than 0 and valueDiff is non-negative. If not, we return False as no such pair ... | 5 | You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.
Find a pair of indices `(i, j)` such that:
* `i != j`,
* `abs(i - j) <= indexDiff`.
* `abs(nums[i] - nums[j]) <= valueDiff`, and
Return `true` _if such pair exists or_ `false` _otherwise_.
**Example 1:**
**Input:** nums = \[1... | Time complexity O(n logk) - This will give an indication that sorting is involved for k elements. Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is... |
[Python3] summarizing Contain Duplicates I, II, III | contains-duplicate-iii | 0 | 1 | This is a very self-consistent series. Although ad-hoc implementations exist (please check the corresponding posts in Dicuss), their solutions bear great resemblance to each other. \n\nStarting with [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/), the key is to memoize what has been seen in... | 48 | You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.
Find a pair of indices `(i, j)` such that:
* `i != j`,
* `abs(i - j) <= indexDiff`.
* `abs(nums[i] - nums[j]) <= valueDiff`, and
Return `true` _if such pair exists or_ `false` _otherwise_.
**Example 1:**
**Input:** nums = \[1... | Time complexity O(n logk) - This will give an indication that sorting is involved for k elements. Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is... |
Two Python Solutions: Memory Optimization, Speed Optimization | contains-duplicate-iii | 0 | 1 | Good Memory Optimization But Slow\n\n```\nimport sortedcontainers\nimport bisect\nclass Solution:\n def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:\n \n k +=1\n res = sortedcontainers.SortedList(nums[:k])\n for i in range(1,len(res)):\n if res[... | 3 | You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.
Find a pair of indices `(i, j)` such that:
* `i != j`,
* `abs(i - j) <= indexDiff`.
* `abs(nums[i] - nums[j]) <= valueDiff`, and
Return `true` _if such pair exists or_ `false` _otherwise_.
**Example 1:**
**Input:** nums = \[1... | Time complexity O(n logk) - This will give an indication that sorting is involved for k elements. Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is... |
Python 3 | Official Solution in Python 3 | 2 Methods | Explanation | contains-duplicate-iii | 0 | 1 | Below is Python 3 version of official solution: https://leetcode.com/problems/contains-duplicate-iii/solution/\n### Approach \\#1\n- Omitted since it will TLE\n\n### Approach \\#2 \n- The official Java solution used `TreeSet` as help, which provide the ability to maintain the order. At the same time it search and remov... | 20 | You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.
Find a pair of indices `(i, j)` such that:
* `i != j`,
* `abs(i - j) <= indexDiff`.
* `abs(nums[i] - nums[j]) <= valueDiff`, and
Return `true` _if such pair exists or_ `false` _otherwise_.
**Example 1:**
**Input:** nums = \[1... | Time complexity O(n logk) - This will give an indication that sorting is involved for k elements. Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is... |
[Python] BST approach with sortedcontainers | contains-duplicate-iii | 0 | 1 | ```\nimport sortedcontainers\nclass Solution:\n def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:\n \n def floor(n, bst):\n le = bst.bisect_right(n) - 1\n return bst[le] if le >= 0 else None\n \n def ceiling(n, bst):\n ge = ... | 11 | You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.
Find a pair of indices `(i, j)` such that:
* `i != j`,
* `abs(i - j) <= indexDiff`.
* `abs(nums[i] - nums[j]) <= valueDiff`, and
Return `true` _if such pair exists or_ `false` _otherwise_.
**Example 1:**
**Input:** nums = \[1... | Time complexity O(n logk) - This will give an indication that sorting is involved for k elements. Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is... |
Python3 easy to understand O(n) solution - Contains Duplicate III | contains-duplicate-iii | 0 | 1 | ```\nclass Solution:\n def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:\n d = OrderedDict()\n window = t + 1\n for i, n in enumerate(nums):\n while len(d) > k:\n d.popitem(last=False)\n if (b := n//window) in d:\n ... | 12 | You are given an integer array `nums` and two integers `indexDiff` and `valueDiff`.
Find a pair of indices `(i, j)` such that:
* `i != j`,
* `abs(i - j) <= indexDiff`.
* `abs(nums[i] - nums[j]) <= valueDiff`, and
Return `true` _if such pair exists or_ `false` _otherwise_.
**Example 1:**
**Input:** nums = \[1... | Time complexity O(n logk) - This will give an indication that sorting is involved for k elements. Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is... |
221: Solution with step by step explanation | maximal-square | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis problem can be solved using dynamic programming. We can define a 2D array dp where dp[i][j] represents the maximum size of a square that can be formed at position (i, j) such that all its elements are 1\'s. We can fill ... | 14 | Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_.
**Example 1:**
**Input:** matrix = \[\[ "1 ", "0 ", "1 ", "0 ", "0 "\],\[ "1 ", "0 ", "1 ", "1 ", "1 "\],\[ "1 ", "1 ", "1 ", "1 ", "1 "\],\[ "1 ", "0 ", "0 ", "1 ", "0 "\]\]
**Output:*... | null |
in M*N time | maximal-square | 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:1\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:n*m\n<!-- Add your space complexity here, e.g. $$O(n)$... | 6 | Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_.
**Example 1:**
**Input:** matrix = \[\[ "1 ", "0 ", "1 ", "0 ", "0 "\],\[ "1 ", "0 ", "1 ", "1 ", "1 "\],\[ "1 ", "1 ", "1 ", "1 ", "1 "\],\[ "1 ", "0 ", "0 ", "1 ", "0 "\]\]
**Output:*... | null |
Easy python solution using DP Grid | maximal-square | 0 | 1 | \n# Code\n```\nclass Solution:\n def maximalSquare(self, matrix: List[List[str]]) -> int:\n dp=[[0]*(len(matrix[0])+1)]\n for i in matrix:\n l=[0]\n for j in i:l.append(int(j))\n dp.append(l)\n mx=0\n for i in range(len(dp)):\n for j in range(le... | 6 | Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_.
**Example 1:**
**Input:** matrix = \[\[ "1 ", "0 ", "1 ", "0 ", "0 "\],\[ "1 ", "0 ", "1 ", "1 ", "1 "\],\[ "1 ", "1 ", "1 ", "1 ", "1 "\],\[ "1 ", "0 ", "0 ", "1 ", "0 "\]\]
**Output:*... | null |
Go, Python3 Solutions | maximal-square | 0 | 1 | #### Go\n\n```go\nfunc maximalSquare(matrix [][]byte) int {\n n := len(matrix)\n m := len(matrix[0])\n dp := make([][]int, n)\n for i := range dp {\n dp[i] = make([]int, m)\n }\n maxDimension := 0\n \n for i := 0; i < n; i++ {\n for j := 0; j < m; j++ {\n if matrix[i][j]... | 2 | Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_.
**Example 1:**
**Input:** matrix = \[\[ "1 ", "0 ", "1 ", "0 ", "0 "\],\[ "1 ", "0 ", "1 ", "1 ", "1 "\],\[ "1 ", "1 ", "1 ", "1 ", "1 "\],\[ "1 ", "0 ", "0 ", "1 ", "0 "\]\]
**Output:*... | null |
[Python] 1D-Array DP - Optimisation Process Explained | maximal-square | 0 | 1 | ### Introduction\n\nWe are given a `m x n` array `matrix` and we need to find the largest square such that all elements in this square is `\'1\'`.\n\n---\n\n### Approach 1: Brute Force\n\nSimple enough. For each coordinate `(x, y)` in `matrix`, we check if `matrix[x][y] == \'1\'`. If so, then we know that there exists ... | 18 | Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_.
**Example 1:**
**Input:** matrix = \[\[ "1 ", "0 ", "1 ", "0 ", "0 "\],\[ "1 ", "0 ", "1 ", "1 ", "1 "\],\[ "1 ", "1 ", "1 ", "1 ", "1 "\],\[ "1 ", "0 ", "0 ", "1 ", "0 "\]\]
**Output:*... | null |
Python | DP | Memoization | maximal-square | 0 | 1 | \n\n```\n\nclass Solution:\n def maximalSquare(self, matrix: List[List[str]]) -> int:\n \n dp={}\n ans=0\n m=len(matrix)\n n=len(matrix[0])\n for i in range(m):\n for j in range(n):\n\t\t\t\t#if 1 is encounteres than we have square of atleast size 1 length.\n ... | 1 | Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_.
**Example 1:**
**Input:** matrix = \[\[ "1 ", "0 ", "1 ", "0 ", "0 "\],\[ "1 ", "0 ", "1 ", "1 ", "1 "\],\[ "1 ", "1 ", "1 ", "1 ", "1 "\],\[ "1 ", "0 ", "0 ", "1 ", "0 "\]\]
**Output:*... | null |
Recursive(caching)+DP solution in python | maximal-square | 0 | 1 | \n\n ----------# DP solution T:O(m*n) S:O(1)----------\n\n def maximalSquare(self, matrix: List[List[str]]) -> int:\n ROWS,COLS=len(matrix)-1,len(matrix[0])-1\n res=0\n for r in range(ROWS,-1,-1):\n for c in range(COLS,-1,-1):\n if r>=ROWS or c>=COLS: #for right ... | 1 | Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_.
**Example 1:**
**Input:** matrix = \[\[ "1 ", "0 ", "1 ", "0 ", "0 "\],\[ "1 ", "0 ", "1 ", "1 ", "1 "\],\[ "1 ", "1 ", "1 ", "1 ", "1 "\],\[ "1 ", "0 ", "0 ", "1 ", "0 "\]\]
**Output:*... | null |
Python || 3 lines code || Very Simple to understand | count-complete-tree-nodes | 0 | 1 | **Plz Upvote ..if you got help from this.**\n# Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def countNodes(self, root: Optional[TreeNod... | 7 | Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
According to **[Wikipedia](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)**, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far lef... | null |
My solution for this problem | count-complete-tree-nodes | 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. -->Using a seprate dfs function\n# Complexity\n- Time complexity:O[n]\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O[h],h=height of the tree... | 0 | Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
According to **[Wikipedia](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)**, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far lef... | null |
2 line solution using recursion | count-complete-tree-nodes | 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\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:1\n<!-- Add your space complexity here, e.g. $$O(n)$$ ... | 15 | Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
According to **[Wikipedia](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)**, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far lef... | null |
Clean Python Solution O(log^2(n)) time | count-complete-tree-nodes | 0 | 1 | ```\n\'\'\' \nOnly traversing along the left and right boundary. \nIf both left and right height are equal then\nthe bottom level would be full from left to right and total no. of nodes in that subtree\nis 2^h - 1. \n\nIf left and right height are not equal then add +1 for current root and go to left child \nand right ... | 22 | Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
According to **[Wikipedia](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)**, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far lef... | null |
Fastest python solution | rectangle-area | 0 | 1 | ```\nclass Solution:\n def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n coxl=max(ax1,bx1)\n coxr=min(ax2,bx2)\n coyl=max(ay1,by1)\n coyr=min(ay2,by2)\n dx=coxr-coxl\n dy=coyr-coyl\n comm=0\n if dx>0... | 9 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
Beats %99 time, %98 space python3 readable and easy to understand, full commented code!! | rectangle-area | 0 | 1 | # Approach\nTo find the intersection points of two rectangles, we first need to determine the maximum and minimum values of their edge lines. Once we have identified these values, we can check whether the rectangles are truly intersecting or not. If they are, we can calculate the new intersecting rectangles using basic... | 3 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
✔️ Python Professional Solution | Fastest | 99% Faster 🔥 | rectangle-area | 0 | 1 | **\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n```\nclass Solution:\n def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n \n # Function to caculate area of rectangle (Length * width)\n def area(x1,y1,x2,y2):\... | 3 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
Python || Easy || 95.39% Faster || O(1) Complexity | rectangle-area | 0 | 1 | ```\nclass Solution:\n def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n a1=(ax2-ax1)*(ay2-ay1)\n a2=(bx2-bx1)*(by2-by1)\n x1=max(ax1,bx1)\n x2=min(ax2,bx2)\n y1=max(ay1,by1)\n y2=min(ay2,by2)\n if x2-x1<0 ... | 1 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
Python (Faster than 92%) | O(1) solution | rectangle-area | 0 | 1 | \n\n```\nclass Solution:\n def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n area1 = (ax2 - ax1) * (ay2 - ay1)\n area2 = (bx2 - ... | 1 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
Simplest Solution || Few Lines of Code | rectangle-area | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:\n area_first = abs(ax1 - ax2) * abs(ay1 - ay2)\n area_second = abs(bx1 - bx2) * abs(by1 - by2)\n x_distance = (min(ax2, bx2) -max(ax1, bx1))\n y... | 1 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
python easy solution | rectangle-area | 0 | 1 | \nclass Solution:\n def computeArea(self, ax1, ay1, ax2, ay2, bx1, by1, bx2, by2):\n commonArea=0\n totalArea=(abs(ax2-ax1)*abs(ay2-ay1)) + (abs(bx2-bx1)*abs(by2-by1))\n [x1,y1]=[max(ax1,bx1),max(ay1,by1)]\n [x2,y2]=[min(ax2,bx2),min(ay2,by2)]\n commonArea=0 if (by1>=ay2 or bx1>=ax... | 1 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
223: Solution with step by step explanation | rectangle-area | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. First, we compute the area of both rectangles using the formula area = width * height.\n2. Then, we compute the width and height of the overlap region between the two rectangles. We use min to find the right-most edge of ... | 4 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
✔️ [Python3] READABLE CODE, Explained | rectangle-area | 0 | 1 | **UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nThis is a very popular algorithm for calculation the IoU (intersection over union) in Deep Learning algorithms. Firs\u0435, we calculate areas of both rectangles and then get their sum `S`. Since the area of the over... | 6 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
Python Elegant & Short | O(1) | Unpacking | rectangle-area | 0 | 1 | ```\ndef computeArea(self, *coords) -> int:\n\tx1, y1, x2, y2, x3, y3, x4, y4 = coords\n\toverlap = max(min(x2, x4) - max(x1, x3), 0) * max(min(y2, y4) - max(y1, y3), 0)\n\treturn (x2 - x1) * (y2 - y1) + (x4 - x3) * (y4 - y3) - overlap\n```\n\nIf you like this solution remember to **upvote it** to let me know.\n\n | 4 | Given the coordinates of two **rectilinear** rectangles in a 2D plane, return _the total area covered by the two rectangles_.
The first rectangle is defined by its **bottom-left** corner `(ax1, ay1)` and its **top-right** corner `(ax2, ay2)`.
The second rectangle is defined by its **bottom-left** corner `(bx1, by1)` ... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.