Skip to content

[ackku] Week 3 #795

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

Merged
merged 2 commits into from
Dec 29, 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
26 changes: 26 additions & 0 deletions combination-sum/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 처음엔 dp라 생각했는데, backtracking인걸 알아차리자마자 풀 수 있었음
// 중간 결과를 계속 전달하는게 팁
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
backtracking(candidates, target, 0, new ArrayList<>(), result);
return result;
}

private void backtracking(int[] candidates, int target, int start, List<Integer> tmp, List<List<Integer>> result) {
if (target < 0) {
return;
}

if (target == 0) {
result.add(new ArrayList<>(tmp));
return;
}

for (int i = start; i < candidates.length; i++) {
tmp.add(candidates[i]);
backtracking(candidates, target - candidates[i], i, tmp, result);
tmp.remove(tmp.size() - 1);
}
}
}
16 changes: 16 additions & 0 deletions maximum-subarray/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// subarray = "연속된 값"의 합을 요구 함 그래서 간단한 풀이가 가능하다.
// 이전 인덱스나 값들을 기억할 필요가 없어서 누적합 느낌으로 풀 수 있다.
// 키포인트는 이전까지의 합보다 다음 숫자가 큰 경우의 수가 존재한다는 것
class Solution {
public int maxSubArray(int[] nums) {
int max = nums[0];
int current = nums[0];

for (int i = 1; i < nums.length; i++) {
current = Math.max(nums[i], current + nums[i]);
max = Math.max(max, current);
}

return max;
}
}
20 changes: 20 additions & 0 deletions product-of-array-except-self/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
// 누적곱을 이용한 풀이 int 크기를 보장해줘서 int를 사용하면된다.
// O(N)의 시간복잡도가 나온다.
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] result = new int[n];
result[0] = 1;
for (int i = 1; i < n; i++) {
result[i] = result[i - 1] * nums[i - 1];
}

int right = 1;
for (int i = n - 1; i >= 0; i--) {
result[i] = result[i] * right;
right *= nums[i];
}

return result;
}
}
23 changes: 23 additions & 0 deletions reverse-bits/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 스택으로 풀기보다 비트연산자로 풀이
// 스택을 사용할 때는 32자리의 불필요한 공간이 생긴다.
public class Solution {
public int reverseBits(int n) {
// n 이 십진수로 들어온다.(중요)
int result = 0;
for (int i = 0; i < 32; i++) {
// 마지막 비트 확인
// 홀수: 마지막 비트가 1 (n & 1 == 1)
// 짝수: 마지막 비트가 0 (n & 1 == 0)
int bit = n & 1;
// result를 왼쪽으로 1비트 이동하고, 추출한 비트를 추가
// - result의 마지막 비트를 비우고 (<< 1)
// - OR 연산(|)으로 추출한 비트를 추가
result = (result << 1) | bit;

// n을 오른쪽으로 1비트 이동하여 다음 비트를 준비
// - n의 마지막 비트를 버리고, 상위 비트를 아래로 이동
n >>= 1;
}
return result;
}
}
59 changes: 59 additions & 0 deletions two-sum/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// hashmap 조회 방식은 O(N)
// 3ms
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
return null;
}

// 첫 생각 : 정렬 -> 투포인터
public int[] twoSum1(int[] nums, int target) {
Arrays.sort(nums);
int left = 0;
int right = nums.length-1;
int sum = 0;
while(left < right) {
sum = nums[left] + nums[right];
if(target > sum) {
left++;
}
if(target < sum) {
right--;
}
if(target == sum) {
break;
}
}
return new int[]{left, right};
}

// 투포인터는 O(N)에 충족하지만 정렬이 nlog(n)임
// 9ms
public int[] twoSum2(int[] nums, int target) {
int[][] indexedNums = new int[nums.length][2];
for (int i = 0; i < nums.length; i++) {
indexedNums[i][0] = nums[i];
indexedNums[i][1] = i;
}

Arrays.sort(indexedNums, Comparator.comparingInt(a -> a[0]));

int left = 0, right = nums.length - 1;
while (left < right) {
int sum = indexedNums[left][0] + indexedNums[right][0];
if (sum == target) {
return new int[] { indexedNums[left][1], indexedNums[right][1] };
} else if (sum < target) {
left++;
} else {
right--;
}
}
return new int[]{left, right};
}
Loading