-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f38fc83
commit 547b2dc
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
} |