From f3fe4e63d6f9af4965286b174e5a45242ccf2b51 Mon Sep 17 00:00:00 2001 From: wad-jangjaejeong Date: Sun, 25 Aug 2024 21:01:52 +0900 Subject: [PATCH 1/6] two sum solution --- two-sum/jaejeong1.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 two-sum/jaejeong1.java diff --git a/two-sum/jaejeong1.java b/two-sum/jaejeong1.java new file mode 100644 index 000000000..279b390e9 --- /dev/null +++ b/two-sum/jaejeong1.java @@ -0,0 +1,36 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +class SolutionTwoSum { + public int[] twoSum(int[] nums, int target) { + // 모든 수를 해시맵에 저장한다. key = nums[i], value = i + // 해시맵을 순회하며 target - key = result를 만족하는 쌍을 찾음 + // key 와 result 의 value를 정답으로 반환한다 + // 시간복잡도: O(N), 공간복잡도: O(N) + // 값별 인덱스 리스트를 저장할 해시맵 + HashMap> indicesByValue = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + indicesByValue.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i); + } + + for (int key : indicesByValue.keySet()) { + int diff = target - key; + + if (indicesByValue.containsKey(diff)) { + int index1 = indicesByValue.get(key).get(0); + + // 동일한 값에 대해 두 개의 다른 인덱스가 있는지 확인 + int index2 = (key == diff && indicesByValue.get(key).size() > 1) + ? indicesByValue.get(key).get(1) + : indicesByValue.get(diff).get(0); + + return new int[]{index1, index2}; + } + } + + // 쌍을 찾지 못한 경우 + return null; + } +} \ No newline at end of file From 236e5ea69c116a2c8ff90416197a34b2fd72113e Mon Sep 17 00:00:00 2001 From: wad-jangjaejeong Date: Sun, 25 Aug 2024 22:58:40 +0900 Subject: [PATCH 2/6] climbing stairs solution --- climbing-stairs/jaejeong1.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 climbing-stairs/jaejeong1.java diff --git a/climbing-stairs/jaejeong1.java b/climbing-stairs/jaejeong1.java new file mode 100644 index 000000000..8c225625a --- /dev/null +++ b/climbing-stairs/jaejeong1.java @@ -0,0 +1,27 @@ +class SolutionClimbStairs { + + public static void main(String[] args) { + SolutionClimbStairs s = new SolutionClimbStairs(); + System.out.println(s.climbStairs(3)); + } + + public int climbStairs(int n) { + if (n == 1) { + return 1; + } + + if (n == 2) { + return 2; + } + + int[] stairs = new int[n+1]; + stairs[1] = 1; + stairs[2] = 2; + + for (int i=3; i<=n; i++) { + stairs[i] = stairs[i-1] + stairs[i-2]; + } + + return stairs[n]; + } +} \ No newline at end of file From b7928640f2bbd7a51cb8b73182219026a32f557f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=9E=AC=EC=A0=95=20=5B=EC=BA=90=ED=85=8C?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=5D?= Date: Fri, 30 Aug 2024 18:20:54 +0900 Subject: [PATCH 3/6] climbing stairs solution --- climbing-stairs/jaejeong1.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/climbing-stairs/jaejeong1.java b/climbing-stairs/jaejeong1.java index 8c225625a..0fd42c47b 100644 --- a/climbing-stairs/jaejeong1.java +++ b/climbing-stairs/jaejeong1.java @@ -1,10 +1,5 @@ class SolutionClimbStairs { - public static void main(String[] args) { - SolutionClimbStairs s = new SolutionClimbStairs(); - System.out.println(s.climbStairs(3)); - } - public int climbStairs(int n) { if (n == 1) { return 1; From 4662d972736edb532997d16efdd6e0a5b367257d Mon Sep 17 00:00:00 2001 From: wad-jangjaejeong Date: Sun, 25 Aug 2024 22:58:40 +0900 Subject: [PATCH 4/6] climbing stairs solution --- climbing-stairs/jaejeong1.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 climbing-stairs/jaejeong1.java diff --git a/climbing-stairs/jaejeong1.java b/climbing-stairs/jaejeong1.java new file mode 100644 index 000000000..0fd42c47b --- /dev/null +++ b/climbing-stairs/jaejeong1.java @@ -0,0 +1,22 @@ +class SolutionClimbStairs { + + public int climbStairs(int n) { + if (n == 1) { + return 1; + } + + if (n == 2) { + return 2; + } + + int[] stairs = new int[n+1]; + stairs[1] = 1; + stairs[2] = 2; + + for (int i=3; i<=n; i++) { + stairs[i] = stairs[i-1] + stairs[i-2]; + } + + return stairs[n]; + } +} \ No newline at end of file From 9a907f17d0931be51fb6bda4e4718bae1e71f495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=9E=AC=EC=A0=95=20=5B=EC=BA=90=ED=85=8C?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=5D?= Date: Sat, 31 Aug 2024 00:04:54 +0900 Subject: [PATCH 5/6] product Except self solution --- product-of-array-except-self/jaejeong1.java | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 product-of-array-except-self/jaejeong1.java diff --git a/product-of-array-except-self/jaejeong1.java b/product-of-array-except-self/jaejeong1.java new file mode 100644 index 000000000..fba547296 --- /dev/null +++ b/product-of-array-except-self/jaejeong1.java @@ -0,0 +1,34 @@ +class SolutionProductExceptSelf { + + public int[] productExceptSelf(int[] nums) { + // answer[i] = nums[0] * nums[1] * ... * nums[i-1] * nums[i+1] * ... * nums[n-1] + // answer[i] = left[i] * right[i] + // left[i] = nums[0] * nums[1] * ... * nums[i-1] + // right[i] = nums[i+1] * ... * nums[n-1] + // left[i] = left[i-1] * nums[i-1] + // right[i] = right[i+1] * nums[i+1] + // answer[i] = left[i] * right[i] + // 시간복잡도: O(N), 공간복잡도: O(N) + int n = nums.length; + int[] left = new int[n]; + int[] right = new int[n]; + int[] answer = new int[n]; + + left[0] = 1; + right[n - 1] = 1; + + for (int i = 1; i < n; i++) { + left[i] = left[i - 1] * nums[i - 1]; + } + + for (int i = n - 2; i >= 0; i--) { + right[i] = right[i + 1] * nums[i + 1]; + } + + for (int i = 0; i < n; i++) { + answer[i] = left[i] * right[i]; + } + + return answer; + } +} \ No newline at end of file From 3013ae5ccc64ad517292404029f50ec997b10fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=9E=AC=EC=A0=95=20=5B=EC=BA=90=ED=85=8C?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=5D?= Date: Sat, 31 Aug 2024 00:11:33 +0900 Subject: [PATCH 6/6] =?UTF-8?q?line=20break=20+=20=EC=84=A4=EB=AA=85=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- climbing-stairs/jaejeong1.java | 9 ++++++++- product-of-array-except-self/jaejeong1.java | 2 +- two-sum/jaejeong1.java | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/climbing-stairs/jaejeong1.java b/climbing-stairs/jaejeong1.java index 0fd42c47b..941401e83 100644 --- a/climbing-stairs/jaejeong1.java +++ b/climbing-stairs/jaejeong1.java @@ -1,6 +1,12 @@ class SolutionClimbStairs { public int climbStairs(int n) { + // n번째 계단까지 오르는 방법의 수 + // 첫 번째 계단까지 오르는 방법은 1가지 + // 두 번째 계단까지 오르는 방법은 2가지 + // 세 번째 계단부터는 이전 두 계단의 방법을 더하여 계산 + // stairs[i]는 i번째 계단까지 오르는 방법의 수 + // 시간복잡도: O(N), 공간복잡도: O(N) if (n == 1) { return 1; } @@ -13,10 +19,11 @@ public int climbStairs(int n) { stairs[1] = 1; stairs[2] = 2; + for (int i=3; i<=n; i++) { stairs[i] = stairs[i-1] + stairs[i-2]; } return stairs[n]; } -} \ No newline at end of file +} diff --git a/product-of-array-except-self/jaejeong1.java b/product-of-array-except-self/jaejeong1.java index fba547296..4c015f1f2 100644 --- a/product-of-array-except-self/jaejeong1.java +++ b/product-of-array-except-self/jaejeong1.java @@ -31,4 +31,4 @@ public int[] productExceptSelf(int[] nums) { return answer; } -} \ No newline at end of file +} diff --git a/two-sum/jaejeong1.java b/two-sum/jaejeong1.java index 279b390e9..835bc7d96 100644 --- a/two-sum/jaejeong1.java +++ b/two-sum/jaejeong1.java @@ -33,4 +33,4 @@ public int[] twoSum(int[] nums, int target) { // 쌍을 찾지 못한 경우 return null; } -} \ No newline at end of file +}