Skip to content

Commit f4eb476

Browse files
authored
Merge pull request #673 from YeomChaeeun/main
[YeomChaeeun] Week 1
2 parents 404d1f5 + 671a3f6 commit f4eb476

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

contains-duplicate/YeomChaeeun.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function containsDuplicate(nums: number[]): boolean {
2+
3+
// 접근 (1) - 시간 복잡도가 매우 커서 실패
4+
// const uniqueArr = nums.filter((item, index) => { return nums.indexOf(item) === index })
5+
// console.log(uniqueArr)
6+
//
7+
// return nums.length !== uniqueArr.length;
8+
9+
// 접근 (2) - 양 옆의 값을 비교 =============
10+
// if(nums.length === 1)
11+
// return false;
12+
//
13+
// // 정렬
14+
// nums.sort()
15+
//
16+
// // 양 옆의 값을 비교
17+
// for(let i = 0; i < nums.length; i++){
18+
// console.log(nums[i], nums[i+1])
19+
// if(nums[i] === nums[i+1]){
20+
// return true;
21+
// }
22+
// }
23+
// return false;
24+
25+
// 접근 (3) - obj를 이용 ================
26+
let obj={}
27+
28+
for(let i = 0; i < nums.length; i++) {
29+
if(obj[nums[i]]) {
30+
return true;
31+
}
32+
obj[nums[i]] = 1;
33+
}
34+
return false;
35+
36+
};

house-robber/YeomChaeeun.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 하나 이상 건너 뛴 값을 더한 것중 가장 높은 값의 합을 구하기
3+
* @param nums
4+
*/
5+
function rob(nums: number[]): number {
6+
7+
if(nums.length === 1) return nums[0];
8+
9+
// 0 + 2 ...
10+
// 1 + 3 ...
11+
// Max(인덱스 0부터 더한 것, 인덱스 1부터 더한 것)
12+
13+
// 접근 (1) - 양 옆의 값을 제외하고 홀수/짝수 인덱스 값들을 더했음
14+
// [2, 1, 1, 2] 일 때, 답이 4가 나와야 함
15+
// let max = 0
16+
// let odd = 0
17+
// let even = 0
18+
// for(let i = 0; i < nums.length; i++) {
19+
// if(i % 2 == 0) {
20+
// console.log(nums[i], 'even >>>', even);
21+
// even += nums[i];
22+
// } else {
23+
// console.log(nums[i], 'odd >>>', odd);
24+
// odd += nums[i];
25+
// }
26+
// }
27+
// console.log(even, '===', odd);
28+
// max = Math.max(even, odd);
29+
30+
// 접근 (2) - max 값을 저장해두고 Math.max(하나 이상 건너뛴 값 + 현재 값 더한 것, max) 를 구함
31+
let prev = 0
32+
let max = 0
33+
let temp = 0
34+
for(let i = 0; i < nums.length; i++) {
35+
temp = max
36+
max = Math.max(prev + nums[i], max) // 이전의 값과 하나 이상 건너뛰고 더한 값 중 최대 값을 구함
37+
prev = temp
38+
39+
// console.log(temp, " - ", max, " - ", prev)
40+
};
41+
// console.log(max);
42+
return max;
43+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 가장 긴 연속의 시퀀스 구하기
3+
* @param nums
4+
*/
5+
function longestConsecutive(nums: number[]): number {
6+
7+
// length 가 0, 1 인 경우
8+
if(nums.length < 2)
9+
return nums.length; // 0, 1
10+
11+
// 정렬
12+
nums = nums.sort((a, b) => a - b)
13+
14+
// 접근 (1) 처음 연속된 count만 리턴함 =============
15+
// let count = 1
16+
// for(let i = 0; i < nums.length-1; i++) {
17+
// if(nums[i] === nums[i+1]) {
18+
// continue;
19+
// } else if(nums[i] - nums[i+1] === 1) {
20+
// count++;
21+
// } else {
22+
// break;
23+
// }
24+
// };
25+
// console.log(count);
26+
// return count;
27+
28+
// =========
29+
30+
let longest = 0;
31+
let current = 1;
32+
for(let i = 0; i < nums.length-1; i++) {
33+
if(nums[i] === nums[i - 1]) {
34+
continue;
35+
} else if(nums[i] + 1 === nums[i + 1] ) {
36+
current += 1;
37+
} else {
38+
longest = Math.max(current, longest);
39+
current = 1;
40+
}
41+
}
42+
43+
return longest;
44+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
*
3+
* @param nums
4+
* @param k
5+
*/
6+
function topKFrequent(nums: number[], k: number): number[] {
7+
let obj = {}
8+
9+
for(const num of nums) {
10+
if(!obj[num])
11+
obj[num] = 0
12+
++obj[num]
13+
}
14+
15+
return Object.entries(obj)
16+
.sort((a, b) => Number(b[1]) - Number(a[1]))
17+
.slice(0, k)
18+
.map(entry => Number(entry[0]));
19+
};

valid-palindrome/YeomChaeeun.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* palindrom: 회문, 거꾸로 읽어도 제대로 읽는 것과 같은 문장
3+
* @param s
4+
*/
5+
function isPalindrome(s: string): boolean {
6+
let word = s.toLowerCase();
7+
const reg = /[^a-z0-9]/g; // removing all non-alphanumeric characters
8+
word = word.replace(reg, '');
9+
10+
for(let i = 0; i < word.length; i++) {
11+
if(word[i] !== word[word.length-i-1])
12+
return false;
13+
}
14+
return true;
15+
};

0 commit comments

Comments
 (0)