-
-
Notifications
You must be signed in to change notification settings - Fork 247
[호돌이] Week 1 #682
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
[호돌이] Week 1 #682
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
345e956
two sum solution
yeeZinu 833cfcf
contains duplicate solution
yeeZinu 7816391
cl 개행추가
yeeZinu e88c1bb
Merge branch 'DaleStudy:main' into main
yeeZinu a5e4185
valid parentheses solve
yeeZinu 59b14a8
top k frequent elements solution
yeeZinu 99aabb5
Merge branch 'DaleStudy:main' into main
yeeZinu 54cbbb5
longest consecutive sequence solution
yeeZinu 243f35d
house robber solution
yeeZinu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
|
||
nums 배열내의 아무 정수나 2개 이상 중복되면 true를 반복하는 함수. | ||
|
||
시간 복잡도: O(n) | ||
*/ | ||
var containsDuplicate = function (nums) { | ||
return nums.length !== new Set(nums).size | ||
} | ||
|
||
console.log(containsDuplicate([1, 2, 3, 1])); // true | ||
// console.log(containsDuplicate([1, 2, 3, 4])); // false | ||
// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var rob = function (nums) { | ||
const n = nums.length; | ||
|
||
// 길이가 1이라면 | ||
if (n === 1) { | ||
return nums[0]; | ||
} | ||
|
||
// nums의 길이만큼 0으로 초기화된 배열 | ||
const dp = Array(n).fill(0); | ||
|
||
// 0번은 nums[0] | ||
dp[0] = nums[0]; | ||
// 1번은 0과 1중 큰것을 선택 | ||
dp[1] = Math.max(nums[0], nums[1]); | ||
|
||
// i-1 과 i + i-2 의 합중 더 큰것을 선택하면됨 | ||
for (let i = 2; i < n; i++) { | ||
dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]); | ||
} | ||
// i가 n - 1까지 반복함 | ||
return dp[n - 1]; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function (nums) { | ||
// set으로 중복된 element 제거 | ||
const numSet = new Set(nums); | ||
// 최대 연속 길이 변수 | ||
let maxLength = 0; | ||
|
||
// 최대 연속 길이 찾기 | ||
for (let num of numSet) { | ||
// 만약 현재 수 -1이 없다면? | ||
if (!numSet.has(num - 1)) { | ||
let currentNum = num; //현재값이 시작 | ||
let currentLength = 1; //최대 길이 1로 초기화 | ||
|
||
// 현재값 +1이 있을 때 까지 반복 | ||
while (numSet.has(currentNum + 1)) { | ||
currentNum++; | ||
currentLength++; | ||
} | ||
|
||
// 최대길이 값과 현재 길이값중 더 높은것이 최대길이값 | ||
maxLength = Math.max(maxLength, currentLength); | ||
} | ||
} | ||
return maxLength; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
* | ||
* nums 배열 내 최빈도 숫자 k개 출력 | ||
* | ||
*/ | ||
var topKFrequent = function (nums, k) { | ||
// 빈도 체크할 객체 | ||
let frequent = {}; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
// 숫자 중복될때마다 +1 | ||
if (frequent[nums[i]] > 0) { | ||
frequent[nums[i]]++; | ||
} | ||
// 없으면 1로 초기화 | ||
else { | ||
frequent[nums[i]] = 1; | ||
} | ||
} | ||
|
||
// 정렬을 위해 entries를 사용해 배열로 변환 | ||
const frequentEntries = Object.entries(frequent); | ||
frequentEntries.sort((a, b) => b[1] - a[1]); // 내림차순 정렬 | ||
|
||
// k갯수만큼 배열 자르기 및 배열 내부 값 number로 변경 | ||
const topK = frequentEntries.slice(0, k).map((i) => Number(i[0])); | ||
return topK; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
|
||
/* | ||
두 수 더하기 | ||
nums: 숫자 배열 | ||
target: 목표 숫자 | ||
|
||
target 숫자에 맞게 nums 배열중 2개를 더함. | ||
return은 더한 두 수의 index | ||
- for i in nums -> target - i 값이 배열내에 있는지 찾기 | ||
*/ | ||
var twoSum = function (nums, target) { | ||
let indexArray = []; // 답을 저장할 배열 | ||
for (let i = 0; i < nums.length; i++) { | ||
let tNum = nums.lastIndexOf(target - nums[i]); // 배열내에 target - nums[i] 찾기 | ||
if (i === tNum) { // 같은 수 사용 방지 | ||
|
||
} | ||
else if (tNum > 0 && indexArray.lastIndexOf(i) === -1) { // 배열내에 원하는 값이 있다면 | ||
indexArray.push(i); | ||
indexArray.push(tNum); | ||
break; | ||
} | ||
} | ||
return indexArray; | ||
}; |
yeeZinu marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
|
||
괄호 유효성 검사( | ||
어느 괄호로 열어도 상관은 없지만 열었으면 닫아야지 true 안닫으면 false | ||
({)} -> 이런식으로 중간에 섞여서 닫혀있지 않다는 전제의 문제 | ||
*/ | ||
var isValid = function (s) { | ||
if (s.length % 2 === 1) { | ||
return false | ||
} | ||
|
||
// 괄호들이 들어갈 스택배열 | ||
const stack = []; | ||
|
||
// 괄호 짝 객체 | ||
const pair = { | ||
"(": ")", | ||
"{": "}", | ||
"[": "]", | ||
} | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
// 열린괄호면 스택추가 | ||
if (s[i] in pair) { | ||
stack.push(s[i]); | ||
} | ||
// 닫힌 괄호라면? | ||
else { | ||
// 스택배열의 마지막이 같은 종류의 괄호라면 제거 | ||
if (pair[stack.at(-1)] === s[i]) { | ||
stack.pop(); | ||
} | ||
// 아니면 false | ||
else { | ||
return false | ||
} | ||
} | ||
} | ||
// 스택배열이 비었으면 true | ||
return stack.length === 0; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
저는 for문을 통해 배열을 돌면서 겹치는 게 있는지 검사했는데 size로 검사하는 걸 생각 못했네요, 배워갑니다! 🔥