From 345281e8059ce5b2a48147a1e85891d4ed00aea4 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Tue, 28 May 2024 18:01:47 +0530 Subject: [PATCH] Added codes for 28 May --- GeeksForGeeks/May/28-5-24/GFG.java | 76 +++++++++++++++++++++++++++++ GeeksForGeeks/May/28-5-24/README.md | 2 + LeetCode/May/28-5-24/README.md | 2 + LeetCode/May/28-5-24/Solution.java | 15 ++++++ 4 files changed, 95 insertions(+) create mode 100644 GeeksForGeeks/May/28-5-24/GFG.java create mode 100644 GeeksForGeeks/May/28-5-24/README.md create mode 100644 LeetCode/May/28-5-24/README.md create mode 100644 LeetCode/May/28-5-24/Solution.java diff --git a/GeeksForGeeks/May/28-5-24/GFG.java b/GeeksForGeeks/May/28-5-24/GFG.java new file mode 100644 index 0000000..5977460 --- /dev/null +++ b/GeeksForGeeks/May/28-5-24/GFG.java @@ -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 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)); + } +} diff --git a/GeeksForGeeks/May/28-5-24/README.md b/GeeksForGeeks/May/28-5-24/README.md new file mode 100644 index 0000000..bb26807 --- /dev/null +++ b/GeeksForGeeks/May/28-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*w) +Space complexity - O(n*w) diff --git a/LeetCode/May/28-5-24/README.md b/LeetCode/May/28-5-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/May/28-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/May/28-5-24/Solution.java b/LeetCode/May/28-5-24/Solution.java new file mode 100644 index 0000000..623da6e --- /dev/null +++ b/LeetCode/May/28-5-24/Solution.java @@ -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; + } +}