Skip to content

Added some important Backtracking question. #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Medium/CombinationSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<<<<<<< HEAD
// https://leetcode.com/problems/combination-sum/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CombinationSum {
public static void main(String[] args) {
int[] candidates = {2,3,6,7};
List<List<Integer>> sum = combinationSum(candidates, 7);
System.out.println(sum);
}
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
findCombination(0, candidates, target, ans, new ArrayList<>());
return ans;
}
public static void findCombination(int index, int[] arr, int target, List<List<Integer>> ans, List<Integer> ds){
if(index == arr.length){
if(target == 0){
ans.add(new ArrayList<>(ds));
}
return;
}

if(arr[index] <= target){
ds.add(arr[index]);
findCombination(index, arr, target - arr[index], ans, ds);
ds.remove(ds.size()-1);
}
findCombination(index+1, arr, target, ans, ds);
}
}
=======
// https://leetcode.com/problems/combination-sum/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CombinationSum {
public static void main(String[] args) {
int[] candidates = {2,3,6,7};
List<List<Integer>> sum = combinationSum(candidates, 7);
System.out.println(sum);
}
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
findCombination(0, candidates, target, ans, new ArrayList<>());
return ans;
}
public static void findCombination(int index, int[] arr, int target, List<List<Integer>> ans, List<Integer> ds){
if(index == arr.length){
if(target == 0){
ans.add(new ArrayList<>(ds));
}
return;
}

if(arr[index] <= target){
ds.add(arr[index]);
findCombination(index, arr, target - arr[index], ans, ds);
ds.remove(ds.size()-1);
}
findCombination(index+1, arr, target, ans, ds);
}
}
>>>>>>> 67aafb10a70e470ee512334062930bef20542fa0
25 changes: 25 additions & 0 deletions Medium/TargetSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// https://leetcode.com/problems/target-sum/
public class TargetSum {
static int count;
public static void main(String[] args) {
int[] nums = {1, 1, 1, 1, 1};
System.out.println(findTargetSumWays(nums, 3));
}
public static int findTargetSumWays(int[] nums, int target){
count = 0;
TargetSum(nums, target, 0, 0);

return count;
}
public static void TargetSum(int[] nums, int target, int index, int val){
if(index == nums.length){
if(target == val){
count++;
}
return;
}

TargetSum(nums, target, index+1, val+nums[index]);
TargetSum(nums, target, index+1, val-nums[index]);
}
}
70 changes: 70 additions & 0 deletions Medium/WordSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// https://leetcode.com/problems/word-search/

public class WordSearch {
public static void main(String[] args) {
char[][] board = {
{'A','B','C','E'},
{'S','F','C','S'},
{'A','D','E','E'}
};
boolean ans = exist(board, "ABCCED");
System.out.println(ans);
}
public static boolean exist(char[][] board, String word) {

for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == word.charAt(0)){
char temp = board[i][j];
board[i][j] = '0';
if(isValid(board, i, j, 1, word)){
return true;
}
board[i][j] = temp;
}
}
}

return false;
}
public static boolean isValid(char[][] board, int row, int col, int index, String word){
if(index == word.length()){
return true;
}
char temp;
if(col != 0 && board[row][col - 1] == word.charAt(index)){
temp = board[row][col - 1];
board[row][col - 1] = '0';
if(isValid(board, row, col-1, index+1, word)){
return true;
}
board[row][col-1] = temp;
}
if(col <= board[0].length-2 && board[row][col + 1] == word.charAt(index)){
temp = board[row][col + 1];
board[row][col + 1] = '0';
if(isValid(board, row, col+1, index+1, word)){
return true;
}
board[row][col+1] = temp;
}
if(row != 0 && board[row - 1][col] == word.charAt(index)){
temp = board[row - 1][col];
board[row - 1][col] = '0';
if(isValid(board, row - 1, col, index+1, word)){
return true;
}
board[row - 1][col] = temp;
}
if(row <= board.length - 2 && board[row + 1][col] == word.charAt(index)){
temp = board[row + 1][col];
board[row + 1][col] = '0';
if(isValid(board, row + 1, col, index+1, word)){
return true;
}
board[row + 1][col] = temp;
}

return false;
}
}