Skip to content

Commit

Permalink
Added codes for 26 Jan and re-arranged files
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 26, 2024
1 parent 9973774 commit 3048cc4
Show file tree
Hide file tree
Showing 104 changed files with 98 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
69 changes: 69 additions & 0 deletions GeeksForGeeks/January/26-1-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//{ Driver Code Starts
import java.io.*;
import java.lang.*;
import java.util.*;

class Item {
int value, weight;
Item(int x, int y){
this.value = x;
this.weight = y;
}
}

class GfG {

public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0){
String inputLine[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(inputLine[0]);
int w = Integer.parseInt(inputLine[1]);
Item[] arr = new Item[n];
inputLine = br.readLine().trim().split(" ");
for(int i=0, k=0; i<n; i++){
arr[i] = new Item(Integer.parseInt(inputLine[k++]), Integer.parseInt(inputLine[k++]));
}
System.out.println(String.format("%.6f", new Solution().fractionalKnapsack(w, arr, n)));
}
}
}
// } Driver Code Ends


/*
class Item {
int value, weight;
Item(int x, int y){
this.value = x;
this.weight = y;
}
}
*/

class Solution
{
//Function to get the maximum total value in the knapsack.
double fractionalKnapsack(int W, Item arr[], int n)
{
// Your code here
Arrays.sort(arr, Comparator.comparing((Item a) -> (double)a.value / a.weight).reversed());

double result = 0;
for(Item item : arr)
{
if(item.weight <= W)
{
result += item.value;
W-=item.weight;
}
else
{
result += (double)(W * item.value) / item.weight;
break;
}
}
return result;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/January/26-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * logn)
Space complexity - O(1)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions LeetCode/January/26-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m*n*N)
Space complexity - (m*n*N)
25 changes: 25 additions & 0 deletions LeetCode/January/26-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution
{
public int findPaths(int m, int n, int maxMove, int startRow, int startColumn)
{
Integer[][][] mem = new Integer[maxMove + 1][m][n];
return findPaths(m, n, maxMove, startRow, startColumn, mem);
}

private static final int kMod = 1_000_000_007;

private int findPaths(int m, int n, int k, int i, int j, Integer[][][] mem)
{
if (i < 0 || i == m || j < 0 || j == n)
return 1;
if (k == 0)
return 0;
if (mem[k][i][j] != null)
return mem[k][i][j];

return mem[k][i][j] = (int) ((findPaths(m, n, k - 1, i + 1, j, mem) * 1L +
findPaths(m, n, k - 1, i - 1, j, mem) +
findPaths(m, n, k - 1, i, j + 1, mem) +
findPaths(m, n, k - 1, i, j - 1, mem)) % kMod);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 3048cc4

Please sign in to comment.