diff --git a/combination-sum/haung921209.md b/combination-sum/haung921209.md new file mode 100644 index 000000000..d90eb4cfd --- /dev/null +++ b/combination-sum/haung921209.md @@ -0,0 +1,66 @@ +# 연관 링크 +- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5) +- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C) + +# Problem +- 문제 링크 : https://leetcode.com/problems/combination-sum/ +- 문제 이름 : Combination Sum +- 문제 번호 : 39 +- 난이도 : Medium +- 카테고리 : DFS + +# 문제 설명 + + +# 아이디어 +- 어떤 방법으로 접근했는지 서술 +- 포스 vs 최적화 아이디어 차이 등 +- 잡도에 대한 고려 + +# ✅ 코드 (Solution) + +### DFS(Brute Force) +```cpp +class Solution { + vector> res; +public: + void helper(vector candidates, vector container, int idx, int sum, int target){ + if(idx>=candidates.size()){ + return; + } + helper(candidates, container, idx+1, sum, target); + container.push_back(candidates[idx]); + int mdfiedSum = sum+candidates[idx]; + if(mdfiedSum==target){ + res.push_back(container); + return; + }else if(mdfiedSum>target){ + return; + } + + helper(candidates, container, idx, mdfiedSum, target); + } + + vector> combinationSum(vector& candidates, int target){ + res = vector>(); + helper(candidates, vector(), 0, 0, target); + return res; + } +}; +``` + + +# 🔍 코드 설명 + + +# 최적화 포인트 (Optimality Discussion) +- helper 함수에서 mdfiedSum을 사용하거나, 혹은 인자로 sum을 받지 않고 깎아나가는 방식으로 처리하면 일부 최적화 할 수 있음. + - 취향 정도로 봐도 무방할듯 + +# 🧪 테스트 & 엣지 케이스 + +# 📚 관련 지식 복습 + +# 🔁 회고 + + diff --git a/decode-ways/haung921209.md b/decode-ways/haung921209.md new file mode 100644 index 000000000..b1d7b9b00 --- /dev/null +++ b/decode-ways/haung921209.md @@ -0,0 +1,124 @@ +# 연관 링크 +- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5) +- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C) + +# Problem +- 문제 링크 : http://leetcode.com/problems/decode-ways/ +- 문제 이름 : Decode ways +- 문제 번호 : 91 +- 난이도 : medium +- 카테고리 : DFS, DP + +# 문제 설명 + + +# 아이디어 +- 어떤 방법으로 접근했는지 서술 +- 포스 vs 최적화 아이디어 차이 등 +- 잡도에 대한 고려 + +# ✅ 코드 (Solution) + +## DFS(Memory Limit Exceeded) +```cpp +class Solution { +int cnt; +public: + bool canBeDecoded(vector st){ + if(st.size()>2){ + return false; + }else if(st.size()==1){ + return st[0]!='0'; + } + //st.size()==2 + + if(st[0]=='1'){ + return true; + }else if(st[0]=='2'){ + return (int)(st[1]-'0') <= 6; + }else{ + return false; + } + } + + void helper(string s, int idx){ + if(idx==s.size()){ + cnt++; + return; + }else if(idx>s.size()){ + return; + } + if(canBeDecoded(vector(s.begin()+idx, s.begin()+idx+1))){ + helper(s, idx+1); + } + + if(idx+2<=s.size() && canBeDecoded(vector(s.begin()+idx, s.begin()+idx+2))){ + helper(s, idx+2); + } + } + + int numDecodings(string s) { + cnt = 0; + helper(s, 0); + return cnt; + } +}; +``` +- 단순 DFS 풀이 +- Memory Limit Exceeded(Heap 메모리 초과) 발생 + +## DP + +```cpp +class Solution { +public: + int numDecodings(string s) { + vector dp = vector(s.size()+1, -1); + return helper(s, 0, dp); + } + + int helper(string s, int idx, vector& dp){ + if(s.size()==idx){ + return 1; + } + if(s[idx]=='0'){ + return 0; + } + + if(dp[idx]!=-1){ + return dp[idx]; + } + + int res = helper(s, idx+1, dp); + + if(idx+1=10 && num <= 26){ + res += helper(s, idx+2, dp); + } + + } + return dp[idx] = res; + + } +}; +``` + +- dp +- 메모이제이션 +- stoi / substr 멤버 함수 사용하여 decode 함수 간략화 + + +# 최적화 포인트 (Optimality Discussion) +- 메모이제이션 +- stoi / substr 멤버 함수 사용하여 decode 함수 간략화 + +# 🧪 테스트 & 엣지 케이스 + +# 📚 관련 지식 복습 +- 메모이제이션 +- stl(stoi / substr 등) + +# 🔁 회고 + + diff --git a/maximum-subarray/haung921209.md b/maximum-subarray/haung921209.md new file mode 100644 index 000000000..eccb9db4a --- /dev/null +++ b/maximum-subarray/haung921209.md @@ -0,0 +1,119 @@ +# 연관 링크 +- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5) +- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C) + +# Problem +- 문제 링크 : https://leetcode.com/problems/maximum-subarray/description/ +- 문제 이름 : Maximum Subarray +- 문제 번호 : 53 +- 난이도 : Medium +- 카테고리 : + +# 문제 설명 + + +# 아이디어 +- 어떤 방법으로 접근했는지 서술 +- 포스 vs 최적화 아이디어 차이 등 +- 잡도에 대한 고려 + +# ✅ 코드 (Solution) +## Brute force +```cpp +class Solution { +public: + int maxSubArray(vector& nums) { + vector partialSum(nums.size()+1, 0); + int res = nums[0]; + for(int i=0;i TLE +## Kadane Algorithm - pass +```cpp +class Solution { +public: + int maxSubArray(vector& nums) { + int currentSum = nums[0]; + int maxSum = nums[0]; + + for (int i = 1; i < nums.size(); ++i) { + currentSum = max(nums[i], currentSum + nums[i]); + maxSum = max(maxSum, currentSum); + } + + return maxSum; + } +}; +``` + +- O(n) +- 다음 원소를 추가했을 때 더 좋아지면 연장, 아니면 새로 시작하며 계속 갱신 + +## Divide and conquer - pass +```cpp +class Solution { +public: + int maxCrossingSum(vector& nums, int left, int mid, int right) { + int leftSum = INT_MIN, sum = 0; + for (int i = mid; i >= left; i--) { + sum += nums[i]; + leftSum = max(leftSum, sum); + } + + int rightSum = INT_MIN; + sum = 0; + for (int i = mid + 1; i <= right; i++) { + sum += nums[i]; + rightSum = max(rightSum, sum); + } + + return leftSum + rightSum; + } + + int maxSubArrayHelper(vector& nums, int left, int right) { + if (left == right) + return nums[left]; + + int mid = (left + right) / 2; + + int leftMax = maxSubArrayHelper(nums, left, mid); + int rightMax = maxSubArrayHelper(nums, mid + 1, right); + int crossMax = maxCrossingSum(nums, left, mid, right); + + return max({leftMax, rightMax, crossMax}); + } + + int maxSubArray(vector& nums) { + return maxSubArrayHelper(nums, 0, nums.size() - 1); + } +}; +``` + +- O(n log n) +- 참고용 + +# 최적화 포인트 (Optimality Discussion) +• 최적화한 이유와 원리 +• 더 줄일 수 있는 여지는 있는가? +• 기존 방법 대비 얼마나 효율적이었는지 + +# 🧪 테스트 & 엣지 케이스 + +# 📚 관련 지식 복습 + +# 🔁 회고 + + diff --git a/number-of-1-bits/haung921209.md b/number-of-1-bits/haung921209.md new file mode 100644 index 000000000..a12634121 --- /dev/null +++ b/number-of-1-bits/haung921209.md @@ -0,0 +1,53 @@ +# 연관 링크 +- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5) +- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C) + +# Problem +- 문제 링크 : https://leetcode.com/problems/number-of-1-bits/description/ +- 문제 이름 : Number of 1 Bits +- 문제 번호 : 191 +- 난이도 : easy +- 카테고리 : + +# 문제 설명 +- 2진수 자릿수 처리 +- 2로 나눠 나머지 고려 및 몇번 나눠지는지 고려가 필요 + +# 아이디어 +- brute force + +# ✅ 코드 (Solution) + +```cpp +class Solution { +public: + int hammingWeight(int n) { + int res = 0; + while(n>0){ + if(n%2==1){ + res++; + n--; + } + n = n/2; + } + return res; + } +}; +``` + +# 🔍 코드 설명 + +brute force + +# 최적화 포인트 (Optimality Discussion) +• 최적화한 이유와 원리 +• 더 줄일 수 있는 여지는 있는가? +• 기존 방법 대비 얼마나 효율적이었는지 + +# 🧪 테스트 & 엣지 케이스 + +# 📚 관련 지식 복습 + +# 🔁 회고 + + diff --git a/valid-palindrome/haung921209.md b/valid-palindrome/haung921209.md new file mode 100644 index 000000000..4fc2de405 --- /dev/null +++ b/valid-palindrome/haung921209.md @@ -0,0 +1,58 @@ +# 연관 링크 +- [문제 풀이 스케줄](https://github.com/orgs/DaleStudy/projects/6/views/5) +- [답안 코드 제출법](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C) + +# Problem +- 문제 링크 : https://leetcode.com/problems/valid-palindrome/description/ +- 문제 이름 : 125. Valid Palindrome +- 문제 번호 : 125 +- 난이도 : easy +- 카테고리 : + +# 문제 설명 + + +# 아이디어 +- brute force + +# ✅ 코드 (Solution) + +```cpp +class Solution { +public: + bool isPalindrome(string s) { + vector pureAlphabat; + for(auto cur: s){ + if(cur >= 'a' && cur <='z'){ + pureAlphabat.push_back(cur); + }else if(cur>= 'A' && cur <='Z'){ + pureAlphabat.push_back(cur-'A'+'a'); + }else if(cur>='0' && cur<='9'){ + pureAlphabat.push_back(cur); + } + } + for(int i=0;i