question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
number-of-subarrays-with-bounded-maximum | King and its Empire !! T.C : O(N) & S.C : O(!) | king-and-its-empire-tc-on-sc-o-by-anurag-1onq | Intuition\n Describe your first thoughts on how to solve this problem. \nKing and his Empire\na[i] > R -> villan\na[i] < L -> am janta\na[e]>=L and a[e]<=R -> K | anuragino | NORMAL | 2023-12-07T07:29:10.854438+00:00 | 2023-12-07T07:29:10.854474+00:00 | 33 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nKing and his Empire\na[i] > R -> villan\na[i] < L -> am janta\na[e]>=L and a[e]<=R -> King \n \n*Alone King and King + am janta can built the empire\nif Villan comes , he destory the empire .*\n\n# Approach\n1. declare... | 1 | 1 | ['Array', 'Two Pointers', 'Sliding Window', 'C++'] | 0 |
number-of-subarrays-with-bounded-maximum | Sliding Window O(N) TC | sliding-window-on-tc-by-venomancer-6lg4 | Approach\n Describe your approach to solving the problem. \napply sliding window approach to solve the problem, maintain a prevcount for test cases to avoid sub | Venomancer | NORMAL | 2023-10-26T04:24:58.803676+00:00 | 2023-10-26T04:24:58.803711+00:00 | 140 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\napply sliding window approach to solve the problem, maintain a prevcount for test cases to avoid subarrays with all elements below the range\n\n# Code\n```\nclass Solution {\n public int numSubarrayBoundedMax(int[] nums, int left, int right) {\n ... | 1 | 0 | ['Java'] | 0 |
number-of-subarrays-with-bounded-maximum | Best Java Solution || Beats 98% | best-java-solution-beats-98-by-ravikumar-5j8b | 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 | ravikumar50 | NORMAL | 2023-09-09T15:34:40.888342+00:00 | 2023-09-09T15:34:40.888374+00:00 | 224 | false | # 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 | 0 | ['Java'] | 1 |
number-of-subarrays-with-bounded-maximum | ✅✔️Easy implementation using NGL and NGR concepts of stack || C++ Solution✈️✈️✈️✈️✈️ | easy-implementation-using-ngl-and-ngr-co-9haf | 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 | ajay_1134 | NORMAL | 2023-07-05T10:54:52.741737+00:00 | 2023-07-05T10:54:52.741758+00:00 | 696 | false | # 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 | 0 | ['Array', 'Stack', 'Monotonic Stack', 'C++'] | 0 |
number-of-subarrays-with-bounded-maximum | Solution | solution-by-deleted_user-x8h0 | C++ []\n#pragma GCC optimize("Ofast","inline","-ffast-math")\n#pragma GCC target("avx,mmx,sse2,sse3,sse4")\n\nclass Solution {\npublic:\n int numSubarrayBoun | deleted_user | NORMAL | 2023-04-30T03:42:32.925909+00:00 | 2023-04-30T04:03:03.088032+00:00 | 470 | false | ```C++ []\n#pragma GCC optimize("Ofast","inline","-ffast-math")\n#pragma GCC target("avx,mmx,sse2,sse3,sse4")\n\nclass Solution {\npublic:\n int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n \n int i = ... | 1 | 0 | ['C++', 'Java', 'Python3'] | 0 |
number-of-subarrays-with-bounded-maximum | 📌📌 C++ || Two Pointers || Faster || Easy To Understand || C++ Code | c-two-pointers-faster-easy-to-understand-hafx | Using Two Pointer\n\n Time Complexity :- O(N)\n\n Space Complexity :- O(1)\n\n\nclass Solution {\npublic:\n int numSubarrayBoundedMax(vector<int>& nums, int | __KR_SHANU_IITG | NORMAL | 2022-10-16T14:33:49.926901+00:00 | 2022-10-16T14:33:49.926933+00:00 | 95 | false | * ***Using Two Pointer***\n\n* ***Time Complexity :- O(N)***\n\n* ***Space Complexity :- O(1)***\n\n```\nclass Solution {\npublic:\n int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n\n int n = nums.size();\n\n long long ans = 0;\n\n // first of all count the total no. of suba... | 1 | 0 | ['Two Pointers', 'C', 'C++'] | 0 |
number-of-subarrays-with-bounded-maximum | Java O(n) Faster than 100%, Memory consumption less than 96% | java-on-faster-than-100-memory-consumpti-vu4r | Here,\nsum = the total number of subarray which fit the condition\nc = the streak of numbers which are less than the right value\nav = avoid = the streak of num | Raunak49 | NORMAL | 2022-09-08T10:02:41.959349+00:00 | 2022-09-08T10:02:41.959388+00:00 | 52 | false | Here,\n**sum** = the total number of subarray which fit the condition\n**c** = the streak of numbers which are less than the right value\n**av** = **avoid** = the streak of numbers which are less than the left value\n- - - ------------\nBecause the av can be more than one for eg [2,1,1,3,4] left=2, right=5. here av wil... | 1 | 0 | ['Java'] | 0 |
number-of-subarrays-with-bounded-maximum | Python - 2 Pointers Approach | python-2-pointers-approach-by-beingab329-fgu0 | ```\nclass Solution:\n def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:\n \n start=count=cur=0\n | beingab329 | NORMAL | 2022-08-22T07:12:40.742715+00:00 | 2022-08-22T07:13:52.551534+00:00 | 189 | false | ```\nclass Solution:\n def numSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int:\n \n start=count=cur=0\n \n for i in range(len(nums)):\n \n if left <= nums[i] <= right:\n cur=i-start+1\n ... | 1 | 0 | ['Python'] | 0 |
number-of-subarrays-with-bounded-maximum | O(n) solution beats 90% in C | on-solution-beats-90-in-c-by-vgagrani-laf1 | \nint numSubarrayBoundedMax(int* nums, int numsSize, int left, int right){\n \n int l=0,r=0;\n int count=0;\n int prev=0;\n while (r < numsSize)\ | vgagrani | NORMAL | 2022-08-18T17:20:46.498148+00:00 | 2022-08-18T17:30:41.206470+00:00 | 115 | false | ```\nint numSubarrayBoundedMax(int* nums, int numsSize, int left, int right){\n \n int l=0,r=0;\n int count=0;\n int prev=0;\n while (r < numsSize)\n {\n if (nums[r] >= left && nums[r] <= right)\n {\n prev = r-l;\n count = 1 + count + prev;\n }\n else\... | 1 | 0 | ['C'] | 0 |
number-of-subarrays-with-bounded-maximum | C++ || Self explanable | c-self-explanable-by-get_the_guns-ykr7 | \nclass Solution {\npublic:\n int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n int s=0;\n int n=nums.size();\n int e=0; | Get_the_Guns | NORMAL | 2022-07-26T20:02:54.940727+00:00 | 2022-07-26T20:02:54.940764+00:00 | 103 | false | ```\nclass Solution {\npublic:\n int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n int s=0;\n int n=nums.size();\n int e=0;\n int prev=0;\n int ans=0;\n while(e<n){\n if(left<=nums[e]&&nums[e]<=right){\n prev=e-s+1;\n ... | 1 | 0 | [] | 0 |
number-of-subarrays-with-bounded-maximum | C++ || TC : O(N) SC : O(1) || Optimized ✅ | c-tc-on-sc-o1-optimized-by-ajayyadavcste-4sxe | \nclass Solution {\npublic:\n int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n int ans = 0; // store ans\n int j=-1; // st | ajayyadavcstech | NORMAL | 2022-07-24T15:32:10.580281+00:00 | 2022-07-24T15:33:20.341275+00:00 | 132 | false | ```\nclass Solution {\npublic:\n int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n int ans = 0; // store ans\n int j=-1; // starting window\n int sub = 0; // if current element is less than left bound then count how may element before current element which is less than left a... | 1 | 0 | ['C'] | 0 |
number-of-subarrays-with-bounded-maximum | Short and easy JAVA solution !! | short-and-easy-java-solution-by-utkarshb-v3o4 | \npublic int numSubarrayBoundedMax(int[] nums, int left, int right) {\n \n int ans = 0;\n \n int s = -1;\n int e = -1;\n | utkarshbisht116 | NORMAL | 2022-07-19T20:32:11.168938+00:00 | 2022-07-19T20:32:11.168969+00:00 | 152 | false | ```\npublic int numSubarrayBoundedMax(int[] nums, int left, int right) {\n \n int ans = 0;\n \n int s = -1;\n int e = -1;\n \n for(int i = 0; i < nums.length; i++){\n\n if(nums[i] >= left && nums[i] <= right){ // in range\n e = i;\n }... | 1 | 0 | ['Java'] | 0 |
number-of-subarrays-with-bounded-maximum | TC: O(N) | Easy and clean solution using two-pointers | tc-on-easy-and-clean-solution-using-two-am4vm | \nclass Solution {\n public int numSubarrayBoundedMax(int[] nums, int left, int right) {\n int start = -1, end = -1, res = 0;\n for(int i=0; i< | __Asrar | NORMAL | 2022-07-19T12:45:02.766186+00:00 | 2022-07-19T12:45:02.766228+00:00 | 180 | false | ```\nclass Solution {\n public int numSubarrayBoundedMax(int[] nums, int left, int right) {\n int start = -1, end = -1, res = 0;\n for(int i=0; i<nums.length; i++){\n if(nums[i] > right){\n start = end = i;\n continue;\n }\n if(nums[i] >=le... | 1 | 0 | ['Two Pointers', 'Java'] | 0 |
number-of-subarrays-with-bounded-maximum | 1D dp | 1d-dp-by-aniketash57-45c5 | in this solution we are storing ans for every element in the dp array \nwe are breaking the array where the number which is bigger than r \nand for number sma | aniketash57 | NORMAL | 2022-05-09T11:07:32.227280+00:00 | 2022-05-09T11:09:27.847442+00:00 | 172 | false | in this solution we are storing ans for every element in the dp array \nwe are breaking the array where the number which is bigger than r \nand for number smaller than l ans will be of previous valid sequence \nand for the valid sequence ans will be the length from where last bigger than r element came \n\nand the to... | 1 | 0 | ['Two Pointers', 'Dynamic Programming', 'C'] | 0 |
number-of-subarrays-with-bounded-maximum | Faster || Easy To Understand || C++ Code | faster-easy-to-understand-c-code-by-__kr-jae1 | Using Two Pointer\n\n Time Complexity : O(N)\n\n Space Complexity : O(1)\n\n\nclass Solution {\npublic:\n int numSubarrayBoundedMax(vector<int>& nums, int le | __KR_SHANU_IITG | NORMAL | 2022-04-21T10:00:52.220457+00:00 | 2022-04-21T10:00:52.220504+00:00 | 152 | false | * ***Using Two Pointer***\n\n* ***Time Complexity : O(N)***\n\n* ***Space Complexity : O(1)***\n\n```\nclass Solution {\npublic:\n int numSubarrayBoundedMax(vector<int>& nums, int left, int right) {\n \n int n = nums.size();\n \n int start = 0;\n \n int end = 0;\n \n ... | 1 | 0 | ['C'] | 0 |
find-the-k-sum-of-an-array | Priority Queue || C++ | priority-queue-c-by-kbp12-hg94 | Intuition: First find the maximum sum possible. We will get maximum possible sum after adding all the elements greater than 0. Next, if we want to find 2nd | kbp12 | NORMAL | 2022-08-21T06:06:07.522001+00:00 | 2022-09-29T04:47:26.951650+00:00 | 9,702 | false | **Intuition**: First find the maximum sum possible. We will get maximum possible sum after adding all the elements greater than 0. Next, if we want to find 2nd maximum sum what we can do?\nWe can either subtract a smallest positive number from this sum or add largest (minimum(abs(num)) negative number in the sum to... | 118 | 0 | ['C', 'Heap (Priority Queue)'] | 13 |
find-the-k-sum-of-an-array | [Python3] Heap/Priority Queue, O(NlogN + klogk) | python3-heappriority-queue-onlogn-klogk-1dxwy | Initial Observation\n1. Given n can be as large as 10^5, the time complexity of this problem has to be no worse than log-linear.\n2. This problem is dealing wit | xil899 | NORMAL | 2022-08-21T04:05:17.945238+00:00 | 2022-08-23T14:41:14.043021+00:00 | 9,114 | false | **Initial Observation**\n1. Given `n` can be as large as `10^5`, the time complexity of this problem has to be no worse than log-linear.\n2. This problem is dealing with *subsequences* but not *subarrays*, indicating we can shuffle the order of the original array, e.g. by sorting it.\n3. Given `-10^9 <= nums[i] <= 10^9... | 97 | 1 | ['Heap (Priority Queue)', 'Python', 'Python3'] | 16 |
find-the-k-sum-of-an-array | Decreasing Subsequence Sums | decreasing-subsequence-sums-by-votrubac-9lg6 | This problem needs some serious unpacking.\n\n#### 1. Sort the Array\nEven though the description says:\n\n> A subsequence is an array that can be derived from | votrubac | NORMAL | 2022-08-22T22:28:52.438709+00:00 | 2022-08-25T05:54:09.482241+00:00 | 5,505 | false | This problem needs some serious unpacking.\n\n#### 1. Sort the Array\nEven though the description says:\n\n> A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.\n\n...sum is a commutative operation, and we can rearran... | 84 | 1 | ['C'] | 8 |
find-the-k-sum-of-an-array | [Python3] priority queue | python3-priority-queue-by-ye15-n8q7 | Please pull this commit for solutions of weekly 307. \n\nIntuition\nHere, I will discuss a strategy to find the kth largest subsequence sum. \n We start from th | ye15 | NORMAL | 2022-08-21T04:03:09.382320+00:00 | 2022-08-22T15:21:38.097548+00:00 | 3,353 | false | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/b7391a11acc4e9dbe563ebef84f8d78f7943a0f7) for solutions of weekly 307. \n\n**Intuition**\nHere, I will discuss a strategy to find the kth largest subsequence sum. \n* We start from the sum of all positive numbers in `nums` which is the largest one ... | 45 | 0 | ['Python3'] | 8 |
find-the-k-sum-of-an-array | Heap | Java | Find "Smallest" | heap-java-find-smallest-by-student2091-djy9 | A pretty difficult problem I think. Here is how I solved it during the contest: \n\nFirst, I consider only the case where the array contains positive integers o | Student2091 | NORMAL | 2022-08-21T04:12:28.696578+00:00 | 2022-08-21T21:00:21.110212+00:00 | 3,068 | false | A pretty difficult problem I think. Here is how I solved it during the contest: \n\nFirst, I consider only the case where the array contains positive integers only. It is obviously that we can find the `k`th smallest subsequence and subtract it from sum of all (positive) elements. Second, I consider the case when negat... | 33 | 0 | ['Heap (Priority Queue)', 'Java'] | 4 |
find-the-k-sum-of-an-array | Java Solution | java-solution-by-solved-2xyv | \nclass Solution {\n public long kSum(int[] nums, int k) {\n long sum = 0;\n Queue<Pair<Long, Integer>> pq = new PriorityQueue<>((a, b) -> Long | solved | NORMAL | 2022-08-21T04:03:12.603898+00:00 | 2022-08-21T04:03:12.603927+00:00 | 2,702 | false | ```\nclass Solution {\n public long kSum(int[] nums, int k) {\n long sum = 0;\n Queue<Pair<Long, Integer>> pq = new PriorityQueue<>((a, b) -> Long.compare(b.getKey(), a.getKey()));\n for (int i = 0; i < nums.length; i++) {\n sum = sum + Math.max(0, nums[i]);\n }\n for (i... | 23 | 1 | ['Heap (Priority Queue)', 'Java'] | 5 |
find-the-k-sum-of-an-array | c++ solution | c-solution-by-dilipsuthar60-4gp0 | \nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n int n=nums.size();\n vector<long long>ans;\n priority_queue<p | dilipsuthar17 | NORMAL | 2022-08-21T04:29:46.440359+00:00 | 2022-08-21T04:29:46.440399+00:00 | 2,338 | false | ```\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n int n=nums.size();\n vector<long long>ans;\n priority_queue<pair<long long,long long>>pq;\n long long sum=0;\n for(int i=0;i<n;i++)\n {\n if(nums[i]>0)\n {\n su... | 20 | 4 | ['C', 'Heap (Priority Queue)', 'C++'] | 2 |
find-the-k-sum-of-an-array | C++ || Priority Queue || Explained | c-priority-queue-explained-by-anis23-hxqb | Approach:\n\n we need to find the kth largest subsequence sum\n the array consist of both +ve and -ve numbers\n the subsequence with maxsum will be the one with | anis23 | NORMAL | 2022-08-22T08:47:59.146067+00:00 | 2022-08-22T08:47:59.146108+00:00 | 1,335 | false | **Approach:**\n\n* we need to find the kth largest subsequence sum\n* the array consist of both +ve and -ve numbers\n* the subsequence with maxsum will be the one with all the non-neg values\n* now, the 2nd maxsum subsequence will either be\n\t* ``` maxsum- min_pos_value```\n\t* ```maxsum + max_neg_value``` (remember t... | 18 | 0 | ['C', 'Heap (Priority Queue)', 'C++'] | 3 |
find-the-k-sum-of-an-array | Detailed, Clean Code Explained with Comments! C++ 🔥 Easy to understand | detailed-clean-code-explained-with-comme-08sm | Upvote if it helped! :)\n\n\nApproach-\n\nTake the sum of all positive values in nums, it will be maximum sum of any subsequence possible.\nMake all the element | ishaankulkarnii | NORMAL | 2022-08-21T08:58:01.188430+00:00 | 2022-08-23T08:11:16.381343+00:00 | 1,600 | false | Upvote if it helped! :)\n\n\n**Approach-**\n\nTake the sum of all positive values in nums, it will be maximum sum of any subsequence possible.\nMake all the elements of nums positive (Reason- Later we will include or exclude the nums[i] in sum,\n\n-> Case1- If nums[i] was negative, if we subtract it from sum, it will b... | 18 | 0 | [] | 3 |
find-the-k-sum-of-an-array | [Java] Priority Queue with Thought Process and Explanation | java-priority-queue-with-thought-process-qzfy | Thought Process\nWe can start by considering what the first few largest subsequence sums are by going through an example. Let\'s take the array [-4, -3, -2, -1, | austincheng | NORMAL | 2022-08-21T13:10:39.439758+00:00 | 2022-08-21T13:11:11.433765+00:00 | 796 | false | **Thought Process**\nWe can start by considering what the first few largest subsequence sums are by going through an example. Let\'s take the array `[-4, -3, -2, -1, 2, 5]` as our example.\n\nFor `k == 1`, i.e. the largest subsequence sum, we should be able to see fairly quickly that in general this is just the sum of ... | 16 | 0 | ['Heap (Priority Queue)', 'Java'] | 5 |
find-the-k-sum-of-an-array | [Detailed Explanation & Cpp Code] Greedy Counting + Time O(N*log(N)+K*log(K)) + Space O(K) | detailed-explanation-cpp-code-greedy-cou-55wj | \n//Document History\n//2022/08/21 Created\n//2022/08/22 Updated: Fix Typos, Edit Typeset\n//2022/08/23. Updated: Add Figure to show the | mkyang | NORMAL | 2022-08-21T08:39:07.539081+00:00 | 2022-08-23T12:13:28.875682+00:00 | 860 | false | ```\n//Document History\n//2022/08/21 Created\n//2022/08/22 Updated: Fix Typos, Edit Typeset\n//2022/08/23. Updated: Add Figure to show the greedy approach of exploring \n```\n\n**The Problem**\nThe "**subsequence**" in the description is misleading. Actually, the order should be ignored. The pr... | 16 | 0 | ['Greedy', 'Heap (Priority Queue)'] | 6 |
find-the-k-sum-of-an-array | Simple solution with a detailed proof | Heap | NlogN + klogK | simple-solution-with-a-detailed-proof-he-fxk3 | I got very frustrated when I failed to tackle this problem and people are sharing their code w/o any proof like "I just know it". Inspired by ye15, finally I co | harttle | NORMAL | 2022-08-21T07:24:18.738537+00:00 | 2022-08-21T07:30:28.839825+00:00 | 607 | false | I got very frustrated when I failed to tackle this problem and people are sharing their code w/o any proof like "I just know it". Inspired by [ye15](https://leetcode.com/problems/find-the-k-sum-of-an-array/discuss/2456675/Python3-priority-queue), finally I come up with a proof for this problem.\n\n## The idea\n\nObviou... | 10 | 0 | ['Heap (Priority Queue)', 'JavaScript'] | 3 |
find-the-k-sum-of-an-array | Heap soultion [EXPLAINED] | heap-soultion-explained-by-r9n-7rin | IntuitionWe want the k-th largest sum from all possible subsequences of an array. To do this efficiently, we focus on maintaining the largest sums dynamically u | r9n | NORMAL | 2024-12-23T22:00:49.393895+00:00 | 2024-12-23T22:00:49.393895+00:00 | 295 | false | # Intuition
We want the k-th largest sum from all possible subsequences of an array. To do this efficiently, we focus on maintaining the largest sums dynamically using a priority queue, avoiding the need to generate all subsequences explicitly.
# Approach
Sort the array by absolute values, calculate the maximum sum us... | 9 | 0 | ['Sorting', 'Heap (Priority Queue)', 'Python3'] | 0 |
find-the-k-sum-of-an-array | [c++] priority queue with explanation on the required knowledge | c-priority-queue-with-explanation-on-the-v6gu | The biggest hurdle is the knowledge of generating a complete set of subsequences of an array. Using [1, 2, 4] as an example, we start with the first element and | coolwenwen | NORMAL | 2022-08-21T07:31:43.578808+00:00 | 2022-08-21T07:42:35.483260+00:00 | 600 | false | The biggest hurdle is the knowledge of generating a complete set of subsequences of an array. Using [1, 2, 4] as an example, we start with the first element and get only one subsequence **[1]** to start filling the subseq set **{[1]}**. Next we take **[1]** out and add two new subsequences back into the set:\n\na). by ... | 9 | 0 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | [c++]Priority queue | cpriority-queue-by-karthik1600-jj2l | cpp\n#define ll long long \nclass Solution {\npublic: \n long long kSum(vector<int>& nums, int k) {\n ll maxsum=0;\n for(auto &num : nums){\n | karthik1600 | NORMAL | 2022-08-21T08:15:17.972728+00:00 | 2022-08-21T08:16:14.496005+00:00 | 575 | false | ```cpp\n#define ll long long \nclass Solution {\npublic: \n long long kSum(vector<int>& nums, int k) {\n ll maxsum=0;\n for(auto &num : nums){\n maxsum+=max(0,num);// adding all pos numbers\n num = abs(num); // we take abs because for pos numbers we remove from maxsum and negitive... | 7 | 0 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | C++ Solution || Priority Queue || Easy to Understand | c-solution-priority-queue-easy-to-unders-hu91 | \nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n long long sum = 0;\n for(int i=0; i<nums.size(); ++i) {\n | aksharma071 | NORMAL | 2022-08-26T17:17:35.444940+00:00 | 2022-08-26T17:17:35.444982+00:00 | 903 | false | ```\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n long long sum = 0;\n for(int i=0; i<nums.size(); ++i) {\n if(nums[i]>=0) sum+=nums[i];\n else nums[i]*=-1;\n }\n sort(nums.begin(), nums.end());\n typedef pair<long long, int> p;\n ... | 5 | 0 | ['C', 'Sorting', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | Java | max Heap | 100% faster | with explanation | java-max-heap-100-faster-with-explanatio-ktlh | \n### Intuitively thinking\n1. The largest sum of the array is the sum of all positive numbers.\n2. The sum of any subsequences is equivalent to the substractin | Rinka006 | NORMAL | 2022-08-21T19:18:40.545925+00:00 | 2022-08-21T19:18:40.545967+00:00 | 694 | false | \n### Intuitively thinking\n1. The largest ***sum*** of the array is the sum of all positive numbers.\n2. The sum of any subsequences is **equivalent** to the **substracting** some positive numbers and **adding** some negative numbers from the sum.\n3. The second largest sum of the array is the **[sum - the minimum num... | 4 | 0 | ['Heap (Priority Queue)', 'Java'] | 1 |
find-the-k-sum-of-an-array | C++ Solution using Priority_Queue || With Comments | c-solution-using-priority_queue-with-com-jt92 | \nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n \n long long totSum = 0;\n \n // calculate sum of all | manoj-22 | NORMAL | 2022-08-21T04:30:19.087661+00:00 | 2022-08-21T08:07:17.545073+00:00 | 755 | false | ```\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n \n long long totSum = 0;\n \n // calculate sum of all positive numbers, i.e maxSubseqSum (2^nth sum)\n for(auto &num : nums)\n {\n if(num > 0)\n totSum += num;\n ... | 4 | 1 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | C++ O(K^2) solution (solved the question 3 minutes after contest : ( ) | c-ok2-solution-solved-the-question-3-min-f4e2 | cpp\ntypedef long long ll;\n\n\nconst ll MAXK = 2e3 + 5;\n\n\nll dp[MAXK];\nll lst[MAXK];\n\nclass Solution {\npublic:\n \n long long kSum(vector<int>& nu | xktz | NORMAL | 2022-08-21T04:07:57.155221+00:00 | 2022-08-21T04:08:19.684566+00:00 | 1,148 | false | ```cpp\ntypedef long long ll;\n\n\nconst ll MAXK = 2e3 + 5;\n\n\nll dp[MAXK];\nll lst[MAXK];\n\nclass Solution {\npublic:\n \n long long kSum(vector<int>& nums, int k) {\n ll N = nums.size();\n memset(dp, 0, sizeof(dp));\n memset(lst, 0, sizeof(lst));\n for (auto &i: nums) {\n ... | 4 | 0 | ['Dynamic Programming'] | 1 |
find-the-k-sum-of-an-array | ✅ Beat 100% | ✨ O(N + k log k) | 🏆 Fastest Solution | 👍 Detailed Explanation | beat-100-on-k-log-k-fastest-solution-det-jt2a | Intuition\n Describe your first thoughts on how to solve this problem. \n\n## Preprocessing\nWe have both positive numbers and negative numbers in the input. Th | hero080 | NORMAL | 2023-04-29T06:00:54.839453+00:00 | 2023-05-26T15:52:28.239111+00:00 | 266 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n## Preprocessing\nWe have both positive numbers and negative numbers in the input. They are actually very different. The **largest sum** is obviously the sum of **all** positive numbers, while the negative numbers will only enter the "p... | 3 | 0 | ['C++'] | 1 |
find-the-k-sum-of-an-array | Share My Solution | Explanation with Logic in code | C++ | share-my-solution-explanation-with-logic-esqg | Preriquisites:\nMerge Sorted Array\nPriority Queue (Heap)\nSubsets\n\nTime Complexity: O( nlog(n) + k + klog(k) )\n\nSpace Complexity: O( n + k )\n\n/*\n Log | shantanu_joshi21 | NORMAL | 2022-08-22T19:02:47.727421+00:00 | 2022-08-22T19:02:47.727468+00:00 | 263 | false | **Preriquisites:**\n[Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)\nPriority Queue (Heap)\n[Subsets](https://leetcode.com/problems/subsets/)\n\n**Time Complexity:** O( n*log(n) + k + k*log(k) )\n\n**Space Complexity:** O( n + k )\n```\n/*\n Logic:\n \n The largest sum possible is the s... | 3 | 0 | ['C', 'Heap (Priority Queue)', 'C++'] | 0 |
find-the-k-sum-of-an-array | [JAVA] Simplest explanation and Intuition [ Priority Queue ] | java-simplest-explanation-and-intuition-jkhog | I was having trouble understanding answers from ye15 (link to answer)) and xil899 (link to answer)\n\nSimplest Explanation:\n\nIntuition:\n1. we need Kth large | vivekgpt107 | NORMAL | 2022-08-21T11:19:57.199104+00:00 | 2022-08-21T11:19:57.199152+00:00 | 342 | false | I was having trouble understanding answers from ye15 ([link to answer](https://leetcode.com/problems/find-the-k-sum-of-an-array/discuss/2456675/Python3-priority-queue))) and xil899 ([link to answer](https://leetcode.com/problems/find-the-k-sum-of-an-array/discuss/2456716/Python3-HeapPriority-Queue-O(NlogN-%2B-klogk)))... | 3 | 0 | ['Heap (Priority Queue)', 'Java'] | 1 |
find-the-k-sum-of-an-array | [Javascript] | explanation | Code commented | PriorityQueue | O(NlogN + klogk) | javascript-explanation-code-commented-pr-quuz | Explanation in comment,\nhope it makes sense\n\n```\n/*\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n /\nvar kSum = function(nums, k) | Atikur-Rahman-Sabuj | NORMAL | 2022-08-21T08:20:31.380449+00:00 | 2022-08-25T05:26:50.848006+00:00 | 287 | false | Explanation in comment,\nhope it makes sense\n\n```\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar kSum = function(nums, k) {\n let pq = new MaxPriorityQueue(), sum = 0, n = nums.length;\n \n //sort numbers in ascending order with absolute value\n nums.sort((a,b) => Mat... | 3 | 0 | ['JavaScript'] | 0 |
find-the-k-sum-of-an-array | ✅ C++ Must know Min/Max Heap Algo Intuition | Explained in Hindi | c-must-know-minmax-heap-algo-intuition-e-fgjb | It was an interesting challenge, The same concept has been used in multiple Leetcode medium/hard challenges.\nThe min/max heap algo to find top k sums.\n\nMy th | Leetcode_Hindi | NORMAL | 2022-08-24T14:35:47.186296+00:00 | 2022-08-29T16:41:33.923574+00:00 | 298 | false | It was an interesting challenge, The same concept has been used in multiple Leetcode medium/hard challenges.\nThe min/max heap algo to find top k sums.\n\nMy thought process with code below:\n\n 1. Naive solution: Number of subsequences that can be created by N sized array are\n 2^N (each element can be considere... | 2 | 0 | [] | 0 |
find-the-k-sum-of-an-array | very easy to understand, | very-easy-to-understand-by-pratyushaggar-0smx | IntuitionThe problem requires finding the k-th largest subsequence sum in an array.Key observations:
The maximum possible sum is obtained by summing all positiv | pratyushaggarwal1281 | NORMAL | 2025-02-11T13:51:47.028365+00:00 | 2025-02-11T13:51:47.028365+00:00 | 40 | false | # Intuition
The problem requires finding the k-th largest subsequence sum in an array.
**Key observations:**
The maximum possible sum is obtained by summing all positive numbers in the array.
Each subset sum can be derived by selectively subtracting elements from this maximum sum.
We can efficiently generate the large... | 1 | 0 | ['C++'] | 0 |
find-the-k-sum-of-an-array | Beats 100% time, Solution with Explanation | beats-100-time-solution-with-explanation-e3pq | Intuition\n\n- For an example let\'s take an array [-5, -3,-2, 1, 7], the largest sum that can be formed from a subsequence is 8. when k = 1, the answer is obvi | Hariprasath_N | NORMAL | 2023-01-05T14:55:19.201714+00:00 | 2023-01-05T14:55:19.201761+00:00 | 118 | false | # Intuition\n\n- For an example let\'s take an array [-5, -3,-2, 1, 7], the largest sum that can be formed from a subsequence is 8. when k = 1, the answer is obviously 10.\n- If k = 2, the second largest subsequence sum is supposed to be returned. In this case we can add(if negative) or subtract(if positive) the lowest... | 1 | 0 | ['Heap (Priority Queue)', 'Java'] | 0 |
find-the-k-sum-of-an-array | c++ | priority queue | 85% faster than all | c-priority-queue-85-faster-than-all-by-v-dg56 | ```\nclass Solution {\npublic:\n long long kSum(vector& nums, int k) {\n long long sum = 0;\n for(int i=0; i=0) sum+=nums[i];\n else | venomhighs7 | NORMAL | 2022-09-17T11:44:55.026081+00:00 | 2022-09-17T11:44:55.026125+00:00 | 230 | false | ```\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n long long sum = 0;\n for(int i=0; i<nums.size(); ++i) {\n if(nums[i]>=0) sum+=nums[i];\n else nums[i]*=-1;\n }\n sort(nums.begin(), nums.end());\n typedef pair<long long, int> p;\n ... | 1 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Priority Queue | C++ | priority-queue-c-by-shubhamdoke-1ifb | \ntypedef long long ll;\n\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n\n int n = nums.size();\n ll tot = 0;\n | ShubhamDoke | NORMAL | 2022-09-06T19:24:30.516931+00:00 | 2022-09-06T19:32:40.102107+00:00 | 289 | false | ```\ntypedef long long ll;\n\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n\n int n = nums.size();\n ll tot = 0;\n for(int i=0;i<n;i++){\n if(nums[i]>0)\n tot += nums[i];\n if(nums[i]<0)\n nums[i] *= -1;\n }\n ... | 1 | 0 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | [Python / Go / C++] heap vs BST on sorted array O(NlogN+KlogK) | python-go-c-heap-vs-bst-on-sorted-array-jwsbe | Python w/ ascending min heap\n\npython\ndef kSum(self, nums, k):\n res, nums = -sum(max(num, 0) for num in nums), sorted(map(abs, nums))\n q = [(res + num | lotceedoo | NORMAL | 2022-08-31T06:49:33.312222+00:00 | 2022-09-03T05:00:32.440286+00:00 | 247 | false | *Python* w/ ascending min heap\n\n```python\ndef kSum(self, nums, k):\n res, nums = -sum(max(num, 0) for num in nums), sorted(map(abs, nums))\n q = [(res + nums[0], 0)]\n for _ in range(k - 1):\n res, i = heapq.heappop(q)\n if (j := i + 1) < len(nums):\n heapq.heappush(q, (res + nums[j... | 1 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Priority Queue||C++||Simple Solution||Loaded with comments for better understanding | priority-queuecsimple-solutionloaded-wit-knlc | \nclass Solution {\npublic:\n // Here the approach is that we want K max right ? so the max value is equal to the sum of the positive integers and after that | gargshivam2001 | NORMAL | 2022-08-25T11:45:59.701201+00:00 | 2022-08-25T11:46:23.917881+00:00 | 305 | false | ```\nclass Solution {\npublic:\n // Here the approach is that we want K max right ? so the max value is equal to the sum of the positive integers and after that it will \n // decrease. So, for decreasing the numbers, we have to add -ve numbers and minus +ve numbers.\n // Since we have taken abs of the num... | 1 | 0 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | Java | heap | java-heap-by-darling_of_02-6699 | java\nclass Solution {\n public long kSum(int[] nums, int k) {\n //1. the max value will be the sum of all the positives\n //2. sums of every s | darling_of_02 | NORMAL | 2022-08-23T17:10:06.883075+00:00 | 2022-08-23T17:10:06.883114+00:00 | 258 | false | ```java\nclass Solution {\n public long kSum(int[] nums, int k) {\n //1. the max value will be the sum of all the positives\n //2. sums of every subsequence can be derived by removing positives from or adding negetives to the max value\n //3. we can turn every negetive into its absolute value, \... | 1 | 0 | ['Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | [Python3] Finding next bigger sum from an all-positive sorted nums | python3-finding-next-bigger-sum-from-an-ah4cz | Max k = 2000. The best strategy is finding next smaller sum one by one for 2000 times.\nHow to find next smaller sum?\nBiggest sum is the sum of all positive nu | yuanzhi247012 | NORMAL | 2022-08-22T03:27:04.335224+00:00 | 2022-08-22T03:35:56.674847+00:00 | 97 | false | Max k = 2000. The best strategy is finding next smaller sum one by one for 2000 times.\nHow to find next smaller sum?\nBiggest sum is the sum of all positive numbers. We start from there and finding the next smaller sum.\nTricky part:\nDon\'t "add" or "remove" numbers to or from subsequence. Instead, we introduce the c... | 1 | 0 | [] | 1 |
find-the-k-sum-of-an-array | Java solution using min heap | java-solution-using-min-heap-by-legit_12-9zpw | Derived from : https://leetcode.com/problems/find-the-k-sum-of-an-array/discuss/2457384/Priority-Queue-oror-C%2B%2B\n\n\n/*\n\n-> Find max sum possible, we get | legit_123 | NORMAL | 2022-08-21T14:32:08.234108+00:00 | 2022-08-21T14:32:08.234150+00:00 | 477 | false | Derived from : https://leetcode.com/problems/find-the-k-sum-of-an-array/discuss/2457384/Priority-Queue-oror-C%2B%2B\n\n```\n/*\n\n-> Find max sum possible, we get max sum after adding all elements greater than or equal to zero\n-> For finding second max sum we can either subtract smallest positive number from sum or ad... | 1 | 0 | ['Greedy', 'Heap (Priority Queue)', 'Java'] | 1 |
find-the-k-sum-of-an-array | Java | Merge Sort | O(n) + O(k^2) | java-merge-sort-on-ok2-by-l9s-lutk | \n public long kSum(int[] nums, int k) {\n //get sum \n long sum = 0;\n for(int i = 0; i < nums.length; i ++ ){\n if(nums[i] | l9s | NORMAL | 2022-08-21T10:10:31.946668+00:00 | 2022-08-21T10:10:31.946711+00:00 | 93 | false | \n public long kSum(int[] nums, int k) {\n //get sum \n long sum = 0;\n for(int i = 0; i < nums.length; i ++ ){\n if(nums[i] > 0){\n sum += nums[i];\n }else{\n nums[i] *= -1;\n }\n }\n \n //sort increasing.\n\t\... | 1 | 0 | [] | 0 |
find-the-k-sum-of-an-array | C++ solution || Priority Queue || With Explanation || Beats 100% runtime and space complexity | c-solution-priority-queue-with-explanati-b5ok | The plan is to find the biggest possible sum first, and then go through the array and find the sums of all possible subsequences, from biggest to smallest, unti | manavdoda7 | NORMAL | 2022-08-21T09:40:03.590452+00:00 | 2022-08-21T09:49:03.354517+00:00 | 99 | false | The plan is to find the biggest possible sum first, and then go through the array and find the sums of all possible subsequences, from biggest to smallest, until we find the kth sum.\nHere, we start by putting the largest sum in the priority queue and making all of the array\'s elements positive.\nThen, we use this lin... | 1 | 1 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | C++ || EXPLAINATION|| with Comments|| | c-explaination-with-comments-by-prabhjot-hhqe | Approach-\n\nTake the sum of all positive values in nums, it will be maximum sum of any subsequence possible.\nMake all the elements of nums positive (Reason- L | prabhjot_2 | NORMAL | 2022-08-21T07:04:25.140168+00:00 | 2022-08-21T07:04:25.140201+00:00 | 171 | false | **Approach**-\n\nTake the sum of all positive values in nums, it will be maximum sum of any subsequence possible.\nMake all the elements of nums positive (Reason- Later we will include or exclude the nums[i] in sum,\n**Case1**- If nums[i] was negative, if we subtract it from sum, it will be same as including it into su... | 1 | 0 | [] | 0 |
find-the-k-sum-of-an-array | C++ Solution with explaination | c-solution-with-explaination-by-cybergho-3d9d | Approach-\n1. Take the sum of all positive values in nums, it will be maximum sum of any subsequence possible.\n2. Make all the elements of nums positive (Reaso | cyberghost2023 | NORMAL | 2022-08-21T06:36:34.271650+00:00 | 2022-08-21T06:36:34.271692+00:00 | 160 | false | Approach-\n1. Take the sum of all positive values in nums, it will be maximum sum of any subsequence possible.\n2. Make all the elements of nums positive (Reason- Later we will include or exclude the nums[i] in sum, \n\t* Case1- If nums[i] was negative, if we subtract it from sum, it will be same as including it into s... | 1 | 0 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | O(nk) brute force solution | onk-brute-force-solution-by-panyan7-w2rl | First of all, I didn\'t pass (TLE), but I saw someone passed using same brute-force approach with many tricks to optimize the constant https://leetcode.com/prob | panyan7 | NORMAL | 2022-08-21T04:53:10.222061+00:00 | 2022-08-21T05:00:01.462565+00:00 | 228 | false | First of all, I didn\'t pass (TLE), but I saw someone passed using same brute-force approach with many tricks to optimize the constant https://leetcode.com/problems/find-the-k-sum-of-an-array/discuss/2456909/O(n-*-klogk)-solution-%2B-clever-tricks, so I guess my solution is worth mentioning.\nThe idea is to keep only t... | 1 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Walk Through of my Understanding || Detailed Explaination 💡. | walk-through-of-my-understanding-detaile-xqso | Understanding optimised take or leave :
-> i like to define it as , you should take atleast one element at any point
-> how can we maintain this and get all uni | naveen_gunnam | NORMAL | 2025-03-06T00:51:46.116230+00:00 | 2025-03-06T00:51:46.116230+00:00 | 9 | false | Understanding optimised take or leave :
-> i like to define it as , you should take atleast one element at any point
-> how can we maintain this and get all unique combinations?
instead of starting with a case of not taking any element, we start with "take" case. 1 -take , 0 - leave... | 0 | 0 | ['Array', 'Sorting', 'Heap (Priority Queue)', 'Java'] | 0 |
find-the-k-sum-of-an-array | Beats 100.00% | beats-10000-by-rhyd3dxa6x-yu01 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | rhyD3DxA6x | NORMAL | 2025-02-01T18:04:18.141246+00:00 | 2025-02-01T18:04:18.141246+00:00 | 16 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Java'] | 0 |
find-the-k-sum-of-an-array | quickselect + bruteforce | quickselect-bruteforce-by-kotomord-8beh | \n# Complexity\n- Time complexity: O(N + K^2)\n\n- Space complexity: O(N)\n\n# Code\n\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) { | kotomord | NORMAL | 2024-07-25T12:07:43.956376+00:00 | 2024-07-25T12:07:43.956410+00:00 | 8 | false | \n# Complexity\n- Time complexity: O(N + K^2)\n\n- Space complexity: O(N)\n\n# Code\n```\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n vector<int> nonneg;\n vector<int> neg;\n vector<int> effect;\n for(auto i: nums) {\n if(i>=0) nonneg.push_back(i);\n ... | 0 | 0 | ['Quickselect', 'C++'] | 0 |
find-the-k-sum-of-an-array | C++ | Binary search + Brute-force solution | c-binary-search-brute-force-solution-by-h13zp | \n# Approach\nUse binary-search to search for the possible sum value, which can generate >= k subsets, so that we can find the kth largest sum from that value.\ | bietdoikiem | NORMAL | 2024-07-20T17:00:54.403626+00:00 | 2024-07-20T17:04:45.184376+00:00 | 41 | false | \n# Approach\nUse binary-search to search for the possible sum value, which can generate >= k subsets, so that we can find the **kth largest sum** from that value.\n\n# Complexity\n- Time complexity: O(NlogN + Klog(|MaxSum| + |MinSum|))\n\n# Code\n```\nclass Solution {\npublic:\n long long INF = 1e18;\n void coun... | 0 | 0 | ['Binary Search', 'Recursion', 'C++'] | 0 |
find-the-k-sum-of-an-array | Heap with candidate values | heap-with-candidate-values-by-vilmos_pro-8eys | Intuition\n\nThe largest sum is obviously the sum of all positive elements of the array. For the $k$-th largest sum we have to remove some of the positive terms | vilmos_prokaj | NORMAL | 2024-07-04T11:33:52.374258+00:00 | 2024-07-04T11:35:00.165462+00:00 | 25 | false | # Intuition\n\nThe largest sum is obviously the sum of all positive elements of the array. For the $k$-th largest sum we have to remove some of the positive terms from this maximal sum and probably add some negative terms. So it has the form:\n\n$$\n \\sum_{i: a_i>0} a_i - \\sum_{j \\in S} |a_j|\n$$\nNow the problem... | 0 | 0 | ['Python3'] | 0 |
find-the-k-sum-of-an-array | Rudimentary explanation for any language / code in go | rudimentary-explanation-for-any-language-agpg | Code\n\ntype Item struct {\n Sum int64\n Index int\n}\n\nfunc kSum(nums []int, k int) int64 {\n\n // ================================================= | no3371 | NORMAL | 2024-04-29T18:45:47.352228+00:00 | 2024-04-29T18:45:47.352259+00:00 | 10 | false | # Code\n```\ntype Item struct {\n Sum int64\n Index int\n}\n\nfunc kSum(nums []int, k int) int64 {\n\n // ===========================================================================================\n\n // I have never really worked on LeetCode before, yet somehow stumbled on this problem and I decided to f... | 0 | 0 | ['Go'] | 0 |
find-the-k-sum-of-an-array | Python (Simple Heap) | python-simple-heap-by-rnotappl-tws4 | 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 | rnotappl | NORMAL | 2024-04-29T16:53:50.145858+00:00 | 2024-04-29T16:53:50.145894+00:00 | 62 | false | # 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 | 0 | ['Python3'] | 0 |
find-the-k-sum-of-an-array | list| sorting | list-sorting-by-11srj11-xfo3 | 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 | 11srj11 | NORMAL | 2024-03-25T10:00:35.191824+00:00 | 2024-03-25T10:00:35.191860+00:00 | 26 | false | # 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 | 0 | ['Python3'] | 0 |
find-the-k-sum-of-an-array | Easy Java Solution || Priority Queue || Beats 80% | easy-java-solution-priority-queue-beats-xxu5s | 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 | ravikumar50 | NORMAL | 2023-08-19T15:16:32.762936+00:00 | 2023-08-19T15:16:32.762960+00:00 | 33 | false | # 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 | 0 | ['Java'] | 0 |
find-the-k-sum-of-an-array | Understand the approch to solve this problem | understand-the-approch-to-solve-this-pro-4vtr | Intuition\n Describe your first making a good problem solving skills to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nap | D9261 | NORMAL | 2023-08-04T07:02:53.542889+00:00 | 2023-08-04T07:02:53.542915+00:00 | 57 | false | # Intuition\n<!-- Describe your first making a good problem solving skills to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\napproach-> our answer store to max_sum and aprove to abs value and compare the vector ele, of kth ele to add the index add adn push the our queue ... | 0 | 0 | ['C++'] | 0 |
find-the-k-sum-of-an-array | Golang by Priority Queue | golang-by-priority-queue-by-alexchangtw-6cmo | Intuition\n Describe your first thoughts on how to solve this problem. \nUsing same method from https://leetcode.com/problems/find-the-k-sum-of-an-array/solutio | alexchangtw | NORMAL | 2023-02-10T05:10:01.146815+00:00 | 2023-02-10T08:06:01.334348+00:00 | 53 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsing same method from https://leetcode.com/problems/find-the-k-sum-of-an-array/solutions/2456716/python3-heap-priority-queue-o-nlogn-klogk/\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUse Priority Queue to sol... | 0 | 0 | ['Go'] | 0 |
find-the-k-sum-of-an-array | explaining so that anybody can understand easily| priority queue | beats 100% time | nlogn +klogk | explaining-so-that-anybody-can-understan-oodw | This is one of the hardest problems that I solved myself!\nPlease upvote if the post helps!\n# Intuition\nlet\'s first start assuming that the given array has a | TheBuddhist | NORMAL | 2023-01-27T05:09:30.554517+00:00 | 2023-01-27T13:47:35.072312+00:00 | 38 | false | This is one of the hardest problems that I solved myself!\nPlease upvote if the post helps!\n# Intuition\nlet\'s first start assuming that the given array has all positive numbers, that will make the question easy to understand.\nlook at the subsequence lengthwise of the sorted array\n```\n[]\n\n[1],[2],[3],[4],[5]\n\n... | 0 | 0 | ['Sorting', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | Clear Py3 solution | clear-py3-solution-by-seraph1212-ftiz | Code\n\nfrom sortedcontainers import SortedList\n\nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n ps = sum([x for x in nums if x | seraph1212 | NORMAL | 2023-01-10T02:04:59.336609+00:00 | 2023-01-10T02:04:59.336647+00:00 | 172 | false | # Code\n```\nfrom sortedcontainers import SortedList\n\nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n ps = sum([x for x in nums if x >= 0])\n al = sorted([x if x >= 0 else -x for x in nums])\n # print(al)\n q = SortedList()\n q.add((ps, 0))\n \n ... | 0 | 0 | ['Python3'] | 0 |
find-the-k-sum-of-an-array | FSM + full binary tree + SortedList | fsm-full-binary-tree-sortedlist-by-matho-f9na | 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 | mathorlee | NORMAL | 2022-12-06T15:38:25.626800+00:00 | 2022-12-06T15:38:25.626843+00:00 | 204 | false | # 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 | 0 | ['Python3'] | 0 |
find-the-k-sum-of-an-array | [Python] min heap + max heap | python-min-heap-max-heap-by-wdhiuq-4wif | \nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n \n sum1 = sum(num for num in nums if num > 0)\n min_q, max_q, q = | wdhiuq | NORMAL | 2022-11-18T14:22:12.543782+00:00 | 2022-11-18T14:34:22.569927+00:00 | 38 | false | ```\nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n \n sum1 = sum(num for num in nums if num > 0)\n min_q, max_q, q = [0], [0], []\n\n for num in islice(sorted(map(abs, nums)), k-1):\n v = min_q[0]\n if len(max_q) == k and v > - max_q[0]:\n ... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | C# max heap and sorting | c-max-heap-and-sorting-by-clewby-9fvz | \npublic class Solution {\n public long KSum(int[] nums, int k) {\n long sum = 0;\n foreach(int n in nums)\n if(n > 0) sum += n;\n | clewby | NORMAL | 2022-11-13T01:19:24.129556+00:00 | 2022-11-13T01:19:24.129588+00:00 | 81 | false | ```\npublic class Solution {\n public long KSum(int[] nums, int k) {\n long sum = 0;\n foreach(int n in nums)\n if(n > 0) sum += n;\n Array.Sort(nums, (a,b) => { return Math.Abs(a)-Math.Abs(b); } );\n PriorityQueue<(long, long), long> pq = new();\n pq.Enqueue((sum-Math.A... | 0 | 0 | ['Heap (Priority Queue)', 'C#'] | 0 |
find-the-k-sum-of-an-array | C++ | priority queue | c-priority-queue-by-ajaythakran1998-2fmt | \ntypedef long long ll;\ntypedef pair<ll, ll> IntPr;\n\nclass Solution {\npublic:\n\n long long kSum(vector<int>& nums, int k) {\n ll sum = 0;\n | ajaythakran1998 | NORMAL | 2022-10-22T14:23:31.804176+00:00 | 2022-10-22T14:23:31.804205+00:00 | 59 | false | ```\ntypedef long long ll;\ntypedef pair<ll, ll> IntPr;\n\nclass Solution {\npublic:\n\n long long kSum(vector<int>& nums, int k) {\n ll sum = 0;\n for(auto& val : nums){\n if(val > 0) sum += val;\n else val = -val;\n }\n if(k == 1) return sum;\n priority_queu... | 0 | 0 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | Python Min-Heap solution, O(NlogN + KlogK), commented | python-min-heap-solution-onlogn-klogk-co-pggg | \nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n # subtract stores the sum which needs to be subtracted from the maximum sum\n | hemantdhamija | NORMAL | 2022-10-16T06:58:45.780415+00:00 | 2022-10-16T06:58:45.780458+00:00 | 220 | false | ```\nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n # subtract stores the sum which needs to be subtracted from the maximum sum\n # ans stores all the largest sum in decreasing order\n # Initialize heap (minheap) to store current least value which needs to be\n # subt... | 0 | 0 | ['Heap (Priority Queue)', 'Python'] | 0 |
find-the-k-sum-of-an-array | Python minheap | python-minheap-by-jzhanggg-sbta | ```\ndef kSum(self, nums: List[int], k: int) -> int:\n n=len(nums)\n mxs=sum(a for a in nums if a>0)\n if k==1:\n return mxs\n | jzhanggg | NORMAL | 2022-09-28T16:08:22.613569+00:00 | 2022-09-28T16:08:22.613599+00:00 | 57 | false | ```\ndef kSum(self, nums: List[int], k: int) -> int:\n n=len(nums)\n mxs=sum(a for a in nums if a>0)\n if k==1:\n return mxs\n k-=1\n q=[]\n heapq.heapify(q)\n nums=[abs(a) for a in nums]\n nums.sort()\n heappush(q,[nums[0],0])\n while k>0... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Java time O(n log n + k log k) space O(k), detailed comments. | java-time-on-log-n-k-log-k-space-ok-deta-10ek | Java\nclass Solution {\n /*\n\t Thanks for Huifeng Guan \'s explanation. ref : https://www.youtube.com/watch?v=pquoywwK0_w \n 1. the largest Sum is | gavinzhan | NORMAL | 2022-09-10T06:53:29.500307+00:00 | 2022-09-10T17:22:31.206403+00:00 | 241 | false | ```Java\nclass Solution {\n /*\n\t Thanks for Huifeng Guan \'s explanation. ref : https://www.youtube.com/watch?v=pquoywwK0_w \n 1. the largest Sum is the sum of all no negative numbers.\n 2. to get the next largest sum we can remove a no negative number (- nogegative) \n or add a negative nu... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Set a limitation and other tips (812 ms in Python3) | set-a-limitation-and-other-tips-812-ms-i-3fej | \nThe fastest solution till now seem like a priority queue(PQ)\nwhile there is still a little step to optimize.\n\nOne of that could be find a approximate downb | yjyjyj | NORMAL | 2022-08-26T05:05:48.184348+00:00 | 2022-08-26T13:55:58.169809+00:00 | 104 | false | \nThe fastest solution till now seem like a priority queue(PQ)\nwhile there is still a little step to optimize.\n\nOne of that could be find a approximate downbound value.\nWith that value, some of the sum will be filtered out, so it maintain a smaller priority queue, in this way, it will cost less to maitain the PQ.\n... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Process from smallest end | process-from-smallest-end-by-yjyjyj-d1no | \nWhen the k is big enough, it could be nearer to the smallest end.\nIn this kind of situation, convert to a problem that find a (2**n + 1 - k) smallest value | yjyjyj | NORMAL | 2022-08-26T04:47:52.020635+00:00 | 2022-08-26T04:47:52.020673+00:00 | 83 | false | \nWhen the k is big enough, it could be nearer to the smallest end.\nIn this kind of situation, convert to a problem that find a (2**n + 1 - k) smallest values\nin the mean time turn all nums to their opposite number\nThen the same logic for the rest of code | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Handle the zeros. | handle-the-zeros-by-yjyjyj-7di0 | \n zeros are so special in this case,\n zeros could make duplicated sum.\n that means all values could repeat at least 2 **(count(0 in nums))\n \n \nremove zero | yjyjyj | NORMAL | 2022-08-26T04:42:19.061754+00:00 | 2022-08-26T04:42:19.061786+00:00 | 55 | false | \n zeros are so special in this case,\n zeros could make duplicated sum.\n that means all values could repeat at least 2 **(count(0 in nums))\n \n \nremove zeros from the array will make it fast when any zero present in the array. while it cost very few if there is no zero\n\nk = (k - 1) // (2 ** iCountOfZeros) + 1\n\n | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | well commented easy to understand C++ solution with dummy example | well-commented-easy-to-understand-c-solu-p532 | \nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n long long sum = 0;\n for (auto &n : nums) {\n if (n >= 0) | vishalrathi02 | NORMAL | 2022-08-24T05:08:08.108712+00:00 | 2022-08-24T05:08:08.108753+00:00 | 105 | false | ```\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n long long sum = 0;\n for (auto &n : nums) {\n if (n >= 0)\n sum += n;\n else\n n = abs(n);\n }\n // get sum of all possitive integers in variable sum\n ... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Rust Sort & BinaryHeap | rust-sort-binaryheap-by-xiaoping3418-od14 | ~~~\nuse std::collections::BinaryHeap;\n\nimpl Solution {\n pub fn k_sum(nums: Vec, k: i32) -> i64 {\n let mut sum = 0i64;\n let n = nums.len() | xiaoping3418 | NORMAL | 2022-08-22T12:04:39.135165+00:00 | 2022-08-22T12:04:39.135202+00:00 | 80 | false | ~~~\nuse std::collections::BinaryHeap;\n\nimpl Solution {\n pub fn k_sum(nums: Vec<i32>, k: i32) -> i64 {\n let mut sum = 0i64;\n let n = nums.len();\n let mut data = vec![];\n \n for a in nums { \n if a > 0 { sum += a as i64; }\n data.push(i64::abs(a as i64))... | 0 | 0 | ['Sorting', 'Rust'] | 0 |
find-the-k-sum-of-an-array | [Python]Fast and Easy solution.(Runtime:87.5%,Memory:87.5%) | pythonfast-and-easy-solutionruntime875me-cdr0 | **Solution\n\n\tclass Solution:\n\t\tdef kSum(self, nums: List[int], k: int) -> int:\n\t\t\tle = len(nums)\n\n\t\t\tnums = [0] + sorted(nums,key = abs)\n\n\t\t\ | p147896325p | NORMAL | 2022-08-22T09:31:38.840677+00:00 | 2022-08-22T09:31:38.840703+00:00 | 288 | false | **Solution\n\n\tclass Solution:\n\t\tdef kSum(self, nums: List[int], k: int) -> int:\n\t\t\tle = len(nums)\n\n\t\t\tnums = [0] + sorted(nums,key = abs)\n\n\t\t\theap = [(-sum(num for num in nums if num > 0), 0)]\n\t\t\tfor _ in range(k):\n\t\t\t\ttmp = heappop(heap)\n\t\t\t\ti_sum = -tmp[0]\n\t\t\t\tpos = tmp[1]\n\n\t\... | 0 | 0 | ['Python'] | 0 |
find-the-k-sum-of-an-array | Python, 3 lines for fun! | python-3-lines-for-fun-by-404akhan-ydgs | Just for fun, condensed solution in 3 lines! It sometimes TLs, should run several times.\n\nBasically, solution is similar as everyone\'s priority queue observa | 404akhan | NORMAL | 2022-08-22T08:49:05.431771+00:00 | 2022-08-22T08:49:05.431802+00:00 | 107 | false | Just for fun, condensed solution in 3 lines! It sometimes TLs, should run several times.\n\nBasically, solution is similar as everyone\'s priority queue observation, Dijkstra logic. Except since you keep track of only 2000 nodes, you can manually append new possibilities to the `pq` list and sort it again.\n\nTime comp... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Rust idiomatic solution | binary heap | rust-idiomatic-solution-binary-heap-by-p-uuhp | \nuse std::collections::BinaryHeap;\nuse std::cmp::Reverse;\nimpl Solution {\n pub fn k_sum(mut nums: Vec<i32>, mut k: i32) -> i64 {\n let mut sum = n | pierrewang | NORMAL | 2022-08-22T08:20:21.267449+00:00 | 2022-08-22T08:20:21.267479+00:00 | 58 | false | ```\nuse std::collections::BinaryHeap;\nuse std::cmp::Reverse;\nimpl Solution {\n pub fn k_sum(mut nums: Vec<i32>, mut k: i32) -> i64 {\n let mut sum = nums.iter().filter(|&&v| v > 0).map(|&v| v as i64).sum::<i64>();\n let mut nums: Vec<_> = nums.into_iter().map(|v| v.abs()).collect();\n nums.so... | 0 | 0 | ['Rust'] | 0 |
find-the-k-sum-of-an-array | [C++] Explained heuristic DFS 296ms faster than 77% | c-explained-heuristic-dfs-296ms-faster-t-xbv0 | Firstly, sum up all positive number as base, sort numbers as abs value and try to search a combination of numbers to subtract from base.\nObserver that 2^11 = 2 | mopriestt | NORMAL | 2022-08-22T07:47:26.170274+00:00 | 2022-08-22T07:52:11.074718+00:00 | 60 | false | Firstly, sum up all positive number as **base**, sort numbers as abs value and try to search a combination of numbers to subtract from **base**.\nObserver that 2^11 = 2048 >= 2000 so we will never pick more than 11 elements\n\nThen, loop through how many numbers we will pick, use a stack to maintain the combinations. W... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Two way Merge || C ++ || O(max(nlogn, k^2)) | two-way-merge-c-omaxnlogn-k2-by-rebe1li0-4zv2 | \nclass Solution {\npublic:\n typedef long long LL;\n long long kSum(vector<int>& nums, int k) {\n LL sum = 0;\n vector<int> pos;\n f | rebe1li0n | NORMAL | 2022-08-22T07:23:39.763908+00:00 | 2022-08-22T07:24:15.886679+00:00 | 57 | false | ```\nclass Solution {\npublic:\n typedef long long LL;\n long long kSum(vector<int>& nums, int k) {\n LL sum = 0;\n vector<int> pos;\n for(auto x : nums){\n if(x > 0){\n sum += x;\n pos.push_back(-x);\n }else{\n pos.push_back(... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | python heap | python-heap-by-sh1v2m-53hc | ```\nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n max_sum = sum([n for n in nums if n > 0])\n ns = sorted([abs(n) for n | sh1v2m | NORMAL | 2022-08-22T06:18:42.884699+00:00 | 2022-08-22T06:18:42.884740+00:00 | 191 | false | ```\nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n max_sum = sum([n for n in nums if n > 0])\n ns = sorted([abs(n) for n in nums])\n \n h = [(ns[0], 0)]\n val = 0\n for _ in range(k - 1):\n val, i = heapq.heappop(h)\n if i < len(ns... | 0 | 0 | ['Greedy', 'Heap (Priority Queue)', 'Python'] | 0 |
find-the-k-sum-of-an-array | C++ | Build a maxHeap storing the subset sums as a tree (with clear explanations) | c-build-a-maxheap-storing-the-subset-sum-j32c | After the contest I came here looking for some insights. This is the same method as suggested by other top posts. I had difficulty understanding how the min hea | baihuaxie | NORMAL | 2022-08-22T03:50:55.702299+00:00 | 2022-08-22T03:53:19.549142+00:00 | 53 | false | After the contest I came here looking for some insights. This is the same method as suggested by other top posts. I had difficulty understanding how the min heap method was constructed after reading other posts, in particular why there were two pushes for each node; so I added some explanations here. Two references: [1... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | C++ | priority_queue | c-priority_queue-by-joey-q0sl | Got TLEs for enumerating all cases.\nUse this case generator instead:\n1. branch one: including element in index i\n2. branch two: excluding element in index i\ | joey___ | NORMAL | 2022-08-22T02:28:00.350411+00:00 | 2022-08-22T02:28:00.350453+00:00 | 43 | false | Got TLEs for enumerating all cases.\nUse this case generator instead:\n1. branch one: including element in index i\n2. branch two: excluding element in index i\nwith those two branches, we are able to generate all cases.\n\n```\n#define pls pair<long long, int>\n\nclass Solution {\npublic:\n long long kSum(vector<in... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | [CPP]: solution with explanation | cpp-solution-with-explanation-by-a1exreb-nj6v | Based on others suggestions. Check the explanation here: https://math.stackexchange.com/questions/89419/algorithm-wanted-enumerate-all-subsets-of-a-set-in-order | a1exrebys | NORMAL | 2022-08-21T22:08:44.452755+00:00 | 2022-08-21T22:25:13.540914+00:00 | 126 | false | Based on others suggestions. Check the explanation here: https://math.stackexchange.com/questions/89419/algorithm-wanted-enumerate-all-subsets-of-a-set-in-order-of-increasing-sums\n```\nlong long kSum(vector<int>& nums, int k) {\n\tlong long maxsum = 0;\n\tfor (auto& n : nums) {\n\t\tif (n >= 0) maxsum += n;\n\t\telse ... | 0 | 0 | ['C++'] | 0 |
find-the-k-sum-of-an-array | [C++] Priority queue | c-priority-queue-by-phi9t-f7y6 | We know the max sum is the sum of all positive values.\nWith the max sum as the starting point, the problem (to find any subsequence sum) becomes to remove any | phi9t | NORMAL | 2022-08-21T16:30:59.556220+00:00 | 2022-08-21T16:32:14.769178+00:00 | 71 | false | We know the max sum is the sum of all positive values.\nWith the max sum as the starting point, the problem (to find any subsequence sum) becomes to remove any subset `|val| : nums`. \nFor any particular sum, we shall proceed by the absolute values in ascending order. \n\nReference\nhttps://leetcode.com/problems/find-t... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | [Java] PriorityQueue || MaxSum ➖ Kth Smallest Comb Sum | java-priorityqueue-maxsum-kth-smallest-c-66nu | \nclass Solution {\n public long kSum(int[] nums, int k) {\n long maxSum = 0L;\n int n = nums.length;\n for(int i = 0; i < n; i++){\n | cooper-- | NORMAL | 2022-08-21T15:33:43.732415+00:00 | 2022-08-21T15:34:09.401463+00:00 | 118 | false | ```\nclass Solution {\n public long kSum(int[] nums, int k) {\n long maxSum = 0L;\n int n = nums.length;\n for(int i = 0; i < n; i++){\n if(nums[i] < 0){\n nums[i] = -nums[i];\n }else{\n maxSum += nums[i];\n }\n }\n \n ... | 0 | 0 | ['Java'] | 0 |
find-the-k-sum-of-an-array | C++ Easy solution | c-easy-solution-by-kunj_m-nrug | class Solution {\npublic:\n \n #define ll long long\n #define pp pair\n \n long long kSum(vector& A, int k) {\n \n | kunj_m | NORMAL | 2022-08-21T12:56:16.212677+00:00 | 2022-08-21T12:56:16.212732+00:00 | 84 | false | **class Solution {\npublic:\n \n #define ll long long\n #define pp pair<long long, int>\n \n long long kSum(vector<int>& A, int k) {\n \n int n = A.size();\n \n vector<ll> sub;\n \n ll mx = 0;\n \n \n ... | 0 | 0 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | Video Explanation (with Intuition of every step) | video-explanation-with-intuition-of-ever-cfxx | https://www.youtube.com/watch?v=VknH84HSeNM | codingmohan | NORMAL | 2022-08-21T11:12:50.423282+00:00 | 2022-08-21T16:26:42.444862+00:00 | 54 | false | https://www.youtube.com/watch?v=VknH84HSeNM | 0 | 0 | ['Breadth-First Search', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | [C++] Max Heap (Explained 🙂) | c-max-heap-explained-by-mjustboring-r841 | \n\u2022 Find sum of +ve numbers. (Max Sum)\n\u2022 Prepare a sorted array of absolute values of the given array\n\u2022 push(sum - a[0], 0) to a max heap.\n\u2 | mjustboring | NORMAL | 2022-08-21T10:24:11.484437+00:00 | 2022-08-21T17:12:25.527378+00:00 | 86 | false | \n\u2022 Find sum of +ve numbers. (Max Sum)\n\u2022 Prepare a sorted array of absolute values of the given array\n\u2022 push(sum - a[0], 0) to a max heap.\n\u2022 for k-1 iterations do\n pop the top from heap (say {sum, i})\n push(sum - a[i+1] + a[i], i+1)\n push(sum - a[i+1], i+1)\n \n> Basically we are s... | 0 | 0 | ['C', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | [C++] Priority Queue Solution with Explanation | c-priority-queue-solution-with-explanati-98bi | Idea:\n1. Find the largest summation of this array, which is sum of all positive numbers.\n2. There are two types of operation to get next number:\n\ta. Remove | acebenson0704 | NORMAL | 2022-08-21T09:50:47.744687+00:00 | 2022-08-21T09:51:43.572156+00:00 | 59 | false | Idea:\n1. Find the largest summation of this array, which is sum of all positive numbers.\n2. There are two types of operation to get next number:\n\ta. Remove one positive number from largest number.\n\tb. Add a new negtive number\n3. Repeat step2 k times.\n```c++\n/*\nTake [2, 4, -3] as example\nmax: 6 (2 + 4)\na. Re... | 0 | 0 | ['C'] | 0 |
find-the-k-sum-of-an-array | Please guide me on how to optimize recursive solution | please-guide-me-on-how-to-optimize-recur-aaiz | Can we optimise a recursive solution. I am getting TLE as this has huge time complexity.\n\ncpp\nclass Solution {\npublic:\n\tvoid solve(int ind, long long sum, | iamshubhamj | NORMAL | 2022-08-21T08:07:50.003413+00:00 | 2022-08-21T08:07:50.003452+00:00 | 189 | false | Can we optimise a recursive solution. I am getting TLE as this has huge time complexity.\n\n```cpp\nclass Solution {\npublic:\n\tvoid solve(int ind, long long sum, vector<int> &nums, vector<long long> &ans, int n, int k) {\n\t\tif(ind < 0) {\n\t\t\tans.push_back(sum);\n\t\t\treturn;\n\t\t}\n\n\t\tsolve(ind-1, sum + num... | 0 | 0 | ['Dynamic Programming', 'Recursion'] | 1 |
find-the-k-sum-of-an-array | easy short efficient clean code | easy-short-efficient-clean-code-by-maver-xvji | \nclass Solution {\n typedef long long ll;\n typedef pair<ll, ll> pi;\npublic:\n long long kSum(vector<int>& v, int k) {\n ll sz = v.size(), ans | maverick09 | NORMAL | 2022-08-21T07:55:45.340941+00:00 | 2022-08-21T07:55:45.340981+00:00 | 77 | false | ```\nclass Solution {\n typedef long long ll;\n typedef pair<ll, ll> pi;\npublic:\n long long kSum(vector<int>& v, int k) {\n ll sz = v.size(), ans = 0;\n for (auto& it : v) {\n if (it > -1) {\n ans += it;\n }\n it = abs(it);\n }\n sor... | 0 | 0 | ['C', 'Sorting', 'Heap (Priority Queue)'] | 0 |
find-the-k-sum-of-an-array | Merge sorted lists | merge-sorted-lists-by-wddd-n349 | This problem is more or less similar to the "Merge sorted lists" problem. https://leetcode.com/problems/merge-k-sorted-lists/\n\nSay the nums array is [1,2,3,4, | wddd | NORMAL | 2022-08-21T07:36:13.815021+00:00 | 2022-08-21T07:36:13.815072+00:00 | 60 | false | This problem is more or less similar to the "Merge sorted lists" problem. https://leetcode.com/problems/merge-k-sorted-lists/\n\nSay the `nums` array is `[1,2,3,4,5]`, then we have 5 sorted lists (desending order) for the subsequence sums:\n\n```\nsum-1, sum-1-2, sum-1-2-3, sum-1-2-3-4, sum-1-2-3-4-5\nsum-2, sum-2-3, s... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | My Solution | my-solution-by-hope_ma-qb3r | \n/**\n * Time Complexity: O(n_nums * log(n_nums) + k * log(k)), where `n_nums` is the length of the vector `nums`\n * Space Complexity: O(k)\n */\nclass Soluti | hope_ma | NORMAL | 2022-08-21T07:30:21.401503+00:00 | 2022-08-21T07:30:21.401542+00:00 | 51 | false | ```\n/**\n * Time Complexity: O(n_nums * log(n_nums) + k * log(k)), where `n_nums` is the length of the vector `nums`\n * Space Complexity: O(k)\n */\nclass Solution {\n public:\n long long kSum(vector<int> &nums, const int k) {\n long long sum = 0LL;\n for (int &num : nums) {\n if (num > 0) {\n sum ... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | Why DP sol getting TLE O( N * min(1<<N,2000)) ?? | why-dp-sol-getting-tle-o-n-min1n2000-by-k7yt0 | \n#define ll long long\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<ll> dp(1);\n\n | cyborg_05 | NORMAL | 2022-08-21T06:06:11.862510+00:00 | 2022-08-21T06:06:11.862572+00:00 | 241 | false | ```\n#define ll long long\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<ll> dp(1);\n\n for (int i = 1; i <= n; i++) {\n int size = (int)dp.size();\n for (int j = 0; j < size; j++) {\n dp.push_back(dp[j... | 0 | 0 | ['Dynamic Programming'] | 2 |
find-the-k-sum-of-an-array | javascript max heap/pq 453ms | javascript-max-heappq-453ms-by-henrychen-364q | \nconst kSum = (a, k) => {\n let sum = 0, n = a.length, pq = new MaxPriorityQueue({ compare: (x, y) => y[0] - x[0] });\n for (let i = 0; i < n; i++) {\n | henrychen222 | NORMAL | 2022-08-21T05:59:57.294783+00:00 | 2022-08-21T05:59:57.294816+00:00 | 204 | false | ```\nconst kSum = (a, k) => {\n let sum = 0, n = a.length, pq = new MaxPriorityQueue({ compare: (x, y) => y[0] - x[0] });\n for (let i = 0; i < n; i++) {\n if (a[i] < 0) {\n a[i] *= -1;\n } else {\n sum += a[i];\n }\n }\n if (k == 1) return sum;\n a.sort((x, y) ... | 0 | 0 | ['Heap (Priority Queue)', 'JavaScript'] | 0 |
find-the-k-sum-of-an-array | Python Solution - we don't need a heap queue! | python-solution-we-dont-need-a-heap-queu-2ndp | Solving for k== 0 is obvious: simply calculate the sum of the positive numbers.\n\nFor each case of k > 0 you need a less optimum answer. You get a lower answer | shepherd_a | NORMAL | 2022-08-21T05:48:34.428991+00:00 | 2022-08-22T05:56:19.493727+00:00 | 223 | false | Solving for k== 0 is obvious: simply calculate the sum of the positive numbers.\n\nFor each case of *k > 0* you need a less optimum answer. You get a lower answer by \n\n- Subtracting one or more of the positive items you added\n- Adding one of more of the negative items you didn\'t add originally\n\nThere are *n* pos... | 0 | 0 | ['Python'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.