Skip to content
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
40 changes: 40 additions & 0 deletions combination-sum/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
중복 조합을 통해 조건에 맞는 조합을 찾는 방식
candidates의 길이 -> N
시간 복잡도 : O(2^N)
공간 복잡도 : O(N)
*/
class Solution {
List<List<Integer>> list;
List<Integer> dataList;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
list = new ArrayList<>();
dataList = new ArrayList<>();
execute(candidates, 0, 0, target);
return list;
}

public void execute(int[] candidates, int idx, int sum, int target) {
if(idx >= candidates.length) {
return;
}

if(sum == target) {
list.add(new ArrayList<>(dataList));
return;
}

if(sum+candidates[idx] > target) {
return;
}




dataList.add(candidates[idx]);
execute(candidates, idx, sum + candidates[idx], target);
dataList.remove(dataList.size()-1);
execute(candidates, idx+1, sum, target);
}
}
69 changes: 69 additions & 0 deletions decode-ways/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
HashSet과 substring을 이용하여 문자열을 비교하면서 가능한 조합의 수를 누적하는 방식
문자열 S 의 길이 -> N
시간 복잡도 : O(N^2)
공간 복잡도 : O(N)
*/
class Solution2 {
Set<String> numberSet = new HashSet<>();
{
numberSet.addAll(Arrays.asList("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"));
}
public int numDecodings(String s) {
if(s.length() == 1) return s.charAt(0) == '0' ? 0 : 1;

int[] list = new int[s.length()];
list[0] = isImpossibleDecode(s.substring(0,1)) ? 0 : 1;
list[1] = isImpossibleDecode(s.substring(0,2)) ? 0 : 1;
list[1] += list[0]==1 && !isImpossibleDecode(s.substring(1,2)) ? 1 : 0;


for(int i = 2; i < s.length() ; i++) {
list[i] += isImpossibleDecode(s.substring(i-1,i+1)) ? 0 : list[i-2];
list[i] += isImpossibleDecode(s.substring(i,i+1)) ? 0 : list[i-1];
}

return list[s.length()-1];

}

public boolean isImpossibleDecode(String target) {
int len = target.length();
if(!numberSet.contains(target)) {
return true;
}

return false;

}
}

/**
이전 substring() 연산 부분 최적화
이전 substring()으로 문자열을 자르지 않고, charAt()을 이용하여 직접 연산하여 조합의 수를 누적시키는 방식
문자열 S 의 길이 -> N
시간 복잡도 : O(N)
공간 복잡도 : O(N)
*/
class Solution {

public int numDecodings(String s) {
if(s.length() == 1) return s.charAt(0) == '0' ? 0 : 1;

int[] list = new int[s.length()];
list[0] = s.charAt(0) == '0' ? 0 : 1;
int target = (s.charAt(0) - '0') * 10 + (s.charAt(1) - '0');
list[1] = (target >= 10) && (target <= 26) ? 1 : 0;
list[1] += list[0]==1 && s.charAt(1) != '0' ? 1 : 0;


for(int i = 2; i < s.length() ; i++) {
target = (s.charAt(i-1) - '0') * 10 + (s.charAt(i) - '0');
list[i] += (target >= 10) && (target <= 26) ? list[i-2] : 0;
list[i] += s.charAt(i) != '0' ? list[i-1] : 0;
}

return list[s.length()-1];

}
}
40 changes: 40 additions & 0 deletions maximum-subarray/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
dp를 이용하여 지속적으로 최대의 값을 기록하면서 연속 부분 배열의 최대 합을 구하는 방식
nums 의 길이 -> N
시간 복잡도 : O(N)
공간 복잡도 : O(N)
*/
class Solution2 {
public int maxSubArray(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = nums[0];
for(int i = 1; i < nums.length; i++) {
dp[i] = Math.max(nums[i], dp[i-1] + nums[i]);
}

for(int i = 0; i < dp.length; i++) {
}
return Arrays.stream(dp)
.max()
.getAsInt();
}
}

/**
이전 방식과 동일하나 변수 하나로 이전 값만 유지하는 방식
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위에 dp를 사용해서 하는 방식도 좋은데 하나의 변수로 처리하는 것도 공간 복잡도를 개선시킬 수 있군요 👍

nums 의 길이 -> N
시간 복잡도 : O(N)
공간 복잡도 : O(1)
*/
class Solution {
public int maxSubArray(int[] nums) {
int prevSum = nums[0];
int result = prevSum;
for(int i = 1; i < nums.length; i++) {
prevSum = Math.max(nums[i], prevSum + nums[i]);
result = Math.max(result, prevSum);
}

return result;
}
}
30 changes: 30 additions & 0 deletions number-of-1-bits/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
비트 연산을 통해, 1의 개수를 구하는 방식
숫자 n -> N
시간 복잡도 : O(logN)
공간 복잡도 : O(1)
*/
class Solution2 {
public int hammingWeight(int n) {
int count = 0;
long bit = 1;
while(bit <= n) {
if((n & bit) > 0) {
count++;
}
bit <<= 1;
}
return count;
}
}

/**
Integer.bitCount() 메소드를 통해, 1의 개수를 구하는 방식
시간 복잡도 : O(1)
공간 복잡도 : O(1)
*/
class Solution {
public int hammingWeight(int n) {
return Integer.bitCount(n);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java는 모르지만 bitCount라는 메소드 강력하네요!

}
}
37 changes: 37 additions & 0 deletions valid-palindrome/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

투포인터로 풀이할 생각은 못했는데..한 수 배워갑니다..

투포인터 방식을 통해 맨 앞과 맨 끝 포인터에서 맨 끝 포인터가 맨 앞 포인터를 앞지를 때까지 비교하는 방식
문자열 s의 길이 -> N
시간 복잡도 : O(N)
공간 복잡도 : O(1)
*/
class Solution {
public boolean isPalindrome(String s) {
s=s.toUpperCase();
int start=0;
int end=s.length()-1;
while(start < end){
char ch=s.charAt(start);
char ch2=s.charAt(end);

// 문자 체크
if(!((ch>=65 && ch<=90) || (ch>=48 && ch<=57))){
start++;
continue;
}
if(!((ch2>=65 && ch2<=90) || (ch2>=48 && ch2<=57))){
end--;
continue;
}

if(ch==ch2){
start++;
end--;
continue;
}

return false;

}
return true;
}
}