Skip to content

Commit

Permalink
Added codes for 23 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 23, 2024
1 parent 1fcb314 commit d1c9a51
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
78 changes: 78 additions & 0 deletions GeeksForGeeks/February/23-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;


class IntArray
{
public static int[] input(BufferedReader br, int n) throws IOException
{
String[] s = br.readLine().trim().split(" ");
int[] a = new int[n];
for(int i = 0; i < n; i++)
a[i] = Integer.parseInt(s[i]);

return a;
}

public static void print(int[] a)
{
for(int e : a)
System.out.print(e + " ");
System.out.println();
}

public static void print(ArrayList<Integer> a)
{
for(int e : a)
System.out.print(e + " ");
System.out.println();
}
}

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine().trim());
while(t-- > 0){

int n;
n = Integer.parseInt(br.readLine().trim());


int[] price = IntArray.input(br, n);

Solution obj = new Solution();
int res = obj.maxProfit(n, price);

System.out.println(res);

}
}
}

// } Driver Code Ends


class Solution
{
public static int maxProfit(int n, int[] price)
{
// code here
int first_buy = Integer.MIN_VALUE;
int first_sell = 0;
int second_buy = Integer.MIN_VALUE;
int second_sell = 0;

for(int i = 0; i < n; i++)
{
first_buy = Math.max(first_buy, -price[i]);
first_sell = Math.max(first_sell, first_buy + price[i]);
second_buy = Math.max(second_buy, first_sell - price[i]);
second_sell = Math.max(second_sell, second_buy + price[i]);
}

return second_sell;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/23-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/February/23-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O((v + e) * logv)
Space complexity - O(v+e)
57 changes: 57 additions & 0 deletions LeetCode/February/23-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Solution
{
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k)
{
List<Pair<Integer, Integer>>[] graph = new List[n];

for (int i = 0; i < n; i++)
graph[i] = new ArrayList<>();

for (int[] flight : flights)
{
int u = flight[0];
int v = flight[1];
int w = flight[2];
graph[u].add(new Pair<>(v, w));
}

return dijkstra(graph, src, dst, k);
}

private int dijkstra(List<Pair<Integer, Integer>>[] graph, int src, int dst, int k)
{
int[][] dist = new int[graph.length][k + 2];
Arrays.stream(dist).forEach(A -> Arrays.fill(A, Integer.MAX_VALUE));
// (d, u, stops)
Queue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]);

dist[src][k + 1] = 0;
minHeap.offer(new int[] {dist[src][k + 1], src, k + 1});

while (!minHeap.isEmpty())
{
int d = minHeap.peek()[0];
int u = minHeap.peek()[1];
int stops = minHeap.poll()[2];

if (u == dst)
return d;
if (stops == 0)
continue;

for (Pair<Integer, Integer> pair : graph[u])
{
int v = pair.getKey();
int w = pair.getValue();

if (d + w < dist[v][stops - 1])
{
dist[v][stops - 1] = d + w;
minHeap.offer(new int[] {dist[v][stops - 1], v, stops - 1});
}
}
}

return -1;
}
}

0 comments on commit d1c9a51

Please sign in to comment.