Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 38 additions & 39 deletions Sort Array 0-1-2.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,19 @@
### [Back2Home](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions) | [Go2Video](#)

##

### Solution 1 `Not Recommended`
#### Time Complexity: `O(n*logn)`

### Solution 1 `Best`
#### Time Complexity: `O(n)`
#### Space Complexity: `O(1)`
```java

class Solution {
public void sortColors(int[] nums) {
int start = 0;
int end = nums.length - 1;
int ptr = 0, temp = 0;

while (ptr <= end) {
switch (nums[ptr]) {
case 0: {
temp = nums[start];
nums[start] = nums[ptr];
nums[ptr] = temp;
start++;
ptr++;
break;
}
case 1: {
ptr++;
break;
}
case 2: {
temp = nums[ptr];
nums[ptr] = nums[end];
nums[end] = temp;
end--;
break;
}
}
}
Arrays.sort(nums);
}
}
```

##

### Solution 2
### Solution 2 `Alternative`
#### Time Complexity: `O(n)`
#### Space Complexity: `O(1)`
```java
Expand Down Expand Up @@ -77,15 +48,43 @@ class Solution {
}
}
}

```
### Solution 3 `Not Recommended`
#### Time Complexity: `O(n*logn)`

### Solution 3 `Best`
#### Time Complexity: `O(n)`
#### Space Complexity: `O(1)`

```java

class Solution {
public void sortColors(int[] nums) {
Arrays.sort(nums);
int start = 0;
int end = nums.length - 1;
int ptr = 0, temp = 0;

while (ptr <= end) {
switch (nums[ptr]) {
case 0: {
temp = nums[start];
nums[start] = nums[ptr];
nums[ptr] = temp;
start++;
ptr++;
break;
}
case 1: {
ptr++;
break;
}
case 2: {
temp = nums[ptr];
nums[ptr] = nums[end];
nums[end] = temp;
end--;
break;
}
}
}
}
}
```
```
52 changes: 28 additions & 24 deletions kth-largest-smallest.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

### Solution 1 : Using Sorting
#### Time Complexity: `O(n*logn)`
`Not Recommended`
```java
class Solution {
public int findKthLargest(int[] nums, int k) {
Expand All @@ -26,6 +27,20 @@ class Solution {

### Solution 2: Min Heap Approach
#### Time Complexity: `O(n + n*logk)`

`Concise Implementation`

```java
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i : nums){
pq.add(i);
if(pq.size()>k){
pq.remove();
}
}
return pq.remove();
```

`Best Solution`
```java
PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
Expand All @@ -41,23 +56,24 @@ PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
return minheap.peek();
```

`Concise Implementation`

##

### Solution 3: Max Heap Approach
#### Time Complexity: `O(n + n*logk)`

`Consice Implementation`
```java
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
for(int i : nums){
pq.add(i);
if(pq.size()>k){
pq.remove();
}
}
return pq.remove();
for(int i=1;i<k;i++)
pq.poll();

return pq.peek();
```

##

### Solution 3: Max Heap Approach
#### Time Complexity: `O(n + n*logk)`
`Best Solution`
```java
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
Expand All @@ -71,16 +87,4 @@ PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseO
}
}
return maxHeap.peek();
```

`Consice Implementation`
```java
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
for(int i : nums){
pq.add(i);

for(int i=1;i<k;i++)
pq.poll();

return pq.peek();
```
```