diff --git "a/2022/Oct/week_1/LeetCode_3Sum_\354\206\220\354\210\230\354\227\260.java" "b/2022/Oct/week_1/LeetCode_3Sum_\354\206\220\354\210\230\354\227\260.java" new file mode 100644 index 0000000..495df88 --- /dev/null +++ "b/2022/Oct/week_1/LeetCode_3Sum_\354\206\220\354\210\230\354\227\260.java" @@ -0,0 +1,30 @@ +package Oct_2022; + +import java.util.*; + +public class Solution_LeetCode_3Sum { + + public List> threeSum(int[] nums) { + + Set> set = new HashSet<>(); + + if(nums == null || nums.length < 3) return new ArrayList<>(set); + + Arrays.sort(nums); + + for (int i = 0; i < nums.length-2; i++) { + int j = i + 1; + int k = nums.length - 1; + + while (j < k){ + int sum = nums[i] + nums[j] + nums[k]; + + if(sum == 0) set.add(Arrays.asList(nums[i], nums[j++], nums[k--])); + else if(sum < 0) j++; + else if(sum > 0) k--; + } + } + + return new ArrayList<>(set); + } +} \ No newline at end of file diff --git "a/2022/Oct/week_1/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_lv2_\355\233\204\353\263\264\355\202\244_\354\206\220\354\210\230\354\227\260.java" "b/2022/Oct/week_1/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_lv2_\355\233\204\353\263\264\355\202\244_\354\206\220\354\210\230\354\227\260.java" new file mode 100644 index 0000000..5336cf2 --- /dev/null +++ "b/2022/Oct/week_1/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_lv2_\355\233\204\353\263\264\355\202\244_\354\206\220\354\210\230\354\227\260.java" @@ -0,0 +1,50 @@ +package Oct_2022; + +import java.util.*; + +public class Solution_Programmers_42890_후보키 { + + static ArrayList> candidate; + + public int solution(String[][] relation){ + candidate = new ArrayList>(); + solve(0, new HashSet(), relation); + + return candidate.size(); + } + + static void solve(int n, HashSet hashSet, String[][] relation){ + if(n == relation[0].length) { + for(int i =0; i < candidate.size(); i++) + if(hashSet.containsAll(candidate.get(i))) return; + + HashSet set = new HashSet<>(); + + for(int r = 0; r < relation.length; r++){ + String temp = ""; + + for(int c : hashSet) temp += relation[r][c] + ","; + + if(set.contains(temp)) return; + + set.add(temp); + } + + candidate.add(hashSet); + + return; + } + + HashSet set1 = new HashSet<>(); + HashSet set2 = new HashSet<>(); + + for(Integer value : hashSet){ + set1.add(value); + set2.add(value); + } + + solve(n+1, set2, relation); + set1.add(n); + solve(n+1, set1, relation); + } +}