-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fcb314
commit d1c9a51
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |