Skip to content

[ohgyulim] WEEK 03 solutions #1800

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions combination-sum/ohgyulim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.*;

class Solution {
List<List<Integer>> answer = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
recur(new ArrayList<Integer>(), candidates, target, 0, 0);
return answer;
}

public void recur(List<Integer> result, int[] candidates, int target, int sum, int index) {
if (sum == target) {
List<Integer> deepCopyRes = new ArrayList<>(result);
answer.add(deepCopyRes);
return;
}
for (int i = index; i < candidates.length; i++) {
if (sum + candidates[i] <= target) {
result.add(candidates[i]);
recur(result, candidates, target, sum + candidates[i], i);
result.remove(Integer.valueOf(candidates[i]));
}
}
}
}
26 changes: 26 additions & 0 deletions decode-ways/ohgyulim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
/* 시간 복잡도: O(N)
* - for 루프: O(N)
* 공간 복잡도: O(N), dp배열
*/
public int numDecodings(String s) {
if (s.charAt(0) == '0') return 0;
int[] dp = new int[s.length() + 1];
dp[0] = 1;
dp[1] = 1;

for (int i = 2; i <= s.length(); i++) {
int one = Integer.parseInt(s.substring(i - 1, i));
int two = Integer.parseInt(s.substring(i - 2, i));

if (one > 0) {
dp[i] += dp[i - 1];
}
if (two >= 10 && two <= 26) {
dp[i] += dp[i - 2];
}
}
return dp[s.length()];
}
}

17 changes: 17 additions & 0 deletions maximum-subarray/ohgyulim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
/* 시간 복잡도: O(N)
* - for 루프: O(N)
* 공간 복잡도: O(N), dp배열
*/
public int maxSubArray(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = nums[0];

int answer = nums[0];
for (int i = 1; i < nums.length; i++) {
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
answer = Math.max(answer, dp[i]);
}
return answer;
}
}
10 changes: 10 additions & 0 deletions number-of-1-bits/ohgyulim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
/* 시간 복잡도: O(1)
*
* 공간 복잡도: O(1)
*/
public int hammingWeight(int n) {
return Integer.bitCount(n);
}
}

26 changes: 26 additions & 0 deletions valid-palindrome/ohgyulim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
/* 시간 복잡도: O(N)
* - for 루프: O(N)
*
* 공간 복잡도: O(N), StringBuilder
*/
public boolean isPalindrome(String s) {
StringBuilder sb = new StringBuilder();

for (char ch : s.toCharArray()) {
if (Character.isLetterOrDigit(ch)) {
sb.append(Character.toLowerCase(ch));
}
}

for (int i = 0; i < sb.length(); i++) {
int left = i;
int right = sb.length() - i - 1;

if (left >= right) return true;
if (sb.charAt(left) != sb.charAt(right)) return false;
}

return true;
}
}