-
-
Notifications
You must be signed in to change notification settings - Fork 245
[YeomChaeeun] Week 1 #673
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
[YeomChaeeun] Week 1 #673
Changes from 9 commits
c17661e
9f58fee
dca5925
9523fc8
c60b5f5
ee72461
1b91949
c1e8ec3
a2e0835
671a3f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
function containsDuplicate(nums: number[]): boolean { | ||
|
||
// 접근 (1) - 시간 복잡도가 매우 커서 실패 | ||
// const uniqueArr = nums.filter((item, index) => { return nums.indexOf(item) === index }) | ||
// console.log(uniqueArr) | ||
// | ||
// return nums.length !== uniqueArr.length; | ||
|
||
// 접근 (2) - 양 옆의 값을 비교 ============= | ||
// if(nums.length === 1) | ||
// return false; | ||
// | ||
// // 정렬 | ||
// nums.sort() | ||
// | ||
// // 양 옆의 값을 비교 | ||
// for(let i = 0; i < nums.length; i++){ | ||
// console.log(nums[i], nums[i+1]) | ||
// if(nums[i] === nums[i+1]){ | ||
// return true; | ||
// } | ||
// } | ||
// return false; | ||
|
||
// 접근 (3) - obj를 이용 ================ | ||
let obj={} | ||
|
||
for(let i = 0; i < nums.length; i++) { | ||
console.log('nums >>', nums[i], 'obj >>', obj) | ||
if(obj[nums[i]]) { | ||
return true; | ||
} | ||
obj[nums[i]] = 1; | ||
} | ||
return false; | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* 하나 이상 건너 뛴 값을 더한 것중 가장 높은 값의 합을 구하기 | ||
* @param nums | ||
*/ | ||
function rob(nums: number[]): number { | ||
|
||
if(nums.length === 1) return nums[0]; | ||
|
||
// 0 + 2 ... | ||
// 1 + 3 ... | ||
// Max(인덱스 0부터 더한 것, 인덱스 1부터 더한 것) | ||
|
||
// 접근 (1) - 양 옆의 값을 제외하고 홀수/짝수 인덱스 값들을 더했음 | ||
// [2, 1, 1, 2] 일 때, 답이 4가 나와야 함 | ||
// let max = 0 | ||
// let odd = 0 | ||
// let even = 0 | ||
// for(let i = 0; i < nums.length; i++) { | ||
// if(i % 2 == 0) { | ||
// console.log(nums[i], 'even >>>', even); | ||
// even += nums[i]; | ||
// } else { | ||
// console.log(nums[i], 'odd >>>', odd); | ||
// odd += nums[i]; | ||
// } | ||
// } | ||
// console.log(even, '===', odd); | ||
// max = Math.max(even, odd); | ||
|
||
// 접근 (2) - max 값을 저장해두고 Math.max(하나 이상 건너뛴 값 + 현재 값 더한 것, max) 를 구함 | ||
let prev = 0 | ||
let max = 0 | ||
let temp = 0 | ||
for(let i = 0; i < nums.length; i++) { | ||
temp = max | ||
max = Math.max(prev + nums[i], max) // 이전의 값과 하나 이상 건너뛰고 더한 값 중 최대 값을 구함 | ||
prev = temp | ||
|
||
// console.log(temp, " - ", max, " - ", prev) | ||
}; | ||
// console.log(max); | ||
return max; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* 가장 긴 연속의 시퀀스 구하기 | ||
* @param nums | ||
*/ | ||
function longestConsecutive(nums: number[]): number { | ||
|
||
// length 가 0, 1 인 경우 | ||
if(nums.length < 2) | ||
return nums.length; // 0, 1 | ||
|
||
// 정렬 | ||
nums = nums.sort((a, b) => a - b) | ||
|
||
// 접근 (1) 처음 연속된 count만 리턴함 ============= | ||
// let count = 1 | ||
// for(let i = 0; i < nums.length-1; i++) { | ||
// if(nums[i] === nums[i+1]) { | ||
// continue; | ||
// } else if(nums[i] - nums[i+1] === 1) { | ||
// count++; | ||
// } else { | ||
// break; | ||
// } | ||
// }; | ||
// console.log(count); | ||
// return count; | ||
|
||
// ========= | ||
|
||
let longest = 0; | ||
let temp = 1; | ||
|
||
|
||
for(let i = 0; i < nums.length-1; i++) { | ||
if(nums[i] === nums[i + 1]) { | ||
// console.log(nums[i], '===', nums[i+1]) | ||
continue; | ||
} else if(nums[i] + 1 === nums[i + 1] ) { | ||
// console.log(nums[i], '+ 1 =', nums[i+1]) | ||
temp += 1; | ||
} else { | ||
// console.log(longest, ' - ', temp) | ||
longest = Math.max(temp, longest); | ||
temp = 1; | ||
} | ||
} | ||
|
||
// i가 마지막인 경우 for문의 else문을 타지 않으므로 다시 한번 체크함 | ||
|
||
longest = Math.max(temp, longest); | ||
return longest; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* | ||
* @param nums | ||
* @param k | ||
*/ | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
let obj = {} | ||
|
||
for(const num of nums) { | ||
if(!obj[num]) | ||
obj[num] = 0 | ||
++obj[num] | ||
} | ||
|
||
// 배열로 변경 | ||
let entries = Object.entries(obj) | ||
// 정렬 | ||
let sort_arr = entries.sort((a, b) => Number(b[1]) - Number(a[1])); | ||
|
||
let result = []; | ||
let l = 0; | ||
for(const item of sort_arr) { | ||
if(l == k) break; | ||
result.push(Number(item[0])); | ||
l++; | ||
} | ||
|
||
return result; | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* palindrom: 회문, 거꾸로 읽어도 제대로 읽는 것과 같은 문장 | ||
* @param s | ||
*/ | ||
function isPalindrome(s: string): boolean { | ||
let word = s.toLowerCase(); | ||
const reg = /[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>@\#$%&\\\=\(\'\" ]/g; | ||
|
||
|
||
word = word.replace(reg, ''); | ||
for(let i = 0; i < word.length; i++) { | ||
for(let j = word.length-i-1; j >= word.length-i-1; j--) { | ||
|
||
// console.log(word[i], '===', word[j]) | ||
if(word[i] !== word[j]) return false; | ||
} | ||
} | ||
return true; | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제출시에는 지워주시는 편이 좋을 것 같아요~🧹
아마 다른 부분은 해주신걸로 보아 이부분만 놓치신것 같네요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗 수정하겠습니다! 감사합니다!