Skip to content

Commit

Permalink
Added codes for 28 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 28, 2024
1 parent 2728255 commit 345281e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
76 changes: 76 additions & 0 deletions GeeksForGeeks/May/28-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//{ 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());
while (t-- > 0) {

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

int w;
w = Integer.parseInt(br.readLine());

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

Solution obj = new Solution();
int res = obj.minimumCost(n, w, cost);

System.out.println(res);
}
}
}

// } Driver Code Ends



class Solution
{
public static int minimumCost(int n, int w, int[] cost)
{
// code here
int dp[][] = new int [n][w+1];
for(int i[] : dp)
Arrays.fill(i, -1);
return solve(0, cost, w, dp);
}

private static int solve(int i, int []arr, int w, int dp[][])
{
if(w==0)
return 0;

if(w<0||i>=arr.length)
return 100000;

if(dp[i][w]!=-1)
return dp[i][w];

return dp[i][w] = Math.min(solve(i+1, arr, w, dp), arr[i] + solve(i, arr, w-(i+1), dp));
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/28-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*w)
Space complexity - O(n*w)
2 changes: 2 additions & 0 deletions LeetCode/May/28-5-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)
15 changes: 15 additions & 0 deletions LeetCode/May/28-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution
{
public int equalSubstring(String s, String t, int maxCost)
{
int j = 0;
for (int i = 0; i < s.length(); ++i)
{
maxCost -= Math.abs(s.charAt(i) - t.charAt(i));
if (maxCost < 0)
maxCost += Math.abs(s.charAt(j) - t.charAt(j++));
}

return s.length() - j;
}
}

0 comments on commit 345281e

Please sign in to comment.