Skip to content

[혜준] Week2 문제 풀이 #351

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

Merged
merged 7 commits into from
Aug 25, 2024
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
31 changes: 31 additions & 0 deletions counting-bits/hyejjun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @param {number} n
* @return {number[]}
*/
var countBits = function (n) {

let result = [];

for (let i = 0; i <= n; i++) {

let binary = i.toString(2);
Comment on lines +7 to +11
Copy link
Member

@sounmind sounmind Aug 24, 2024

Choose a reason for hiding this comment

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

Suggested change
let result = [];
for (let i = 0; i <= n; i++) {
let binary = i.toString(2);
�const result = [];
for (let i = 0; i <= n; i++) {
const binary = i.toString(2);

저는 재할당 되지 않을 변수는 const 키워드로 선언하는 편인데요. 이는 코드를 읽는 사람으로 하여금 이 변수는 재할당되지 않을 것임을 예측할 수 있도록 해주고, 나중에 실수로 해당 변수에 다른 값을 할당하게 되는 버그를 사전에 예방할 수 있기 때문이에요.

let sum = 0;

for (let char of binary) {
sum += Number(char);
}
result.push(sum);
}

return result;

};


console.log(countBits(2));
console.log(countBits(5));

/*
시간 복잡도: O(n * log n)
Copy link
Member

Choose a reason for hiding this comment

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

안녕하세요~ 어떤 이유로 시간 복잡도가 n log n 이 나오는지, 여기서 n은 무엇을 가리키는지 설명해주시면 더 좋을 것 같아요! 나중에 실제 코딩 테스트 면접에서도 도움이 될 거라고 생각합니다 ☺️

공간 복잡도: O(n)
*/
42 changes: 42 additions & 0 deletions decode-ways/hyejjun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @param {string} s
* @return {number}
*/

var numDecodings = function (s) {
if (s == null || s.length === 0) {
return 0;
}

const n = s.length;
const dp = new Array(n + 1).fill(0);
dp[0] = 1;
dp[1] = s[0] === '0' ? 0 : 1;

for (let i = 2; i <= n; i++) {
const oneDigit = parseInt(s.substring(i - 1, i));
const twoDigits = parseInt(s.substring(i - 2, i));

// Check if single digit decoding is possible
if (oneDigit >= 1 && oneDigit <= 9) {
dp[i] += dp[i - 1];
}

// Check if two digit decoding is possible
if (twoDigits >= 10 && twoDigits <= 26) {
dp[i] += dp[i - 2];
}
}

return dp[n];
};


console.log(numDecodings("12"));
console.log(numDecodings("226"));
console.log(numDecodings("06"));

/*
시간 복잡도: O(n)
공간 복잡도: O(n)
*/
33 changes: 33 additions & 0 deletions valid-anagram/hyejjun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
if (s.length !== t.length) return false;

let countS = {};
let countT = {};

for (let i = 0; i < s.length; i++) {
countS[s[i]] = (countS[s[i]] || 0) + 1;
countT[t[i]] = (countT[t[i]] || 0) + 1;
}

for (let key in countS) {
if (countS[key] !== countT[key]) {
return false;
}
}

return true;

};

console.log(isAnagram("anagram", "nagaram"));
console.log(isAnagram("rat", "car"));

/*
시간 복잡도: O(n)
공간 복잡도: O(n)
*/
Loading