From b7f8de25d3d9b9c2787339617ba563583b451755 Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Fri, 27 Dec 2024 22:38:26 +0900 Subject: [PATCH 1/5] feat: Two Sum #219 --- two-sum/donghyeon95.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 two-sum/donghyeon95.java diff --git a/two-sum/donghyeon95.java b/two-sum/donghyeon95.java new file mode 100644 index 000000000..8f72e6b5c --- /dev/null +++ b/two-sum/donghyeon95.java @@ -0,0 +1,31 @@ +import java.util.HashMap; +import java.util.HashSet; + +class Solution { + public int[] twoSum(int[] nums, int target) { + // // O(N^2) + // for (int i =0; i< nums.length-1; i++) { + // for (int j=i+1; j map = new HashMap<>(); + for (int i=0; i Date: Sat, 28 Dec 2024 00:14:59 +0900 Subject: [PATCH 2/5] feat:Reverse Bits #234 --- reverse-bits/donghyeon95.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 reverse-bits/donghyeon95.java diff --git a/reverse-bits/donghyeon95.java b/reverse-bits/donghyeon95.java new file mode 100644 index 000000000..41c84f513 --- /dev/null +++ b/reverse-bits/donghyeon95.java @@ -0,0 +1,16 @@ +public class Solution { + // you need treat n as an unsigned value + public int reverseBits(int n) { + int result = 0; + + for (int i = 0; i < 32; i++) { + // 왼쪽으로 비트를 한 칸 이동하고, n의 마지막 비트를 추가 + result = (result << 1) | (n & 1); + // n을 오른쪽으로 한 칸 이동 + n >>= 1; + } + + return result; + } +} + From f0bf882eb228c4333495bdb1d54853535ec6979e Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Sat, 28 Dec 2024 16:46:32 +0900 Subject: [PATCH 3/5] feat: Product of Array Except Self #239 --- product-of-array-except-self/donghyeon95.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 product-of-array-except-self/donghyeon95.java diff --git a/product-of-array-except-self/donghyeon95.java b/product-of-array-except-self/donghyeon95.java new file mode 100644 index 000000000..016c0b0aa --- /dev/null +++ b/product-of-array-except-self/donghyeon95.java @@ -0,0 +1,31 @@ +class Solution { + public int[] productExceptSelf(int[] nums) { + int[] result = new int[nums.length]; + int[] right = new int[nums.length]; + int[] left = new int[nums.length]; + + // -> 이쪽 방향으로 한번 계산 + right[0] = nums[0]; + for (int i=1; i-1; i--) { + left[i] = left[i+1]*nums[i]; + } + + // f(i) = right(i-1) * left(i+1) + result[0] = left[1]; + result[nums.length-1] = right[nums.length-2]; + for (int i=1; i Date: Sat, 28 Dec 2024 18:15:04 +0900 Subject: [PATCH 4/5] feat: Combination Sum #254 --- combination-sum/donghyeon95.java | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 combination-sum/donghyeon95.java diff --git a/combination-sum/donghyeon95.java b/combination-sum/donghyeon95.java new file mode 100644 index 000000000..007154ab3 --- /dev/null +++ b/combination-sum/donghyeon95.java @@ -0,0 +1,60 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.stream.Collectors; + +class Solution { + private HashMap> dp = new HashMap<>(); + private HashSet set; + + public List> combinationSum(int[] candidates, int target) { + set = new HashSet<>(Arrays.stream(candidates).boxed().toList()); + recurse(target); + // Convert dp entries back to List> for return + return dp.getOrDefault(target, new ArrayList<>()).stream() + .map(str -> Arrays.stream(str.split(" ")) + .map(Integer::valueOf) + .collect(Collectors.toList())) + .collect(Collectors.toList()); + } + + public void recurse(int target) { + if (dp.containsKey(target)) return; + + List combinations = new ArrayList<>(); + for (int i = 1; i < target + 1; i++) { + if (set.contains(i)) { + int remaining = target - i; + recurse(remaining); + if (dp.containsKey(remaining)) { + for (String combination : dp.get(remaining)) { + List newCombination = new ArrayList<>(Arrays.stream(combination.split(" ")) + .map(Integer::valueOf) + .toList()); + newCombination.add(i); + newCombination.sort(Comparator.reverseOrder()); + + String newCombinationStr = newCombination.stream() + .map(String::valueOf) + .collect(Collectors.joining(" ")); + if (!combinations.contains(newCombinationStr)) { + combinations.add(newCombinationStr); + } + } + } + } + } + if (set.contains(target)) { + String singleCombination = String.valueOf(target); + if (!combinations.contains(singleCombination)) { + combinations.add(singleCombination); + } + } + dp.put(target, combinations); + } + +} + From 150b12c2babfedd0c0b34cb59c88b7943778c935 Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Sat, 28 Dec 2024 18:26:42 +0900 Subject: [PATCH 5/5] feat: Maximum Subarray #275 --- maximum-subarray/donghyeon95.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 maximum-subarray/donghyeon95.java diff --git a/maximum-subarray/donghyeon95.java b/maximum-subarray/donghyeon95.java new file mode 100644 index 000000000..533e64227 --- /dev/null +++ b/maximum-subarray/donghyeon95.java @@ -0,0 +1,22 @@ +import java.util.Arrays; + +class Solution { + public int maxSubArray(int[] nums) { + int max = Integer.MIN_VALUE; + int current = 0; + + for (int num: nums) { + System.out.println(num + " " +max); + if (current + num >=0) { + max = Math.max(max, current+num); + current = current+num; + } else { + current = 0; + } + } + + // 전부 음수일 경우 => 가장 큰수 return + return max>=0? max: Arrays.stream(nums).max().getAsInt(); + } +} +