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
sort-an-array
JavaScript MergeSort Solution
javascript-mergesort-solution-by-justbig-6j6f
\nconst sortArray = nums => {\n if(nums.length <= 1) return nums\n \n const middle = Math.floor(nums.length / 2)\n const left = nums.slice(0, middle
justbigmack
NORMAL
2020-05-30T14:43:26.017553+00:00
2020-05-30T14:45:52.321909+00:00
1,982
false
```\nconst sortArray = nums => {\n if(nums.length <= 1) return nums\n \n const middle = Math.floor(nums.length / 2)\n const left = nums.slice(0, middle)\n const right = nums.slice(middle)\n \n return merge(sortArray(left), sortArray(right))\n};\n\nconst merge = (left, right) => {\n const result ...
13
0
['Merge Sort', 'JavaScript']
8
sort-an-array
Merge sort - Java
merge-sort-java-by-rakesh_v-sxtx
```\n public int[] sortArray(int[] nums) {\n mergesort(nums, 0, nums.length-1);\n return nums;\n }\n\t\n public void mergesort(int[] nums, int
Rakesh_v
NORMAL
2021-04-11T15:45:20.473047+00:00
2022-04-28T02:37:36.509067+00:00
2,201
false
```\n public int[] sortArray(int[] nums) {\n mergesort(nums, 0, nums.length-1);\n return nums;\n }\n\t\n public void mergesort(int[] nums, int start, int end){\n if(start < end){\n int mid = (start + end) / 2;\n mergesort(nums, start, mid);\n mergesort(nums, mid+...
12
0
['Merge Sort', 'Java']
3
sort-an-array
Javascript Quick Sort faster than 95%
javascript-quick-sort-faster-than-95-by-02zi5
\tvar sortArray = function(nums) {\n\t\tlet len = nums.length;\n\t\tif(len < 2) return nums;\n\n\t\tquickSort(nums, 0, len-1)\n\t\treturn nums\n\t};\n\n\tvar qu
sindyxin
NORMAL
2020-06-05T00:00:56.570667+00:00
2020-06-05T00:00:56.570714+00:00
2,106
false
\tvar sortArray = function(nums) {\n\t\tlet len = nums.length;\n\t\tif(len < 2) return nums;\n\n\t\tquickSort(nums, 0, len-1)\n\t\treturn nums\n\t};\n\n\tvar quickSort = function(nums, start, end){\n\t\tif(start >= end) return\n\t\tlet left = start, right = end;\n\t\tlet pivot = nums[Math.floor((start+end) / 2)];\n\t\t...
12
0
['Sorting', 'JavaScript']
2
sort-an-array
Bottom-up iterative merge sort in Python
bottom-up-iterative-merge-sort-in-python-njpe
\nclass Solution(object):\n def sortArray(self, nums):\n """\n :type nums: List[int]\n :rtype: List[int]\n """\n prev = [[
fluffycoder
NORMAL
2019-12-20T06:26:08.022772+00:00
2019-12-20T06:26:08.022825+00:00
977
false
```\nclass Solution(object):\n def sortArray(self, nums):\n """\n :type nums: List[int]\n :rtype: List[int]\n """\n prev = [[n] for n in nums]\n while len(prev) > 1:\n cur = []\n for i in range(0, len(prev), 2):\n cur.append(self.merge(pr...
12
1
['Merge Sort']
3
sort-an-array
Quicksort Java
quicksort-java-by-deleted_user-y3ed
java\nint[] A;\nint[] sortArray(int[] A) {\n\tthis.A = A;\n\tsort(0, A.length - 1);\n\treturn A;\n}\nvoid sort(int l, int r) {\n\tif (l >= r) return;\n\tint p =
deleted_user
NORMAL
2019-04-25T23:10:33.131867+00:00
2019-04-25T23:10:33.131899+00:00
36,706
false
```java\nint[] A;\nint[] sortArray(int[] A) {\n\tthis.A = A;\n\tsort(0, A.length - 1);\n\treturn A;\n}\nvoid sort(int l, int r) {\n\tif (l >= r) return;\n\tint p = part(l, r);\n\tsort(l, p - 1);\n\tsort(p + 1, r);\n}\nint part(int l, int r) {\n\tint p = A[r];\n\tint i = l - 1;\n\tfor (int j = i + 1; j < r; ++j)\n\t\tif...
12
0
[]
5
sort-an-array
Java. Quick Sort.
java-quick-sort-by-chibug-com5
\n public int[] sortArray(int[] nums) {\n \tQuickSort(nums, 0, nums.length - 1);\n \tint result[] = new int[nums.length]; \n for (int i = 0; i <
chibug
NORMAL
2020-03-30T15:51:15.653692+00:00
2020-03-30T15:51:15.653722+00:00
2,751
false
```\n public int[] sortArray(int[] nums) {\n \tQuickSort(nums, 0, nums.length - 1);\n \tint result[] = new int[nums.length]; \n for (int i = 0; i < nums.length; i++) {\n \tresult[i] = nums[i];\n }\n return result;\n }\n \n public static void QuickSort(int nums[], int lhs, i...
11
0
[]
1
sort-an-array
SORTING USING MERGE SORT ( C++ ) :)
sorting-using-merge-sort-c-by-js0657096-solo
Intuition\n Describe your first thoughts on how to solve this problem. \nin the Given Problem , We have to sort an array.\nTo Sort an array these are the most c
js0657096
NORMAL
2023-03-06T09:41:12.798694+00:00
2023-03-06T09:42:19.914725+00:00
1,613
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nin the Given Problem , We have to sort an array.\nTo Sort an array these are the most common algorithms : \n\n(1) Bubble Sort\n--------------------------------------------------------------\n- Approach : switches neighbouring parts freque...
10
0
['Sorting', 'Merge Sort', 'C++']
2
sort-an-array
Accepted Python Quick Sort solution | Beats 93%
accepted-python-quick-sort-solution-beat-8r5t
Have to compromise with space to knock off the leetcode acceptance. \nTime: O(NlogN)\nSpace: O(N^2)\n\n\nclass Solution:\n def sortArray(self, nums: List[int
subconciousCoder
NORMAL
2022-12-14T03:33:59.729851+00:00
2022-12-14T03:51:56.598560+00:00
2,528
false
Have to compromise with space to knock off the leetcode acceptance. \nTime: O(NlogN)\nSpace: O(N^2)\n\n```\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]:\n def quicksort(nums):\n if len(nums) <= 1: return nums\n \n #picking a random pivot\n pi...
10
0
['Sorting', 'Python', 'Python3']
5
sort-an-array
C++ || Easy Merge Sort
c-easy-merge-sort-by-suniti0804-9m6p
\n \n void merge(vector& nums, int l, int m, int r)\n {\n int n1 = m - l + 1;\n int n2 = r - m;\n int A[n1], B[n2];\n \n
suniti0804
NORMAL
2021-04-16T06:12:45.936751+00:00
2021-04-16T06:12:45.936779+00:00
2,006
false
\n \n void merge(vector<int>& nums, int l, int m, int r)\n {\n int n1 = m - l + 1;\n int n2 = r - m;\n int A[n1], B[n2];\n \n for(int i = 0; i < n1; i++)\n A[i] = nums[l + i];\n \n for(int i = 0; i < n2; i++)\n B[i] = nums[m + 1 + i];\n ...
10
0
['C', 'Merge Sort', 'C++']
1
sort-an-array
C++ | Quick sort | Merge sort | Visual explanation
c-quick-sort-merge-sort-visual-explanati-1xvc
Quick sort\nThis works just like how a preorder tree traversal does. Here I use Lomuto partition scheme.\n1. For each recursion call, get one element sorted as
yoyotvyoo888
NORMAL
2022-08-19T15:35:15.852881+00:00
2022-08-20T16:56:56.247050+00:00
1,695
false
**Quick sort**\nThis works just like how a preorder tree traversal does. Here I use Lomuto partition scheme.\n1. For each recursion call, get one element sorted as pivot. \n2. Recurse on the left part and the right part\n\n![image](https://assets.leetcode.com/users/images/9acbc10c-aa20-43f3-987e-9d99f0b202dd_166092315...
9
0
['C', 'C++']
3
sort-an-array
Java quick sort beat 95% time; time:O(nlogn)
java-quick-sort-beat-95-time-timeonlogn-8zjsf
\nclass Solution {\n public int[] sortArray(int[] nums) {\n \n if (nums.length == 0) { return nums;}\n \n QuickSort(nums, 0, nums
shushujie
NORMAL
2020-06-24T06:09:02.387607+00:00
2020-06-24T06:09:58.815534+00:00
2,742
false
```\nclass Solution {\n public int[] sortArray(int[] nums) {\n \n if (nums.length == 0) { return nums;}\n \n QuickSort(nums, 0, nums.length - 1);\n return nums;\n \n }\n \n private void QuickSort(int[] nums, int start, int end) {\n \n if(start >= end) ...
9
2
['Java']
5
sort-an-array
💯✅🔥Detailed Easy Java ,Python3 ,C++ Solution|| 25 ms ||≧◠‿◠≦✌
detailed-easy-java-python3-c-solution-25-m4md
Intuition\n\nMerge Sort is a divide-and-conquer algorithm that recursively divides the input array into smaller subarrays until they are small enough to sort. I
suyalneeraj09
NORMAL
2024-07-25T02:46:05.357779+00:00
2024-07-25T02:46:05.357816+00:00
1,039
false
# Intuition\n\nMerge Sort is a divide-and-conquer algorithm that recursively divides the input array into smaller subarrays until they are small enough to sort. It then merges these sorted subarrays back together to form the final sorted array.\n\n---\n# Approach\n\nThe code consists of three main methods:\n\n1. `sortA...
8
1
['Array', 'Divide and Conquer', 'Sorting', 'Merge Sort', 'C++', 'Java', 'Python3']
4
sort-an-array
✅Easy Merge Sort Algorithm with Video Explanation
easy-merge-sort-algorithm-with-video-exp-7htq
Video Explanation\nhttps://youtu.be/lsrEitj-fO8?si=5un2qApRDftE0IyW\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is t
prajaktakap00r
NORMAL
2024-07-25T00:59:11.226599+00:00
2024-07-25T03:54:20.551670+00:00
2,222
false
# Video Explanation\nhttps://youtu.be/lsrEitj-fO8?si=5un2qApRDftE0IyW\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to sort an array of integers, and we are using the merge sort algorithm to achieve this. Merge sort is a classic divide-and-conquer algorithm that split...
8
0
['Array', 'Divide and Conquer', 'Sorting', 'Heap (Priority Queue)', 'Merge Sort', 'Bucket Sort', 'C++']
5
sort-an-array
Easiest Solution | Python 1-liner
easiest-solution-python-1-liner-by-fromd-0byv
Intuition\nIt\'s not that deep.\n\n# Code\n\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]:\n return sorted(nums)\n
fromdihpout
NORMAL
2023-03-01T00:48:59.480247+00:00
2023-03-01T00:48:59.480288+00:00
1,896
false
# Intuition\nIt\'s not that deep.\n\n# Code\n```\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]:\n return sorted(nums)\n```
8
7
['Python3']
5
sort-an-array
C++ Easy 1 line Solution
c-easy-1-line-solution-by-anushhhka-pvck
Please upvote, if this helps!! (\u25D4\u203F\u25D4)\n\n\n\nclass Solution {\npublic:\n vector sortArray(vector& nums) {\n\t\n sort(nums.begin(), nums.
anushhhka
NORMAL
2022-08-27T20:06:36.116285+00:00
2022-08-27T20:07:30.712333+00:00
1,102
false
**Please upvote, if this helps!! (\u25D4\u203F\u25D4)**\n\n\n\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& nums) {\n\t\n sort(nums.begin(), nums.end());\n return nums;\n }\n};
8
1
['C', 'C++']
0
sort-an-array
Just using simple recursion to sort the array
just-using-simple-recursion-to-sort-the-lwvum
Although this will not pass all test cases but still this is the most fundamental way to sort the array using recursion\n\n vector<int> insertAT(vector<int>
chocoTaqo
NORMAL
2021-08-25T09:55:52.819375+00:00
2021-08-25T09:55:52.819412+00:00
6,131
false
Although this will not pass all test cases but still this is the most fundamental way to sort the array using recursion\n```\n vector<int> insertAT(vector<int> &V, int val) {\n int len = V.size(); \n if((len == 0) || (V[len-1] <= val)) {\n V.push_back(val); return V;\n }\n\n int last = V[l...
8
0
['Recursion', 'Sorting']
5
sort-an-array
[C++] Sorting an array using recursion
c-sorting-an-array-using-recursion-by-tu-sg8j
Please note that this method will result in a TLE and only pass 10/13 of the test cases.\nI am sharing it because I feel it\'s a good application of recursion t
ihavehiddenmyid
NORMAL
2021-07-06T06:54:35.177687+00:00
2021-07-06T06:57:58.226792+00:00
1,878
false
**Please note that this method will result in a TLE and only pass 10/13 of the test cases.\nI am sharing it because I feel it\'s a good application of recursion to sort an array**\n\n```\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& nums) \n {\n\t\t// Base Condition\n if (nums.size() == 1...
8
2
['Recursion', 'C']
2
sort-an-array
MergeSort Explained in detail with DIAGRAM 🔥| C++✔️ | Java✔️ | Python✔️
mergesort-explained-in-detail-with-diagr-f27r
Intuition\n Describe your first thoughts on how to solve this problem. \n- Merge sort is a divide-and-conquer algorithm that splits the array into halves, recu
atharvf14t
NORMAL
2024-07-25T01:46:07.925712+00:00
2024-07-25T01:46:07.925733+00:00
2,962
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Merge sort is a divide-and-conquer algorithm that splits the array into halves, recursively sorts each half, and then merges the sorted halves back together. Here\'s a detailed explanation of the code, along with the intuition, approac...
7
0
['Array', 'Divide and Conquer', 'Merge Sort', 'Python', 'C++', 'Java']
11
sort-an-array
[Python] Quick Sort with Optimization
python-quick-sort-with-optimization-by-h-41q7
Implementation 1: Quick Sort (Using auxiliary space - easy to implement)\n- Firstly, we pick pivot as a random number.\n- We particle nums arrays into 3 section
hiepit
NORMAL
2023-12-18T23:20:01.208427+00:00
2023-12-18T23:22:08.166013+00:00
720
false
**Implementation 1: Quick Sort (Using auxiliary space - easy to implement)**\n- Firstly, we pick pivot as a random number.\n- We particle `nums` arrays into 3 section:\n - Left: All elements < pivot\n - Mid: All elements == pivot\n - Right: All elements > pivot\n```python\nclass Solution:\n def sortArray(...
7
1
['Quickselect', 'Python3']
0
sort-an-array
JAVA | Quick sort with explaination
java-quick-sort-with-explaination-by-kus-0znz
In-place,Not Stable,Not adaptive,Divide and conquer.\n\nFor primitive it is preferred and for objects merge sort is preferred. As merge sort takes extra o(n) me
kushguptacse
NORMAL
2022-12-18T07:06:48.497584+00:00
2022-12-18T07:16:50.312769+00:00
1,308
false
In-place,Not Stable,Not adaptive,Divide and conquer.\n\nFor primitive it is preferred and for objects merge sort is preferred. As merge sort takes extra o(n) memory it is not preferred for array. But for linked list merge sort does not need extra space.\n\n# Approach\nWe will take one element as pivot(here the last one...
7
0
['Sorting', 'Java']
0
sort-an-array
Sort an array using merge sort
sort-an-array-using-merge-sort-by-pavith-ppnm
Intuition\n I thought of solving using merge sort using O(nlogn) complexity \n\n# Approach\n Merge sort \n\n# Complexity\n- Time complexity:\n O(nlogn) \n\n- Sp
pavithra_devi7
NORMAL
2022-11-05T09:30:11.482595+00:00
2022-11-05T09:33:06.664927+00:00
2,019
false
# Intuition\n<!-- I thought of solving using merge sort using O(nlogn) complexity -->\n\n# Approach\n<!-- Merge sort -->\n\n# Complexity\n- Time complexity:\n<!-- O(nlogn) -->\n\n- Space complexity:\n<!-- O(logn) -->\n\n# Code\n```\nclass Solution {\n public int[] sortArray(int[] nums) {\n if(nums.length==1){...
7
0
['Divide and Conquer', 'Merge Sort', 'Java']
1
sort-an-array
Classical Sorting Algorithms // JAVA // Some with detailed explanations
classical-sorting-algorithms-java-some-w-9m5w
1. Quick Sort\n\n\tclass Solution {\n\n\t\tpublic int[] sortArray(int[] nums) {\n\t\t\tquickSort(nums, 0, nums.length - 1);\n\t\t\treturn nums;\n\t\t}\n\n\t\tpu
robinsooooon
NORMAL
2021-09-27T03:26:07.891936+00:00
2021-09-27T07:38:55.397131+00:00
1,328
false
**1. Quick Sort**\n```\n\tclass Solution {\n\n\t\tpublic int[] sortArray(int[] nums) {\n\t\t\tquickSort(nums, 0, nums.length - 1);\n\t\t\treturn nums;\n\t\t}\n\n\t\tpublic void quickSort(int[] nums, int start, int end) {\n\t\t\t// if the start index is larger or equal to the end index, the sort of the subarray is finis...
7
0
['Sorting', 'Heap (Priority Queue)', 'Merge Sort', 'Java']
3
sort-an-array
Python Merge Sort Easy Implementation
python-merge-sort-easy-implementation-by-w0q2
\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]: \n # Length 1 is always sorted\n if len(nums) == 1:\n re
squidimenz
NORMAL
2020-06-25T15:16:33.957493+00:00
2020-06-25T15:16:33.957541+00:00
2,985
false
```\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]: \n # Length 1 is always sorted\n if len(nums) == 1:\n return nums\n \n # Split array into two subarrays\n half = len(nums) // 2\n left = nums[:half]\n right = nums[half:]\n ...
7
1
[]
0
sort-an-array
6 Lines Java Solution | Simple Solution | Easy To Understand | Beginner Friendly
6-lines-java-solution-simple-solution-ea-p3rp
Intuition\n Describe your first thoughts on how to solve this problem. \nUsed PriorityQueue to sort the input array in ascending order. A PriorityQueue is a he
antovincent
NORMAL
2023-05-12T08:38:03.411003+00:00
2023-05-12T08:38:03.411033+00:00
1,332
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. --> \nUsed PriorityQueue to sort the input array in ascending order. A PriorityQueue is a heap-based data structure that maintains the order of its elements based on their priority. In this case, we are using a default natural ordering where s...
6
0
['Heap (Priority Queue)', 'Quickselect', 'Java']
0
sort-an-array
Basic Merge Sort C++|| No In-Built Function Used
basic-merge-sort-c-no-in-built-function-kb8p2
Complexity\n- Time complexity:\nO(NLOGN)\n\n- Space complexity:\nO(NLOGN)\n\n# Code\n\nclass Solution {\npublic:\n vector<int> mergesorted(vector<int>&arr)\n
Heisenberg2003
NORMAL
2023-03-01T00:21:09.767457+00:00
2023-03-01T00:46:30.409856+00:00
11,474
false
# Complexity\n- Time complexity:\nO(NLOGN)\n\n- Space complexity:\nO(NLOGN)\n\n# Code\n```\nclass Solution {\npublic:\n vector<int> mergesorted(vector<int>&arr)\n {\n if(arr.size()==1)\n {\n return arr;\n }\n int dividesize=arr.size()/2;\n vector<int>merge1,merge2;\n ...
6
2
['C++']
2
sort-an-array
Randomize Quick-Sort | worst case Time : O(NlogN)
randomize-quick-sort-worst-case-time-onl-ubn7
cpp\n int partition(vector<int>&arr,int start,int end){\n int pivot=arr[end];\n int partIdx=start;\n for(int i=start;i<end;i++){\n
shashank9Aug
NORMAL
2021-09-24T08:45:03.755198+00:00
2021-09-24T08:45:03.755233+00:00
845
false
```cpp\n int partition(vector<int>&arr,int start,int end){\n int pivot=arr[end];\n int partIdx=start;\n for(int i=start;i<end;i++){\n if(arr[i]<=pivot){\n swap(arr[i],arr[partIdx]);\n partIdx++;\n }\n }\n swap(arr[partIdx],arr[end...
6
1
['C', 'Sorting', 'C++']
2
sort-an-array
js All Sorting Algorithms | Quick, Merge, Bucket ,Tree, heap and others...
js-all-sorting-algorithms-quick-merge-bu-59r4
Merge Sort : O(n log n) time O(n) space \n\n\nconst merge = (left, right) => {\n const mergeArr = [];\n let i=0,j=0;\n while(i < left.length && j < rig
brahmavid
NORMAL
2021-08-11T03:00:18.644601+00:00
2021-09-11T12:13:23.943813+00:00
963
false
## Merge Sort : O(n log n) time O(n) space \n\n```\nconst merge = (left, right) => {\n const mergeArr = [];\n let i=0,j=0;\n while(i < left.length && j < right.length) {\n if (left[i] <right[j]) {\n mergeArr.push(left[i++]);\n } else {\n mergeArr.push(right[j++]);\n }...
6
0
['Sorting', 'Heap (Priority Queue)', 'Merge Sort', 'JavaScript']
1
sort-an-array
Java iterative/recursive quickSort, mergeSort, 3 ways quickSort, 3 ways mergeSort, max/min heapSort
java-iterativerecursive-quicksort-merges-n6dt
Iterative quick sort\n\nclass Solution {\n public int[] sortArray(int[] nums) {\n return quickIterative(nums, 0, nums.length - 1);\n }\n \n p
lisze1
NORMAL
2021-05-22T22:06:06.805718+00:00
2021-08-07T00:13:31.483597+00:00
1,509
false
Iterative quick sort\n```\nclass Solution {\n public int[] sortArray(int[] nums) {\n return quickIterative(nums, 0, nums.length - 1);\n }\n \n private int[] quickIterative(int[] nums, int l, int h) {\n if (l >= h) {\n return nums;\n }\n // create a stack to store the l...
6
0
['Recursion', 'Sorting', 'Heap (Priority Queue)', 'Merge Sort', 'Iterator', 'Java']
0
sort-an-array
Iterative merge sort
iterative-merge-sort-by-antrixm-f3tj
\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]:\n \n nums = [[num] for num in nums]\n\n def merge(l1, l2):\n
antrixm
NORMAL
2020-05-11T14:28:49.454410+00:00
2020-05-11T14:28:49.454460+00:00
1,358
false
```\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]:\n \n nums = [[num] for num in nums]\n\n def merge(l1, l2):\n i = 0\n j = 0\n new = []\n \n while i < len(l1) and j < len(l2):\n if l1[i] < l2[j]:\n ...
6
0
['Merge Sort', 'Iterator', 'Python', 'Python3']
1
sort-an-array
C++ -- Merge sort, Quick sort, Heap sort, Bubble sort, Insert sort, Selection sort.
c-merge-sort-quick-sort-heap-sort-bubble-g1br
\nclass Solution {\npublic:\n /*\n 1. Bubble sort:\n Worstcase time: O(n^2), Bestcase Time: O(n) - list already sorted, Space: O(1)\n */\n vecto
magrawa4
NORMAL
2020-03-02T00:19:53.731861+00:00
2020-03-02T00:19:53.731908+00:00
1,063
false
```\nclass Solution {\npublic:\n /*\n 1. Bubble sort:\n Worstcase time: O(n^2), Bestcase Time: O(n) - list already sorted, Space: O(1)\n */\n vector<int> bubbleSort(vector<int>& nums) {\n for(int i=0;i<nums.size()-1;i++){\n bool found = false;\n for (int j=0;j<nums.size()-i-...
6
1
[]
1
sort-an-array
JavaScript Merge Sort and Quick Sort Solution
javascript-merge-sort-and-quick-sort-sol-6llx
Merge Short Solution\n\n\nvar sortArray = function(nums) {\n let len = nums.length;\n if(len < 2) return nums;\n const mid = Math.floor(len / 2);\n
ruzdi
NORMAL
2019-12-07T19:42:39.363914+00:00
2019-12-07T19:42:39.363961+00:00
1,171
false
**Merge Short Solution\n**\n```\nvar sortArray = function(nums) {\n let len = nums.length;\n if(len < 2) return nums;\n const mid = Math.floor(len / 2);\n const left = sortArray(nums.slice(0, mid));\n const right = sortArray(nums.slice(mid, len));\n return merge(left, right);\n \n};\n\nfunction mer...
6
0
['Merge Sort', 'JavaScript']
2
sort-an-array
golang merge sort
golang-merge-sort-by-craig08-2hrj
Merge sort, O(n) space and O(nlog(n)) time complexity.\ngo\nfunc sortArray(nums []int) []int {\n mergesort(nums, 0, len(nums))\n return nums\n}\n\nfunc me
craig08
NORMAL
2019-06-19T14:38:04.697347+00:00
2019-06-19T15:11:03.912698+00:00
558
false
Merge sort, O(n) space and O(nlog(n)) time complexity.\n```go\nfunc sortArray(nums []int) []int {\n mergesort(nums, 0, len(nums))\n return nums\n}\n\nfunc mergesort(nums []int, start, end int) {\n if start >= end-1 {\n return\n }\n mergesort(nums, start, (start+end)/2)\n mergesort(nums, (start+...
6
0
['Go']
0
sort-an-array
O(N) Sort Solution💕🐍
on-sort-solution-by-jyot_150-cgtd
\n# Complexity\n- Time complexity:\n O(n) \n\n- Space complexity:\n O(n) \n\n# Code\njs\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]:
jyot_150
NORMAL
2024-07-25T03:36:41.325804+00:00
2024-07-25T03:36:41.325840+00:00
789
false
\n# Complexity\n- Time complexity:\n $$O(n)$$ \n\n- Space complexity:\n $$O(n)$$ \n\n# Code\n```js\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]:\n arr=[0]*(10**5+1)\n for i in nums:\n arr[i+(50000)]+=1\n new=[]\n for i in range(len(arr)):\n if...
5
0
['Python3']
3
sort-an-array
Using QuickSort(median as a pivot) which works and mergeSort!!
using-quicksortmedian-as-a-pivot-which-w-xphk
Intuition\n Describe your first thoughts on how to solve this problem. \nSince this problem requires O(nlog\u2061n) time complexity, two sorting techniques that
sarahejaz77
NORMAL
2024-06-14T18:04:10.931291+00:00
2024-06-14T18:59:25.003864+00:00
167
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince this problem requires O(nlog\u2061n) time complexity, two sorting techniques that comes to our mind at first is merge and quick sort (without using any built-in functions).\nThe plan wasn\'t to write this post and on that account th...
5
0
['Java']
0
sort-an-array
Heap
heap-by-riya_bhadauriya-wwe7
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
Riya_bhadauriya
NORMAL
2023-04-21T06:57:29.574571+00:00
2023-04-21T06:57:29.574599+00:00
571
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)$$ --...
5
0
['Heap (Priority Queue)', 'Python3']
3
sort-an-array
Merge Sort | Most Optimized
merge-sort-most-optimized-by-_aman_gupta-v5m3
# Intuition \n\n\n\n\n\n# Complexity\n- Time complexity: O(n * log(n))\n\n\n- Space complexity: O(n)\n\n\n# Code\n\nvoid merge(int arr[], int l, int r, int mi
_aman_gupta_
NORMAL
2023-03-15T09:51:21.262103+00:00
2023-03-15T09:51:21.262131+00:00
620
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...
5
0
['Array', 'C', 'Merge Sort']
1
sort-an-array
912. Sort an Array c++solution
912-sort-an-array-csolution-by-loverdram-jjh3
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
loverdrama699
NORMAL
2023-03-01T15:09:52.611797+00:00
2023-03-01T15:09:52.611832+00:00
1,587
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)$$ --...
5
0
['C++']
1
sort-an-array
HEAP SORT🔄 | O(1) Space Complexity🔥| C++
heap-sort-o1-space-complexity-c-by-bahni-wwjp
Intuition\nHeap Sort and Merge Sort are such Sorting Algorithms that has the time complexity of O(nlogn). Merge Sort has a Space Complexity of O(n), but Heap so
Bahniman14
NORMAL
2023-03-01T11:41:12.755054+00:00
2023-06-19T17:47:56.403782+00:00
258
false
# Intuition\nHeap Sort and Merge Sort are such Sorting Algorithms that has the time complexity of O(nlogn). Merge Sort has a Space Complexity of O(n), but Heap sort can be done in O(1), as it doesn\'t take extra space. As the Question suggests to take the **smallest space complexity possible,** it is better to go with ...
5
0
['C++']
3
sort-an-array
[Python][QuickSort] Quick sort with random partition
pythonquicksort-quick-sort-with-random-p-d563
Quick sort with random parition to make the sorting faster and to handle the worst case (when all elements are already sorted)\n\n\nclass Solution:\n def sor
deafcon
NORMAL
2022-03-27T00:34:50.955436+00:00
2022-03-27T00:34:50.955485+00:00
781
false
Quick sort with random parition to make the sorting faster and to handle the worst case (when all elements are already sorted)\n\n```\nclass Solution:\n def sortArray(self, nums: List[int]) -> List[int]:\n self.quicksort(nums, 0, len(nums)-1)\n return nums\n \n def quicksort(self, nums, start, en...
5
0
['Sorting', 'Python']
1
sort-an-array
SELECTION SORT
selection-sort-by-harsh07khuda-69gu
\n\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& nums) {\n // Selection sort algorithm \n for( int i=0; i <nums.size()-1 ; i++)
harsh07khuda
NORMAL
2022-03-26T06:18:54.592677+00:00
2022-03-26T06:18:54.592711+00:00
19,173
false
``` \n\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& nums) {\n // Selection sort algorithm \n for( int i=0; i <nums.size()-1 ; i++)\n {\n int min = i;\n for( int j = i+1; j<nums.size(); j++)\n {\n if(nums[j] < nums[min])\n ...
5
0
[]
15
sort-an-array
Fix if you are getting TLE using Quick Sort
fix-if-you-are-getting-tle-using-quick-s-h18a
I was getting TLE using QuickSort.\nI was always selecting the end element as the pivot. It seems that there are test cases designed to punish you for this, for
kyrixia
NORMAL
2022-02-26T10:33:31.044351+00:00
2022-02-26T10:33:31.044382+00:00
237
false
I was getting TLE using QuickSort.\nI was always selecting the end element as the pivot. It seems that there are test cases designed to punish you for this, for example if the test case is nearly sorted, it will become O(n^2).\n\nA simple solution is to swap the middle element with the end element, before selecting the...
5
0
[]
1
sort-an-array
C++ recursive merge-sort
c-recursive-merge-sort-by-yehudisk-bbgh
\nclass Solution {\npublic:\n vector<int> mergeSorted(vector<int>& a, vector<int>& b)\n {\n vector<int> res;\n int i=0, j=0;\n while
yehudisk
NORMAL
2020-08-16T06:51:36.198281+00:00
2020-08-16T06:51:36.198311+00:00
554
false
```\nclass Solution {\npublic:\n vector<int> mergeSorted(vector<int>& a, vector<int>& b)\n {\n vector<int> res;\n int i=0, j=0;\n while (i<a.size() && j<b.size())\n {\n if (a[i] < b[j])\n {\n res.push_back(a[i]);\n i++;\n }...
5
0
[]
0
sort-an-array
JavaScript - Quicksort
javascript-quicksort-by-ahmedengu-q543
\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar sortArray = function(nums) {\n if (nums.length <= 1) return nums;\n const pivot = nums.p
ahmedengu
NORMAL
2020-07-30T11:10:59.227797+00:00
2020-07-30T11:10:59.227844+00:00
695
false
```\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar sortArray = function(nums) {\n if (nums.length <= 1) return nums;\n const pivot = nums.pop();\n const less = sortArray(nums.filter(n => n <= pivot));\n const more = sortArray(nums.filter(n => n > pivot));\n return less.concat(pivot, mor...
5
1
['JavaScript']
4
sort-an-array
Java LazySort
java-lazysort-by-hobiter-8xdx
\nclass Solution {\n private static final int BBL = 30;\n private static final int SLT = 50;\n private static final int IST = 80;\n private static f
hobiter
NORMAL
2020-03-10T05:51:20.672396+00:00
2020-03-10T06:08:32.215096+00:00
867
false
```\nclass Solution {\n private static final int BBL = 30;\n private static final int SLT = 50;\n private static final int IST = 80;\n private static final int QCK = 300;\n private static final int LZY = 1000;\n private static final Random rnd = new Random();\n int[] ns;\n int n;\n public Lis...
5
0
[]
0
sort-an-array
Merge-sort bottom-up Java
merge-sort-bottom-up-java-by-savochkin-u5q7
Have not found the bottom-up java merge sort solution here.\n\n\nclass Solution {\n\n public int[] sortArray(int[] nums) {\n // starting from nums.len
savochkin
NORMAL
2019-10-28T06:38:04.893339+00:00
2019-10-28T06:38:43.726778+00:00
990
false
Have not found the bottom-up java merge sort solution here.\n\n```\nclass Solution {\n\n public int[] sortArray(int[] nums) {\n // starting from nums.length arrays of size 1 (already sorted)\n // on each step merge two adjacent sorted arrays so that they remain sorted\n // you may notice that si...
5
2
['Merge Sort', 'Java']
1
sort-an-array
Java HeapSort
java-heapsort-by-pekras-vend
I haven\'t seen any posts here using heapsort, despite it being reasonably good for sorting arrays in-place. A heap is a binary tree where a parent has a value
pekras
NORMAL
2019-07-19T19:43:45.248131+00:00
2019-08-19T15:13:48.670595+00:00
2,576
false
I haven\'t seen any posts here using heapsort, despite it being reasonably good for sorting arrays in-place. A heap is a binary tree where a parent has a value greater than or equal to its children.\n\nBuilding a heap is nlogn. Each value is added as a leaf node, then it bubbles up the heap to its correct location. To ...
5
1
[]
0
sort-an-array
Swift - Using Quicksort
swift-using-quicksort-by-georrgee-vbq3
Hey everyone! Did this in swift and used a high order function: filter\n\nclass Solution {\n \n func sortArray(_ nums: [Int]) -> [Int] {\n \n
georrgee
NORMAL
2019-06-05T19:49:46.013197+00:00
2019-06-05T19:49:46.013246+00:00
863
false
Hey everyone! Did this in swift and used a high order function: filter\n```\nclass Solution {\n \n func sortArray(_ nums: [Int]) -> [Int] {\n \n let sortedArray = quickSort(array: nums)\n return sortedArray\n }\n \n func quickSort(array: [Int]) -> [Int] {\n \n if array....
5
1
['Swift']
3
sort-an-array
[C++] QuickSort and CountingSort solutions
c-quicksort-and-countingsort-solutions-b-dtqn
Here are two C++ soltuions with different runtimes:\n1. QuickSort: Runtime: 68 ms, Memory Usage: 12.6 MB\n\n void quickSort(vector<int>& V, int from, int to)
todor91
NORMAL
2019-04-24T13:28:28.633256+00:00
2019-04-24T13:28:28.633349+00:00
2,103
false
Here are two C++ soltuions with different runtimes:\n1. QuickSort: Runtime: 68 ms, Memory Usage: 12.6 MB\n```\n void quickSort(vector<int>& V, int from, int to) {\n if (from + 1 >= to) return;\n // choose random pivot:\n int piv = V[from + rand() % (to - from)];\n \n int i = from - 1, j...
5
1
[]
2
sort-an-array
Sorting Technique🚀
sorting-technique-by-guhan0907-0sba
Intuition\nThe problem asks us to sort an array of integers. A radix sort seems like a natural fit here since it sorts numbers by processing individual digits.A
Guhan0907
NORMAL
2024-10-01T03:37:08.452427+00:00
2024-10-01T03:37:08.452454+00:00
232
false
# Intuition\nThe problem asks us to sort an array of integers. A radix sort seems like a natural fit here since it sorts numbers by processing individual digits.Also Radix sort can be used in the places like numbers which having more number of digits. This is an efficient sorting algorithm when the maximum number of di...
4
0
['Sorting', 'Radix Sort', 'C++']
2
sort-an-array
✅💯🔥Simple Code📌🚀| 🔥✔️Easy to understand🎯 | 🎓🧠Beginner friendly🔥| O(N logN) Time Comp.💀💯
simple-code-easy-to-understand-beginner-8hlfv
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
atishayj4in
NORMAL
2024-07-29T19:46:15.971712+00:00
2024-08-01T19:35:18.019504+00:00
414
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)$$ --...
4
0
['Array', 'Divide and Conquer', 'C', 'Sorting', 'Heap (Priority Queue)', 'Merge Sort', 'Bucket Sort', 'Python', 'C++', 'Java']
0
sort-an-array
Very Easy || Easy to understand
very-easy-easy-to-understand-by-abhimany-bc59
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nPriority Queue\nstep 1: Push array elemnt in queue(max heap)\nstep 2: mov
skipper_108
NORMAL
2024-07-25T17:56:47.772227+00:00
2024-07-25T17:56:47.772257+00:00
38
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nPriority Queue\nstep 1: Push array elemnt in queue(max heap)\nstep 2: move queue element in ans array\nstep 3: Now reverse the ans array\n\n# Complexity\n- Time complexity:O(nlogn)\n<!-- Add your time complexity here, e.g. $...
4
0
['C++']
0
sort-an-array
Why Heap Sort is the Ultimate Sorting Solution! 🧠✨
why-heap-sort-is-the-ultimate-sorting-so-xwl4
"Code is like humor. When you have to explain it, it\u2019s bad." \u2014 Cory House\n\n# Do upvote if the solution is useful for you <3\n\n\n# Why Heap Sort is
i210844
NORMAL
2024-07-25T03:57:23.887257+00:00
2024-07-25T03:57:23.887293+00:00
572
false
> **"Code is like humor. When you have to explain it, it\u2019s bad." \u2014 Cory House**\n\n# Do upvote if the solution is useful for you <3\n\n\n# Why Heap Sort is the Best Solution \uD83C\uDF1F\n**Time Complexity \u23F3:**\n\nHeap Sort runs in O(n log n) time in all cases (best, average, and worst), making it consis...
4
1
['C++']
2
sort-an-array
Efficient Solution for Sorting an Array in 𝑂(𝑛 log ⁡ 𝑛) Time Complexity
efficient-solution-for-sorting-an-array-ttyhf
If you found this solution helpful, please upvote! \uD83D\uDE0A\n\n# Intuition\nThe problem requires sorting an array in ascending order with optimal time and s
user4612MW
NORMAL
2024-07-25T03:57:00.715733+00:00
2024-07-25T03:57:00.715767+00:00
23
false
# **If you found this solution helpful, please upvote! \uD83D\uDE0A**\n\n# Intuition\nThe problem requires sorting an array in ascending order with optimal time and space complexity. Given the constraints, the Merge Sort algorithm is a suitable choice because it divides the array into smaller subarrays, sorts them, and...
4
0
['C++']
0
sort-an-array
beats 95% of users
beats-95-of-users-by-jatadhara_sai-q2id
Intuition\ncounting sort approach\n\n# Approach\n-50000 is min possible value in array \n50000 is max possible value in array\n\ncreating a new cnt array of siz
Jatadhara_Sai
NORMAL
2024-06-25T10:24:42.459541+00:00
2024-06-25T10:24:42.459569+00:00
372
false
# Intuition\ncounting sort approach\n\n# Approach\n-50000 is min possible value in array \n50000 is max possible value in array\n\ncreating a new cnt array of size 50000+50000+1\nto store all counts of elements\n\nfor(int i=-50000 ; i<=50000 ; i++)\nvalues may be negative also\n \nsorting based on count values\n\n# Com...
4
0
['Counting Sort', 'C++']
0
sort-an-array
C++ and Python3 || Merge Sort || Simple and Optimal
c-and-python3-merge-sort-simple-and-opti-wzma
\n# Complexity\n- Time complexity: O(nlogn)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n)\n Add your space complexity here, e.g. O(n)
meurudesu
NORMAL
2024-02-20T16:12:24.445394+00:00
2024-02-20T16:12:24.445427+00:00
2,193
false
\n# Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n void merge(vector<int> &arr, int low, int mid, int high) {\n vector<i...
4
0
['Sorting', 'Merge Sort', 'C++', 'Python3']
1
sort-an-array
Easy Solution || Using Min Heap || C++
easy-solution-using-min-heap-c-by-007ans-dhgn
\n\n# Code\n\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& nums) {\n \n priority_queue<int,vector <int>,greater<int>> p;\n\n
007anshuyadav
NORMAL
2024-02-03T19:35:01.991839+00:00
2024-02-03T19:35:01.991864+00:00
241
false
\n\n# Code\n```\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& nums) {\n \n priority_queue<int,vector <int>,greater<int>> p;\n\n for(auto i:nums){\n p.push(i);\n }\n int i=0;\n while(!p.empty()){\n nums[i++]=p.top();\n p...
4
0
['Array', 'Divide and Conquer', 'Heap (Priority Queue)', 'C++']
0
sort-an-array
O(n) || Counting Sort || 99% beat (4ms)
on-counting-sort-99-beat-4ms-by-priyansh-42ag
Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Tim
Priyanshu_pandey15
NORMAL
2024-01-12T04:55:52.746920+00:00
2024-01-12T04:55:52.746951+00:00
1,227
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![Screenshot (89).png](https://assets.leetcode.com/users/images/1d2a84d9-9cb9-4792-8431-20db88905632_1705035344.6062953.png)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- ...
4
0
['C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']
3
sort-an-array
Quick Sort or Count Sort? Why even choose? Let's have both. C++ 47 ms.
quick-sort-or-count-sort-why-even-choose-m77r
Intuition\nOn top of sorting algorithms there are two fundamentally different approaches:\n1. Count sort: O(n + r) time, O(r) space.\n2. Quick sort: O(n \times
sergei99
NORMAL
2023-11-03T13:27:57.830111+00:00
2024-07-25T04:15:14.809555+00:00
141
false
# Intuition\nOn top of sorting algorithms there are two fundamentally different approaches:\n1. Count sort: $$O(n + r)$$ time, $$O(r)$$ space.\n2. Quick sort: $$O(n \\times log(n))$$ time, $$O(1)$$ space.\nwhere $$n$$ is the number of elements to sort, and $$r$$ is the range between the minimal and the maximal elements...
4
0
['Array', 'Divide and Conquer', 'Sorting', 'Quickselect', 'Counting Sort', 'C++']
0
sort-an-array
Counting Sort || O(n) Solution || JAVA
counting-sort-on-solution-java-by-kshiti-5nw6
\n# Approach\n- Create an integer map array integer map Array of size mapRange(max - min +2).//(+2 is just to avoid overflow here in mapRange)\n- Traverse whole
Kshitij_Pandey
NORMAL
2023-03-01T08:23:49.055818+00:00
2023-03-01T08:23:49.055861+00:00
31
false
\n# Approach\n- **Create an integer map array integer map Array of size mapRange(max - min +2).**//(+2 is just to avoid overflow here in mapRange)\n- **Traverse whole array and store the frequency in the map.**\n- **Again traverse the whole map and put the (index + min(offset here)) in the new sorted Array.**\n\n# Comp...
4
0
['Java']
0
sort-an-array
🚀Fast | C# & TypeScript Solution | Merge Sort ❇️✅
fast-c-typescript-solution-merge-sort-by-94k8
\u2B06\uFE0FLike|\uD83C\uDFAFShare|\u2B50Favourite\n\n# Runtime & Memory\n\n\n\n# Complexity\n- Time complexity:\nO(nlog(n))\n\n- Space complexity:\nO(n)\n\n# C
arafatsabbir
NORMAL
2023-03-01T06:41:40.763364+00:00
2023-03-01T08:46:21.517003+00:00
829
false
# \u2B06\uFE0FLike|\uD83C\uDFAFShare|\u2B50Favourite\n\n# Runtime & Memory\n![image.png](https://assets.leetcode.com/users/images/16827e93-dc52-4c83-bab2-d44d8184796f_1677652745.368328.png)\n\n\n# Complexity\n- Time complexity:\nO(nlog(n))\n\n- Space complexity:\nO(n)\n\n# C# Code\n```\npublic class Solution\n{\n pu...
4
0
['Array', 'Divide and Conquer', 'Merge Sort', 'TypeScript', 'C#']
0
sort-an-array
🧐Simple way 💻 🔥Merge Sort in Java 📝, Python 🐍, and C++ 🖥️ with Video Explanation 🎥
simple-way-merge-sort-in-java-python-and-ofwe
Intuition\n Describe your first thoughts on how to solve this problem. \nMerge sort is a sorting algorithm that uses the divide and conquer approach to sort an
Vikas-Pathak-123
NORMAL
2023-03-01T01:07:44.131604+00:00
2023-03-01T16:18:17.669457+00:00
930
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMerge sort is a sorting algorithm that uses the divide and conquer approach to sort an array or a list. In this algorithm, the given array is recursively divided into two halves until the base case of having a single element is reached. T...
4
0
['Divide and Conquer', 'Merge Sort', 'Python', 'C++', 'Java']
2
sort-an-array
EASIEST SOLUTION, O(1), ONE-LINE SOLUTION, TOURSIT APPROVED
easiest-solution-o1-one-line-solution-to-0lo5
\nclass Solution {\n public int[] sortArray(int[] nums) {\n Arrays.sort(nums);\n return nums;\n }\n}
nekidaz
NORMAL
2022-10-26T09:44:19.409259+00:00
2022-10-26T09:44:19.409315+00:00
583
false
\nclass Solution {\n public int[] sortArray(int[] nums) {\n Arrays.sort(nums);\n return nums;\n }\n}
4
1
[]
1
sort-an-array
Quick Sort with explanantion
quick-sort-with-explanantion-by-llu2020-ox3i
Pick the middle number as the pivot.\n2. Set two poiners, left and right, on the start index and end index.\n3. left pointer searches for next number that is bi
LLU2020
NORMAL
2022-10-07T01:27:13.662080+00:00
2022-10-07T01:44:55.092398+00:00
1,594
false
1. Pick the middle number as the pivot.\n2. Set two poiners, left and right, on the start index and end index.\n3. left pointer searches for next number that is bigger or equal to pivot.\n4. right pointer searches for next number that is smaller or equal to pivot.\n5. if there exists two numbers as described, swap them...
4
0
[]
0
sort-an-array
✅✅C++ || MERGE SORT | QUICKSORT | INSERTION SORT | HEAP SORT |BUBBLE SORT
c-merge-sort-quicksort-insertion-sort-he-97hf
Bubble sort\n\n\t// bubbleSort(nums);\n\tvoid bubbleSort(vector& nums){\n for(int i = nums.size() - 1; i >= 0; i--)\n for(int j = 0; j < i; j+
Pianist_01
NORMAL
2022-10-01T15:41:50.507215+00:00
2022-11-23T21:18:15.858435+00:00
336
false
Bubble sort\n\n\t// bubbleSort(nums);\n\tvoid bubbleSort(vector<int>& nums){\n for(int i = nums.size() - 1; i >= 0; i--)\n for(int j = 0; j < i; j++)\n if(nums[j] > nums[j + 1]) \n swap(nums[j], nums[j + 1]);\n }\nInsertion sort\n\n\t// insertionSort(nums);\n\tvoid...
4
0
['Sorting', 'Heap (Priority Queue)', 'Merge Sort']
0
sort-an-array
merge sort solution
merge-sort-solution-by-abbos99-3nch
\n public int[] sortArray(int[] nums) {\n mergeSort(nums, 0, nums.length - 1);\n return nums;\n }\n\n private void mergeSort(int[] array, in
abbos99
NORMAL
2022-08-17T11:03:07.081761+00:00
2022-08-17T11:03:07.081798+00:00
775
false
```\n public int[] sortArray(int[] nums) {\n mergeSort(nums, 0, nums.length - 1);\n return nums;\n }\n\n private void mergeSort(int[] array, int left, int right) {\n if (left < right) {\n var middle = left + (right - left) / 2;\n\n mergeSort(array, left, middle);\n ...
4
0
['Java']
0
sort-an-array
Optimal Javascript Solution
optimal-javascript-solution-by-gautamgun-2h77
\nOne Liner\n\n\n\nvar sortArray = function(nums) {\n return nums.sort((a, b) => a - b)\n};\n\n\n\nMerge Sort: Time Complexity - O(N Log N) & Space complexit
GautamGunecha
NORMAL
2022-08-11T06:44:33.845072+00:00
2022-08-11T06:59:09.907382+00:00
1,176
false
```\nOne Liner\n```\n\n```\nvar sortArray = function(nums) {\n return nums.sort((a, b) => a - b)\n};\n```\n\n```\nMerge Sort: Time Complexity - O(N Log N) & Space complexity - O(n)\n```\n\n```\nvar sortArray = function(nums) {\n if(nums.length <= 1) return nums\n \n let midIdx = Math.floor(nums.length / 2)\...
4
0
['JavaScript']
2
sort-an-array
C++ shortest solution
c-shortest-solution-by-easy0peasy1-gnyg
\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n return nums;\n }\n};\n\n
easy0peasy1
NORMAL
2022-07-18T05:18:43.019930+00:00
2022-07-18T05:18:43.019996+00:00
511
false
```\nclass Solution {\npublic:\n vector<int> sortArray(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n return nums;\n }\n};\n```\n
4
0
['C', 'C++']
0
sort-an-array
Java - Beats 99.6% - Count Sort // with comments
java-beats-996-count-sort-with-comments-fh62r
\nclass Solution {\n \n // Count Sort\n public int[] sortArray(int[] arr) {\n int max = Integer.MIN_VALUE;\n int min = Integer.MAX_VALUE;
Samarth-Khatri
NORMAL
2022-06-22T19:18:05.480109+00:00
2022-06-22T19:18:25.481959+00:00
486
false
```\nclass Solution {\n \n // Count Sort\n public int[] sortArray(int[] arr) {\n int max = Integer.MIN_VALUE;\n int min = Integer.MAX_VALUE;\n for (int i=0;i<arr.length;++i) {\n max = Math.max(max, arr[i]);\n min = Math.min(min, arr[i]);\n }\n \n int ...
4
0
['Sorting', 'Java']
0
three-equal-parts
[C++] O(n) time, O(1) space, 12 ms with explanation & comments
c-on-time-o1-space-12-ms-with-explanatio-7mm9
Algorithm:\n\t1) Count no. of 1\'s in the given array, say countNumberOfOnes.\n\t2) If no 1 is found ie. countNumberOfOnes == 0, just return {0,size-1}\n\t3) If
primenumber
NORMAL
2018-10-21T03:41:57.127831+00:00
2018-10-24T08:53:32.502927+00:00
9,567
false
Algorithm:\n\t1) Count no. of 1\'s in the given array, say ```countNumberOfOnes```.\n\t2) If no 1 is found ie. ```countNumberOfOnes == 0```, just return ```{0,size-1}```\n\t3) If ``` countNumberOfOnes % 3 != 0``` , then we cannot partition the given array for sure. This is because, there is no way to put equal no. of 1...
191
2
[]
13
three-equal-parts
Java O(n) simple solution (don't know why official solution is that long)
java-on-simple-solution-dont-know-why-of-90y5
Key obseration is that three parts must have same number and pattern of 1s except the leading part. My idea is to:\n\n1. count how many ones (if num%3!=0 retu
vincexu
NORMAL
2019-01-22T19:43:37.132772+00:00
2021-01-10T06:15:12.800926+00:00
4,531
false
Key obseration is that three parts must have same number and pattern of 1s except the leading part. My idea is to:\n\n1. count how many ones (if num%3!=0 return [-1,-1])\n2. search from right side to left, until we found num/3 1s. This index is not final answer, but it defines patten of 1s\n3. from feft, ignore lead...
78
0
['Java']
9
three-equal-parts
[Python] O(n) fast solution, explained
python-on-fast-solution-explained-by-dba-hjkq
In this problem we need to split number into three parts, such that number in each part after removed all zeroes are equal.\n1. Let us check all the places, whe
dbabichev
NORMAL
2021-07-17T12:58:53.998929+00:00
2021-07-17T12:58:53.998973+00:00
2,211
false
In this problem we need to split number into three parts, such that number in each part after removed all zeroes are equal.\n1. Let us check all the places, where we have `1.` Let us have `m` elements such that.\n2. If `m == 0`, it means that we have all zeroes, so we can split in any way, let us split it `[0, 2]`.\n3....
58
2
[]
5
three-equal-parts
Logical Thinking
logical-thinking-by-gracemeng-tnae
What is \'same binary value\' like? \n> Digits following first 1 in each part should be identical. \n\n> How could we decide which 1 is the first 1 of a divided
gracemeng
NORMAL
2019-03-06T16:12:14.335513+00:00
2019-03-06T16:12:14.335546+00:00
2,199
false
> What is \'same binary value\' like? \n> Digits following first 1 in each part should be identical. \n\n> How could we decide which 1 is the first 1 of a divided part?\n> \n> Assuming `numOnes` is number of 1s in A, each part should be with `numOnesPerPart = numOnes / 3` 1s. \n> So the first \'1\' in left part is `1`...
46
1
[]
6
three-equal-parts
✅ Three Equal Parts || C++ Easy Solution || With Approach
three-equal-parts-c-easy-solution-with-a-3627
APPROACH\n1. Store every 1\'s position in a vector.\n2. It is easy to know the first 1\'s position of three parts as [x, y, z].\n3. Then verify three parts whet
Maango16
NORMAL
2021-07-17T07:13:15.217146+00:00
2021-07-17T09:39:19.402239+00:00
1,876
false
# **APPROACH**\n1. Store every 1\'s position in a vector.\n2. It is easy to know the first 1\'s position of three parts as [x, y, z].\n3. Then verify three parts whether equal\n\n***NOTE:***\n* If in cases where number of ones are not a multiple of 3, there will be no case of equal parts so return {-1,-1}\n```\nclass S...
42
4
['C']
6
three-equal-parts
C++ | O(N) | Commented Code | easy to understand
c-on-commented-code-easy-to-understand-b-rzcg
\nclass Solution {\npublic:\n vector<int> threeEqualParts(vector<int>& arr) {\n \n int ones = 0;\n int n = arr.size();\n \n
reetisharma
NORMAL
2021-07-17T17:24:05.168685+00:00
2021-07-17T17:24:19.941404+00:00
1,285
false
```\nclass Solution {\npublic:\n vector<int> threeEqualParts(vector<int>& arr) {\n \n int ones = 0;\n int n = arr.size();\n \n for(int i : arr){\n if(i==1)\n ones++;\n }\n \n //if no ones\n if(ones == 0) return {0,n-1};\n ...
26
1
['C']
3
three-equal-parts
[C++] O(N) time O(N) space, 40ms, 14 lines, 2 loops, easy understand with explanation
c-on-time-on-space-40ms-14-lines-2-loops-vpus
\nclass Solution {\npublic:\n vector<int> threeEqualParts(vector<int>& A) {\n vector<int> dp;\n for(int i = 0 ; i < A.size(); i++) // this loop
fangzzh
NORMAL
2018-10-21T05:21:21.834299+00:00
2018-10-22T02:41:17.850837+00:00
1,434
false
```\nclass Solution {\npublic:\n vector<int> threeEqualParts(vector<int>& A) {\n vector<int> dp;\n for(int i = 0 ; i < A.size(); i++) // this loop is used to store the index of all 1s\n if(A[i]) dp.push_back(i);\n if(dp.size() % 3) return {-1, -1}; // if the number of 1s cannot be dev...
20
2
[]
2
three-equal-parts
[Python] O(n) Straightforward Solution
python-on-straightforward-solution-by-wt-ii7e
The solution is inspired by this Logical Thinking.\n\n\nclass Solution:\n def threeEqualParts(self, A: List[int]) -> List[int]:\n \n num_ones =
wt262
NORMAL
2020-06-08T19:17:20.398853+00:00
2020-06-08T19:21:15.788688+00:00
847
false
The solution is inspired by this [Logical Thinking](https://leetcode.com/problems/three-equal-parts/discuss/250203/Logical-Thinking).\n\n```\nclass Solution:\n def threeEqualParts(self, A: List[int]) -> List[int]:\n \n num_ones = sum(A)\n \n if num_ones == 0:\n return [0, 2]\n ...
18
0
[]
3
three-equal-parts
Python Easy Understand Solution With Explanation
python-easy-understand-solution-with-exp-ydrk
The idea is pretty straight forward, you just need some obervation before coding.\n1. If the solution exists, the number of 1 in list should be times of 3.\n2.
xiaozhi1
NORMAL
2018-10-21T07:12:48.104035+00:00
2018-10-22T17:29:30.776420+00:00
1,044
false
The idea is pretty straight forward, you just need some obervation before coding.\n1. If the solution exists, the number of 1 in list should be times of 3.\n2. Since the ending zeros affect the binary value ([1,1] != [1,1, 0]) and the leading zeros mean nothing, we can find the value of each part (I called potential in...
11
1
[]
2
three-equal-parts
Java O(n) Solution
java-on-solution-by-self_learner-a8hp
``` // The solution can be optimized to O(1) space, by not using StringBuilder, but only record the index of the first one digit of the right part. class Solut
self_learner
NORMAL
2018-10-21T03:28:51.600988+00:00
2018-10-23T05:35:41.101410+00:00
1,945
false
``` // The solution can be optimized to O(1) space, by not using StringBuilder, but only record the index of the first one digit of the right part. class Solution { public int[] threeEqualParts(int[] A) { if (A == null || A.length < 3) return new int[] {-1, -1}; int n = A.length; int cntOn...
10
1
[]
3
three-equal-parts
Java 10ms solution with O(n) time and O(1)space
java-10ms-solution-with-on-time-and-o1sp-641m
It is obviously that if the array can be divided into three part. The 1\'s number in each should be same. Besides, the 0\'s number in the last part should be th
zhuojiansen
NORMAL
2018-10-22T19:37:40.934740+00:00
2018-10-22T19:37:40.934780+00:00
683
false
It is obviously that if the array can be divided into three part. The 1\'s number in each should be same. Besides, the 0\'s number in the last part should be the true 0\'s number in the end of each part.\nWe get the 1\'s number and divide them into 3 parts. If numOf1 % 3 is not a integer then just return [-1, -1]. Supp...
9
1
[]
2
three-equal-parts
2 clean Python linear solutions
2-clean-python-linear-solutions-by-cthlo-6u1m
\nclass Solution:\n def threeEqualParts(self, arr: List[int]) -> List[int]:\n # count number of ones\n ones = sum(arr)\n if ones % 3 !=
cthlo
NORMAL
2021-07-17T13:28:56.593513+00:00
2021-07-18T20:37:20.718557+00:00
665
false
```\nclass Solution:\n def threeEqualParts(self, arr: List[int]) -> List[int]:\n # count number of ones\n ones = sum(arr)\n if ones % 3 != 0:\n return [-1, -1]\n elif ones == 0: # special case: all zeros\n return [0, 2]\n \n # find the start index of e...
8
0
['Python', 'Python3']
0
three-equal-parts
✅ Three Equal Parts | Java | Easy to understand | Detailed approach
three-equal-parts-java-easy-to-understan-0b8m
Intuition\n We can easily see that in order for the array to have equal parts, in each part the number of ones should be equal.\n The above implies that the tot
varunsharma-codes
NORMAL
2021-07-17T20:41:31.712662+00:00
2021-07-17T20:41:48.895980+00:00
575
false
**Intuition**\n* We can easily see that in order for the array to have equal parts, in each part the number of ones should be equal.\n* The above implies that the `totalOnes` should be divisible by 3.\n* Now lets understand of basic cases\n* `totalOnes = 0`: If this is the case any two indices can divide into 3 equal p...
7
1
['Java']
0
three-equal-parts
Java Solution with detailed Comments & Examples
java-solution-with-detailed-comments-exa-7ruf
\nclass Solution {\n public int[] threeEqualParts(int[] arr) {\n int ones = 0;\n for (int i : arr) if (i == 1) ones++;\n\n\t\t/**\n\t\t * The f
gogagubi
NORMAL
2021-07-17T15:39:07.667699+00:00
2021-07-17T17:06:13.299612+00:00
439
false
```\nclass Solution {\n public int[] threeEqualParts(int[] arr) {\n int ones = 0;\n for (int i : arr) if (i == 1) ones++;\n\n\t\t/**\n\t\t * The first base case is a hardcoded case when there are only zeros in the array. \n\t\t * Since the array size is at least 3 in the case of only zeros there are st...
5
1
['Java']
1
three-equal-parts
JAVA 50ms with my thinking process
java-50ms-with-my-thinking-process-by-ha-if6c
thinking process is chinese:https://www.jianshu.com/p/c8dbaaf87644\n\npublic int[] threeEqualParts(int[] A) {\n int i = 0, j = A.length-1;\n Deque
happyleetcode
NORMAL
2018-10-21T03:26:35.896784+00:00
2018-10-22T05:09:58.165040+00:00
526
false
thinking process is chinese:https://www.jianshu.com/p/c8dbaaf87644\n```\npublic int[] threeEqualParts(int[] A) {\n int i = 0, j = A.length-1;\n Deque<Integer> pre = new ArrayDeque<>();\n Deque<Integer> mid = new ArrayDeque<>();\n Deque<Integer> last = new ArrayDeque<>();\n if(A[0]!=0)...
5
2
[]
2
three-equal-parts
[C++] Easy, Clean Solution
c-easy-clean-solution-by-rsgt24-btpl
Solution:\n\nclass Solution {\npublic:\n vector<int> threeEqualParts(vector<int>& ar) {\n int cnt0 = 0, cnt1 = 0;\n for(auto i: ar){\n
rsgt24
NORMAL
2021-07-17T22:39:33.046182+00:00
2021-07-17T22:41:09.259746+00:00
368
false
**Solution:**\n```\nclass Solution {\npublic:\n vector<int> threeEqualParts(vector<int>& ar) {\n int cnt0 = 0, cnt1 = 0;\n for(auto i: ar){\n if(i)\n cnt1++;\n else\n cnt0++;\n }\n \n if(cnt1 % 3 != 0)\n return {-1, -1}...
4
0
['Bit Manipulation', 'C']
0
three-equal-parts
12 lines js solution faster than 100% js solutions
12-lines-js-solution-faster-than-100-js-g4wkt
First I found out how many ones are in the array. Then I checked if the array is dividable into 3 with the same amount of ones. Then I iterated from the end to
iamawebgeek
NORMAL
2021-07-17T08:46:04.895325+00:00
2021-07-17T08:55:01.153846+00:00
320
false
First I found out how many ones are in the array. Then I checked if the array is dividable into 3 with the same amount of ones. Then I iterated from the end to find out the number. Once I had the number all I had to do is to validate if the sequence of first and second sets match.\n```\n/**\n * @param {number[]} arr\n ...
4
0
['JavaScript']
1
three-equal-parts
[C++] O(n) time, O(n) space, easy to implement
c-on-time-on-space-easy-to-implement-by-5uxti
Store every 1\'s position, easy to know the first 1\'s position of three parts as [x, y, z].\nThen verify three parts whether equal.\n\n\nclass Solution {\npubl
wenpeng2021
NORMAL
2020-06-19T01:30:13.237437+00:00
2020-06-19T01:30:13.237479+00:00
254
false
Store every 1\'s position, easy to know the first 1\'s position of three parts as [x, y, z].\nThen verify three parts whether equal.\n\n```\nclass Solution {\npublic:\nvector<int> threeEqualParts(vector<int>& v) {\n int n = v.size();\n vector<int> ones;\n for (int i = 0; i < n; ++i) if (v[i]) ones.push_back(i);\n i...
4
0
[]
1
three-equal-parts
python O(n) 108ms
python-on-108ms-by-dashidhy-26ca
``` class Solution: def threeEqualParts(self, A): """ :type A: List[int] :rtype: List[int] """ N = len(A) if
dashidhy
NORMAL
2018-10-21T03:15:40.161992+00:00
2018-10-21T03:15:40.162044+00:00
383
false
``` class Solution: def threeEqualParts(self, A): """ :type A: List[int] :rtype: List[int] """ N = len(A) if N < 3: return [-1, -1] num_ones = sum(A) if num_ones % 3 != 0: return [-1, -1] if num_on...
4
0
[]
3
three-equal-parts
C++ | Detailed Explanation with Proofs |
c-detailed-explanation-with-proofs-by-pr-0ens
Intuition\n Describe your first thoughts on how to solve this problem. \nNot much intuitive as it does not invole any famous algorithm but a bit of observation.
prathamn1
NORMAL
2023-01-27T07:08:33.190994+00:00
2023-01-27T07:08:33.191026+00:00
389
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nNot much intuitive as it does not invole any famous algorithm but a bit of observation.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- First and the only thing to notice is that if we have to divide the array in...
3
0
['Array', 'Math', 'C++']
2
three-equal-parts
100% TC easy python solution
100-tc-easy-python-solution-by-nitanshri-ca5w
Hint\nCount the num of ones, and think how they will be split between the 3 segments :)\n\ndef threeEqualParts(self, arr: List[int]) -> List[int]:\n\tn = len(ar
nitanshritulon
NORMAL
2022-09-04T10:21:06.473422+00:00
2022-09-04T10:21:06.473455+00:00
260
false
Hint\nCount the num of ones, and think how they will be split between the 3 segments :)\n```\ndef threeEqualParts(self, arr: List[int]) -> List[int]:\n\tn = len(arr)\n\tpos = [i for i in range(n) if(arr[i])]\n\tl = len(pos)\n\tif(l == 0):\n\t\treturn [0, 2]\n\tif(l % 3):\n\t\treturn [-1, -1]\n\tones = l//3\n\tc = 0\n\t...
3
0
['Python', 'Python3']
0
three-equal-parts
[C++/Python] observation leads to solutions & 3-pointer solution
cpython-observation-leads-to-solutions-3-gnfz
Approach 1:\nTrain of thought [1]:\nStep 1: observation: Find 1st 1 of 3rd sub-array\nStep 2: locate the ending of 1st and 2nd sub-arrays\n\nTime Complexity: O(
codedayday
NORMAL
2021-07-17T19:42:16.647327+00:00
2021-07-18T05:18:43.938734+00:00
286
false
Approach 1:\nTrain of thought [1]:\nStep 1: observation: Find 1st 1 of 3rd sub-array\nStep 2: locate the ending of 1st and 2nd sub-arrays\n\nTime Complexity: O(N)\nSpace Complexity: O(1)\n\n```\nclass Solution {\npublic:\n vector<int> threeEqualParts(vector<int>& A) {\n int tot = 0;\n for(auto num: A) ...
3
0
['C', 'Python']
0
three-equal-parts
[C++] Single Pass Solution Explained, ~100% Time, ~90% Space
c-single-pass-solution-explained-100-tim-z4t9
Nice problem that becomes much easier once you figure out a few tricks:\n first of all, in order for the problem to be solvable, we need an amount of 1s divisib
ajna
NORMAL
2021-07-17T19:41:09.101711+00:00
2021-07-17T19:52:03.326092+00:00
256
false
Nice problem that becomes much easier once you figure out a few tricks:\n* first of all, in order for the problem to be solvable, we need an amount of `1`s divisible by `3`;\n* with this insight, we can figure out that while we move on checking for the amount of `1`s, we might as well keep track of pointers separating ...
3
1
['C', 'C++']
0
three-equal-parts
O(n) Java Runtime: 2 ms, faster than 87.50% of Java online submissions
on-java-runtime-2-ms-faster-than-8750-of-1ucl
\npublic int[] threeEqualParts(int[] arr) {\n int oneCount = 0;\n int n = arr.length;\n for(int t : arr){\n if(t == 1) oneCount+
watermalon
NORMAL
2021-07-17T12:29:28.926392+00:00
2021-07-17T12:29:28.926421+00:00
212
false
```\npublic int[] threeEqualParts(int[] arr) {\n int oneCount = 0;\n int n = arr.length;\n for(int t : arr){\n if(t == 1) oneCount++;\n }\n if(oneCount == 0) return new int[]{0,n-1};\n if(oneCount % 3 != 0) return new int[]{-1,-1};\n int averageOneCount = oneC...
3
1
['Java']
0
three-equal-parts
[python 3] brutal force with explanation
python-3-brutal-force-with-explanation-b-oaeq
First, special cases:\n \tIf there is no 1 in list, we can just return [0, len(n)-1].\n \tIf number of 1 cant be divied by 3, return [-1, -1].\n\nThen we find t
Bakerston
NORMAL
2021-02-05T23:21:31.163448+00:00
2021-02-05T23:21:31.163477+00:00
120
false
First, special cases:\n* \tIf there is no 1 in list, we can just return ```[0, len(n)-1]```.\n* \tIf number of 1 cant be divied by 3, return ```[-1, -1]```.\n\nThen we find the valid binary number, since the leading zeros dont matter here, we just need to find the binary start with "1", therefore we traverse the list u...
3
1
[]
0
three-equal-parts
Ruby with Regexp
ruby-with-regexp-by-jieqi-bckz
\ndef three_equal_parts(a)\n return [0, a.size-1] if a.all?(&:zero?)\n s = a.join\n tmp = s.match(/^0*(1.*)0*(\\1)0*(\\1)$/)[1]\n i = s.index(tmp) + tmp.siz
jieqi
NORMAL
2020-06-06T04:50:44.640299+00:00
2020-06-06T04:50:44.640335+00:00
47
false
```\ndef three_equal_parts(a)\n return [0, a.size-1] if a.all?(&:zero?)\n s = a.join\n tmp = s.match(/^0*(1.*)0*(\\1)0*(\\1)$/)[1]\n i = s.index(tmp) + tmp.size - 1\n j = s.index(tmp, i+1) + tmp.size\n [ i , j ]\nrescue => e\n [-1, -1]\nend\n```
3
0
[]
0
three-equal-parts
my O( N ) C++ solution
my-o-n-c-solution-by-fish1996-mdwa
At first, we calculate the total number of \'1\', and check if it can be divided by 3. Because each part must contain the same number of \'1\'.\nIf not, we just
fish1996
NORMAL
2019-08-16T14:27:52.955030+00:00
2019-08-16T14:31:33.277238+00:00
285
false
At first, we calculate the total number of \'1\', and check if it can be divided by 3. Because each part must contain the same number of \'1\'.\nIf not, we just return { -1, -1 }.\nThen, we calculate the number of \'0\' at the end of the array, which means that we won\'t stop until we find the first \'1\' at the end. I...
3
1
[]
0
three-equal-parts
C++ Easy to Understand O(n)
c-easy-to-understand-on-by-ekaanshgoel-e4yz
If we can divide the number of 1s into 3 equal parts, we can just check the three parts for equality. Dividing 1s into 3 partitions and checking for equality i
ekaanshgoel
NORMAL
2018-10-21T05:05:38.200501+00:00
2018-10-22T07:21:34.582211+00:00
604
false
If we can divide the number of 1s into 3 equal parts, we can just check the three parts for equality. Dividing 1s into 3 partitions and checking for equality is a necessary and sufficient condition for the answer. We need to take care of 0s only in the third partition because they will be trailing zeros always and wil...
3
1
[]
1
three-equal-parts
[C++] O(n^2) time O(1) space, 96ms, with explanation
c-on2-time-o1-space-96ms-with-explanatio-ana2
For this problem, we can regarde the string as the type, 000(some zeros)***(a binary number starts with \'1\' and has length k)000(some zeros)***(same binary nu
strange
NORMAL
2018-10-21T03:18:06.427336+00:00
2018-10-21T20:13:52.533655+00:00
271
false
For this problem, we can regarde the string as the type, `000(some zeros)***(a binary number starts with \'1\' and has length k)000(some zeros)***(same binary number)0000(some zeros)***(same binary number)`.\n\nSo what we need to do is try to find the first no-zero bit and iterate the value of k from `1` to `(l-start)/...
3
3
[]
2
three-equal-parts
Three Equal Parts | Easiest Approach | O(N) Time complexity and O(1) Space Complexity |
three-equal-parts-easiest-approach-on-ti-pfj0
Intuition\nThe problem requires us to divide an array of zeros and ones into three parts, each representing the same binary value. At first glance, it might see
nah_man
NORMAL
2024-05-21T06:59:30.468167+00:00
2024-05-21T06:59:30.468201+00:00
321
false
# Intuition\nThe problem requires us to divide an array of zeros and ones into three parts, each representing the same binary value. At first glance, it might seem complex, but by breaking it down into steps, we can solve it systematically. The key insight is to ensure that the count of 1s in each part is equal, which ...
2
0
['Array', 'Math', 'Two Pointers', 'Greedy', 'C++']
2
three-equal-parts
Simple Python Solution with explanation 🔥
simple-python-solution-with-explanation-qqnno
Approach\n\n> divide the array into three non-empty parts such that all of these parts represent the same binary value\n\nSame binary value means \n\n each part
wilspi
NORMAL
2023-01-17T18:13:38.338738+00:00
2023-01-17T22:29:18.633711+00:00
944
false
# Approach\n\n> *divide the array into three non-empty parts such that all of these parts represent the same binary value*\n\nSame binary value means \n\n* each part has to have **<ins>equal number of ones<ins>** and zeroes\n* order of ones and zeroes should be same\n\n\n### Part 1: Equal number of ones and ending zero...
2
0
['Binary Search', 'Prefix Sum', 'Python', 'Python3']
0
three-equal-parts
Three Pointers || C++ || O(N) time || O(1) space
three-pointers-c-on-time-o1-space-by-aka-n02c
Approach: First of all, it is necessary for the given array to have sufficient count of ones which can be equally divided among three groups. If not, the answer
Aka_YS
NORMAL
2021-07-18T07:04:50.667653+00:00
2021-07-18T07:04:50.667683+00:00
105
false
**Approach:** First of all, it is necessary for the given array to have sufficient count of ones which can be equally divided among three groups. If not, the answer is not possible. If yes, find the occurances of the first ones of each group. Now, simply iterate over these pointers and check whether you can atleast rea...
2
0
['C']
0