-
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.
Added codes for 26 Jan and re-arranged files
- Loading branch information
1 parent
9973774
commit 3048cc4
Showing
104 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
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.
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,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; | ||
} | ||
} |
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 * 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.
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(m*n*N) | ||
Space complexity - (m*n*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,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.