Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d97f033

Browse files
committed
2 parents d1d363c + 90a6ef9 commit d97f033

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
/*

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
/*

LeetCode/ConcatenationArr.java renamed to LeetCode/LeetcodeV1/ConcatenationArr.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class ConcatenationArr {

LeetCode/FindPeakElementHigherThanElements.java renamed to LeetCode/LeetcodeV1/FindPeakElementHigherThanElements.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 FindPeakElementHigherThanElements {
23
public static void main(String[] args) {
34
int[] array= {5,10, 20,15};

LeetCode/InsertInterval.java renamed to LeetCode/LeetcodeV1/InsertInterval.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.LinkedList;
23
/*
34
* You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and

LeetCode/KclosestPointsToOrigin_Lt973.java renamed to LeetCode/LeetcodeV1/KclosestPointsToOrigin_Lt973.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Comparator;
23
import java.util.PriorityQueue;
34

LeetCode/LengthOfLastWord.java renamed to LeetCode/LeetcodeV1/LengthOfLastWord.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 LengthOfLastWord {
23
public static void main(String[] args) {
34
System.out.println(lengthOfLastWord("Hello World"));

LeetCode/LongestSubstringWithoutRepetitions.java renamed to LeetCode/LeetcodeV1/LongestSubstringWithoutRepetitions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.HashMap;
23

34
public class LongestSubstringWithoutRepetitions {

LeetCode/Lt100.java renamed to LeetCode/LeetcodeV1/Lt100.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
//Definition for a binary tree node.
23
class TreeNode {
34
int val;

LeetCode/Lt142.java renamed to LeetCode/LeetcodeV1/Lt142.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 Lt142 {
23
public static void main(String[] args) {
34

LeetCode/Lt1470.java renamed to LeetCode/LeetcodeV1/Lt1470.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class Lt1470 {

LeetCode/Lt160.java renamed to LeetCode/LeetcodeV1/Lt160.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
/*
23
* Given the heads of two singly linked-lists headA and headB,
34
* return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

LeetCode/Lt2011.java renamed to LeetCode/LeetcodeV1/Lt2011.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 Lt2011 {
23
public static void main(String[] args) {
34
String[] operations={"X++","++X","--X","X--"};

LeetCode/Lt203.java renamed to LeetCode/LeetcodeV1/Lt203.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
/*Remove linked list elements that have the same val and variable val */
23
class ListNode {
34
int val;

LeetCode/Lt2469.java renamed to LeetCode/LeetcodeV1/Lt2469.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23
import java.util.Scanner;
34

LeetCode/Lt5.java renamed to LeetCode/LeetcodeV1/Lt5.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.HashMap;
23

34
public class Lt5 {

LeetCode/MergeSortedArray.java renamed to LeetCode/LeetcodeV1/MergeSortedArray.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class MergeSortedArray {

LeetCode/MoveZeroes_Lt283.java renamed to LeetCode/LeetcodeV1/MoveZeroes_Lt283.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class MoveZeroes_Lt283 {

LeetCode/PlusOne.java renamed to LeetCode/LeetcodeV1/PlusOne.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class PlusOne {

LeetCode/PlusOne2.java renamed to LeetCode/LeetcodeV1/PlusOne2.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class PlusOne2 {

LeetCode/ReverseLinkedList.java renamed to LeetCode/LeetcodeV1/ReverseLinkedList.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.ArrayList;
23
import java.util.LinkedList;
34

LeetCode/RichestCustomerWealth.java renamed to LeetCode/LeetcodeV1/RichestCustomerWealth.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 RichestCustomerWealth {
23
public static void main(String[] args) {
34
int[][] accounts= {{1,2,3},

LeetCode/RunninSum.java renamed to LeetCode/LeetcodeV1/RunninSum.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class RunninSum {

LeetCode/SearchInsertPosition.java renamed to LeetCode/LeetcodeV1/SearchInsertPosition.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 SearchInsertPosition {
23
public static void main(String[] args) {
34
int[] nums= {1,3,5,6};

LeetCode/Sqrt.java renamed to LeetCode/LeetcodeV1/Sqrt.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Scanner;
23

34
public class Sqrt {

LeetCode/SquaresOfSortedArray_Lt977.java renamed to LeetCode/LeetcodeV1/SquaresOfSortedArray_Lt977.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class SquaresOfSortedArray_Lt977 {

LeetCode/longestCommonPrefix.java renamed to LeetCode/LeetcodeV1/longestCommonPrefix.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 longestCommonPrefix {
23
public static void main(String[] args) {
34
String[] words= {"flower", "flight", "flow"};

LeetCode/merge2sorterLists.java renamed to LeetCode/LeetcodeV1/merge2sorterLists.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
// You are given the heads of two sorted linked lists list1 and list2.
23
// Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
34
// Return the head of the merged linked list.

LeetCode/palindromeNum.java renamed to LeetCode/LeetcodeV1/palindromeNum.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 palindromeNum{
23
public static void main(String[] args){
34
int num=121;

LeetCode/removeDupsSorted2.java renamed to LeetCode/LeetcodeV1/removeDupsSorted2.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class removeDupsSorted2 {

LeetCode/removeDupsSortedArray.java renamed to LeetCode/LeetcodeV1/removeDupsSortedArray.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package LeetcodeV1;
12
import java.util.Arrays;
23

34
public class removeDupsSortedArray {

0 commit comments

Comments
 (0)