DAA Practical Code
DAA Practical Code
// 1
import java.util.Scanner;
class Dsa {
}
}
***************************************************************
//Fibonacci number using non-recursive(iterative) method
//TC: O(n), SC:O(1)
import java.util.Scanner;
int a = 0, b = 1;
for (int i = 2; i <= n; i++) {
int temp = a + b;
a = b;
b = temp;
}
return b;
}
import java.io.*;
import java.util.*;
class Node{
char ch;
int freq;
Node left;
Node right;
Node(char ch, int freq, Node left, Node right) {
this.ch = ch;
this.freq = freq;
this.left = left;
this.right = right;
}
}
class Solution {
public static void printHcodes(char[] arr, int[] freq) {
PriorityQueue<Node> h = new PriorityQueue<>(
(n1, n2) -> n1.freq - n2.freq);
for(int i=0; i<arr.length; i++)
h.add(new Node(arr[i], freq[i], null, null));
while(h.size() > 1) {
Node l = h.poll();
Node r = h.poll();
h.add(new Node('$', l.freq+r.freq, l, r));
}
printRec(h.peek(), "");
}
import java.util.*;
import java.io.*;
import java.lang.*;
Item(int w, int v)
{
wt = w;
val = v;
}
class GFG
{
W = W - arr[i].wt;
}
else
{
res += arr[i].val * ((double) W / arr[i].wt);
break;
}
}
return res;
}
int n = 4, W = 50;
System.out.println(fracKnapSack(arr, n, W));
}
}
-----------------------------------------------------------------------------------
-----------------------
//4
import java.io.*;
import java.util.*;
import static java.lang.System.out;
class Dsa {
return dp[n][W];
}
}
}----------------------------------------------------------------------------------
--------------------------------------
//5
//TC:O(N!), SC:O(N^2)
import java.util.*;
import java.io.*;
import java.lang.*;
class Dsa {
static final int N = 4;
static int board[][] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
return true;
}
if (solveRec(col + 1) == true)
return true;
board[i][col] = 0;
}
}
return false;
}
if (solveRec(0) == false) {
System.out.print("Solution does not exist");
return false;
}
printSolution(board);
return true;
}