Pdpra
Pdpra
}
return traping;
}
Output :
For Input:
3010402
Your Output:
10
Practical : 02
AIM : Best Time to Buy and Sell Stock.
Code :
public int maxProfit(int[] prices) {
int maxProfit=0;
int minprice=Integer.MAX_VALUE;
for(int i=0; i<prices.length; i++){
int profit=prices[i]-minprice;
maxProfit=Math.max(maxProfit,profit);
minprice=Math.min(minprice,prices[i]);
}
return maxProfit;
}
Output :
Input
prices =
[3,1,4,8,7,2,5]
Output
7
Practical : 03
AIM : Best Time to Buy and Sell Stock using LinkedList.
Code :
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
class Solution {
public int maxProfit(int[] prices) {
Node res=new Node(0);
Node p=res;
for(int i=0; i<prices.length; i++){
Node t=new Node(prices[i]);
p.next=t;
p=p.next;
}
Node temp=res.next;
int maxProfit=0;
int minprice=Integer.MAX_VALUE;
while(temp !=null){
int profit=temp.data-minprice;
maxProfit=Math.max(maxProfit,profit);
minprice=Math.min(minprice,temp.data);
temp=temp.next;
}
return maxProfit;
}
}
Output :
Input
prices =
[3,1,4,8,7,2,5]
Output:
7
Practical : 03
AIM : Middle of the Linked List.
Code :
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
class Solution {
int getMiddle(Node head) {
Node slow=head;
Node fast=head;
while(fast !=null && fast.next !=null){
slow=slow.next;
fast=fast.next.next;
}
return slow.data;
}
}
Output:
For Input:
12345
Your Output:
3
Expected Output:
3