Skip to content

Commit 36e41ea

Browse files
committed
formatted code
1 parent df25ecf commit 36e41ea

File tree

2 files changed

+125
-113
lines changed

2 files changed

+125
-113
lines changed

Sort Array 0-1-2.md

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,91 @@
11
### [Back2Home](https://github.com/CodingWallah/Arrays-DSA-Coding-Questions) | [Go2Video](#)
2+
3+
4+
5+
### Solution 1 `Best`
6+
#### Time Complexity: `O(n)`
7+
#### Space Complexity: `O(1)`
28
```java
39

4-
//Best Solution 1 || Time = O(n) , Space = O(1)
510
class Solution {
6-
public void sortColors(int[] nums) {
7-
int start = 0;
8-
int end = nums.length - 1;
9-
int ptr = 0, temp = 0;
10-
while (ptr <= end) {
11-
switch (nums[ptr]) {
12-
case 0: {
13-
temp = nums[start];
14-
nums[start] = nums[ptr];
15-
nums[ptr] = temp;
16-
start++;
17-
ptr++;
18-
break;
19-
}
20-
case 1:
21-
ptr++;
22-
break;
23-
case 2: {
24-
temp = nums[ptr];
25-
nums[ptr] = nums[end];
26-
nums[end] = temp;
27-
end--;
28-
break;
29-
}
30-
}
11+
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;
3136
}
37+
}
3238
}
39+
}
3340
}
34-
3541
```
3642

43+
##
44+
45+
### Solution 2
46+
#### Time Complexity: `O(n)`
47+
#### Space Complexity: `O(1)`
3748
```java
3849

39-
//Solution2 - time complexity = O(n) | space = O(1)
4050
class Solution {
41-
public void sortColors(int[] nums) {
42-
int i=0, cnt0 = 0, cnt1 = 0, cnt2 = 0;
43-
for (i = 0; i < nums.length; i++) {
44-
if(nums[i]==0)
45-
cnt0++;
46-
else if(nums[i]==1)
47-
cnt1++;
48-
else if(nums[i]==2)
49-
cnt2++;
50-
}
51-
52-
i = 0;
53-
54-
while (cnt0 > 0) {
55-
nums[i++] = 0;
56-
cnt0--;
57-
}
58-
59-
while (cnt1 > 0) {
60-
nums[i++] = 1;
61-
cnt1--;
62-
}
63-
64-
while (cnt2 > 0) {
65-
nums[i++] = 2;
66-
cnt2--;
67-
}
51+
public void sortColors(int[] nums) {
52+
int i = 0, count0 = 0, count1 = 0, count2 = 0;
53+
for (i = 0; i < nums.length; i++) {
54+
if (nums[i] == 0)
55+
count0++;
56+
else if (nums[i] == 1)
57+
count1++;
58+
else if (nums[i] == 2)
59+
count2++;
60+
}
61+
62+
i = 0;
63+
64+
while (count0 > 0) {
65+
nums[i++] = 0;
66+
count0--;
67+
}
68+
69+
while (count1 > 0) {
70+
nums[i++] = 1;
71+
count1--;
6872
}
73+
74+
while (count2 > 0) {
75+
nums[i++] = 2;
76+
count2--;
77+
}
78+
}
6979
}
7080

7181
```
82+
### Solution 3 `Not Recommended`
83+
#### Time Complexity: `O(n*logn)`
7284

7385
```java
74-
75-
//sol3 || Time = O(nlogn)
76-
class Solution {
77-
public void sortColors(int[] nums) {
78-
Arrays.sort(nums);
79-
}
86+
class Solution {
87+
public void sortColors(int[] nums) {
88+
Arrays.sort(nums);
89+
}
8090
}
8191
```

kth-largest-smallest.md

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

3-
1) min heap = uses for largest ?
3+
##
4+
5+
#### `When` max Heaps and when min Heaps and `why`?
46

7+
1) min heap = uses for largest ?
58
2) max heap = uses for smallest ?
69

7-
### WHEN max and when min HEAPS and WHY ?
10+
> #### :exclamation: k is small = min; | k is large = MAX;
811
9-
Note- k is small = min; | k is large = MAX;
10-
```java
12+
##
1113

12-
//sol1 = using sorting or trick
13-
// Time complexity = O(NlogN)
14+
### Solution 1 : Using Sorting
15+
#### Time Complexity: `O(n*logn)`
16+
```java
1417
class Solution {
1518
public int findKthLargest(int[] nums, int k) {
1619
Arrays.sort(nums);
1720
return nums[nums.length-k];
1821
}
1922
}
20-
2123
```
2224

23-
```java
25+
##
2426

25-
//MIN HEAP APPROACH = Best Solution O(N + NlogK) time complexity
27+
### Solution 2: Min Heap Approach
28+
#### Time Complexity: `O(n + n*logk)`
29+
`Best Solution`
30+
```java
2631
PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
27-
for(int i=0;i<k;i++)
28-
minheap.add(nums[i]);
29-
30-
31-
for(int i=k;i<nums.length;i++){
32-
if(nums[i]>minheap.peek()){
33-
minheap.poll();
34-
minheap.add(nums[i]);
35-
}
36-
}
32+
for(int i=0;i<k;i++)
33+
minheap.add(nums[i]);
34+
35+
for(int i=k;i<nums.length;i++){
36+
if(nums[i]>minheap.peek()){
37+
minheap.poll();
38+
minheap.add(nums[i]);
39+
}
40+
}
3741
return minheap.peek();
3842
```
39-
```java
4043

41-
//MIN HEAP EASY & CONCISE IMPLEMENTATION
42-
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
43-
for(int i : nums){
44-
pq.add(i);
45-
if(pq.size()>k){
46-
pq.remove();
47-
}
48-
}
49-
50-
return pq.remove();
44+
`Concise Implementation`
5145

46+
```java
47+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
48+
for(int i : nums){
49+
pq.add(i);
50+
if(pq.size()>k){
51+
pq.remove();
52+
}
53+
}
54+
return pq.remove();
5255
```
5356

57+
##
5458

59+
### Solution 3: Max Heap Approach
60+
#### Time Complexity: `O(n + n*logk)`
61+
`Best Solution`
5562
```java
56-
//MAX HEAP APPROACH = Best Solution O(N + NlogK) time complexity
5763
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
58-
for(int i=0;i<nums.length-k+1;i++)
59-
maxHeap.add(nums[i]);
60-
61-
for(int i=nums.length-k+1;i<nums.length;i++){
62-
if(nums[i]<maxHeap.peek()){
63-
maxHeap.poll();
64-
maxHeap.add(nums[i]);
65-
}
66-
}
67-
68-
return maxHeap.peek();
64+
for(int i=0;i<nums.length-k+1;i++)
65+
maxHeap.add(nums[i]);
66+
67+
for(int i=nums.length-k+1;i<nums.length;i++){
68+
if(nums[i]<maxHeap.peek()){
69+
maxHeap.poll();
70+
maxHeap.add(nums[i]);
6971
}
70-
72+
}
73+
return maxHeap.peek();
7174
```
7275

76+
`Consice Implementation`
7377
```java
74-
//MAX HEAP EASY & CONCISE IMPLEMENTATION
7578
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
76-
for(int i : nums){
77-
pq.add(i);
78-
79-
for(int i=1;i<k;i++)
80-
pq.poll();
81-
82-
return pq.peek();
83-
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();
8486
```

0 commit comments

Comments
 (0)