diff --git a/Medium/CombinationSum.java b/Medium/CombinationSum.java new file mode 100644 index 0000000..2fb1bc2 --- /dev/null +++ b/Medium/CombinationSum.java @@ -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> sum = combinationSum(candidates, 7); + System.out.println(sum); + } + public static List> combinationSum(int[] candidates, int target) { + List> ans = new ArrayList<>(); + findCombination(0, candidates, target, ans, new ArrayList<>()); + return ans; + } + public static void findCombination(int index, int[] arr, int target, List> ans, List 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> sum = combinationSum(candidates, 7); + System.out.println(sum); + } + public static List> combinationSum(int[] candidates, int target) { + List> ans = new ArrayList<>(); + findCombination(0, candidates, target, ans, new ArrayList<>()); + return ans; + } + public static void findCombination(int index, int[] arr, int target, List> ans, List 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 diff --git a/Medium/TargetSum.java b/Medium/TargetSum.java new file mode 100644 index 0000000..d882f67 --- /dev/null +++ b/Medium/TargetSum.java @@ -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]); + } +} \ No newline at end of file diff --git a/Medium/WordSearch.java b/Medium/WordSearch.java new file mode 100644 index 0000000..3e6f6d6 --- /dev/null +++ b/Medium/WordSearch.java @@ -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; + } +}