Skip to content

[김대의] 5주차 제출합니다. #42

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions week-05/3sum/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Intuition
특정 조건을 만족하는 수 찾기 -> 투 포인터로 접근

# Approach
1. 주어진 배열을 오름차순으로 정렬한다.
2. 반복문을 통해 세 수의 합이 0이 되는 값을 res에 저장한다.
2.1. 이때, 중복되는 숫자는 생략한다.
2.2. 세 수의 합이 0보다 크면 끝값을 줄인다.
2.3. 세 수의 합이 0보다 작으면 시작값을 증가시킨다.
2.4. 세 수의 합이 0이면 결괏값에 저장하고 시작값을 증가시켜 추가로 가능한 조합을 찾는다.
3. 시작값이 끝값보다 작을 경우헤만 반복한다.

# Complexity
- Time complexity: $$O(n^2)$$
- 중첩 반복문
- Space complexity: $$O(n)$$

# Code
```js
var threeSum = function(nums) {
let res = [];
nums.sort((a,b) => a - b);
for(let i = 0; i < nums.length;i++) {
if(i > 0 && nums[i] === nums[i-1]) continue;
let start = i + 1;
let end = nums.length - 1;

while(start < end) {
let sum = nums[i] + nums[start] + nums[end];

if(sum > 0) {
end--;
} else if(sum < 0) {
start++;
} else {
res.push([nums[i], nums[start], nums[end]]);
start++;

while(start < end && nums[start] === nums[start -1]) start++;
}
}
}

return res;
};
```

46 changes: 46 additions & 0 deletions week-05/encode-and-decode-strings/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Intuition
빈 문자열일 때 결괏값이 다른 것을 확인하여 '문자열 길이'+'특수문자'의 형태로 변환한다는 풀이를 참고하였다.

# Approach
1. '#'을 구분자로 활용함과 동시에 문자열 길이를 인코딩 결과에 기록해 놓는다.
2. '#'과 문자열 길이를 제외한 문자열을 디코딩한다.

# Code
```js
class Solution {
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
let encoded = '';
for(let s of strs) {
encoded += s.length + '#' + s;
}

return encoded;
}

/**
* @param {string} str
* @returns {string[]}
*/
decode(str) {
let decoded = [];
let i = 0;
while(i < str.length) {
let j = i;
while(str[j] !== '#') {
j++;
}
let length = parseInt(str.slice(i,j), 10);
i = j + 1;
decoded.push(str.slice(i,i+length));

i+=length;
}
return decoded;
}
}

```
38 changes: 38 additions & 0 deletions week-05/longest-consecutive-sequence/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Intuition
배열을 오름차순으로 정렬한 후 현재의 값이 이전값의 차가 1이면 연속된 수열을 증가한다.

# Approach

1. 주어진 배열을 오름차순으로 정렬한다.
2. 빈 배열일 경우 0으로 반환한다.
3. 현재의 숫자가 이전 숫자보다 1 크면 연속된 수열의 길이를 증가한다.
4. 현재의 숫자가 이전의 숫자보다 1 크기 않고, 값이 일치하지 않으면(=연속된 수열이 끊긴 경우) 연속된 수열의 길이를 결괏값에 갱신한다.
5. 반복문이 끝난 후에 저장된 값이 연속된 최대 수열의 길이일 수 있으므로 결괏값에 다시 갱신한다.

# Complexity
- Time complexity: $$O(nlogn)$$
- 정렬로 인한 시간복잡도
- Space complexity: $$O(n)$$

# Code
```js
var longestConsecutive = function(nums) {
nums.sort((a,b)=>a-b);
let cnt = 1;
let res = 1;
if(nums.length === 0) return 0;
for(let i = 1; i < nums.length;i++) {
if(nums[i] - nums[i-1] === 1) {
cnt++;
}else if(nums[i] !== nums[i - 1]) {
res = Math.max(res, cnt);
cnt = 1;
}
}

res = Math.max(res, cnt);

return res;
};
```

37 changes: 37 additions & 0 deletions week-05/product-of-array-except-self/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Intuition
현재 위치의 왼쪽값과 오른쪽값의 곲들을 결괏값에 저장한다.

# Approach
1. 결과값의 초기 배열을 1로 설정한다.
2. 반복문을 순회하면서 현재 위치의 왼쪽에 있는 모든 값을 결과값에 저장하면서 왼쪽 곲 계산을 진행한다.
3. 오른쪽 곱 계산도 2번과 같이 동일하게 진행한다.


# Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(1)$$
- 오른쪽, 왼쪽 값을 저장하는 공간

# Code
```js
var productExceptSelf = function(nums) {
let res = Array.from(nums).fill(1);
let left = 1;
let right = 1;

// 왼쪽 곱
for(let i = 0; i < nums.length;i++) {
res[i] *= left;
left *=nums[i];
}

// 오른쪽 곱
for(let i = nums.length - 1; i >= 0; i--) {
res[i] *= right;
right *= nums[i];
}
return res;

};
```

31 changes: 31 additions & 0 deletions week-05/top-k-frequent-elements/kimeodml.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Intuition
빈도수를 계산하여 정렬한 후, k까지 추출한다.

# Approach
1. Map을 활용해 각 요소의 빈도수를 계산한다.
2. 빈도수를 내림차순으로 정렬한다.
3. k값까지의 키값을 추출한다.


# Complexity
- Time complexity: $$O(nlogn)$$
- 정렬로 인한 시간 복잡도 nlogn
- Space complexity: $$O(n)$$
- 문자열 nums의 길이 n

# Code
```js
var topKFrequent = function(nums, k) {
let map = new Map();
nums.forEach((num)=> {
if(!map.has(num)) {
map.set(num,[]);
}
map.get(num).push(num);
});

let sorted = Array.from(map.entries()).sort((a,b)=> b[1].length - a[1].length);
return sorted.map((num) => num[0]).slice(0,k);
};
```