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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
random-point-in-non-overlapping-rectangles | Simple pure C solution (with prefix sum) | simple-pure-c-solution-with-prefix-sum-b-lwul | Code | ggandrew1218 | NORMAL | 2025-02-25T10:40:05.751759+00:00 | 2025-02-25T10:40:25.568331+00:00 | 4 | false |
# Code
```c []
typedef struct {
int** coord;
int num;
int* prefix;
} Solution;
Solution* solutionCreate(int** rects, int rectsSize, int* rectsColSize) {
srand( (unsigned) time(NULL) );
Solution* obj = malloc(sizeof(Solution));
obj->num=0;
obj->coord = malloc(sizeof(int*)*rectsSize);
... | 0 | 0 | ['C', 'Prefix Sum'] | 0 |
random-point-in-non-overlapping-rectangles | My Rust solution | my-rust-solution-by-omgeeky1-85s1 | ApproachCalculate the area of each rectangle and use that as weight when picking rects, to choose a point from. This ensures that each point has the same chance | omgeeky1 | NORMAL | 2025-01-17T18:39:58.809168+00:00 | 2025-01-17T18:39:58.809168+00:00 | 2 | false | # Approach
Calculate the area of each rectangle and use that as weight when picking rects, to choose a point from. This ensures that each point has the same chance of being picked.
# Note
sometimes, while optimizing the code, it would just say the solution was wrong, even though it hasn't changed logically. On the s... | 0 | 0 | ['Rust'] | 0 |
random-point-in-non-overlapping-rectangles | Random Point Picker in Rectangles | random-point-picker-in-rectangles-by-lee-bmm2 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | leet_coderps050303 | NORMAL | 2025-01-13T17:06:53.816770+00:00 | 2025-01-13T17:06:53.816770+00:00 | 4 | 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 |
random-point-in-non-overlapping-rectangles | C++ solution of Random point in non-overlapping rectangles | c-solution-of-random-point-in-non-overla-sdv8 | Intuitionfollow the approach of algomonster blog --> https://algo.monster/liteproblems/497Complexity
Time complexity:O(log n)
Space complexity:O(n)
Code | gjit515 | NORMAL | 2025-01-09T07:22:22.351180+00:00 | 2025-01-09T07:22:22.351180+00:00 | 10 | false | # Intuition
follow the approach of algomonster blog --> https://algo.monster/liteproblems/497
# Complexity
- Time complexity:O(log n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
vector<i... | 0 | 0 | ['Binary Search', 'Prefix Sum', 'Randomized', 'C++'] | 0 |
random-point-in-non-overlapping-rectangles | 📌📌 WEIGHTED AREA APPROACH FOR RANDOM INDEX SELECTION📌📌 | Binary Search Solution✅✅ | weighted-area-approach-for-random-index-svjxr | IntuitionApproachComplexity
Time complexity: O(n * log n)
Space complexity: O(n)
Code | Sankar_Madhavan | NORMAL | 2025-01-07T16:01:32.475553+00:00 | 2025-01-07T16:01:32.475553+00:00 | 9 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(n * log n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(... | 0 | 0 | ['Binary Search', 'C++'] | 0 |
the-k-strongest-values-in-an-array | C++/Java/Python Two Pointers + 3 Bonuses | cjavapython-two-pointers-3-bonuses-by-vo-swsn | My initial solution during the contest was to sort the array to figure out the median, and then sort the array again based on the abs(arr[i] - median) criteria. | votrubac | NORMAL | 2020-06-07T04:02:50.250536+00:00 | 2020-06-07T21:51:19.060753+00:00 | 8,497 | false | My initial solution during the contest was to sort the array to figure out the `median`, and then sort the array again based on the `abs(arr[i] - median)` criteria.\n\nThen I noticed that we have smallest and largest elements in the array in the result. That makes sense based on the problem definition. So, instead of s... | 155 | 1 | [] | 18 |
the-k-strongest-values-in-an-array | Why not right definition of median | why-not-right-definition-of-median-by-le-6ixm | median = A[(n - 1) / 2] ??\n\nNo, why did leetcode define median itself....\n\nmedian = (A[n/2] + A[(n-1)/2]) / 2\n\nPlease noted that that\'s universal definit | lee215 | NORMAL | 2020-06-07T04:04:43.132192+00:00 | 2020-06-07T04:05:35.189158+00:00 | 4,896 | false | `median = A[(n - 1) / 2]` ??\n\nNo, why did leetcode define median itself....\n\n`median = (A[n/2] + A[(n-1)/2]) / 2`\n\nPlease noted that that\'s universal definition of "median"\n\nTime `O(nlogn)`\nSpace `O(n)`\n\n**C++**\n```cpp\n vector<int> getStrongest(vector<int> A, int k) {\n sort(A.begin(), A.end());... | 103 | 5 | [] | 17 |
the-k-strongest-values-in-an-array | Learn randomized algorithm today which solves this problem in O(n). | learn-randomized-algorithm-today-which-s-a7w9 | Two Approaches: O(nlogn) and O(n) - Sort and Randomized Algorithm\n\nThe problem is not complex to find the solution of but it can get complex in implementation | interviewrecipes | NORMAL | 2020-06-07T04:02:01.138702+00:00 | 2020-06-10T18:17:50.129802+00:00 | 3,083 | false | **Two Approaches: O(nlogn) and O(n) - Sort and Randomized Algorithm**\n\nThe problem is not complex to find the solution of but it can get complex in implementation. Basically you want to find the median of the array and then find k values that result in the maximum value for the requested operation.\n\n**A Straightfor... | 42 | 2 | [] | 8 |
the-k-strongest-values-in-an-array | [Python3] straightforward 2 lines with custom sort | python3-straightforward-2-lines-with-cus-yvgw | get median by sorting and getting the (n-1)/2 th element\n2. use lambda to do custom sort. since we need maximum, take negative of difference. if difference is | manparvesh | NORMAL | 2020-06-07T04:18:02.071240+00:00 | 2020-06-07T16:10:09.217635+00:00 | 1,518 | false | 1. get median by sorting and getting the `(n-1)/2` th element\n2. use lambda to do custom sort. since we need maximum, take negative of difference. if difference is equal, we just get the larger value element\n\n```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n m = sorted(a... | 16 | 0 | ['Python3'] | 2 |
the-k-strongest-values-in-an-array | [C++] Simple solution using two pointers | c-simple-solution-using-two-pointers-by-0n6zu | \ncpp\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(), arr.end());\n int left = 0;\n | hayashi-ay | NORMAL | 2020-06-07T10:52:08.288875+00:00 | 2020-06-07T10:52:08.288910+00:00 | 1,343 | false | \n```cpp\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(), arr.end());\n int left = 0;\n int right = arr.size()-1;\n int midIndex = (arr.size() -1)/2;\n int mid = arr[midIndex];\n \n vector<int> answer(k);\n in... | 15 | 0 | ['C', 'C++'] | 1 |
the-k-strongest-values-in-an-array | Python3 solution beats 100% of submissions | python3-solution-beats-100-of-submission-7w9s | \nclass Solution:\n \n def getStrongest(self, arr: List[int], K: int) -> List[int]:\n arr.sort()\n length = len(arr)\n res = arr[(len | dark_wolf_jss | NORMAL | 2020-06-07T05:12:17.088064+00:00 | 2020-06-07T05:12:17.088104+00:00 | 594 | false | ```\nclass Solution:\n \n def getStrongest(self, arr: List[int], K: int) -> List[int]:\n arr.sort()\n length = len(arr)\n res = arr[(length-1)//2]\n i = 0\n j = length-1\n result = []\n while K>0:\n a = abs(arr[i]-res)\n b = abs(arr[j]-res)\n ... | 13 | 1 | [] | 0 |
the-k-strongest-values-in-an-array | Java O(nlogn) solution:Sorting done once | java-onlogn-solutionsorting-done-once-by-suqu | To do this question,we need to sort the array,after sorting we can find the median by using the formula given in question.\nA value arr[i] is said to be stronge | manasviiiii | NORMAL | 2021-05-04T10:22:14.345050+00:00 | 2021-05-04T10:22:14.345091+00:00 | 736 | false | To do this question,we need to sort the array,after sorting we can find the median by using the formula given in question.\nA value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array.\nSince we need to find stronger values,we can see from the above expres... | 10 | 0 | ['Java'] | 3 |
the-k-strongest-values-in-an-array | Python solution (2 lines) 100% faster runtime | python-solution-2-lines-100-faster-runti-hj77 | \n\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n med = sorted(arr[(len(arr)-1)//2])\n \xA0 \xA0 \xA0 \xA0return sorted(array, key=l | it_bilim | NORMAL | 2020-06-07T09:05:22.626380+00:00 | 2020-06-07T20:09:07.821706+00:00 | 941 | false | \n```\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n med = sorted(arr[(len(arr)-1)//2])\n \xA0 \xA0 \xA0 \xA0return sorted(array, key=lambda x:(abs(x-med),x))[-k:]\n``` | 8 | 0 | ['Sorting', 'Python', 'Python3'] | 2 |
the-k-strongest-values-in-an-array | Java Custom Sort [easy] | java-custom-sort-easy-by-jatinyadav96-cso4 | \n public static int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int med = arr[((arr.length - 1) / 2)];\n int[] kStrongest = new int[k];\ | jatinyadav96 | NORMAL | 2020-06-07T04:17:26.484092+00:00 | 2020-06-07T04:17:26.484147+00:00 | 1,231 | false | ```\n public static int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int med = arr[((arr.length - 1) / 2)];\n int[] kStrongest = new int[k];\n ArrayList<Integer> ns = new ArrayList<>();\n for (int f : arr) ns.add(f);// please comment if any better way to do this [copying int[] into ArrayList... | 8 | 2 | ['Sorting', 'Java'] | 8 |
the-k-strongest-values-in-an-array | [Java/Python 3] Two codes: Two Pointer and PriorityQueue w/ brief explanation and analysis. | javapython-3-two-codes-two-pointer-and-p-xqwo | First sort then \nMethod 1:\nUse Two Pointer: \n1. starts from the two ends of the sorted array, choose the stronger one and move the corresponding pointer;\n2. | rock | NORMAL | 2020-06-07T04:04:06.589144+00:00 | 2020-06-13T04:34:39.948628+00:00 | 871 | false | First sort then \n**Method 1:**\nUse Two Pointer: \n1. starts from the two ends of the sorted array, choose the stronger one and move the corresponding pointer;\n2. repeats the above till get k elements.\n\n```java\n public int[] getStrongest(int[] arr, int k) {\n int n = arr.length, i = 0, j = n - 1;\n ... | 8 | 0 | [] | 5 |
the-k-strongest-values-in-an-array | Java solution | Two approaches | java-solution-two-approaches-by-sourin_b-kl2w | Please Upvote !!!\n##### 1. Using Priority Queue (Max Heap)\n\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n | sourin_bruh | NORMAL | 2022-08-27T18:51:27.442765+00:00 | 2022-08-27T18:51:27.442804+00:00 | 849 | false | ### *Please Upvote !!!*\n##### 1. Using Priority Queue (Max Heap)\n```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int m = arr[(arr.length - 1) / 2];\n PriorityQueue<Integer> pq = new PriorityQueue<>(\n (a, b) -> Math.abs(a - m) == Math.... | 6 | 0 | ['Two Pointers', 'Heap (Priority Queue)', 'Java'] | 2 |
the-k-strongest-values-in-an-array | [C++] Why time limit on sort method during contest, and got accepted on re-submission? | c-why-time-limit-on-sort-method-during-c-61g9 | No need for explanation, straightforward solution.\n71 / 71 test cases passed, but took too long. Resubmission of same code got accepted.\nWhy it happened? Is i | mr_robot11 | NORMAL | 2020-06-07T04:11:17.575792+00:00 | 2020-06-07T04:11:17.575841+00:00 | 454 | false | No need for explanation, straightforward solution.\n71 / 71 test cases passed, but took too long. Resubmission of same code got accepted.\nWhy it happened? Is it normal or there is something wrong in my code.\n\n```\nbool comp(const pair<int,int> &a, const pair<int,int> &b)\n{\n if(a.first==b.first) return a.second>... | 6 | 1 | ['C', 'Sorting'] | 1 |
the-k-strongest-values-in-an-array | Java Simple Heap | java-simple-heap-by-hobiter-vdgn | Should be straightforward;\njust pay attention, the new defination of median makes this problem easier;\nTime:\nO(nlgN) to find the median;\nO(N), strictly O(lg | hobiter | NORMAL | 2020-06-07T04:02:10.276285+00:00 | 2020-06-07T20:37:18.741655+00:00 | 544 | false | Should be straightforward;\njust pay attention, the new defination of median makes this problem easier;\nTime:\nO(nlgN) to find the median;\nO(N), strictly O(lg3 * N) = O(N), to find result;\nSpace,\nO(k + ...) = O(k);\n```\n\tpublic int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int m = ar... | 5 | 0 | [] | 2 |
the-k-strongest-values-in-an-array | JavaScript Solution - Two Pointers Approach | javascript-solution-two-pointers-approac-x4zk | \nvar getStrongest = function(arr, k) {\n const n = arr.length;\n \n arr.sort((a, b) => a - b);\n \n let left = 0;\n let right = n - 1;\n \ | Deadication | NORMAL | 2021-08-16T18:02:38.647223+00:00 | 2021-08-16T18:02:38.647270+00:00 | 181 | false | ```\nvar getStrongest = function(arr, k) {\n const n = arr.length;\n \n arr.sort((a, b) => a - b);\n \n let left = 0;\n let right = n - 1;\n \n const idx = Math.floor((n - 1) / 2);\n const median = arr[idx];\n \n const res = [];\n \n while (k > 0) {\n const leftNum = arr[le... | 4 | 0 | ['Two Pointers', 'JavaScript'] | 0 |
the-k-strongest-values-in-an-array | Too Simple with C++ 2pointers | too-simple-with-c-2pointers-by-aryan29-epth | \nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n //In a sorted vector either 1st or last will be strongest\n | aryan29 | NORMAL | 2020-08-08T17:20:22.730987+00:00 | 2020-08-08T17:20:22.731039+00:00 | 211 | false | ```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n //In a sorted vector either 1st or last will be strongest\n //Seems like a simple 2pointer question\n int i=0,j=arr.size()-1;\n sort(arr.begin(),arr.end());\n int med=arr[(arr.size()-1)/2];\n ... | 4 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | 👉C++ MAX HEAP 👀!!! | c-max-heap-by-akshay_ar_2002-pg9b | Intuition\n Describe your first thoughts on how to solve this problem. \n- MAX HEAP + PAIR\n\n# Approach\n Describe your approach to solving the problem. \n- Th | akshay_AR_2002 | NORMAL | 2023-04-07T19:55:12.502867+00:00 | 2023-04-07T19:55:12.502906+00:00 | 222 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- MAX HEAP + PAIR\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- The first step is to sort the array arr in non-decreasing order. This is because we need to find the median of the array.\n\n- Once we have sorted... | 3 | 0 | ['C++'] | 0 |
the-k-strongest-values-in-an-array | simple cpp solution | simple-cpp-solution-by-prithviraj26-ul4p | 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 | prithviraj26 | NORMAL | 2023-02-06T05:25:48.004265+00:00 | 2023-02-06T05:26:20.960943+00:00 | 526 | 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)$$ -->\no(nlogn)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $... | 3 | 0 | ['Array', 'C++'] | 0 |
the-k-strongest-values-in-an-array | Highly optimized & easiest C++ solution using two pointer | highly-optimized-easiest-c-solution-usin-d8h4 | \nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(),arr.end());\n int i = 0 , j = arr.size()-1;\n | NextThread | NORMAL | 2022-01-24T17:06:32.400014+00:00 | 2022-01-24T17:06:32.400059+00:00 | 219 | false | ```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(),arr.end());\n int i = 0 , j = arr.size()-1;\n int median = arr[(arr.size()-1)/2];\n while(--k >= 0){\n if(median - arr[i] > arr[j] - median)\n i++;\n else... | 3 | 0 | ['C'] | 0 |
the-k-strongest-values-in-an-array | ✔ JAVA | 2 Solutions | 2-Pointer and Priority-Queue | java-2-solutions-2-pointer-and-priority-b24yg | 2-Pointer Approach\n\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n int[] result = new int[k];\n int n = arr.length, left | norman22194plus1 | NORMAL | 2021-11-25T13:54:26.560783+00:00 | 2021-11-25T13:54:26.560812+00:00 | 222 | false | **2-Pointer Approach**\n```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n int[] result = new int[k];\n int n = arr.length, left = 0, right = n - 1, idx = 0;\n Arrays.sort(arr);\n int median = arr[(n - 1) / 2];\n while (left <= right) {\n int diff_l ... | 3 | 0 | ['Array', 'Java'] | 0 |
the-k-strongest-values-in-an-array | C++ STL but no sorting O(N) | c-stl-but-no-sorting-on-by-willwsxu-luoi | \n\n vector<int> getStrongest(vector<int>& arr, int k) {\n const int median = (arr.size()-1)/2;\n nth_element(begin(arr), begin(arr)+median, en | willwsxu | NORMAL | 2020-07-29T22:51:15.553065+00:00 | 2020-07-29T23:37:41.863464+00:00 | 263 | false | ```\n\n vector<int> getStrongest(vector<int>& arr, int k) {\n const int median = (arr.size()-1)/2;\n nth_element(begin(arr), begin(arr)+median, end(arr)); // find median, O(N)\n const int M = *(begin(arr)+median);\n // find strongest K elements\n nth_element(begin(arr), begin(arr)... | 3 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | 2 line Python | 2-line-python-by-auwdish-422b | Summary\nFirst sort to calc the median arr[(len(arr) - 1) // 2]\nThen sort by key abs(num - arr[(len(arr) - 1) // 2]) and num\nTake the last k elements\n\npytho | auwdish | NORMAL | 2020-06-12T19:43:58.443076+00:00 | 2020-06-12T19:43:58.443130+00:00 | 145 | false | **Summary**\nFirst sort to calc the median `arr[(len(arr) - 1) // 2]`\nThen sort by key `abs(num - arr[(len(arr) - 1) // 2])` and `num`\nTake the last k elements\n\n```python\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n return sorted(arr, key=lambda num... | 3 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | Py3 Easy Explained Sol | py3-easy-explained-sol-by-ycverma005-bkvh | \n# Idea- Strongest value are willing to realy on boundary either side, and then inward with decreasing intesity.\n# Steps to solve\n# Sort, get median, and ass | ycverma005 | NORMAL | 2020-06-11T10:35:20.412231+00:00 | 2020-06-11T10:35:20.412282+00:00 | 295 | false | ```\n# Idea- Strongest value are willing to realy on boundary either side, and then inward with decreasing intesity.\n# Steps to solve\n# Sort, get median, and assign lastPointer, startPointer\n# compare lastPointer\'s element with startPointer\'s element, then add accordingly\n# only iterate loop for k, because elemen... | 3 | 0 | ['Sorting', 'Python', 'Python3'] | 0 |
the-k-strongest-values-in-an-array | Python 3 PriorityQueue solution | python-3-priorityqueue-solution-by-liian-co5l | Since I didn\'t see the PriorityQuene implementation in python I would like to provide my implementation for python guys.\n\n#### PriorityQuene\nTime: O(nlogn + | liianghuang | NORMAL | 2020-06-07T05:20:08.753626+00:00 | 2020-06-07T05:34:46.565232+00:00 | 125 | false | Since I didn\'t see the PriorityQuene implementation in python I would like to provide my implementation for python guys.\n\n#### PriorityQuene\nTime: O(nlogn + nlogk) where n is `len(arr)`\nSpace: O(k)\n```python\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n ... | 3 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | C++ | beats 100% in memory and time | O(n Log n) | sorting | c-beats-100-in-memory-and-time-on-log-n-3mwll | No mater which algo we perform on this question, we need to sort (in O(n Log n)) our array first.\nThe basic idea is after we sort our array, then we will check | yashsaxena9 | NORMAL | 2020-06-07T04:32:30.546256+00:00 | 2020-06-07T04:33:34.841098+00:00 | 274 | false | **No mater which algo we perform on this question, we need to sort (in O(n Log n)) our array first.**\nThe basic idea is after we sort our array, then we will check values from beginning and last for the condition mentioned in question (abs(arr[j] - median) > abs(arr[i] - median)) to find strong elements.\nWe will incr... | 3 | 0 | ['C', 'Sorting', 'C++'] | 0 |
the-k-strongest-values-in-an-array | JAVA TWO POINTERS VERY EASY TO UNDERSTAND | java-two-pointers-very-easy-to-understan-l374 | \nclass Solution {\n public int[] getStrongest(int[] nums, int k) {\n int[] res = new int[k];\n Arrays.sort(nums);\n int size = nums.len | jianhuilin1124 | NORMAL | 2020-06-07T04:12:52.678690+00:00 | 2020-06-07T04:12:52.678754+00:00 | 346 | false | ```\nclass Solution {\n public int[] getStrongest(int[] nums, int k) {\n int[] res = new int[k];\n Arrays.sort(nums);\n int size = nums.length;\n int m = (size - 1) / 2;\n int median = nums[m];\n int index = 0;\n int left = 0, right = size - 1;\n while (left <=... | 3 | 0 | ['Java'] | 1 |
the-k-strongest-values-in-an-array | Clean Python 3, generator with deque | clean-python-3-generator-with-deque-by-l-q3wn | Time: O(sort), it can reach O(N) if we adopt counting sort\nSpace: O(N) for output\nThanks grokus for his suggestion.\n\nimport collections\nclass Solution:\n | lenchen1112 | NORMAL | 2020-06-07T04:03:52.031235+00:00 | 2020-06-07T04:28:03.270124+00:00 | 272 | false | Time: `O(sort)`, it can reach O(N) if we adopt counting sort\nSpace: `O(N)` for output\nThanks [grokus](https://leetcode.com/grokus/) for his suggestion.\n```\nimport collections\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n def gen(A):\n dq = collections.deque(A... | 3 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | C# - Simple Sorting | c-simple-sorting-by-christris-z5fv | csharp\npublic int[] GetStrongest(int[] arr, int k) \n{\n\tArray.Sort(arr);\n\tint m = arr[(arr.Length - 1)/ 2];\n\n\tvar result = arr.OrderByDescending(x => Ma | christris | NORMAL | 2020-06-07T04:02:26.245636+00:00 | 2020-06-07T04:02:26.245690+00:00 | 124 | false | ```csharp\npublic int[] GetStrongest(int[] arr, int k) \n{\n\tArray.Sort(arr);\n\tint m = arr[(arr.Length - 1)/ 2];\n\n\tvar result = arr.OrderByDescending(x => Math.Abs(x - m)).ThenByDescending(x => x).Take(k).ToArray();\n\treturn result;\n}\n``` | 3 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | SIMPLE TWO-POINTER C++ SOLUTION | simple-two-pointer-c-solution-by-jeffrin-rzvr | 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 | Jeffrin2005 | NORMAL | 2024-07-22T12:05:51.722673+00:00 | 2024-07-22T12:05:51.722692+00:00 | 169 | 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:o(k)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:o(k)\n<!-- Add your space complexity here, e.g. $$O... | 2 | 0 | ['C++'] | 0 |
the-k-strongest-values-in-an-array | BEST C++ solution || Beginner-friendly approach || Beats 90% !! | best-c-solution-beginner-friendly-approa-xl8h | Code\n\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n vector<int> ans;\n sort(arr.begin(), arr.end());\n | prathams29 | NORMAL | 2023-06-29T05:38:04.527248+00:00 | 2023-06-29T05:38:04.527274+00:00 | 244 | false | # Code\n```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n vector<int> ans;\n sort(arr.begin(), arr.end());\n int a = arr.size(), m = arr[(a-1)/2], i=0, j=a-1;\n while(i<=j){\n if(abs(arr[i]-m) > abs(arr[j]-m))\n ans.push_back(... | 2 | 0 | ['C++'] | 0 |
the-k-strongest-values-in-an-array | Easy python solution using two pointers | easy-python-solution-using-two-pointers-hnbsv | \n# Code\n\nclass Solution(object):\n def getStrongest(self, arr, k):\n """\n :type arr: List[int]\n :type k: int\n :rtype: List[ | Lalithkiran | NORMAL | 2023-03-30T09:42:40.695471+00:00 | 2023-03-30T09:42:40.695517+00:00 | 249 | false | \n# Code\n```\nclass Solution(object):\n def getStrongest(self, arr, k):\n """\n :type arr: List[int]\n :type k: int\n :rtype: List[int]\n """\n arr.sort()\n lo=0\n hi=len(arr)-1\n m=arr[hi//2]\n lst=[]\n for i in arr:\n lst.appe... | 2 | 0 | ['Array', 'Two Pointers', 'Sorting', 'Python'] | 0 |
the-k-strongest-values-in-an-array | python3 two pointer | python3-two-pointer-by-seifsoliman-l9t4 | 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 | seifsoliman | NORMAL | 2023-03-01T11:48:12.372000+00:00 | 2023-03-01T11:51:31.838016+00:00 | 295 | 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 O(NlogN)\n\n- Space complexity:\n O(N)\n# Code\n```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> ... | 2 | 0 | ['Array', 'Two Pointers', 'Sorting', 'Python3'] | 0 |
the-k-strongest-values-in-an-array | C++✅✅ | Beginner Friendly Approach | Sorting + Vector of Pairs | | c-beginner-friendly-approach-sorting-vec-u9cj | 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 | mr_kamran | NORMAL | 2022-10-05T10:35:35.808562+00:00 | 2022-10-05T10:35:35.808600+00:00 | 676 | 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)$$ --... | 2 | 0 | ['C++'] | 1 |
the-k-strongest-values-in-an-array | C++ K strongest values in the array | c-k-strongest-values-in-the-array-by-gur-zvs9 | \nif like it upvote.......\u2661\n\n\nvector getStrongest(vector& arr, int k) {\n vectorans;\n sort(arr.begin(),arr.end());\n int mid=arr[( | Gurjeet07 | NORMAL | 2022-09-21T21:00:01.323838+00:00 | 2022-09-21T21:02:30.896126+00:00 | 286 | false | \n**if like it upvote.......\u2661**\n\n\nvector<int> getStrongest(vector<int>& arr, int k) {\n vector<int>ans;\n sort(arr.begin(),arr.end());\n int mid=arr[(arr.size()-1)/2];\n int s=0;\n int e=arr.size()-1;\n while(s<=e)\n {\n if(abs(arr[s]-mid)>abs(arr[e]-m... | 2 | 1 | ['Two Pointers', 'C', 'Sorting'] | 0 |
the-k-strongest-values-in-an-array | Two pointer implementation question(It took couple of minutes to figure out the logic 🤨) | two-pointer-implementation-questionit-to-pwx2 | \nclass Solution\n{\n public:\n vector<int> getStrongest(vector<int> &arr, int k)\n {\n vector<int> vec;\n sort(arr.begin | Tan_B | NORMAL | 2022-09-14T04:39:57.321752+00:00 | 2022-09-14T04:39:57.321796+00:00 | 176 | false | ```\nclass Solution\n{\n public:\n vector<int> getStrongest(vector<int> &arr, int k)\n {\n vector<int> vec;\n sort(arr.begin(), arr.end());\n int med = arr[(arr.size() - 1) / 2];\n int i = 0;//Take one pointer at starting\n int j = arr.size() - 1;/... | 2 | 0 | ['Two Pointers'] | 1 |
the-k-strongest-values-in-an-array | 🔥 Javascript Solution - Clean Logic | javascript-solution-clean-logic-by-joeni-m5i8 | \nfunction getStrongest(A, k) {\n // get abs methods\n const { abs, floor } = Math\n \n // sort first\n A.sort((a, b) => a - b)\n \n // get | joenix | NORMAL | 2022-09-05T10:50:57.002924+00:00 | 2022-09-05T10:50:57.002965+00:00 | 260 | false | ```\nfunction getStrongest(A, k) {\n // get abs methods\n const { abs, floor } = Math\n \n // sort first\n A.sort((a, b) => a - b)\n \n // get middle\n const m = A[floor((A.length-1) / 2)]\n \n // soring by middle\n A.sort((a, b) => abs(a - m) - abs(b - m))\n \n // reverse\n A.... | 2 | 0 | ['JavaScript'] | 0 |
the-k-strongest-values-in-an-array | Java || O(NlogN) || Single Sort || Two Pointer | java-onlogn-single-sort-two-pointer-by-s-phmj | Once the array is sorted, use the fact that smallest and largest element is going to have the largest absolute difference with the median.\n\n\nclass Solution { | Shrekpozer02 | NORMAL | 2022-01-31T14:44:36.197350+00:00 | 2022-01-31T14:44:36.197398+00:00 | 90 | false | Once the array is sorted, use the fact that smallest and largest element is going to have the largest absolute difference with the median.\n\n```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n int n = arr.length;\n \n Arrays.sort(arr);\n \n int m = arr[(n-1)/2]... | 2 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | C++ | Sorting using lambda function | With comments | c-sorting-using-lambda-function-with-com-fnma | \n\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(),arr.end()); //sort to find the median\n | prnvgautam15 | NORMAL | 2021-07-06T16:03:11.450482+00:00 | 2021-07-06T16:04:49.290182+00:00 | 170 | false | \n```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(),arr.end()); //sort to find the median\n int n=arr.size();\n int med=arr[(n-1)/2]; //median\n \n sort(arr.begin(),arr.end(),[med](int &a,int &b){ //sorting using lambda... | 2 | 0 | ['C', 'Sorting'] | 1 |
the-k-strongest-values-in-an-array | C++ | QuickSelect | Faster than 99.86% | c-quickselect-faster-than-9986-by-srijan-5mlv | Naive Approach:\n1. Sort the array in non-decreasing order.\n2. Choose median as (n-1)/2th element.\n3. Sort again based on a criteria given in question.\n\nMy | srijansankrit | NORMAL | 2020-10-14T14:23:58.417181+00:00 | 2020-10-14T14:24:57.287707+00:00 | 136 | false | Naive Approach:\n1. Sort the array in non-decreasing order.\n2. Choose median as (n-1)/2th element.\n3. Sort again based on a criteria given in question.\n\n**My Naive Approach Code:** (940 ms, faster than 39%)\n```\nclass Solution {\npublic:\n int median;\n vector<int> getStrongest(vector<int>& arr, int k) {\n ... | 2 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | [C++] O(n) shortest quickselect solution | c-on-shortest-quickselect-solution-by-al-he5d | Average time complexity : O(n)\nWorst time complexity : O(n^2)\n\n\nint getMedian(vector<int> arr)\n{\n\tnth_element(arr.begin(), next(arr.begin(), \n\t\t(arr.s | aleksey12345 | NORMAL | 2020-06-20T11:31:18.231050+00:00 | 2020-06-20T11:31:18.231076+00:00 | 160 | false | Average time complexity : O(n)\nWorst time complexity : O(n^2)\n\n```\nint getMedian(vector<int> arr)\n{\n\tnth_element(arr.begin(), next(arr.begin(), \n\t\t(arr.size() - 1)/ 2), arr.end());\n\n\treturn arr[(arr.size() - 1)/ 2];\n}\n\nvector<int> getStrongest(vector<int>& arr, int k) \n{\n\tauto median = getMedian(arr)... | 2 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | Java Solution | java-solution-by-bhaveshan-1nw8 | \nclass Solution {\n \n public int[] getStrongest(int[] arr, int k) {\n \n int i = 0, j = arr.length - 1, l = 0;\n if (j == -1) retur | bhaveshan | NORMAL | 2020-06-16T13:23:51.405513+00:00 | 2020-06-16T13:23:51.405557+00:00 | 93 | false | ```\nclass Solution {\n \n public int[] getStrongest(int[] arr, int k) {\n \n int i = 0, j = arr.length - 1, l = 0;\n if (j == -1) return new int[0];\n Arrays.sort(arr);\n int median = arr[j / 2];\n int[] res = new int[k];\n while (l < k){\n if (median -... | 2 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | scala functional programming (anyone knows why scala is so slow on leetcode?) | scala-functional-programming-anyone-know-z80t | object Solution {\n def getStrongest(arr: Array[Int], k: Int): Array[Int] = {\n val sortArr = arr.sorted\n val len = arr.size\n val med | ls1229 | NORMAL | 2020-06-10T15:09:57.389412+00:00 | 2020-06-10T15:09:57.389449+00:00 | 57 | false | ```object Solution {\n def getStrongest(arr: Array[Int], k: Int): Array[Int] = {\n val sortArr = arr.sorted\n val len = arr.size\n val med = sortArr((len-1)/2)\n def helper(acc:Array[Int], k:Int, l:Int, r:Int):Array[Int] = \n if (k==0) acc\n else if(med-sortArr(l)>so... | 2 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Best Video Explanation in Hindi | Two Pointer Approach | best-video-explanation-in-hindi-two-poin-w5p8 | Link to the video - https://www.youtube.com/watch?v=mvbTN2I3HxY\n\nI found the best video explanation for the problem on CodingBeast youtube channel. I guess th | CodingBeastOfficial | NORMAL | 2020-06-08T22:41:57.765949+00:00 | 2020-06-11T16:53:06.824036+00:00 | 62 | false | Link to the video - https://www.youtube.com/watch?v=mvbTN2I3HxY\n\nI found the best video explanation for the problem on CodingBeast youtube channel. I guess the target audience are Indians as the language spoken in the video is Hindi. | 2 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | Java stream&sorting solution | java-streamsorting-solution-by-msmych-d1cx | java\nimport static java.lang.Math.*;\nimport static java.util.Arrays.*;\nimport static java.util.Comparator.*;\n\nclass Solution {\n public int[] getStronge | msmych | NORMAL | 2020-06-08T10:25:47.159350+00:00 | 2020-06-08T10:25:47.159383+00:00 | 182 | false | ```java\nimport static java.lang.Math.*;\nimport static java.util.Arrays.*;\nimport static java.util.Comparator.*;\n\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n sort(arr);\n var median = arr[(arr.length - 1) / 2];\n return stream(arr)\n .boxed() // need to box/... | 2 | 0 | ['Sorting', 'Java'] | 0 |
the-k-strongest-values-in-an-array | Python Sort using Lambda 100% Faster | python-sort-using-lambda-100-faster-by-t-7qpd | \nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n n = len(arr)\n m = arr[(n-1)//2] # Comp | tad_ko | NORMAL | 2020-06-07T05:10:48.815402+00:00 | 2020-06-07T05:11:04.630422+00:00 | 103 | false | ```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n n = len(arr)\n m = arr[(n-1)//2] # Compute median\n arr.sort(key=lambda x:(-abs(x-m),-x)) \n return arr[:k]\n``` | 2 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Javascript Sorting solution | javascript-sorting-solution-by-seriously-jv6s | \nvar getStrongest = function(arr, k) {\n arr.sort(function(a,b){return a-b});\n\t//find median\n var med = arr[Math.floor((arr.length-1)/2)];\n\t//sort u | seriously_ridhi | NORMAL | 2020-06-07T05:00:54.977503+00:00 | 2020-06-07T05:00:54.977539+00:00 | 97 | false | ```\nvar getStrongest = function(arr, k) {\n arr.sort(function(a,b){return a-b});\n\t//find median\n var med = arr[Math.floor((arr.length-1)/2)];\n\t//sort using median\n arr.sort(function(a,b){return Math.abs(a-med) - Math.abs(b-med)});\n\t//reverse the array and slice for K strongest values\n arr.reverse(... | 2 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | [Python] 2-pointers solution beats 90% | python-2-pointers-solution-beats-90-by-l-stld | python\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n N = len(arr)\n median = arr[(N-1)/ | luolingwei | NORMAL | 2020-06-07T04:28:14.520343+00:00 | 2020-06-07T04:28:42.456881+00:00 | 102 | false | ```python\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n N = len(arr)\n median = arr[(N-1)//2]\n i,j = 0,N-1\n res = []\n while i<=j and len(res)<k:\n if abs(arr[i]-median)>abs(arr[j]-median):\n res.ap... | 2 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | Python, using sorted, Custom Sort | python-using-sorted-custom-sort-by-akshi-bh9d | \n # Sort the array to find the median\n arr.sort()\n med = arr[(len(arr)-1 )//2 ]\n \n # Now sort the array in Descending or | akshit0699 | NORMAL | 2020-06-07T04:20:17.573319+00:00 | 2020-06-07T04:20:53.737983+00:00 | 190 | false | ```\n # Sort the array to find the median\n arr.sort()\n med = arr[(len(arr)-1 )//2 ]\n \n # Now sort the array in Descending order of values abs(num - med)\n # If two values are same, use the face value(num)\n\t\t# Choose only the first k values.\n return sorted(arr, ke... | 2 | 0 | ['Python'] | 1 |
the-k-strongest-values-in-an-array | Custom lambda sort O(N) amortized update | custom-lambda-sort-on-amortized-update-b-r0ej | \nint n = arr.size();\nnth_element(begin(arr), begin(arr) + (n-1)/2, end(arr));\nint m = arr[(n-1)/2];\nsort(begin(arr), begin(arr) + k, end(arr), [&m] (const a | gh05t | NORMAL | 2020-06-07T04:03:01.069715+00:00 | 2020-06-10T01:22:32.554205+00:00 | 266 | false | ```\nint n = arr.size();\nnth_element(begin(arr), begin(arr) + (n-1)/2, end(arr));\nint m = arr[(n-1)/2];\nsort(begin(arr), begin(arr) + k, end(arr), [&m] (const auto &i, const auto &j) {\n if(abs(m - i) != abs(m - j))\n return abs(m - i) > abs(m - j);\n else\n return i > j;\n});\nreturn {begin(arr)... | 2 | 0 | ['C'] | 0 |
the-k-strongest-values-in-an-array | [C++] Custom sort | c-custom-sort-by-zhanghuimeng-pzmq | Sort an array by the absolute difference of each element from the median.\n\n# Explanation\n\nUse a custom sort. The first key is |a[i] - median|, and the secon | zhanghuimeng | NORMAL | 2020-06-07T04:02:18.542371+00:00 | 2020-06-07T04:02:18.542415+00:00 | 103 | false | Sort an array by the absolute difference of each element from the median.\n\n# Explanation\n\nUse a custom sort. The first key is `|a[i] - median|`, and the second key is `a[i]`. The biggest `k` elements are the strongest ones.\n\nThe time complexity is `O(n*log(n))`.\n\n# C++ Solution\n\n```cpp\nclass Solution {\npubl... | 2 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | [Java] Sort + Two Pointers | java-sort-two-pointers-by-manrajsingh007-uxem | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n int n = arr.length;\n Arrays.sort(arr);\n int mid = (n - 1) / 2; | manrajsingh007 | NORMAL | 2020-06-07T04:01:12.037985+00:00 | 2020-06-07T04:01:12.038038+00:00 | 225 | false | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n int n = arr.length;\n Arrays.sort(arr);\n int mid = (n - 1) / 2;\n int left = 0, right = n - 1, count = 0;\n int[] ans = new int[k];\n int it = 0;\n while(count < k && left <= right) {\n ... | 2 | 1 | [] | 1 |
the-k-strongest-values-in-an-array | Java Solution | java-solution-by-dsuryaprakash89-vmj8 | 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 | dsuryaprakash89 | NORMAL | 2024-11-27T07:05:20.240202+00:00 | 2024-11-27T07:05:20.240224+00:00 | 56 | 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:O(N log N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e... | 1 | 0 | ['Array', 'Sorting', 'Heap (Priority Queue)', 'Java'] | 0 |
the-k-strongest-values-in-an-array | Shortest, Easiest & Well Explained || To the Point & Beginners Friendly Approach (❤️ ω ❤️) | shortest-easiest-well-explained-to-the-p-n8of | Welcome to My Coding Family\u30FE(\u2267 \u25BD \u2266)\u309D\nThis is my shortest, easiest & to the point approach. This solution mainly aims for purely beginn | Nitansh_Koshta | NORMAL | 2023-12-27T14:33:49.089870+00:00 | 2023-12-27T14:33:49.089895+00:00 | 3 | false | # Welcome to My Coding Family\u30FE(\u2267 \u25BD \u2266)\u309D\n*This is my shortest, easiest & to the point approach. This solution mainly aims for purely beginners so if you\'re new here then too you\'ll be very easily be able to understand my this code. If you like my code then make sure to UpVOTE me. Let\'s start^... | 1 | 0 | ['C++'] | 0 |
the-k-strongest-values-in-an-array | Simple Python Solution | simple-python-solution-by-djdjned-8cjj | Intuition\n Describe your first thoughts on how to solve this problem. \nThink of the median as a balancing point in the array. Elements on one side of the medi | djdjned | NORMAL | 2023-08-07T12:24:46.566991+00:00 | 2023-08-08T12:48:45.683192+00:00 | 29 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThink of the median as a balancing point in the array. Elements on one side of the median have maximum element than the other side. Stronger elements are those that are farther away from the median, causing an imbalance.\n\n# Approach\n<!... | 1 | 0 | ['Array', 'Two Pointers', 'Sorting', 'Python3'] | 0 |
the-k-strongest-values-in-an-array | c# : Easy Solution | c-easy-solution-by-rahul89798-esf9 | Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n\npublic class Solution {\n public int[] GetStrongest(int[] arr, int k) {\n | rahul89798 | NORMAL | 2023-06-04T07:54:11.723651+00:00 | 2023-06-04T07:54:11.723703+00:00 | 17 | false | # Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\npublic class Solution {\n public int[] GetStrongest(int[] arr, int k) {\n Array.Sort(arr);\n var ans = new int[k];\n var median = arr[(arr.Length - 1) / 2];\n\n Array.Sort(arr, (a, b) => {\n var... | 1 | 0 | ['Sorting', 'C#'] | 0 |
the-k-strongest-values-in-an-array | USING HEAP. | using-heap-by-heythereguys-fgca | Intuition\nWeight=abs(arr[i]-m), where m is median.\nIn this problem we need to return the vector ans . Giving the biggest elements having the biggest weight.\n | HeyThereGuys | NORMAL | 2023-05-31T10:03:46.830065+00:00 | 2023-05-31T10:03:46.830097+00:00 | 16 | false | # Intuition\nWeight=abs(arr[i]-m), where m is median.\nIn this problem we need to return the vector ans . Giving the biggest elements having the biggest weight.\nMeans if an element have a bigger weight u have to store that element. But if u have a clash with element having equal weights , bigger element wins. \n\n# Ap... | 1 | 0 | ['Hash Table', 'Heap (Priority Queue)', 'C++'] | 1 |
the-k-strongest-values-in-an-array | C | c-by-tinachien-eyi6 | \nint cmp(const void* a, const void* b){\n return *(int*)a - *(int*)b;\n}\nint* getStrongest(int* arr, int arrSize, int k, int* returnSize){\n qsort(arr, | TinaChien | NORMAL | 2022-11-30T12:14:59.493185+00:00 | 2022-11-30T12:14:59.493228+00:00 | 84 | false | ```\nint cmp(const void* a, const void* b){\n return *(int*)a - *(int*)b;\n}\nint* getStrongest(int* arr, int arrSize, int k, int* returnSize){\n qsort(arr, arrSize, sizeof(int), cmp);\n int median = arr[(arrSize-1)/2];\n int* ans = malloc(k * sizeof(int));\n int left = 0, right = arrSize-1;\n for(in... | 1 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | C++ Easy 2 Pointers | c-easy-2-pointers-by-decimalx-oi6q | C++ Simple and easy\n\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(), arr.end());\n int me | decimalx | NORMAL | 2022-07-18T02:01:45.521242+00:00 | 2022-07-18T02:01:45.521288+00:00 | 215 | false | C++ Simple and easy\n```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(), arr.end());\n int median = arr[(arr.size() - 1) / 2];\n vector<int> res;\n int left = 0, right = arr.size() - 1;\n while(left <= right && k--){\n ... | 1 | 0 | ['Two Pointers', 'C', 'Sorting'] | 0 |
the-k-strongest-values-in-an-array | C++ Two Pointer + Sorting | c-two-pointer-sorting-by-user7710z-hp0z | \nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(),arr.end());\n int n=arr.size();\n i | user7710z | NORMAL | 2022-06-10T12:17:05.916164+00:00 | 2022-06-10T12:17:05.916194+00:00 | 150 | false | ```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(),arr.end());\n int n=arr.size();\n int m=arr[(n-1)/2];\n vector<int>ans;\n int i=0,j=n-1;\n int sz=0;\n while(i<=j and sz<k){\n if(m-arr[i]>arr[j]-m){\n ... | 1 | 0 | ['Two Pointers', 'C', 'Sorting'] | 0 |
the-k-strongest-values-in-an-array | javascript easy understanding | javascript-easy-understanding-by-sherif-o9afs | \nvar getStrongest = function(arr, k) { \n\t// sort array so we can easily find median\n const sorted = arr.sort((a,b) => a-b)\n\t// get index of median\n | sherif-ffs | NORMAL | 2022-06-06T03:20:46.524178+00:00 | 2022-06-06T03:20:46.524226+00:00 | 88 | false | ```\nvar getStrongest = function(arr, k) { \n\t// sort array so we can easily find median\n const sorted = arr.sort((a,b) => a-b)\n\t// get index of median\n\tconst medianIndex = Math.floor(((sorted.length-1)/2))\n\t// get median\n const median = sorted[medianIndex]\n\n\t// custom sort function following the p... | 1 | 0 | ['JavaScript'] | 0 |
the-k-strongest-values-in-an-array | easyyyy | easyyyy-by-aavii-gko8 | ```\nclass Solution {\npublic:\n vector getStrongest(vector& arr, int k) {\n int n = arr.size();\n sort(arr.begin(), arr.end());\n int m | aavii | NORMAL | 2022-06-01T12:39:52.570521+00:00 | 2022-06-01T12:40:13.738484+00:00 | 99 | false | ```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n int n = arr.size();\n sort(arr.begin(), arr.end());\n int med = arr[(n-1)/2];\n int left = 0, right = arr.size()-1;\n \n k = min(k,(int) arr.size());\n vector<int> res;\n whi... | 1 | 0 | ['Two Pointers', 'C'] | 0 |
the-k-strongest-values-in-an-array | Custom Sorting || PAIRS || EASY || CPP | custom-sorting-pairs-easy-cpp-by-peerona-hbe3 | \nclass Solution {\npublic:\n static bool comp(pair<int,int> &A,pair<int,int> &B){\n if(A.first>B.first) return true;\n else if(A.first==B.firs | PeeroNappper | NORMAL | 2022-05-03T09:24:45.986852+00:00 | 2022-05-03T09:24:45.986895+00:00 | 68 | false | ```\nclass Solution {\npublic:\n static bool comp(pair<int,int> &A,pair<int,int> &B){\n if(A.first>B.first) return true;\n else if(A.first==B.first) return A.second>B.second;\n return false;\n }\n vector<int> getStrongest(vector<int>& arr, int k) {\n vector<int> P=arr;\n sort... | 1 | 0 | ['Sorting', 'C++'] | 0 |
the-k-strongest-values-in-an-array | C++ | Custom Sort | c-custom-sort-by-__struggler-4gk1 | \nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(),arr.end());\n int n=arr.size();\n i | __struggler__ | NORMAL | 2022-04-08T14:18:29.930310+00:00 | 2022-04-08T14:18:29.930351+00:00 | 38 | false | ```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(),arr.end());\n int n=arr.size();\n int m=arr[(n-1)/2];\n sort( arr.begin( ), arr.end( ), [m]( const auto& x1, const auto& x2 )\n{\n return abs(x1-m) == abs(x2-m) ? x1 > x2 : abs(x1-m) ... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | C++ Solution -Using Priority Queue | c-solution-using-priority-queue-by-sakth-m9ot | ```\nclass Solution {\npublic:\n struct compare{\n bool operator()(pair p1,pair p2){\n if (p1.first==p2.first){\n return p1. | sakthi37 | NORMAL | 2021-08-17T12:09:06.829549+00:00 | 2021-08-17T12:09:06.829596+00:00 | 69 | false | ```\nclass Solution {\npublic:\n struct compare{\n bool operator()(pair<int,int> p1,pair<int,int> p2){\n if (p1.first==p2.first){\n return p1.second<p2.second;\n }\n else{\n return p1.first<p2.first;\n }\n }\n };\n vector<i... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | [C++] : O(nlog(n)) : Sorting | c-onlogn-sorting-by-zanhd-himo | \n\tvector<int> getStrongest(vector<int>& a, int k) \n {\n int n = a.size();\n sort(a.begin(),a.end());\n int m = a[(n - 1) / 2];\n | zanhd | NORMAL | 2021-08-16T11:06:32.406171+00:00 | 2021-08-16T11:06:32.406209+00:00 | 65 | false | ```\n\tvector<int> getStrongest(vector<int>& a, int k) \n {\n int n = a.size();\n sort(a.begin(),a.end());\n int m = a[(n - 1) / 2];\n \n vector<pair<int,int>> b;\n for(auto x : a)\n b.push_back({abs(x - m),x});\n sort(b.begin(),b.end(),greater<pair<int,int... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | C++ || Two Pointer Approach | c-two-pointer-approach-by-agautam992-oysd | \nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) \n {\n sort(arr.begin(),arr.end());\n int m = arr[(arr.size( | agautam992 | NORMAL | 2021-06-16T10:33:08.580724+00:00 | 2021-06-16T10:33:08.580767+00:00 | 121 | false | ```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) \n {\n sort(arr.begin(),arr.end());\n int m = arr[(arr.size()-1)/2];\n int i=0,j=arr.size()-1;\n vector<int >res;\n for(;i<=j;)\n {\n if(abs(arr[j]-m)>abs(arr[i]-m))\n ... | 1 | 0 | ['C', 'C++'] | 0 |
the-k-strongest-values-in-an-array | Easy to understand Java Solution. Two pointer approach. | easy-to-understand-java-solution-two-poi-7ki2 | \nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n \n int first=0;\n\t\tint last=arr.length-1;\n\t\t\n\t\tint retStrongest[]= | Dynamic_Suvam | NORMAL | 2021-06-06T19:45:51.477014+00:00 | 2021-06-06T19:45:51.477040+00:00 | 55 | false | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n \n int first=0;\n\t\tint last=arr.length-1;\n\t\t\n\t\tint retStrongest[]=new int[k];\n\t\tint index=0;\n\t\t\n\t\tArrays.sort(arr);\n\t\t\n\t int median=arr[(arr.length-1)/2];\n\t \n\t \n\t \n\t for(int i=1;i<=k;i++)\n\t... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | c++ solution || easy || explained in comments | c-solution-easy-explained-in-comments-by-71ix | \nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n\t//the key logic is geting as farthest element as possible from median\n | avyakt | NORMAL | 2021-05-23T19:55:14.480244+00:00 | 2021-05-23T19:55:14.480271+00:00 | 79 | false | ```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n\t//the key logic is geting as farthest element as possible from median\n int n = arr.size();\n sort(arr.begin(), arr.end());\n int median = arr[(n-1)/2];\n vector<int> ans;\n int i = 0, j = n-1;\... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | C++ using nth_element | c-using-nth_element-by-chirags_30-i0bo | \nclass Solution {\npublic:\n int findMedian(vector<int> a, int n) \n { \n \n if (n % 2 == 0) \n { \n nth_element( a | chirags_30 | NORMAL | 2021-05-14T17:59:02.975123+00:00 | 2021-05-14T17:59:02.975149+00:00 | 72 | false | ```\nclass Solution {\npublic:\n int findMedian(vector<int> a, int n) \n { \n \n if (n % 2 == 0) \n { \n nth_element( a.begin() , a.begin() + (n-1)/2 , a.end() );\n \n return ( a[(n - 1) / 2] ); \n }\n else\n { \n // Applying... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | using STL | using-stl-by-ranjan_1997-4zho | \nclass Solution {\npublic:\n int median = 0;\n \n vector<int> getStrongest(vector<int>& arr, int k) {\n int n = arr.size();\n sort(arr.beg | ranjan_1997 | NORMAL | 2021-05-14T17:58:35.550898+00:00 | 2021-05-14T17:58:35.550944+00:00 | 52 | false | ```\nclass Solution {\npublic:\n int median = 0;\n \n vector<int> getStrongest(vector<int>& arr, int k) {\n int n = arr.size();\n sort(arr.begin(),arr.end());\n median = arr[(n-1)/2];\n vector<pair<int,int>>pq;\n for(int i = 0;i<arr.size();i++)\n {\n pq.push... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Python 2 pointers | python-2-pointers-by-rehasantiago-i5ie | \nclass Solution(object):\n def getStrongest(self, arr, k):\n arr.sort()\n m = (len(arr)-1)/2\n i=0\n j=len(arr)-1\n res = | rehasantiago | NORMAL | 2021-04-11T11:53:09.681734+00:00 | 2021-04-11T11:53:09.681764+00:00 | 104 | false | ```\nclass Solution(object):\n def getStrongest(self, arr, k):\n arr.sort()\n m = (len(arr)-1)/2\n i=0\n j=len(arr)-1\n res = []\n while(k>0):\n if(abs(arr[m]-arr[i])>abs(arr[m]-arr[j])):\n res.append(arr[i])\n i+=1\n k... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Python 5-line solution using sort with comments | python-5-line-solution-using-sort-with-c-bux6 | class Solution:\n\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort(reverse = True) # sort the arr in descending order, this w | flyingspa | NORMAL | 2021-03-23T11:56:36.557731+00:00 | 2021-03-23T11:56:36.557770+00:00 | 153 | false | class Solution:\n\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort(reverse = True) # sort the arr in descending order, this will put the larger number at first if they have same distance to the median\n m_index = len(arr) - (len(arr)-1)//2 - 1 # median index in reversely sorted... | 1 | 0 | ['Sorting', 'Python'] | 0 |
the-k-strongest-values-in-an-array | Java solution - easy to understand | java-solution-easy-to-understand-by-akir-wgfy | \nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int n = arr.length;\n int median = arr[(n - 1) | akiramonster | NORMAL | 2021-03-04T03:55:39.644815+00:00 | 2021-03-04T03:55:39.644883+00:00 | 104 | false | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int n = arr.length;\n int median = arr[(n - 1) / 2];\n\n int[] res = new int[k];\n int i = 0, l = 0, r = n - 1;\n while (i < k) {\n int left = Math.abs(arr[l] - median);\n ... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | C++ solution beats 95% speed | c-solution-beats-95-speed-by-pbabbar-3s7t | \nvector<int> getStrongest(vector<int>& arr, int k) {\n if(arr.size()<2) return arr;\n sort(arr.begin(),arr.end());\n int med=arr[(arr.size | pbabbar | NORMAL | 2021-02-26T16:24:11.805202+00:00 | 2021-02-26T16:24:11.805243+00:00 | 103 | false | ```\nvector<int> getStrongest(vector<int>& arr, int k) {\n if(arr.size()<2) return arr;\n sort(arr.begin(),arr.end());\n int med=arr[(arr.size()-1)/2];\n int left=0,right=arr.size()-1;\n vector<int> answer;\n while(k>0){\n int val_l=abs(arr[left]-med);\n i... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | python - 2 lines | python-2-lines-by-mateosz-76rv | \nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n m = sorted(arr)[(len(arr)-1)//2]\n return sorted(arr, key=la | mateosz | NORMAL | 2020-12-12T12:14:50.772017+00:00 | 2020-12-12T12:14:50.772045+00:00 | 87 | false | ```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n m = sorted(arr)[(len(arr)-1)//2]\n return sorted(arr, key=lambda x: (abs(x-m), x))[-k:]\n``` | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Python with multiple IF ELSE | Faster than 72% | python-with-multiple-if-else-faster-than-xnsy | \nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n\n if len(arr) == 1: return arr\n\n resul | annnguyen | NORMAL | 2020-10-25T10:35:41.222904+00:00 | 2020-10-25T10:35:41.222935+00:00 | 69 | false | ```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n\n if len(arr) == 1: return arr\n\n result_ = []\n num = 0\n i = 0\n j = len(arr) - 1\n m = arr[int((len(arr)-1)/2)]\n new_arr = [abs(x - m) for x in arr]\n\n ... | 1 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | python O(nlogn), sort | python-onlogn-sort-by-landsurveyork-iy4a | \nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n n = len(arr)\n m = (n - 1) // 2\n sort_arr = sorted(a | landsurveyork | NORMAL | 2020-10-25T03:03:41.138712+00:00 | 2020-10-25T03:03:41.138744+00:00 | 149 | false | ```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n n = len(arr)\n m = (n - 1) // 2\n sort_arr = sorted(arr)\n med = sort_arr[m]\n \n A = {}\n for x in arr:\n dist = abs(x - med)\n if dist in A:\n ... | 1 | 0 | ['Python'] | 0 |
the-k-strongest-values-in-an-array | Java - better than 92%, Clean solution | java-better-than-92-clean-solution-by-do-cb5l | \npublic int[] getStrongest(int[] arr, int k) {\n int n = arr.length;\n Arrays.sort(arr);\n int median = arr[((n - 1) / 2)];\n \n | dorelb72 | NORMAL | 2020-09-21T16:23:41.573717+00:00 | 2020-09-21T16:23:41.573752+00:00 | 370 | false | ```\npublic int[] getStrongest(int[] arr, int k) {\n int n = arr.length;\n Arrays.sort(arr);\n int median = arr[((n - 1) / 2)];\n \n int[] output = new int[k];\n int index = 0;\n int l = 0, r = n - 1;\n \n while (index < k) {\n int dist1 = Math.a... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Simple Python Solution | simple-python-solution-by-whissely-x80s | \nclass Solution:\n # Time: O(n*log(n))\n # Space: O(n)\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n med | whissely | NORMAL | 2020-09-15T04:55:40.338575+00:00 | 2020-09-15T04:55:40.338631+00:00 | 170 | false | ```\nclass Solution:\n # Time: O(n*log(n))\n # Space: O(n)\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n median = arr[(len(arr) - 1)//2]\n res, i, j = [], 0, len(arr) - 1\n while i <= j and k:\n if abs(arr[i] - median) > abs(arr[j] - median... | 1 | 0 | ['Sorting', 'Python'] | 0 |
the-k-strongest-values-in-an-array | python3 2 pointers | python3-2-pointers-by-harshalkpd93-xiqo | ```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n n = len(arr)\n m = arr[(n-1)//2]\n | harshalkpd93 | NORMAL | 2020-09-06T01:35:48.961970+00:00 | 2020-09-06T01:35:48.962029+00:00 | 53 | false | ```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n n = len(arr)\n m = arr[(n-1)//2]\n strong = []\n # for i in range(len(arr)):\n i,j = 0, len(arr)-1\n while i<=j:\n if abs(arr[i]-m) <= abs(arr[j]-m):\n ... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | C++ Simple Sort Solution | c-simple-sort-solution-by-rom111-qyzs | \nclass Solution {\npublic:\n static int m;\n static bool compare(int a,int b){\n if(abs(a-m)>abs(b-m)){\n return true;\n }else i | rom111 | NORMAL | 2020-09-01T12:18:24.324864+00:00 | 2020-09-01T12:18:24.324916+00:00 | 127 | false | ```\nclass Solution {\npublic:\n static int m;\n static bool compare(int a,int b){\n if(abs(a-m)>abs(b-m)){\n return true;\n }else if(abs(a-m)==abs(b-m)){\n if(a>b){\n return true;\n }\n }\n return 0;\n }\n vector<int> getStrongest(... | 1 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | [C++] simple sort function using comparator | c-simple-sort-function-using-comparator-ravyg | c++\nint m;\nbool comp(int a,int b){\n int a1=abs(a-m),a2=abs(b-m);\n if(a1>a2)return true;\n else if(a2>a1)return false;\n return a>b;\n}\nclass So | suhailakhtar039 | NORMAL | 2020-09-01T07:56:16.313717+00:00 | 2020-09-01T07:56:16.313766+00:00 | 134 | false | ```c++\nint m;\nbool comp(int a,int b){\n int a1=abs(a-m),a2=abs(b-m);\n if(a1>a2)return true;\n else if(a2>a1)return false;\n return a>b;\n}\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n sort(arr.begin(),arr.end());\n int n=arr.size();\n m=arr[(n... | 1 | 0 | ['C', 'Sorting', 'C++'] | 0 |
the-k-strongest-values-in-an-array | O(Nlog(N)) JAVA solution! | onlogn-java-solution-by-lcq_dev-vbi6 | \nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int[] res = new int[k];\n int[] nums = | lcq_dev | NORMAL | 2020-07-25T21:36:09.540873+00:00 | 2020-07-25T21:36:09.540907+00:00 | 120 | false | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int[] res = new int[k];\n int[] nums = arr;\n int mid_index = (nums.length-1)/2;\n int median = arr[mid_index];\n \n int i = 0;\n int j = nums.le... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Java TreeMap | java-treemap-by-mayankbansal-gnjy | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n int[] c=new int[arr.length];\n for(int i=0;i<arr.length;i++){\n | mayankbansal | NORMAL | 2020-07-20T06:05:59.777612+00:00 | 2020-07-20T06:05:59.777663+00:00 | 73 | false | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n int[] c=new int[arr.length];\n for(int i=0;i<arr.length;i++){\n c[i]=arr[i];\n }\n Arrays.sort(arr);\n int med=arr[(arr.length-1)/2];\n TreeMap<Integer,ArrayList<Integer>> map=new TreeMap<>();... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Python3 93.52% (1052 ms)/100.00% (27.4 MB) | python3-9352-1052-ms10000-274-mb-by-numi-egf2 | \nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n median = arr[len(arr) // 2] if len(arr) % 2 els | numiek_p | NORMAL | 2020-06-23T12:28:07.469043+00:00 | 2020-06-23T12:28:07.469091+00:00 | 99 | false | ```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr.sort()\n median = arr[len(arr) // 2] if len(arr) % 2 else arr[len(arr) // 2 - 1]\n left = 0\n right = len(arr) - 1\n ret = []\n \n \n while (len(ret) != k):\n l... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | two pointers | two-pointers-by-fengyindiehun-z6rp | \nvector<int> getStrongest(vector<int>& arr, int k) {\n vector<int> ans;\n int n = arr.size();\n sort(arr.begin(), arr.end());\n int m_idx = (n - 1) | fengyindiehun | NORMAL | 2020-06-23T10:50:27.591151+00:00 | 2020-06-23T10:50:27.591196+00:00 | 67 | false | ```\nvector<int> getStrongest(vector<int>& arr, int k) {\n vector<int> ans;\n int n = arr.size();\n sort(arr.begin(), arr.end());\n int m_idx = (n - 1) >> 1;\n int m = arr[m_idx];\n int i = 0;\n int j = n-1;\n while (k--) {\n if (m - arr[i] < arr[j] - m) {\n ans.push_back(arr[j... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | C++ O(N) solution 99% with explanation | c-on-solution-99-with-explanation-by-guc-2sbd | Because we need kth greatest elements, in no particular order, we can simply use selection algorithms (such as quickselect, implemented as nth_element) to solve | guccigang | NORMAL | 2020-06-13T01:18:25.497034+00:00 | 2020-06-13T01:19:18.568503+00:00 | 180 | false | Because we need `k`th greatest elements, in no particular order, we can simply use selection algorithms (such as quickselect, implemented as `nth_element`) to solve the problem in linear time. We first calculate the median using a selection algorithm which is `O(N)`. Then, we apply the selection algorithm again to find... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Python3, simple solution O(nlogn) for sorting + O(k) for finding the result | python3-simple-solution-onlogn-for-sorti-iidp | \ndef getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr = sorted(arr)\n medianIndex = (len(arr) - 1) // 2\n median = arr[media | sraghav2 | NORMAL | 2020-06-10T09:22:18.680681+00:00 | 2020-06-10T09:22:18.680709+00:00 | 63 | false | ```\ndef getStrongest(self, arr: List[int], k: int) -> List[int]:\n arr = sorted(arr)\n medianIndex = (len(arr) - 1) // 2\n median = arr[medianIndex]\n \n result = []\n backPointer = -1\n frontPointer = 0\n for i in range(k):\n if(abs(arr[backPointer] -... | 1 | 0 | [] | 1 |
the-k-strongest-values-in-an-array | c++ solution using custom sorting. | c-solution-using-custom-sorting-by-rajuj-s73c | \nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n \n int len = arr.size();\n sort(arr.begin(), arr.end | rajujnvgupta | NORMAL | 2020-06-09T12:48:46.583635+00:00 | 2020-06-09T12:49:13.810333+00:00 | 88 | false | ```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n \n int len = arr.size();\n sort(arr.begin(), arr.end()); // sort to get median\n int idx = (len-1)/2;\n int median = arr[idx];\n \n auto cmp = [&](int a, int b){ // lambda functio... | 1 | 1 | ['C', 'Sorting'] | 0 |
the-k-strongest-values-in-an-array | How random quickselect should be implemented and why | how-random-quickselect-should-be-impleme-fi3i | Lots of folks without the benefit of nth_element from STL may be wondering how one could implement random quickselect by hand. Random quickselect is very easy t | algomelon | NORMAL | 2020-06-09T02:38:16.783372+00:00 | 2020-06-09T02:53:55.735246+00:00 | 186 | false | Lots of folks without the benefit of nth_element from STL may be wondering how one could implement random quickselect by hand. Random quickselect is very easy to implement once you understand the subtleties but it can behave badly on some inputs. (There is a far more complicated worst-case linear-time selection algorit... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Java Solution - Quick Select | java-solution-quick-select-by-mycafebabe-ipmk | \nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n int n = arr.length;\n int median = findKthLargest(arr, 0, n - 1, n / 2 + | mycafebabe | NORMAL | 2020-06-08T06:53:58.448113+00:00 | 2020-06-08T06:53:58.448162+00:00 | 100 | false | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n int n = arr.length;\n int median = findKthLargest(arr, 0, n - 1, n / 2 + 1);\n int idx = helper(arr, 0, n - 1, k, median);\n int[] ans = new int[k];\n for (int i = 0; i < k; i++) {\n ans[i] = arr[i]... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | C++ [Easy] | c-easy-by-pal_96-47j1 | \nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n if(arr.size() == 1) return arr;\n sort(arr.begin(), arr.end | pal_96 | NORMAL | 2020-06-08T02:56:01.175788+00:00 | 2020-06-08T02:56:01.175825+00:00 | 38 | false | ```\nclass Solution {\npublic:\n vector<int> getStrongest(vector<int>& arr, int k) {\n if(arr.size() == 1) return arr;\n sort(arr.begin(), arr.end());\n vector<int> ans;\n int len = arr.size();\n int medPos = (len-1)/2;\n int med = arr[medPos];\n vector< pair<int, int... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Short Python Time O(nlogn) | short-python-time-onlogn-by-yuchen_peng-pzp8 | \nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n if k == 0 or not arr:\n return []\n \n n = | yuchen_peng | NORMAL | 2020-06-07T15:05:06.859840+00:00 | 2020-06-07T15:05:06.859889+00:00 | 47 | false | ```\nclass Solution:\n def getStrongest(self, arr: List[int], k: int) -> List[int]:\n if k == 0 or not arr:\n return []\n \n n = len(arr)\n arr.sort()\n median = arr[(n - 1) // 2]\n \n abs_arr = sorted(arr, key=lambda x: (abs(x - median), x), reverse=True)\... | 1 | 0 | [] | 2 |
the-k-strongest-values-in-an-array | C++ code : using a MULTIMAP. | c-code-using-a-multimap-by-xxdil-ya1m | \nclass Solution {\npublic: \n vector<int> getStrongest(vector<int>& arr, int k) \n {\n sort(arr.begin(), arr.end());\n int n = arr.size( | xxdil | NORMAL | 2020-06-07T08:48:54.306746+00:00 | 2020-07-14T11:32:39.749225+00:00 | 50 | false | ```\nclass Solution {\npublic: \n vector<int> getStrongest(vector<int>& arr, int k) \n {\n sort(arr.begin(), arr.end());\n int n = arr.size();\n int m = (n - 1) / 2;\n \n multimap<int, int> M;\n int c=0;\n for(int i : arr)\n {\n M.insert({abs(i... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | Swift Two Pointers | swift-two-pointers-by-achoas-0qqp | \nclass Solution { \n func getStrongest(_ arr: [Int], _ k: Int) -> [Int] {\n let sorted = arr.sorted()\n let mIndex = (arr.count - 1) / 2\n | achoas | NORMAL | 2020-06-07T08:48:10.358030+00:00 | 2020-06-07T08:48:41.749876+00:00 | 70 | false | ```\nclass Solution { \n func getStrongest(_ arr: [Int], _ k: Int) -> [Int] {\n let sorted = arr.sorted()\n let mIndex = (arr.count - 1) / 2\n let m = sorted[mIndex]\n var p1 = 0\n var p2 = arr.count - 1\n \n var rest = [Int]()\n while rest.count < k {\n ... | 1 | 0 | [] | 0 |
the-k-strongest-values-in-an-array | lazy java solution | lazy-java-solution-by-trustno1-qo34 | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int n = arr.length, mid = arr[(n - 1) / 2];\n | trustno1 | NORMAL | 2020-06-07T07:46:39.698062+00:00 | 2020-06-07T07:46:39.698101+00:00 | 91 | false | ```\nclass Solution {\n public int[] getStrongest(int[] arr, int k) {\n Arrays.sort(arr);\n int n = arr.length, mid = arr[(n - 1) / 2];\n int[][] tmp = new int[n][2];\n for(int i = 0; i < n; i++)\n tmp[i] = new int[]{Math.abs(arr[i] - mid), i};\n Arrays.sort(tmp, (a, b) ... | 1 | 0 | [] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.