Skip to content

Commit

Permalink
Merge pull request #828 from GotPrgmer/main
Browse files Browse the repository at this point in the history
[Gotprgmer] Week4
  • Loading branch information
SamTheKorean authored Jan 5, 2025
2 parents d4a5f10 + c12d459 commit 11eb1fd
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
37 changes: 37 additions & 0 deletions decode-ways/Gotprgmer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ์™„์ „ํƒ์ƒ‰์„ ํ†ตํ•ด ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜์˜€์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.
// dfs๋ฅผ ํ†ตํ•ด ํ’€์ดํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ O(2^N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ์ธํ•ด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.
// ์ดํ›„ dp๋กœ ํ’€์ด๋ฅผ ์‹œ์ž‘ํ•˜์˜€๊ณ  ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ’€์ดํ•˜์˜€์Šต๋‹ˆ๋‹ค.
// dp[i] = dp[i-1] + dp[i-2]๋กœ ํ’€์ดํ•˜์˜€์Šต๋‹ˆ๋‹ค.
// ์ด๋•Œ i๋ฒˆ์งธ ๋ฌธ์ž์—ด์„ 1์ž๋ฆฌ๋กœ ์ทจ๊ธ‰ํ• ์ง€ 2์ž๋ฆฌ๋กœ ์ทจ๊ธ‰ํ• ์ง€์— ๋”ฐ๋ผ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค.
// 1์ž๋ฆฌ๋กœ ์ทจ๊ธ‰ํ•  ๊ฒฝ์šฐ 1~9๊นŒ์ง€ ๊ฐ€๋Šฅํ•˜๊ณ 
// 2์ž๋ฆฌ๋กœ ์ทจ๊ธ‰ํ•  ๊ฒฝ์šฐ 10~26๊นŒ์ง€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

// ์‹œ๊ฐ„๋ณต์žก๋„ : O(N)
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(N)
class SolutionGotprgmer {
public int numDecodings(String s) {
// ์˜ˆ์™ธ ์ฒ˜๋ฆฌ: ๋ฌธ์ž์—ด์ด "0"์œผ๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋นˆ ๋ฌธ์ž์—ด์ด๋ฉด
if (s == null || s.length() == 0 || s.charAt(0) == '0') {
return 0;
}
int[] dp = new int[s.length()+1];
dp[0] = 1;
for(int i=0;i<s.length();i++){
int ith = s.charAt(i)-'0';
if(ith != 0){
dp[i+1] = dp[i];
}
if(i>0){
String twoDigitStr = s.substring(i-1,i+1);
int twoDigitNum = Integer.valueOf(twoDigitStr);
if(twoDigitNum>=10 && twoDigitNum <27){
dp[i+1] += dp[i-1];
}
}

}
return dp[s.length()];
}


}
18 changes: 18 additions & 0 deletions missing-number/Gotprgmer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// ๋‹จ์ˆœํ•˜๊ฒŒ ์ •๋ ฌํ•ด์„œ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ์ถœ๋ ฅํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ๊ทธ๋Œ€๋กœ checkNum์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(NlogN)
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(1)

class SolutionGotprgmer {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
int checkNum = 0;
for(int i=0;i<nums.length;i++){
if(nums[i] != checkNum){
return checkNum;
}
checkNum += 1;
}
return checkNum;

}
}
13 changes: 13 additions & 0 deletions reverse-bits/Gotprgmer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ๋ดค์„๋•Œ๋Š” ์ดํ•ด๊ฐ€ ์ž˜ ๊ฐ€์ง€ ์•Š์•˜์ง€๋งŒ,
// ๋น„ํŠธ๋“ค์„ ๋’ค์ง‘์œผ๋ผ๋Š” ์„ค๋ช…์œผ๋กœ ํ’€์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
// Integer.reverse() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์—ˆ๋‹ค.
// ์ง€ํ”ผํ‹ฐ์˜ ๋„์›€์œผ๋กœ Integer.reverse()๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๋Š” ํžŒํŠธ๋ฅผ ์–ป์—ˆ๋‹ค.
// ์ฐพ์•„๋ณด๋‹ˆ reverse(N)๋Š” N์„ 2์˜ ๋ณด์ˆ˜ ๋น„ํŠธ๋กœ ๋ฐ”๊พธ๊ณ  ๊ทธ๊ฒƒ์„ ๋’ค์ง‘๋Š” ๋ฐฉ์‹์ด์—ˆ๋‹ค.
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(1) -> Integer๊ฐ€ 32๋น„ํŠธ ๊ณ ์ •์ด๋ผ์„œ O(1)
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(1) -> 32๋น„ํŠธ ๊ณ ์ •
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
return Integer.reverse(n);
}
}
54 changes: 54 additions & 0 deletions two-sum/Gotprgmer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜์—ฌ ํˆฌํฌ์ธํ„ฐ๋กœ ์ ‘๊ทผํ•˜์—ฌ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
// ์ •๋ ฌ๋œ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด indexOf ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

// ์‹œ๊ฐ„๋ณต์žก๋„ : O(NlogN) -> ์ •๋ ฌ์„ ์œ„ํ•ด O(NlogN) + ํˆฌํฌ์ธํ„ฐ๋กœ O(N)์ด๋ฏ€๋กœ O(NlogN)
// ๊ณต๊ฐ„๋ณต์žก๋„ : O(N) -> ์ •๋ ฌ์„ ์œ„ํ•ด ๋ณต์‚ฌํ•œ ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜๋ฏ€๋กœ O(N)
class SolutionGotprgmer {
public int[] twoSum(int[] nums, int target) {
int[] original = new int[nums.length];

for(int i=0;i<nums.length;i++){
original[i] = nums[i];
}
Arrays.sort(nums);

int l = 0;
int r = nums.length-1;
while (l<r){
int lV = nums[l];
int rV = nums[r];
int total = lV + rV;
if(total > target){
r -= 1;
}
else if(total < target){
l += 1;
}
else{
int[] ans = indexOf(lV,rV,original);
l = ans[0];
r = ans[1];
break;
}
}
return new int[] {l,r};
}

public int[] indexOf(int l,int r, int[] nums){
int lIdx = -1;
int rIdx = -1;
for(int i = 0;i<nums.length;i++){
if(nums[i] == l){
lIdx = i;
break;
}
}
for(int i = nums.length-1;i>-1;i--){
if(nums[i] == r){
rIdx = i;
break;
}
}
return new int[] {lIdx,rIdx};
}
}

0 comments on commit 11eb1fd

Please sign in to comment.