Skip to content

Commit

Permalink
feat : week 3
Browse files Browse the repository at this point in the history
  • Loading branch information
imsosleepy committed Dec 27, 2024
1 parent f38fc83 commit 547b2dc
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
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/imssoleepy.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};
}

0 comments on commit 547b2dc

Please sign in to comment.