Skip to content

Commit

Permalink
Added codes for 3 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 3, 2024
1 parent f1b4131 commit 2b0c41f
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
117 changes: 117 additions & 0 deletions GeeksForGeeks/February/3-2-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/3-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/3-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*k)
Space complexity - O(n)
24 changes: 24 additions & 0 deletions LeetCode/February/3-2-24/Solution.java
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];
}
}

0 comments on commit 2b0c41f

Please sign in to comment.