Skip to content

Commit 7a62a0e

Browse files
committed
arrange in proper order
1 parent 3f201b9 commit 7a62a0e

File tree

2 files changed

+66
-63
lines changed

2 files changed

+66
-63
lines changed

Sort Array 0-1-2.md

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,19 @@
11
### [Back2Home](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions) | [Go2Video](#)
22

3+
##
34

5+
### Solution 1 `Not Recommended`
6+
#### Time Complexity: `O(n*logn)`
47

5-
### Solution 1 `Best`
6-
#### Time Complexity: `O(n)`
7-
#### Space Complexity: `O(1)`
88
```java
9-
109
class Solution {
1110
public void sortColors(int[] nums) {
12-
int start = 0;
13-
int end = nums.length - 1;
14-
int ptr = 0, temp = 0;
15-
16-
while (ptr <= end) {
17-
switch (nums[ptr]) {
18-
case 0: {
19-
temp = nums[start];
20-
nums[start] = nums[ptr];
21-
nums[ptr] = temp;
22-
start++;
23-
ptr++;
24-
break;
25-
}
26-
case 1: {
27-
ptr++;
28-
break;
29-
}
30-
case 2: {
31-
temp = nums[ptr];
32-
nums[ptr] = nums[end];
33-
nums[end] = temp;
34-
end--;
35-
break;
36-
}
37-
}
38-
}
11+
Arrays.sort(nums);
3912
}
4013
}
4114
```
4215

43-
##
44-
45-
### Solution 2
16+
### Solution 2 `Alternative`
4617
#### Time Complexity: `O(n)`
4718
#### Space Complexity: `O(1)`
4819
```java
@@ -77,15 +48,43 @@ class Solution {
7748
}
7849
}
7950
}
80-
8151
```
82-
### Solution 3 `Not Recommended`
83-
#### Time Complexity: `O(n*logn)`
52+
53+
### Solution 3 `Best`
54+
#### Time Complexity: `O(n)`
55+
#### Space Complexity: `O(1)`
8456

8557
```java
58+
8659
class Solution {
8760
public void sortColors(int[] nums) {
88-
Arrays.sort(nums);
61+
int start = 0;
62+
int end = nums.length - 1;
63+
int ptr = 0, temp = 0;
64+
65+
while (ptr <= end) {
66+
switch (nums[ptr]) {
67+
case 0: {
68+
temp = nums[start];
69+
nums[start] = nums[ptr];
70+
nums[ptr] = temp;
71+
start++;
72+
ptr++;
73+
break;
74+
}
75+
case 1: {
76+
ptr++;
77+
break;
78+
}
79+
case 2: {
80+
temp = nums[ptr];
81+
nums[ptr] = nums[end];
82+
nums[end] = temp;
83+
end--;
84+
break;
85+
}
86+
}
87+
}
8988
}
9089
}
91-
```
90+
```

kth-largest-smallest.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
### Solution 1 : Using Sorting
1515
#### Time Complexity: `O(n*logn)`
16+
`Not Recommended`
1617
```java
1718
class Solution {
1819
public int findKthLargest(int[] nums, int k) {
@@ -26,6 +27,20 @@ class Solution {
2627

2728
### Solution 2: Min Heap Approach
2829
#### Time Complexity: `O(n + n*logk)`
30+
31+
`Concise Implementation`
32+
33+
```java
34+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
35+
for(int i : nums){
36+
pq.add(i);
37+
if(pq.size()>k){
38+
pq.remove();
39+
}
40+
}
41+
return pq.remove();
42+
```
43+
2944
`Best Solution`
3045
```java
3146
PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
@@ -41,23 +56,24 @@ PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
4156
return minheap.peek();
4257
```
4358

44-
`Concise Implementation`
4559

60+
##
61+
62+
### Solution 3: Max Heap Approach
63+
#### Time Complexity: `O(n + n*logk)`
64+
65+
`Consice Implementation`
4666
```java
47-
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
67+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
4868
for(int i : nums){
4969
pq.add(i);
50-
if(pq.size()>k){
51-
pq.remove();
52-
}
53-
}
54-
return pq.remove();
70+
71+
for(int i=1;i<k;i++)
72+
pq.poll();
73+
74+
return pq.peek();
5575
```
5676

57-
##
58-
59-
### Solution 3: Max Heap Approach
60-
#### Time Complexity: `O(n + n*logk)`
6177
`Best Solution`
6278
```java
6379
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
@@ -71,16 +87,4 @@ PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseO
7187
}
7288
}
7389
return maxHeap.peek();
74-
```
75-
76-
`Consice Implementation`
77-
```java
78-
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
79-
for(int i : nums){
80-
pq.add(i);
81-
82-
for(int i=1;i<k;i++)
83-
pq.poll();
84-
85-
return pq.peek();
86-
```
90+
```

0 commit comments

Comments
 (0)