-
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.
- Loading branch information
1 parent
86153da
commit 0b8253c
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
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,100 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
class IntMatrix | ||
{ | ||
public static int[][] input(BufferedReader br, int n, int m) throws IOException | ||
{ | ||
int[][] mat = new int[n][]; | ||
|
||
for(int i = 0; i < n; i++) | ||
{ | ||
String[] s = br.readLine().trim().split(" "); | ||
mat[i] = new int[s.length]; | ||
for(int j = 0; j < s.length; j++) | ||
mat[i][j] = Integer.parseInt(s[j]); | ||
} | ||
|
||
return mat; | ||
} | ||
|
||
public static void print(int[][] m) | ||
{ | ||
for(var a : m) | ||
{ | ||
for(int e : a) | ||
System.out.print(e + " "); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
public static void print(ArrayList<ArrayList<Integer>> m) | ||
{ | ||
for(var a : m) | ||
{ | ||
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 m; | ||
m = Integer.parseInt(br.readLine()); | ||
|
||
|
||
int[][] edges = IntMatrix.input(br, m, 2); | ||
|
||
Solution obj = new Solution(); | ||
int res = obj.vertexCover(n, m, edges); | ||
|
||
System.out.println(res); | ||
|
||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
class Solution | ||
{ | ||
public static int vertexCover(int n, int m, int[][] edges) | ||
{ | ||
// code here | ||
int ans = Integer.MAX_VALUE; | ||
|
||
// The use of labels is especially handy when dealing with nested loops. | ||
// It allows you to specify which loop the continue or break statement should affect | ||
// especially when there are multiple levels of nested loops. | ||
outerloop: | ||
for(int i = 0; i <= 1 << n; i++) | ||
{ | ||
for(int j = 0; j < m; j++) | ||
{ | ||
if(!((1 << (edges[j][0] - 1) & i) != 0 | ||
|| (1 << (edges[j][1] - 1) & i) != 0)) | ||
{ | ||
continue outerloop; // This continues outerloop and not innerloop | ||
} | ||
} | ||
|
||
ans = Math.min(ans, Integer.bitCount(i)); | ||
} | ||
|
||
return ans; | ||
} | ||
} |
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^2) | ||
Space complexity - O(1) |
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) | ||
Space complexity - O(1) |
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,19 @@ | ||
class Solution | ||
{ | ||
// using dp, but not array | ||
|
||
public int rob(int[] nums) | ||
{ | ||
int prev1 = 0; // dp[i - 1] | ||
int prev2 = 0; // dp[i - 2] | ||
|
||
for (int num : nums) | ||
{ | ||
int dp = Math.max(prev1, prev2 + num); | ||
prev2 = prev1; | ||
prev1 = dp; | ||
} | ||
|
||
return prev1; | ||
} | ||
} |