Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02867cf

Browse files
committed
continuation
1 parent e3c9c8d commit 02867cf

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package DataAndAlgoL.Chpt7BinaryHeapsPriorityQs;
2+
3+
public class Heap {
4+
public int[] array;
5+
public int count; //number of elements in heap
6+
public int capacity; // size of the heap
7+
public int heap_type; // Min heap or Max Heap
8+
9+
10+
//contructor
11+
public Heap(int capacitty , int heap_type){
12+
this.heap_type= heap_type;
13+
this.count= 0; //default value
14+
this.capacity= capacity;
15+
this.array= new int[capacity];
16+
}
17+
18+
//Parent of a node, complexity -> O(1)
19+
public int parent(int i){
20+
if(i <= 0 || i >= this.count){
21+
return -1;
22+
}
23+
24+
return i-1/2;
25+
}
26+
27+
//Children of node
28+
29+
//left child
30+
public int leftChild(int i){
31+
int left= 2*i+1;
32+
33+
if(left >= this.count){
34+
return -1;
35+
}
36+
37+
return left;
38+
}
39+
40+
public int rightChild(int i){
41+
int right= 2*i+2;
42+
if(right >= this.count){
43+
return -1;
44+
}
45+
46+
return right;
47+
}
48+
49+
//get max element in heap
50+
public int getMax(){
51+
if(this.count==0){
52+
return -1;
53+
}
54+
55+
return this.array[0]; //if it is max heap root (array[0]) is the largest element in heap
56+
}
57+
58+
//Heapifying an element at location i for max heap
59+
public void percolateDown(int i){
60+
int l, r, max, temp;
61+
l=leftChild(i);
62+
r=rightChild(i);
63+
64+
if(l != -1 && this.array[l] > this.array[i]){ //check which node is max, parent (nnew node i) or its child
65+
max=l;
66+
}else{
67+
max=i;
68+
}
69+
70+
if(r != -1 && this.array[r] > this.array[max]){
71+
max=r;
72+
}
73+
//Swap array[i] with arra[max]
74+
if(max != i){
75+
temp= this.array[i];
76+
this.array[i]= this.array[max];
77+
this.array[max]=temp;
78+
}
79+
80+
percolateDown(max); //recursive call untill all nodes are correct location.
81+
}
82+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Comparator;
2+
import java.util.PriorityQueue;
3+
4+
public class KclosestPointsToOrigin_Lt973 {
5+
public static void main(String[] args) {
6+
7+
}
8+
9+
public static int[][] kClosest(int[][] points, int k) {
10+
// If the k points close to origin are all the points in the array, return the array
11+
if (k == points.length){
12+
return points;
13+
}
14+
15+
// priority queue holds each point as max heap, thus sorted by the distance from origin in descending order
16+
PriorityQueue<int[]> pq= new PriorityQueue<>(k, new Comparator<int[]>() {
17+
// Comparator is used to compare two points and return the one that is closer to the origin
18+
// compares which points ? -> compares the distance from origin of each point
19+
@Override
20+
public int compare(int[] a, int[] b){
21+
return (b[0] *b[0] +b[1]*b[1]) - (a[0] *a[0] + a[1]* a[1]); // b-a because we want max heap
22+
}
23+
});
24+
25+
for(int[] point: points){
26+
pq.add(point);
27+
if(pq.size() > k){ // When size of PQ is > than k we poll remove first element (root) as it is the furthest from the origin
28+
pq.poll();
29+
}
30+
31+
}
32+
33+
return pq.toArray(new int[0][0]);
34+
}
35+
}

‎LeetCode/MoveZeroes_Lt283.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.Arrays;
2+
3+
public class MoveZeroes_Lt283 {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
public static void moveZeroes(int[] nums) {
9+
if (nums == null || nums.length ==0){
10+
return;
11+
}
12+
13+
int insertPos=0;
14+
15+
for(int i=0; i< nums.length; i++){
16+
if(nums[i] == 0){
17+
insertPos++;
18+
}else if(insertPos !=0){
19+
nums[i-insertPos]= nums[i];
20+
nums[i]=0;
21+
}
22+
}
23+
24+
System.out.println(Arrays.toString(nums));
25+
}
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.Arrays;
2+
3+
public class SquaresOfSortedArray_Lt977 {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
public int[] sortedSquares(int[] nums) {
9+
for(int i=0; i< nums.length; i++){
10+
nums[i]= (int) Math.pow(nums[i],2);
11+
}
12+
Arrays.sort(nums);
13+
return nums;
14+
}
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /