-
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
f1b4131
commit 2b0c41f
Showing
4 changed files
with
145 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,117 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
class Node | ||
{ | ||
int data; | ||
Node next; | ||
Node(int d) | ||
{ | ||
data = d; | ||
next = null; | ||
} | ||
} | ||
|
||
class LinkedList1 | ||
{ | ||
Node head; // head of lisl | ||
/* Inserts a new Node at front of the list. */ | ||
public void addToTheLast(Node node) | ||
{ | ||
if (head == null) | ||
{ | ||
head = node; | ||
} | ||
else | ||
{ | ||
Node temp = head; | ||
while (temp.next != null) | ||
temp = temp.next; | ||
|
||
temp.next = node; | ||
} | ||
} | ||
/* Function to print linked list */ | ||
void printList() | ||
{ | ||
Node temp = head; | ||
while (temp != null) | ||
{ | ||
System.out.print(temp.data+" "); | ||
temp = temp.next; | ||
} | ||
System.out.println(); | ||
} | ||
public static void main(String args[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int t=sc.nextInt(); | ||
String s = sc.nextLine(); | ||
while(t>0) | ||
{ | ||
int n = sc.nextInt(); | ||
String s1 = sc.nextLine(); | ||
LinkedList1 llist = new LinkedList1(); | ||
|
||
if (n > 0) | ||
{ | ||
int a1=sc.nextInt(); | ||
Node head= new Node(a1); | ||
llist.addToTheLast(head); | ||
} | ||
for (int i = 1; i < n; i++) | ||
{ | ||
int a = sc.nextInt(); | ||
llist.addToTheLast(new Node(a)); | ||
} | ||
System.out.println(new Solution().DecimalValue(llist.head)); | ||
t--; | ||
} | ||
} | ||
} | ||
|
||
|
||
// } Driver Code Ends | ||
|
||
|
||
/* Node of a linked list | ||
class Node { | ||
int data; | ||
Node next; | ||
Node(int d) { data = d; next = null; } | ||
} | ||
Linked List class | ||
class LinkedList | ||
{ | ||
Node head; // head of list | ||
} | ||
This is method only submission. You only need to complete the method. */ | ||
|
||
class Solution | ||
{ | ||
long DecimalValue(Node head) | ||
{ | ||
ArrayList<Integer> list=new ArrayList<>(); | ||
long mod=(long)1000000007; | ||
Node curr=head; | ||
while(curr!=null) | ||
{ | ||
list.add(curr.data); | ||
curr=curr.next; | ||
} | ||
|
||
Collections.reverse(list); | ||
long base=1; | ||
long ans=0; | ||
int i=0; | ||
while(i<list.size()) | ||
{ | ||
long temp=list.get(i); | ||
ans +=((base%mod)*temp)%mod; | ||
|
||
base=((base%mod)*2)%mod; | ||
i++; | ||
} | ||
|
||
return ans%mod; | ||
} | ||
} |
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(n*k) | ||
Space complexity - O(n) |
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,24 @@ | ||
class Solution | ||
{ | ||
public int maxSumAfterPartitioning(int[] arr, int k) | ||
{ | ||
int N = arr.length; | ||
int K = k + 1; | ||
|
||
int[] dp = new int[k + 1]; | ||
Arrays.fill(dp, 0); | ||
|
||
for (int start = N - 1; start >= 0; start--) | ||
{ | ||
int currMax = 0; | ||
int end = Math.min(N, start + k); | ||
|
||
for (int i = start; i < end; i++) | ||
{ | ||
currMax = Math.max(currMax, arr[i]); | ||
dp[start % K] = Math.max(dp[start % K], dp[(i + 1) % K] + currMax * (i - start + 1)); | ||
} | ||
} | ||
return dp[0]; | ||
} | ||
} |