From 47faf38ea76d05a80079c8175e16cb7d269a2e44 Mon Sep 17 00:00:00 2001 From: roseline Date: Wed, 5 Oct 2022 20:50:01 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Code:=20Solve=20LeetCode=203Sum=20(?= =?UTF-8?q?=EC=88=98=EC=97=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\206\220\354\210\230\354\227\260.java" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "2022/Oct/week_1/LeetCode_3Sum_\354\206\220\354\210\230\354\227\260.java" 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 From 1b0c7bdcb26f2e9f681ab5ec98f482beaffab81d Mon Sep 17 00:00:00 2001 From: roseline Date: Wed, 5 Oct 2022 20:50:23 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Code:=20Solve=20=ED=94=84=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EB=9E=98=EB=A8=B8=EC=8A=A4=20lv2=20=ED=9B=84=EB=B3=B4=ED=82=A4?= =?UTF-8?q?=20(=EC=88=98=EC=97=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\206\220\354\210\230\354\227\260.java" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "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" 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); + } +}