diff --git a/combination-sum/hyeri0903.java b/combination-sum/hyeri0903.java new file mode 100644 index 0000000000..f8037ec430 --- /dev/null +++ b/combination-sum/hyeri0903.java @@ -0,0 +1,26 @@ +class Solution { + public List> combinationSum(int[] candidates, int target) { + List> answer = new ArrayList<>(); + Arrays.sort(candidates); //불필요한 탐색을 줄이기 위해 sorting + dfs(0, candidates, target, new ArrayList<>(), answer); + return answer; + + } + + public void dfs(int start, int[] candidates, int target, List curList, List> answer) { + if (target == 0) { + answer.add(new ArrayList<>(curList)); + return; + } + for(int i = start; i < candidates.length; i++) { + if (candidates[i] > target) { + break; + } + + curList.add(candidates[i]); + dfs(i, candidates, target - candidates[i], curList, answer); + curList.remove(curList.size()-1); //다음 경우의 수를 위해 backtracking + + } + } +} diff --git a/decode-ways/hyeri0903.java b/decode-ways/hyeri0903.java new file mode 100644 index 0000000000..96b15121e9 --- /dev/null +++ b/decode-ways/hyeri0903.java @@ -0,0 +1,45 @@ +class Solution { + public int numDecodings(String s) { + /** + 1.문제: decode 할 수 있는 모든 경우의 수 return + 2.조건: + - 여러 개의 경우의 수 있을 수 있다. + - decode 불가능한 경우가 있을 수 있다. + - 모두 불가능한 경우 return 0 + 3.풀이 + - dp (해석할 수 있는 방법 수를 누적해서 더함) + e.g 226 + 0자리 : dp[0] = 0 + 1자리(2) : 2 -> dp]1] = 1 + 2자리(22): 2(B), 22(v) -> dp[2] = 2 + 3자리(226): (2, 2, 6), (22, 6), (2, 26) -> dp[3] = 3 + + time complexity: O(N) + space complexity: O(N) + + */ + if (s.charAt(0) == '0') { + return 0; + } + int n = s.length(); + int[] dp = new int[n+1]; + dp[0] = 1; //아무것도 없는 상태 + dp[1] = 1; + + for(int i = 2; i<=n; i++) { + //1자리수 + if (s.charAt(i-1) != '0') { + dp[i] += dp[i-1]; + } + //2자리수 + if(i > 1) { + int number = Integer.parseInt(s.substring(i-2, i)); + //2자리 수는 10 ~ 26사이만 가능 + if (number >= 10 && number <= 26) { + dp[i] += dp[i-2]; + } + } + } + return dp[n]; + } +} diff --git a/maximum-subarray/hyeri0903.java b/maximum-subarray/hyeri0903.java new file mode 100644 index 0000000000..319839f54e --- /dev/null +++ b/maximum-subarray/hyeri0903.java @@ -0,0 +1,23 @@ +class Solution { + public int maxSubArray(int[] nums) { + /** + 1. 문제: 가장 큰 합을 가지는 subarray 의 sum 을 반환 + 2. 조건: 원소값은 음수 ~ 양수, 배열 최대 길이 = 10^5, 최소 길이 = 1 + - time complexity: O(N) + - space complexity: O(1) + */ + + if (nums.length == 1) { + return nums[0]; + } + + int maxSum = nums[0]; //전체 최대 + int curSum = nums[0]; //현재 합 + + for(int i = 1; i 0) { + // if (n % 2 != 0) { + // count += 1; + // } + // n = n / 2; + // } + + while(n > 0) { + if ((n & 1) == 1) { + count += 1; + } + n >>= 1; + } + return count; + } +} diff --git a/valid-palindrome/hyeri0903.java b/valid-palindrome/hyeri0903.java new file mode 100644 index 0000000000..b245a1202c --- /dev/null +++ b/valid-palindrome/hyeri0903.java @@ -0,0 +1,40 @@ +class Solution { + public boolean isPalindrome(String s) { + /** + 1. 문제: 팰린드롬인지 판단하는 문제. + 2. 조건: 알파벳 소문자로 변환, 문자 또는 숫자가 아닌건 모두 제거 (공백, 쉼표 등)) + - left, right index 이중 포인터로 풀이 + time complexity : O(n) + space complexity : O(1) + */ + boolean answer = true; + //문자열 추가하면 space complexity : O(n) + //s = s.toLowerCase(); + //s = s.replaceAll("[^0-9a-z]", ""); + int left = 0 ; + int right = s.length() - 1; + + while(left < right) { + char l = s.charAt(left); + char r = s.charAt(right); + + //왼쪽이 알파벳 or 숫자가 아니면 skip + if (!Character.isLetterOrDigit(l)) { + left += 1; + continue; + } + //오른쪽이 알파벳 or 숫자가 아니면 skip + if (!Character.isLetterOrDigit(r)) { + right -= 1; + continue; + } + if (Character.toLowerCase(l) != Character.toLowerCase(r) ) { + return false; + } + left += 1; + right -= 1; + } + + return answer; + } +}