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 |
|---|---|---|---|---|---|---|---|
Python short and clean with explanation. SortedList (BST). | data-stream-as-disjoint-intervals | 0 | 1 | # Approach\nOverall algorithm:\n\n1. Initialize a `SortedList` of `intervals`, where an interval is of form `[a, b]`.\n\n2. Maintain the `non overlapping` invariant after each `addNum` operation. (Described below in `addNum` operation)\n\n3. In `getIntervals` operation, return the `intervals` list as is.\n\n\nOperation... | 6 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
python3 | binary search and merge or insert. | data-stream-as-disjoint-intervals | 0 | 1 | Consider ranges as a array maintained sorted. [(r1, r2), (r3,r4) .... ] => r1 < r2 < r3 < r4 ....\nThe above should also hold good after inserting a certain value.\n\nThe following are the cases to consider to insert a value into the range. The value ... \n1. is inside the current ranges ( can be eliminated during bina... | 6 | Given a data stream input of non-negative integers `a1, a2, ..., an`, summarize the numbers seen so far as a list of disjoint intervals.
Implement the `SummaryRanges` class:
* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int value)` Adds the integer `value` to the stream.
* `int... | null |
Python LIS based approach | russian-doll-envelopes | 0 | 1 | The prerequisite for this problem is to understand and solve [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/). So if you are familiar with LIS and have solved it, the difficulty of this problem is reduced.\n\nNow, before we can start implementing LIS for this problem, we n... | 23 | You are given a 2D array of integers `envelopes` where `envelopes[i] = [wi, hi]` represents the width and the height of an envelope.
One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height.
Return _the maximum number of envelope... | null |
354: Space 98.16%, Solution with step by step explanation | russian-doll-envelopes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. We first sort the envelopes based on width in ascending order, and if the widths are equal, we sort them based on height in descending order. This is because when we are trying to find the maximum number of envelopes we c... | 7 | You are given a 2D array of integers `envelopes` where `envelopes[i] = [wi, hi]` represents the width and the height of an envelope.
One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height.
Return _the maximum number of envelope... | null |
355: Time 94.7% and Space 95.87%, Solution with step by step explanation | design-twitter | 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:\n94.7%\n\n- Space complexity:\n95.87%\n\n# Code\n```\nclass Twitter:\n def __init__(self):\n # Use itertools.count to generate a... | 11 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
[Python] 12ms 99.1% better without heap and queue | design-twitter | 0 | 1 | \n```\nclass User:\n def __init__(self, userId):\n self.userId = userId\n self.tweets = {}\n self.followers = set()\n self.follows = set()\n\n\nclass Twitter(object):\n\n def __... | 20 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
[ Python ] ✅✅ Simple Python Solution Using HashMap | Hash Table 🥳✌👍 | design-twitter | 0 | 1 | # If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Runtime: 35 ms, faster than 47.64% of Python3 online submissions for Design Twitter.\n# Memory Usage: 14 MB, less than 95.67% of Python3 online submissions for Design Twitter.\n\n\tclass Twitter:\n\n\t\t\tdef __init__(sel... | 1 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
Simple Python Solution using Hashmap and Hashset with step-by-step explanation | design-twitter | 0 | 1 | \n```\nclass Twitter:\n def __init__(self):\n self.count = 0\n self.follows = collections.defaultdict(list)\n self.tweets = collections.defaultdict(list)\n self.time = collections.defaultdict(int)\n\n def postTweet(self, userId: int, tweetId: int) -> None:\n self.count += 1\n ... | 1 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
Python solution with VERY detailed explanations | design-twitter | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThere are 3 entities/objects in this probelm: user, post, and the system. These entities have their own properties and ther are related in some ways. So I create the 3 classes for them.\nA user object has a unique UserId, a collection of ... | 3 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
Python3 super simple solution, faster than 94.61% | design-twitter | 0 | 1 | ```\nclass Twitter:\n\n def __init__(self):\n """\n Initialize your data structure here.\n """\n self.users=collections.defaultdict(list)\n self.time=1\n self.followers=collections.defaultdict(set)\n \n\n def postTweet(self, userId: int, tweetId: int) -> None:\n ... | 13 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
Simple Python two approaches: Sort naively or heapify | design-twitter | 0 | 1 | ```py\n# assign a timestamp to tweets and sort the tweets naively when you get feed \nclass Twitter:\n\n def __init__(self):\n self.user_to_tweets_map = collections.defaultdict(list) # userId -> list of [tweetIds]\n self.follower_to_followee_map = collections.defaultdict(set) # userId -> set of follow... | 1 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
Clean and simple Implementation || Heap || Hashmap | design-twitter | 0 | 1 | \n\n# Code\n```\nclass Twitter:\n\n def __init__(self):\n self.followMap = collections.defaultdict(set)\n self.postMap = collections.defaultdict(list)\n self.count = 0\n\n def postTweet(self, userId: int, tweetId: int) -> None:\n self.postMap[userId].append([self.count, tweetId])\n ... | 2 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
Approach by storing posts separately. | design-twitter | 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 postTweet: O(1)\n getNewsFeed: O(n) Where n is the total number of posts of all the users\n Follow: O(1)\n Unfollow: O... | 0 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
List and Dictionary Approach | design-twitter | 0 | 1 | # Intuition\nI first thought to use a heap but on later reflection ended up just using a list, since tweets are stored at the end of the list and so they are ordered in time anyways.\n\n# Approach\nThe provided code defines a Twitter class with methods to post tweets, get news feeds, follow users, and unfollow users. I... | 0 | Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the `10` most recent tweets in the user's news feed.
Implement the `Twitter` class:
* `Twitter()` Initializes your twitter object.
* `void postTweet(int userId, int tweetId)` Composes a new tweet w... | null |
Python | simple solution | Permutation | count-numbers-with-unique-digits | 0 | 1 | \tdef countNumbersWithUniqueDigits(self, n: int) -> int:\n ans = 1\n temp = 1\n for i in range(1,n+1):\n ans = 9*temp + ans\n temp = temp*(10-i)\n \n return ans | 3 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Python | Backtracking | slow but mine | count-numbers-with-unique-digits | 0 | 1 | \n\n# Approach\nUse backtracking and search for all possible results and increment count whenever we find the legal number.\nTip : look out for numbers involving 0\n\n\n# Code\n```\nclass Solution:\n def countNumbersWithUniqueDigits(self, n: int) -> int:\n self.count = 0\n if n == 0:\n retur... | 1 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
357: Time 95.29% and Space 94.84%, Solution with step by step explanation | count-numbers-with-unique-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Check if n is equal to 0. If it is, then there is only 1 possible number with unique digits, which is 0. Return 1 as the result.\n2. If n is greater than 10, then the count will be the same as countNumbersWithUniqueDigits... | 6 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Python O(1) 99.80% math with explanation | count-numbers-with-unique-digits | 0 | 1 | ```\nclass Solution(object):\n def countNumbersWithUniqueDigits(self, n):\n """\n :type n: int\n :rtype: int\n """\n def count(k):\n if k == max(10 - n, 0):\n return 0\n return k*(1 + count(k - 1))\n if n == 0:\n return 1\n ... | 23 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Python O(1) Solution both Math, DP explained | count-numbers-with-unique-digits | 0 | 1 | n = 2\nFrom 11 -> 20 => 11,22,33,..99 excluded. \nso ans = 10(for 1->10) + 9*9(as 1 element excluded in each 10 range). = 10 + 81 = 91\n\nn = 3\nfor 1 -> 100 => ans = 91\nFrom 101 -> 200 => (100,101), (110,112,113...119), (121,122),..(191,199) excluded.\nans(100, 199) = 8*9\nFrom 201 -> 300 => (200,202), (211,212), (22... | 4 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Combinatorics | count-numbers-with-unique-digits | 0 | 1 | ```\nclass Solution:\n def countNumbersWithUniqueDigits(self, n: int) -> int:\n res = 10\n if n == 0:\n return 1\n if n == 1:\n return res\n def get_count_of_len_i(i):\n ans = 9\n for k in range(2, i+1):\n ans*=(9-k+2)\n ... | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Python Simple Recursive Solution | count-numbers-with-unique-digits | 0 | 1 | Good explanation can be found here: https://youtu.be/nrDNGVW59c8?feature=shared\n```\nclass Solution:\n def countNumbersWithUniqueDigits(self, n: int) -> int:\n def numUnique(n):\n if n == 0:\n return 1\n else:\n tmp = n - 1\n res = 9\n ... | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Just use math bro | count-numbers-with-unique-digits | 0 | 1 | # Intuition\nBase cases: \nn==0 return 1\nn == 1 return 10 (0-9)\n\nNow for case n == 2, for all 2 digit numbers:\nFirst digit: 9 possible digits can fill this spot (1-9)\nSecond digit: Still 9 possible digits (exclude first digit, but include 0)\n\nCombs = 9x9 + (combinations of 1 digit numbers)\n\nNow for case n == 3... | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Python 3 || Solution | count-numbers-with-unique-digits | 0 | 1 | # 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(n)$$ -->\n\n# Code\n```\nclass Solution:\n def countNumbersWithUniqueDigits(self, n: int) -> int:\n if n == 0:\n return 1\n ... | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
[Python3] Good enough | count-numbers-with-unique-digits | 0 | 1 | ``` Python3 []\nclass Solution:\n def countNumbersWithUniqueDigits(self, n: int) -> int:\n unique = 0\n\n def helper(x, seen, digits):\n nonlocal unique\n unique += 1\n\n for y in range(10):\n if digits<n and not 2**y&seen and not(not x and not y):\n ... | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Easy Python Solution | count-numbers-with-unique-digits | 0 | 1 | # Code\n```\nclass Solution:\n def countNumbersWithUniqueDigits(self, n: int) -> int:\n dp = [1 for _ in range(n + 1)]\n\n for i in range(len(dp)):\n if i == 0: \n dp[i] = 1\n\n elif i > 0:\n count = i - 1\n prod = 1\n m... | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
357. Count Numbers with Unique Digits | count-numbers-with-unique-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Python3 solution with Permutation, Explained | count-numbers-with-unique-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe solve this in a permutation approach, counting the number of unique values for each n. So for n = 3, we count the unique values between 0 - 9, 10 - 99, 100 - 999.... | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
Simple solution | count-numbers-with-unique-digits | 0 | 1 | ```\nclass Solution:\n def countNumbersWithUniqueDigits(self, n: int) -> int:\n if not n:\n return 1\n cnt_choice, total = 9, 10\n for i in range(9, 10-n, -1):\n cnt_choice *= i\n total += cnt_choice\n return total \n``` | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
python solution ( constant time and space ) | count-numbers-with-unique-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n... | 0 | Given an integer `n`, return the count of all numbers with unique digits, `x`, where `0 <= x < 10n`.
**Example 1:**
**Input:** n = 2
**Output:** 91
**Explanation:** The answer should be the total numbers in the range of 0 <= x < 100, excluding 11,22,33,44,55,66,77,88,99
**Example 2:**
**Input:** n = 0
**Output:** 1... | A direct way is to use the backtracking approach. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach n... |
363: Solution with step by step explanation | max-sum-of-rectangle-no-larger-than-k | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Firstly, iterate over all the columns starting from 0 to n. For each iteration, the sum of the values in the current column and all the previous columns will be stored in the list temp.\n2. Using the Kadane\'s algorithm, ... | 2 | Given an `m x n` matrix `matrix` and an integer `k`, return _the max sum of a rectangle in the matrix such that its sum is no larger than_ `k`.
It is **guaranteed** that there will be a rectangle with a sum no larger than `k`.
**Example 1:**
**Input:** matrix = \[\[1,0,1\],\[0,-2,3\]\], k = 2
**Output:** 2
**Explana... | null |
Solution In Python | max-sum-of-rectangle-no-larger-than-k | 0 | 1 | The bisect_left() method is provided by the bisect module, which returns the left-most index to insert the given element, while maintaining the sorted order.\nThe insort function is the second step that does the real insertion process. The function returns the list after the element has been inserted in the rightmost i... | 9 | Given an `m x n` matrix `matrix` and an integer `k`, return _the max sum of a rectangle in the matrix such that its sum is no larger than_ `k`.
It is **guaranteed** that there will be a rectangle with a sum no larger than `k`.
**Example 1:**
**Input:** matrix = \[\[1,0,1\],\[0,-2,3\]\], k = 2
**Output:** 2
**Explana... | null |
🔥[Python] DFS, BFS || Complexity analysis || Video explanation | water-and-jug-problem | 0 | 1 | # Approach\n- Keep tracking the total water of two jugs, ```total```\n- Four potiential operations: ```+jug1Capacity ```, ```-jug1Capacity ```, ```+jug2Capacity ```, ```-jug2Capacity ```\n- Use either DFS or BFS to search for ```total == targetCapacity ```\n - Creat ```set``` to track all ```seen``` total to avo... | 50 | You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
Python Solution using BFS traversal | water-and-jug-problem | 0 | 1 | ```\nclass Solution:\n def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:\n edges=[jug1Capacity,jug2Capacity,abs(jug2Capacity-jug1Capacity)]\n lst=[0]\n mx=max(jug1Capacity,jug2Capacity,targetCapacity)\n visited=[0]*1000001\n if targetCapa... | 2 | You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
Use Breadth First search | Initial state (0, 0) | water-and-jug-problem | 0 | 1 | ```\nclass Solution:\n def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:\n """\n we can solve this problemm using simple bfs approach\n let the initial state be (0, 0), every time a state is dequeued, \n we can perform either of the following op... | 3 | You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
Beats 100% | Simple Math | 0ms | 3 Lines of code 🔥🚀 | water-and-jug-problem | 1 | 1 | # Intuition\nUpvote if you find helpful!!\n\n\n# Approach\nJust do dry run on any example you will easily get the algorithm.\n\n# Complexity\n- Time complexity: O(logn)\n\n- Space com... | 4 | You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
Easy python solution with 80% TC and 99% SC | water-and-jug-problem | 0 | 1 | ```\ndef canMeasureWater(self, jug1: int, jug2: int, tar: int) -> bool:\n\tif(jug1 + jug2 < tar):\n\t\treturn 0\n\treturn tar % math.gcd(jug1, jug2) == 0\n``` | 2 | You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
365: Solution with step by step explanation | water-and-jug-problem | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. First, we import the gcd function from the math module since we will need it later to compute the greatest common divisor of the jug capacities.\n\n2. We define a class called Solution and a method called canMeasureWater ... | 2 | You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
Python - Math Solution | water-and-jug-problem | 0 | 1 | # Intuition\n For Example 1,\n\n I can take j1=3 pour to j2 and then take another j1=3 pour to j2, \n so I have j1=(3+3-5)=1 and j2=5 which I can throw away. so I have \n one unit of water between j1 & j2.\n in order to get to the target=4, I need to repeat this 4x(3+3-5) times.\n\n How many t... | 2 | You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
Simple GCD Solution - beats 80% | water-and-jug-problem | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:\n if jug1Capacity + jug2Capacity < targetCapacity:\n return False\n \n gcd = math.gcd(jug1Capacity, jug2Capacity)\n \n return targetCapacity %... | 0 | You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
Readable Python, BFS, no time to think | water-and-jug-problem | 0 | 1 | # Approach\nI use a standard BFS template and adapt it for the solution. We search a graph where the nodes represent the currently filled state of the jugs. The neighboring states occur due to pouring or filling of jugs. The termination criteria is when the the capacity of the jugs individually or in total meet the tar... | 0 | You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
Simple logic - Python | water-and-jug-problem | 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 two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.
If `targetCapacity` liters of water are measurable, you must have `targetCapacity` li... | null |
Easy solution in Python3 | valid-perfect-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:\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 a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
Without using SQRT( ) lib function | valid-perfect-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:\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 a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
Python || 2 Approach || 85% beats | valid-perfect-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:\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 a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
Python3 || Beats 86.21%|| Beginner oneliner. | valid-perfect-square | 0 | 1 | # Please upvote if you find the solution helpful.\n# Code\n```\nclass Solution:\n def isPerfectSquare(self, num: int) -> bool:\n return sqrt(num) == floor(sqrt(num))\n\n``` | 1 | Given a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
Python and CPP Solution in O(SQRT(NUM)) Time Complexity | valid-perfect-square | 0 | 1 | # Intuition\nnum = 16\n\ni = 1 , num/i = 16/1 = 16, 16>1 i.e. num/i > i\ni = 2 , num/i = 16/2 = 8, 8>2 i.e. num/i >i\ni = 3 , num/i = 16/3 = 5, 5>3 i.e. num/i > i\ni = 4 , num/i = 16/4 = 4, 4==4 i.e. num/i==i\ni = 5 , num/i = 16/5 = 3, 3<5 i.e. num/i<i\n\nSo here we can Observe a pattern, while finding for the square r... | 2 | Given a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
python | Binary Search | beats 99% | valid-perfect-square | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def isPerfectSquare(self, num: int) -> bool:\n if num == 1:\n return True\n lo = 2\n hi = num // 2\n while lo <= hi:\n mid = lo + (hi - lo) //2\n print(mid)\n if mid * mid == num:\n return True\n... | 1 | Given a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
Binary Search and Linear Search Approach | valid-perfect-square | 0 | 1 | \n1.Binary Search Approach\n```\nclass Solution:\n def isPerfectSquare(self, num: int) -> bool:\n left,right=1,num\n while left<=right:\n mid=(left+right)//2\n if mid*mid==num:\n return True\n elif (mid*mid)>num:\n right=mid-1\n ... | 3 | Given a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
367: Solution with step by step explanation | valid-perfect-square | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution uses binary search to find the square root of the given number, which allows for a faster runtime compared to iterating through all possible squares.\n\nFirst, it checks if the given number is 1, which is consi... | 3 | Given a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
Using Binary Search to Check for Perfect Squares | valid-perfect-square | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo check whether a number is a perfect square, we need to find the square root of the number. One way to do this is to use binary search. We can start by setting the left boundary to 1 and the right boundary to num. Then, we can repeatedl... | 15 | Given a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
Easy to Understand Python Solution using Binary Search! | valid-perfect-square | 0 | 1 | # Intuition\nUsing binary search to find if there is a sqrt of n that is an integer. \n\n# Code\n```\nclass Solution:\n def isPerfectSquare(self, num: int) -> bool:\n # if using sqrt func\n # rounded_sqrt = round(sqrt(num))\n # return rounded_sqrt == num \n\n # without using sqrt func \n ... | 2 | Given a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
📌 Python3 extension of binary search | valid-perfect-square | 0 | 1 | ```\nclass Solution:\n def isPerfectSquare(self, num: int) -> bool:\n left,right = 1,num\n while left<=right:\n middle = (left+right)//2\n if middle**2==num:\n return True\n if middle**2>num:\n right = middle-1\n else:\n ... | 5 | Given a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
No brain Python solution | valid-perfect-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:\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 a positive integer num, return `true` _if_ `num` _is a perfect square or_ `false` _otherwise_.
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.
You must not use any built-in library function, such as `sqrt`.
**Example 1:**
**In... | null |
SIMPLE PYTHON SOLUTION || BOTTOM-UP APPROACH | largest-divisible-subset | 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(NlogN + N^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space compl... | 2 | Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
* `answer[i] % answer[j] == 0`, or
* `answer[j] % answer[i] == 0`
If there are multiple solutions, return any of them.
**Example 1:**
**Inp... | null |
Python || 94.02% Faster || LIS Approach | largest-divisible-subset | 0 | 1 | **Since the list is sorted so if a element is divisible by previous element it means it also divisible by all the elements before that previous element because the list is sorted and it become similar to Longest Increasing Subsequence: https://leetcode.com/problems/longest-increasing-subsequence/**\n```\nclass Solution... | 1 | Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
* `answer[i] % answer[j] == 0`, or
* `answer[j] % answer[i] == 0`
If there are multiple solutions, return any of them.
**Example 1:**
**Inp... | null |
[Python] Short, simple, Time-95%, Space-87%, Iterative | largest-divisible-subset | 0 | 1 | Please feel free to ask questions and give suggestions to improve. **Upvote** if you liked the solution.\n**Main Idea**: \n* If we traverse nums in ascending order and we need any 1 longest subset, we need to add exactly 1 new subset for each number. \n\t* If it\'s an extension of a subset we saw before, only 1 of the ... | 4 | Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
* `answer[i] % answer[j] == 0`, or
* `answer[j] % answer[i] == 0`
If there are multiple solutions, return any of them.
**Example 1:**
**Inp... | null |
✅ || python || clean solution | largest-divisible-subset | 0 | 1 | ```\nclass Solution:\n def largestDivisibleSubset(self, nums):\n \n \n n= len(nums)\n \n if n==0:return []\n \n nums.sort()\n \n dp=[ [i] for i in nums]\n \n for i in range(n):\n for j in range(i):\n \n ... | 9 | Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
* `answer[i] % answer[j] == 0`, or
* `answer[j] % answer[i] == 0`
If there are multiple solutions, return any of them.
**Example 1:**
**Inp... | null |
Python DP solution | largest-divisible-subset | 0 | 1 | ```\nclass Solution:\n def largestDivisibleSubset(self, nums: List[int]) -> List[int]:\n n = len(nums)\n nums.sort()\n res = [[num] for num in nums] #contains sets starting with that number\n \n for i in range(n):\n for j in range(i):\n if (nums[i] % nums... | 5 | Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
* `answer[i] % answer[j] == 0`, or
* `answer[j] % answer[i] == 0`
If there are multiple solutions, return any of them.
**Example 1:**
**Inp... | null |
368: Solution with step by step explanation | largest-divisible-subset | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis is a dynamic programming solution to find the largest divisible subset of a set of distinct positive integers.\n\nThe basic idea is to sort the input array so that we can process it in increasing order. We initialize tw... | 2 | Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
* `answer[i] % answer[j] == 0`, or
* `answer[j] % answer[i] == 0`
If there are multiple solutions, return any of them.
**Example 1:**
**Inp... | null |
Easy Python Solution | largest-divisible-subset | 0 | 1 | # Code\n```\nclass Solution:\n def largestDivisibleSubset(self, nums: List[int]) -> List[int]:\n\n nums.sort()\n \n answer = [[nums[i]] for i in range(len(nums))]\n\n for i in range(1, len(nums)):\n subsets = []\n for j in range(i):\n last = answer[j][... | 0 | Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
* `answer[i] % answer[j] == 0`, or
* `answer[j] % answer[i] == 0`
If there are multiple solutions, return any of them.
**Example 1:**
**Inp... | null |
Read this if you want to learn about masks | sum-of-two-integers | 0 | 1 | In Python unlike other languages the range of bits for representing a value is not 32, its much much larger than that. This is great when dealing with non negative integers, however this becomes a big issue when dealing with negative numbers ( two\'s compliment) \n\nWhy ?\n\nLets have a look, say we are adding -2 and 3... | 182 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
[Python3] Math solution with explanation | sum-of-two-integers | 0 | 1 | The main idea is the school formulas for the logarithm: \n1) \n2) \n\n\nThe first gives us the oppor... | 16 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
By Python using AND , XOR Logic also using Mask to tackle negative number testcases ! | sum-of-two-integers | 0 | 1 | We will do AND of A and B to get carry ,also XOR of A and B to get sum \n\nDo carry<<1 to shift the carry where it is to be added in sum ,then\nstore XOR of a and b again in \'a\' and carry again in \'b\' and repeat the process until carry is 0 !\n\nNow since Python cant handle negative numbers we must return the ans b... | 3 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
(Python/C++/Java) Genius hack for similar questions | sum-of-two-integers | 0 | 1 | # Approach\nDo what they don\'t want you to. Question authority. Establish dominance.\n# Complexity\n- Time complexity:\nO(1)\n- Space complexity:\nO(1)\n# Code\n```\nclass Solution:\n def getSum(self, a: int, b: int) -> int:\n return a+b\n``` | 3 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
One Liner || Python Solution | sum-of-two-integers | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def getSum(self, a: int, b: int) -> int:\n return sum([a,b])\n``` | 3 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
371: Solution with step by step explanation | sum-of-two-integers | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution implements the bitwise operation approach to add two integers without using the + or - operators.\n\nThe algorithm starts by initializing a mask variable to 0xFFFFFFFF which is used to ensure that the result st... | 7 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
Python || 95.20% Faster || Bit Manipulation || O(1) Solution | sum-of-two-integers | 0 | 1 | ```\nclass Solution:\n def getSum(self, a: int, b: int) -> int:\n f=0\n if a<0 and b<0:\n f=1\n mask=0xffffffff\n add=a^b\n carry=(a&b)<<1\n while carry!=0:\n add,carry=(add^carry)&mask,((add&carry)<<1)&mask\n if f:\n return ~(add^mask... | 11 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
Marvelous Logic Python3,Java,Golang--->One Line | sum-of-two-integers | 1 | 1 | \n\n# One Line of Code Python3\n```\nclass Solution:\n def getSum(self, a: int, b: int) -> int:\n return sum([a,b])\n```\n# 2. Python3---->Bit Manipulation\n```\nclass Solution:\n def getSum(self, a: int, b: int) -> int:\n while b!=0:\n temp=(a&b)<<1\n a=a^b\n b=temp\... | 7 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
Python | Recursive solution | Thought Process | sum-of-two-integers | 0 | 1 | **Prior Knowledge**\n\nYou guys should check out the top posts to understand the general solution of:\n\n```\ndef getSum(a, b):\n\tif b == 0: return a\n\treturn getSum(a ^ b, (a & b) << 1)\n```\n\nBut here\'s a quick summary:\n- Essentially, `a ^ b` add\'s binary numbers that do not require us to "carry" a "bit".\n- Th... | 31 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
✅ Python one-line | sum-of-two-integers | 0 | 1 | Simply $$a + b = \\log_x(x^a * x^b)$$\n\n# Code\n```\nimport math\n\n\nclass Solution:\n def getSum(self, a: int, b: int) -> int:\n return int(math.log(2 ** a * 2 ** b, 2))\n``` | 7 | Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
**Example 1:**
**Input:** a = 1, b = 2
**Output:** 3
**Example 2:**
**Input:** a = 2, b = 3
**Output:** 5
**Constraints:**
* `-1000 <= a, b <= 1000` | null |
C++uses Chinese Remainder& Fermat's Little Theorems Beats 99.6% | super-pow | 0 | 1 | # Intuition\nLike implementing RSA encryption\n\n# Approach\nUse least significant bit first/most significant bit first algorithm to compute the modular exponentiation. And use the Euler-Fermat Theorem to reduce exponent number modulo phi(n). \n**Euler-Fermat Theorem:**\n$$\na^{\\phi(n)}\\equiv 1 \\pmod{n}\n$$\nfor $\... | 5 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Python Solution One Linear || easy to understand for begineers || pow function | super-pow | 0 | 1 | ```\nclass Solution:\n def superPow(self, a: int, b: List[int]) -> int:\n return pow(a,int(\'\'.join(map(str,b))),1337)\n``` | 1 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Python3 (1 liner code) | super-pow | 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)$$ --... | 8 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Easy Solution with explanation | super-pow | 0 | 1 | # Intuition\nGo digit by digit for b, starting from last digit.\n\n# Approach\n`x^(a+b) = (x^a)*(x^b)` ---(i)\n`x^(a*b) = (x^a)^b` ---(ii)\n\nlet `b = [x, y, z]`, here x, y, z are just digits, x00 means x\\*100, y0 means y\\*10.\n`b = xyz = x00 + y0 + z`\n`a^xyz = (a^x00) * (a^y0) * (a^z)` --- Using(i)\n`a^b = prod... | 3 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
372: Time 100%, Solution with step by step explanation | super-pow | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. The input a is a positive integer, and b is an array representing an extremely large positive integer.\n\n2. The first step is to compute a_mod, which is a modulo 1337. This is done because we need to return the final res... | 5 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Python [EXPLAINED] | recursive | O(log(n)) | comparison with Pow(x, n) | super-pow | 0 | 1 | # Approach :\n- We can use recursive method.\n- when `n` will be even we can decrease the recursive calls by calculating it for `n/2` let\'s say `res` and return <sub>res</sub>2.\n- when `n` will be odd we just use previous step for calculating it for`(n-1)` as `res` and this time return x*(<sub>res</sub>2).\n\n\n\n# C... | 4 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Just Python Things | Easy | super-pow | 0 | 1 | If you are a **python** lover than this program is very easy for you as there is **no Integer Overflow**. So just covert the *power array to an integer* and calculate* a power b* with the given modulo.\n\n\n```\ndef superPow(self, a: int, b: List[int]) -> int:\n mod = 1337\n p = \'\'\n for i in b:\... | 8 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
easiest python soln | super-pow | 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 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Python 3 (With Explanation) (Handles All Test Cases) (one line) (beats ~97%) | super-pow | 0 | 1 | _Explanation:_\n\nThis program is based on [Euler\'s Theorem](https://en.wikipedia.org/wiki/Euler%27s_theorem) which states that: a^( phi(n) ) = 1 (mod n), where phi(n) is Euler\'s Totient Function, provided that a and n are relatively prime (meaning they have no common factors). In our case, n = 1337, which has prime ... | 12 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
pow | super-pow | 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 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Python 1 line solution | super-pow | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDisclaimer : Joke solution, I find enjoyment cheesing LeetCode problems.\n\nThe inbuilt `pow()` function has this functionality already, we just need to convert `b` to a number\n\n# Code\n```\nclass Solution(object):\n def superPow(sel... | 0 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Easy and short Python solution base on the Multiplicative Order | super-pow | 0 | 1 | # Intuition\nWe try to find the [Multiplicative_order](https://en.wikipedia.org/wiki/Multiplicative_order) `r` (aka `len(cycle)`) and the next powers of `a` $(a^1, a^2 ..., a^{r-1})$ modulo `1337`. Because then $ a^b = a^{r*k + remainder} \\equiv a^{reminder} \\pmod{1337}$. Since from pigeonhole principle there there a... | 0 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Simple solution using Chinese remainder theorem with latex | super-pow | 0 | 1 | # Approach\nWe have $1337 = 7 \\cdot 191$, since $7$ and $191$ are coprime, by Chinese remainder theorem, the problem is equivalent to solving the following system of equations $\\begin{cases} x \\equiv a^b & \\pmod{7} \\\\ x \\equiv a^b & \\pmod{191} \\end{cases}$\n\nSince $7$ and $191$ are coprime, there exists two i... | 0 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
☑ 2 Liner using pow( ) ☑ | super-pow | 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 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
best solution | super-pow | 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 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Beats 74.25%of users --->Python3 | super-pow | 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 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
simplepythoncode3 | super-pow | 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 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Python easy solution | super-pow | 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 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Super pow | super-pow | 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 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Easy solution Python | super-pow | 0 | 1 | # Complexity\n- Time\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nRuntime : 79 ms 68.35%\n\nMemory : 13.3 MB 97.47%\n\n# Code\n```\nclass Solution(object):\n def superPow(self, a, b):\n B = map(str,b)\n B = int("".join(B))\n return pow(a,B,1337)\n``` | 0 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
372. Super Pow [Code in Python] Full explanation using Fermat–Euler theorem | super-pow | 0 | 1 | ## Euler\'s theorem (a.k.a the Fermat\u2013Euler theorem or Euler\'s Totient theorem)\nIt states that if there are two co-prime number $$a$$, $$n$$\n$$a^{\\psi(n)}\\equiv 1$$ $$mod$$ $$n$$ \n$$a^{\\psi(n)}$$ $$mod$$ $$n$$ $$=1$$ \nGiven that $$gcd(a,n)=1$$, where $$\\psi(n)$$ is Euler\'s totient function\n\nThe problem... | 0 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Easy Approach for Python Peeps: | super-pow | 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 | Your task is to calculate `ab` mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.
**Example 1:**
**Input:** a = 2, b = \[3\]
**Output:** 8
**Example 2:**
**Input:** a = 2, b = \[1,0\]
**Output:** 1024
**Example 3:**
**Input:** a = 1, b = \[4,3,... | null |
Python Beginner || Solution using Heap 🐍🐍 | find-k-pairs-with-smallest-sums | 0 | 1 | # Code\n```\nclass Solution:\n def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:\n h = []; res = []\n for i in range(min(len(nums1), k)):\n tup = [nums1[i], nums2[0]]\n heapq.heappush(h, [sum(tup), tup, 0])\n while k>0 and len(h)>0:\n ... | 1 | You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
Return _the_ `k` _pairs_ `(u1, v1), (u2, v2), ..., (uk, vk)` _with the smallest sums_.
**Example 1:**... | null |
Python3 Solution | find-k-pairs-with-smallest-sums | 0 | 1 | \n```\nfrom heapq import *\nclass Solution:\n def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:\n heap=[]\n for i in range(min(k,len(nums1))):\n for j in range(min(k,len(nums2))):\n if len(heap)<k:\n heappush(heap,(-(nu... | 1 | You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
Return _the_ `k` _pairs_ `(u1, v1), (u2, v2), ..., (uk, vk)` _with the smallest sums_.
**Example 1:**... | null |
From Dumb to Pro with Just One Visit - My Promise to You with Efficient Selection of k Smallest Pair | find-k-pairs-with-smallest-sums | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe code aims to find the k smallest pairs from two sorted arrays, `nums1` and `nums2`, based on their pair sums. The approach used in the code is optimized to avoid inserting all pairs into the priority queue, which would result in a tim... | 94 | You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
Return _the_ `k` _pairs_ `(u1, v1), (u2, v2), ..., (uk, vk)` _with the smallest sums_.
**Example 1:**... | null |
🔰Python 3 Solution with Explanation✌😀 | find-k-pairs-with-smallest-sums | 0 | 1 | # Approach\nTo solve this problem, we can utilize the concept of a priority queue or a min-heap. Here\'s a step-by-step explanation of the approach:\n\n- Create a min-heap or priority queue to store the pairs based on their sums.\n\n- Add elements of nums1 and add pairs (nums1[i], nums2[0]) to the min-heap, along with ... | 2 | You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
Return _the_ `k` _pairs_ `(u1, v1), (u2, v2), ..., (uk, vk)` _with the smallest sums_.
**Example 1:**... | null |
Priority Queue and Set | find-k-pairs-with-smallest-sums | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBasically the two pointer approach will not work for this problem ...\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe will be using a priority queue in order to actually tackle this kind of problem..\n# Complexity... | 1 | You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
Return _the_ `k` _pairs_ `(u1, v1), (u2, v2), ..., (uk, vk)` _with the smallest sums_.
**Example 1:**... | null |
🚀 Lightning Fast Solution: K Pairs with Smallest Sums | Python Coding with vanAmsen | find-k-pairs-with-smallest-sums | 0 | 1 | # Intuition\nWhen first encountering this problem, we might consider brute force - calculating the sum of all pairs, sorting them, and choosing the smallest k sums. However, this solution is not efficient and will exceed the time limit for large inputs. Observing that the arrays are sorted, we can leverage this propert... | 2 | You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
Return _the_ `k` _pairs_ `(u1, v1), (u2, v2), ..., (uk, vk)` _with the smallest sums_.
**Example 1:**... | null |
373: Solution with step by step explanation | find-k-pairs-with-smallest-sums | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe problem asks to find the k pairs of numbers, one from nums1 and one from nums2, that have the smallest sum. The arrays nums1 and nums2 are sorted in ascending order.\n\nThe algorithm uses a heap to keep track of the smal... | 6 | You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
Return _the_ `k` _pairs_ `(u1, v1), (u2, v2), ..., (uk, vk)` _with the smallest sums_.
**Example 1:**... | null |
Easy Heap solution || Beats 93% 🔥|| Python3, Java, C++ | find-k-pairs-with-smallest-sums | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe needed to find the K smallest pairs of numbers from two different sorted arrays. What better way than to use a heap (priority queue) to maintain the sorted order(to find the minimum) while also constantly changing the data structure.\n... | 8 | You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
Return _the_ `k` _pairs_ `(u1, v1), (u2, v2), ..., (uk, vk)` _with the smallest sums_.
**Example 1:**... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.