Skip to content

Commit a9af524

Browse files
author
Rakesh Venkatesh
committed
more code
1 parent aeb9690 commit a9af524

10 files changed

Lines changed: 388 additions & 20 deletions

File tree

lambdas/src/main/java/com/rakeshv/Lambdas.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.rakeshv;
22

3+
import java.time.LocalDateTime;
4+
import java.util.List;
35
import java.util.Optional;
46

57
/**
@@ -12,6 +14,8 @@ public static void main( String[] args )
1214
{
1315
Long value = null;
1416

17+
List<Integer> integers = List.of(1, 3, 5, 7, 9, 100, 2, 4);
18+
1519
Long finalValue = Optional.ofNullable(value)
1620
.map(x -> 10L)
1721
.orElseGet(() -> 5L);
@@ -22,8 +26,21 @@ public static void main( String[] args )
2226
.map(x -> lambdas.doThisIfNotNull())
2327
.orElseGet(lambdas::doThisIfNull);
2428
System.out.println(result);
29+
System.out.println("current time is " + LocalDateTime.now());
30+
integers.parallelStream()
31+
.filter(Lambdas::isEven)
32+
.findAny()
33+
.ifPresent(System.out::println);
34+
System.out.println("new time is " + LocalDateTime.now());
2535
}
2636

37+
public static boolean isEven(int x) {
38+
try {
39+
Thread.sleep(x * 100);
40+
} catch (Exception e) {}
41+
42+
return x % 2 == 0;
43+
}
2744
public String doThisIfNotNull() {
2845
return ("i accept only non null values");
2946
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.rakeshv;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class StrategyPattern {
7+
public static void main(String[] args) {
8+
Map<String, Payment> map = new HashMap<>();
9+
map.put("random", Payment.random());
10+
map.put("another", Payment.another());
11+
map.put("onemore", Payment.onemore());
12+
13+
double payment = map.get("onemore").pay(10, 2, 3, 4, 5);
14+
System.out.println(payment);
15+
}
16+
17+
}
18+
19+
interface Payment {
20+
double pay(double amount, double a, double b, double c, double d);
21+
22+
static Payment random() {
23+
return (amount, a, b, c, d) -> amount;
24+
}
25+
26+
static Payment another() {
27+
return ((amount, a, b, c, d) -> amount + a +b);
28+
}
29+
30+
static Payment onemore() {
31+
return ((amount, a, b, c, d) -> amount * a * b * c * d);
32+
}
33+
}

src/Arrays/FractionalKnapsack.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package Arrays;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
https://www.geeksforgeeks.org/fractional-knapsack-problem/?ref=lbp
7+
*/
8+
public class FractionalKnapsack {
9+
static class Item {
10+
double weight;
11+
double value;
12+
Double cost;
13+
14+
Item(double w, double v) {
15+
this.weight = w;
16+
this.value = v;
17+
this.cost = this.value / this.weight;
18+
}
19+
}
20+
21+
private double solution(double[] weights, double[] values, double capacity) {
22+
Item[] items = new Item[weights.length];
23+
24+
for (var i = 0; i < weights.length; i++) {
25+
items[i] = new Item(weights[i], values[i]);
26+
}
27+
28+
Arrays.sort(items, (i1, i2) -> i2.cost.compareTo(i1.cost));
29+
30+
double totalValue = 0;
31+
for (Item item : items) {
32+
double currentWeight = item.weight;
33+
double currentValue = item.value;
34+
35+
if (capacity - currentWeight >= 0) {
36+
totalValue += currentValue;
37+
capacity -= currentWeight;
38+
} else {
39+
double fraction = capacity / currentWeight;
40+
double value = currentValue * fraction;
41+
totalValue += value;
42+
break;
43+
}
44+
}
45+
46+
return totalValue;
47+
}
48+
49+
public static void main(String[] args) {
50+
FractionalKnapsack fKnapsack =
51+
new FractionalKnapsack();
52+
53+
double[] wt = { 10, 40, 20, 30 };
54+
double[] val = { 60, 40, 100, 120 };
55+
int capacity = 50;
56+
57+
System.out.println(fKnapsack.solution(wt, val, capacity));
58+
}
59+
}

src/Arrays/GoldMining.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Arrays;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
https://www.geeksforgeeks.org/gold-mine-problem/?ref=lbp
7+
*/
8+
public class GoldMining {
9+
public static void main(String[] args) {
10+
int gold[][]= { {1, 3, 1, 5},
11+
{2, 2, 4, 1},
12+
{5, 0, 2, 3},
13+
{0, 6, 1, 2} };
14+
15+
System.out.println(solution(gold));
16+
}
17+
18+
public static int solution(int[][] goldMine) {
19+
int[][] dp = new int[goldMine.length][goldMine[0].length];
20+
21+
for (var arr : dp) {
22+
Arrays.fill(arr, 0);
23+
}
24+
25+
for (var col = goldMine[0].length - 1; col >= 0; col--) {
26+
for (var row = 0; row < goldMine.length; row++) {
27+
int right = (col == goldMine[0].length - 1) ? 0 : dp[row][col + 1];
28+
int rightUp = ((col == goldMine[0].length -1) || (row == 0)) ? 0 : dp[row-1][col + 1];
29+
int rightDown = ((col == goldMine[0].length - 1) || (row == goldMine.length - 1)) ? 0 : dp[row + 1][col + 1];
30+
31+
dp[row][col] = goldMine[row][col] + Math.max(right, Math.max(rightUp, rightDown));
32+
}
33+
}
34+
35+
int max = dp[0][0];
36+
37+
for (var i = 1; i < goldMine.length; i++) {
38+
max = Math.max(max, dp[i][0]);
39+
}
40+
41+
return max;
42+
}
43+
}

src/Arrays/JobScheduling.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package Arrays;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/*
8+
https://www.geeksforgeeks.org/job-sequencing-using-disjoint-set-union/?ref=lbp
9+
*/
10+
public class JobScheduling {
11+
static class Job {
12+
char id;
13+
int profit;
14+
int deadline;
15+
16+
Job(char a, int d, int p) {
17+
this.id = a;
18+
this.profit = p;
19+
this.deadline = d;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "Job{" +
25+
"id=" + id +
26+
", profit=" + profit +
27+
", deadline=" + deadline +
28+
'}';
29+
}
30+
}
31+
32+
static class DisjointSet {
33+
int[] parent;
34+
DisjointSet(int length) {
35+
this.parent = new int[length + 1];
36+
37+
for (var i = 0; i < parent.length; i++) {
38+
parent[i] = i;
39+
}
40+
}
41+
42+
int find(int slot) {
43+
if (slot == parent[slot])
44+
return slot;
45+
46+
return parent[slot] = find(parent[slot]);
47+
}
48+
49+
void merge(int par, int child) {
50+
this.parent[child] = par;
51+
}
52+
}
53+
54+
int maxTimeSlot(List<Job> jobs) {
55+
int ans = Integer.MIN_VALUE;
56+
for (var job : jobs) {
57+
ans = Math.max(ans, job.deadline);
58+
}
59+
60+
return ans;
61+
}
62+
63+
64+
void scheduleJobs(List<Job> jobs) {
65+
Collections.sort(jobs, (j1, j2) -> {return (j1.profit > j2.profit) ? -1 : 1;});
66+
int maxSlot = maxTimeSlot(jobs);
67+
DisjointSet disjointSet = new DisjointSet(maxSlot);
68+
69+
for (var job : jobs) {
70+
// System.out.println(" job is " + job);
71+
int availableSlot = disjointSet.find(job.deadline);
72+
// System.out.println("available slot is " + availableSlot);
73+
if (availableSlot > 0) {
74+
disjointSet.merge(disjointSet.find(availableSlot - 1), availableSlot);
75+
System.out.print(job + " ");
76+
}
77+
}
78+
}
79+
80+
public static void main(String[] args) {
81+
ArrayList<Job> arr =new ArrayList<Job>();
82+
arr.add(new Job('a',2,100));
83+
arr.add(new Job('b',1,19));
84+
arr.add(new Job('c',2,27));
85+
arr.add(new Job('d',1,25));
86+
arr.add(new Job('e',3,15));
87+
88+
JobScheduling jobScheduling =
89+
new JobScheduling();
90+
jobScheduling.scheduleJobs(arr);
91+
}
92+
}

src/Arrays/KLargestElements.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Arrays;
2+
3+
4+
import java.util.PriorityQueue;
5+
6+
/*
7+
https://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/?ref=lbp
8+
*/
9+
public class KLargestElements {
10+
PriorityQueue<Integer> priorityQueue;
11+
int[] array;
12+
13+
KLargestElements(int[] a) {
14+
this.array = a;
15+
this.priorityQueue = new PriorityQueue();
16+
}
17+
18+
public void solution(int k) {
19+
for (var i = 0; i < k; i++) {
20+
priorityQueue.add(array[i]);
21+
}
22+
23+
for (var i = k ; i < array.length; i++) {
24+
if (array[i] > priorityQueue.peek()) {
25+
priorityQueue.poll();
26+
priorityQueue.add(array[i]);
27+
}
28+
}
29+
30+
for (Integer integer : priorityQueue) {
31+
System.out.print(integer + " ");
32+
}
33+
}
34+
35+
public static void main(String[] args) {
36+
int arr[] = { 11, 3, 2, 1, 15, 5, 4,
37+
45, 88, 96, 50, 45 };
38+
KLargestElements kLargestElements =
39+
new KLargestElements(arr);
40+
kLargestElements.solution(3);
41+
}
42+
}

src/Arrays/MinHeapToMaxHeap.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Arrays;
2+
3+
/*
4+
https://www.geeksforgeeks.org/convert-min-heap-to-max-heap/?ref=rp
5+
*/
6+
public class MinHeapToMaxHeap {
7+
public static void main(String[] args) {
8+
int arr[] = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9};
9+
printArray(arr);
10+
11+
convertToMaxHeap(arr);
12+
printArray(arr);
13+
}
14+
15+
public static void printArray(int[] array) {
16+
for (var i : array)
17+
System.out.print(i + " ");
18+
System.out.println();
19+
}
20+
21+
public static void convertToMaxHeap(int[] array) {
22+
int n = array.length;
23+
24+
for (var i = (n-2)/2; i >=0; i--) {
25+
maxHeapify(array, i, n);
26+
}
27+
}
28+
29+
public static void maxHeapify(int[] array, int i, int n) {
30+
int left = 2* i + 1;
31+
int right = 2 * i + 2;
32+
int largest = i;
33+
if (left < n && (array[left] > array[i]))
34+
largest = left;
35+
36+
if (right < n && (array[right] > array[largest]))
37+
largest = right;
38+
39+
if (largest != i) {
40+
int tmp = array[i];
41+
array[i] = array[largest];
42+
array[largest] = tmp;
43+
maxHeapify(array, largest, n);
44+
}
45+
46+
}
47+
}

src/Arrays/UnboundedKnapsack.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package Arrays;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
https://iq.opengenus.org/unbounded-knapsack-problem/
7+
*/
8+
public class UnboundedKnapsack {
9+
public static void main(String[] args) {
10+
int val[] = {10,30,20};
11+
int weight[] = {5, 10, 15};
12+
13+
int Capacity = 100;
14+
15+
System.out.println(solution(Capacity, weight, val));
16+
}
17+
18+
public static double solution(int capacity, int[] weight, int[] value) {
19+
int[] dp = new int[capacity +1];
20+
Arrays.fill(dp, 0);
21+
22+
if (weight.length == 0 || capacity == 0)
23+
return 0;
24+
25+
for (var i = 0; i <= capacity; i++) {
26+
for (var j = 0; j < weight.length; j++) {
27+
if (weight[j] <= i) {
28+
dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]);
29+
}
30+
}
31+
}
32+
33+
return dp[capacity];
34+
}
35+
}

0 commit comments

Comments
 (0)