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 90a6ef9

Browse files
committed
update
1 parent 61094b3 commit 90a6ef9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+225
-41
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package DataAndAlgoL.Chpt7BinaryHeapsPriorityQs;
2+
3+
4+
/*
5+
* Given a min heap, what is an algorithm to find the maximum element
6+
* In a min heap, the max element is always at the leaf only
7+
*/
8+
public class Problem7 {
9+
public static void main(String[] args) {
10+
Heap h= new Heap(5, 0);
11+
12+
h.Insert(5);
13+
h.Insert(14);
14+
h.Insert(2);
15+
h.Insert(10);
16+
h.Insert(21);
17+
18+
System.out.println(FindMaxElementMinHeap(h));
19+
}
20+
21+
public static int FindMaxElementMinHeap(Heap h){
22+
int max= -1;
23+
for( int i= (h.count+1)/2; i < h.count; i++){
24+
if(h.array[i] >max){
25+
max=h.array[i];
26+
}
27+
}
28+
29+
return max;
30+
}
31+
}

‎LeetCode/Array Exercises/ArrayFromPermutation.java‎

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package Grind169.Arrays;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
6+
public class TwoSum_1 {
7+
public static void main(String[] args) {
8+
int nums[]={2,7,11,15};
9+
int target=9;
10+
System.out.println(Arrays.toString(twoSum(nums, target)));
11+
12+
}
13+
14+
public static int[] twoSum(int[] nums, int target){
15+
int res[]= new int[2]; //stores both indices of num array that have values that add up to target
16+
HashMap<Integer, Integer> map= new HashMap<>(); //stores numbers of num array as key and its indices as values in map
17+
18+
//iterated through the array
19+
for(int i=0; i< nums.length; i++){
20+
if(map.containsKey(target- nums[i])){ //checks if a key with value target- nums[i] exists in hashmap
21+
//if it does
22+
res[1]=i; //store the current i at position 1 of array result
23+
res[0]=map.get(target - nums[i]); //store at position 0 the value of the number in the hashmap that is equal to target-nums[i]
24+
}
25+
//if the if statement is not satisfied, put in map current i and its value from nums array
26+
map.put(nums[i], i);
27+
}
28+
29+
return res; //return result array
30+
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Grind169.Heaps;
2+
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
6+
public class KclosestPointsToOrigin_973 {
7+
public static void main(String[] args) {
8+
9+
}
10+
11+
public static int[][] kClosest(int[][] points, int k) {
12+
// If the k points close to origin are all the points in the array, return the array
13+
if (k == points.length){
14+
return points;
15+
}
16+
17+
// priority queue holds each point as max heap, thus sorted by the distance from origin in descending order
18+
PriorityQueue<int[]> pq= new PriorityQueue<>(k, new Comparator<int[]>() {
19+
// Comparator is used to compare two points and return the one that is closer to the origin
20+
// compares which points ? -> compares the distance from origin of each point
21+
@Override
22+
public int compare(int[] a, int[] b){
23+
return (b[0] *b[0] +b[1]*b[1]) - (a[0] *a[0] + a[1]* a[1]); // b-a because we want max heap
24+
}
25+
});
26+
27+
for(int[] point: points){
28+
pq.add(point);
29+
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
30+
pq.poll();
31+
}
32+
33+
}
34+
35+
return pq.toArray(new int[0][0]);
36+
}
37+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package Grind169.Heaps;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.PriorityQueue;
6+
7+
/*
8+
Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task.
9+
Tasks could be done in any order. Each task is done in one unit of time.
10+
For each unit of time, the CPU could complete either one task or just be idle.
11+
However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array),
12+
that is that there must be at least n units of time between any two same tasks.
13+
Return the least number of units of times that the CPU will take to finish all the given tasks.
14+
15+
Example 1:
16+
17+
Input: tasks = ["A","A","A","B","B","B"], n = 2
18+
Output: 8
19+
Explanation:
20+
A -> B -> idle -> A -> B -> idle -> A -> B
21+
There is at least 2 units of time between any two same tasks.
22+
*/
23+
24+
public class TaskScheduler_621 {
25+
public static void main(String[] args) {
26+
char[] tasks= {'A','A','A','B','B','B'};
27+
int n=2;
28+
29+
System.out.println(leastInterval(tasks, n));
30+
}
31+
32+
public static int leastInterval(char[] tasks, int n) {
33+
//if there is no cooldown time between same tasks, units of time are the amount of tasks
34+
if( n==0){
35+
return tasks.length;
36+
}
37+
38+
//To store the occurrences of each task
39+
HashMap<Character, Integer> map= new HashMap<>();
40+
41+
for(int i=0; i< tasks.length; i++){
42+
map.put(tasks[i], map.getOrDefault(tasks[i], 0)+1); // if tasks are equal increase value, default value for each task that has no occurrence
43+
}
44+
45+
//by default pq is a Min heap; hence the reverse order
46+
PriorityQueue<Integer> pq= new PriorityQueue<>(Collections.reverseOrder());
47+
48+
//add all the frequency values into pq
49+
pq.addAll(map.values());
50+
51+
//find the max frequency
52+
int maxFreq= pq.poll(); // polls highest frequency by priority
53+
54+
//total idle time is the maxFrequency of a task * time units n of cooldown
55+
int totalIdleTime=(maxFreq-1) * n;
56+
57+
//Looping through all the frequencies
58+
while(!pq.isEmpty()){
59+
//find current frequency
60+
int currentFreq= pq.poll();
61+
62+
//if it is equal to max frequency then we need to add 1 since we to consider last task
63+
//Example A -> B -> Idle -> A -> B -> Idle -> A -> B (this B has to be considered)
64+
if(maxFreq == currentFreq){
65+
totalIdleTime-=currentFreq;
66+
totalIdleTime+=1;
67+
}else{
68+
//else we just keep subtracting idle time
69+
totalIdleTime-=currentFreq;
70+
}
71+
}
72+
73+
//if its greater than 0 then add it to tasks.length
74+
if(totalIdleTime >0){
75+
return totalIdleTime + tasks.length;
76+
}else{
77+
return tasks.length;
78+
}
79+
80+
}
81+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package Grind169.Heaps;
2+
3+
public class TopKFrequentWords_692 {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
9+
}

‎LeetCode/Add2Nums.java‎ renamed to ‎LeetCode/LeetcodeV1/Add2Nums.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12

23

34
//Definition for singly-linked list.

‎LeetCode/AddBinary.java‎ renamed to ‎LeetCode/LeetcodeV1/AddBinary.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12

23

34
/*
File renamed without changes.

‎LeetCode/ClimbingStairs.java‎ renamed to ‎LeetCode/LeetcodeV1/ClimbingStairs.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
public class ClimbingStairs {
23

34
/*

0 commit comments

Comments
(0)

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