Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[donghyeon95] Week3 #803

Merged
merged 5 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions combination-sum/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -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<Integer, List<String>> dp = new HashMap<>();
private HashSet<Integer> set;

public List<List<Integer>> combinationSum(int[] candidates, int target) {
set = new HashSet<>(Arrays.stream(candidates).boxed().toList());
recurse(target);
// Convert dp entries back to List<List<Integer>> 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<String> 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<Integer> 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);
}

}

22 changes: 22 additions & 0 deletions maximum-subarray/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

31 changes: 31 additions & 0 deletions product-of-array-except-self/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -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<nums.length; i++) {
right[i] = right[i-1]*nums[i];
}

// <- 이쪽 방향으로 한번 계산
left[nums.length-1] = nums[nums.length-1];
for (int i=nums.length-2; 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<nums.length-1; i++) {
result[i] = right[i-1] * left[i+1];
}

return result;
}

// 2 포인터를 활용하면 O(1)가능
}

16 changes: 16 additions & 0 deletions reverse-bits/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

31 changes: 31 additions & 0 deletions two-sum/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -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<nums.length; j++) {
// if (nums[i] + nums[j] == target)
// return new int[] {i, j};
// }
// }
// return null;

// O(N)
// HashMap 사용
HashMap<Integer, Integer> map = new HashMap<>();
for (int i=0; i<nums.length; i++) {
map.putIfAbsent(nums[i], i);
}

for (int i=0; i<nums.length; i++) {
int num = nums[i];
int anoterNum = target - num;
if (map.containsKey(anoterNum) && i!=map.get(anoterNum))
return new int[] {i, map.get(anoterNum)};
}
return null;
}
}

Loading