Skip to content
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
45 changes: 45 additions & 0 deletions 3sum/hozzijeong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
nums.sort((a,b) => a-b);
const length = nums.length;

const answer = [];


for(let i = 0; i < length - 2; i++){
if(i > 0 && nums[i] === nums[i-1]) continue;
if(nums[i] > 0) break;

let left = i + 1;
let right = length -1;

while(left < right){
const result = nums[left]+ nums[right] + nums[i];

if(result > 0) {
right -= 1;
}

if(result < 0) {
left +=1;
}

if(result === 0) {
answer.push([nums[i],nums[left],nums[right]]);

while(left < right && nums[left] === nums[left + 1]) left += 1;
while(left < right && nums[right] === nums[right - 1]) right -=1;

left += 1;
right -= 1;
}
}


}

return answer
};
21 changes: 21 additions & 0 deletions climbing-stairs/hozzijeong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* DP 문제라는 것을 알았지만 개념을 잘 몰라서 약간의 검색을 했습니다.
* 하나의 문제를 여러 작은 문제들로 쪼갤 수 있고, 그 쪼갠 문제가 다시 해당 문제의 해답이 될 수 있으며, 중복된 값이 존재하는 조건에 맞기 떄문에 DP로 풀었습니다.
* 처음에는 피보나치 형식으로 접근했다가 시간초과가 나버려서 bottom-up 방식으로 변경해서 적용했습니다
*/


/**
* @param {number} n
* @return {number}
*/
var climbStairs = function(n) {
const answer = [1,2];

for(let i = 2; i < n; i++){
const value = answer[i-1] + answer[i-2];
Copy link
Contributor

Choose a reason for hiding this comment

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

이렇게 DP 테이블의 가장 최근 n개의 원소만 사용하는 경우에는 O(n) space의 DP 테이블 대신 O(1)space의 변수를 사용해서 공간 복잡도를 한 단계 최적화 할 수 있을 것 같아요~!

answer.push(value);
}

return answer[n-1];
};
23 changes: 23 additions & 0 deletions product-of-array-except-self/hozzijeong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
// 0이 2개 이상인 경우에는 무조건 0
if(nums.filter((num) => num === 0).length > 1) return Array.from({length:nums.length}).fill(0);

// 0이 1개인 경우에 나타나는 수
const hasZero = nums.includes(0);

const allMatrix = nums.filter(Boolean). reduce((acc, cur) => (acc * cur),1);

return nums.map((num) => {
if(hasZero){
if(num !== 0) return 0;

return allMatrix
}

return allMatrix / num
Copy link
Contributor

Choose a reason for hiding this comment

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

문제의 without using the division operation 조건이 위배되는 부분인 것 같습니다! prefix sum을 응용해서 풀어보시는 걸 추천드려요~!

});
};
48 changes: 48 additions & 0 deletions valid-anagram/hozzijeong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/

/**
* map을 통해 문자의 개수를 저장하고, 두 문자열의 문자 개수를 비교해서 다른 값이 있다면 false를 반환하도록 풀이를작성했습니다
*
*/
var isAnagram = function(s, t) {
if(s.length !== t.length) return false;

const length = s.length;

const stringMap = new Map();
const targetMap = new Map();

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

const stringChar = s[i];
const currentStringMap = stringMap.get(stringChar);

if(currentStringMap){
stringMap.set(stringChar, currentStringMap + 1)
}else{
stringMap.set(stringChar, 1)
}

const targetChar = t[i];
const currentTargetMap = targetMap.get(targetChar);

if(currentTargetMap){
targetMap.set(targetChar, currentTargetMap + 1)
}else{
targetMap.set(targetChar, 1)
}
}

for(const [char, count] of [...stringMap]){
const targetCount = targetMap.get(char);

if(!targetCount) return false;
if(targetCount !== count) return false;
}

return true
};