-
Notifications
You must be signed in to change notification settings - Fork 126
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
[jdy8739] Week9 #980
[jdy8739] Week9 #980
Changes from 5 commits
f6e95c8
e682e7b
090ccc5
66c3e3a
284e10e
024f8d3
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,23 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var findMin = function (nums) { | ||
while (1 < nums.length) { | ||
|
||
const mid = Math.floor(nums.length / 2); | ||
|
||
const firstToHalf = [...nums].slice(0, mid); | ||
|
||
const midToEnd = [...nums].slice(mid, nums.length); | ||
|
||
const isInFront = Math.min(...firstToHalf) < Math.min(...midToEnd); | ||
|
||
nums = isInFront ? firstToHalf : midToEnd; | ||
} | ||
|
||
return nums[0]; | ||
}; | ||
|
||
// 시간복잡도 0(logn) -> 이진탐색을 통해 배열 nums를 반으로 쪼개가며 해답을 찾기때문에 | ||
// 공간복잡도 O(n) -> 처음 while문이 돌 때, nums를 쪼갠 두 배열 총 길이의 합은 nums의 처음 길이와 같기때문에 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 문제의 경우 LinkedList의 특징을 살려서 푸는것이 포인트입니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 거북이와 토끼 알고리즘에 대해 알아보고 풀이 커밋했습니다! :) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val) { | ||
* this.val = val; | ||
* this.next = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {ListNode} head | ||
* @return {boolean} | ||
*/ | ||
var hasCycle = function (head) { | ||
if (!head?.next) { | ||
return false; | ||
} | ||
|
||
let current = head; | ||
|
||
const set = new Set(); | ||
|
||
while (current.next) { | ||
if (set.has(current)) { | ||
return true; | ||
} | ||
|
||
set.add(current); | ||
current = current.next; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
// 시간복잡도 O(n) -> 최대 리스트의 노드 수 만큼 while문이 반복되므로 | ||
// 공간복잡도 O(n) -> set에 최대 리스트의 노드 수 만큼 size가 증가되므로 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxProduct = function (nums) { | ||
let answer = nums[0]; | ||
let max = 1; | ||
let min = 1; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
const current = nums[i]; | ||
|
||
const candidates = [max * current, min * current, current]; | ||
|
||
max = Math.max(...candidates); | ||
min = Math.min(...candidates); | ||
answer = Math.max(answer, max); | ||
} | ||
|
||
return answer; | ||
}; | ||
|
||
// 시간복잡도 O(n) * O(8) -> nums의 길이 만큼을 for 문으로 순환하면서 Math클래스의 max, min메소드를 호출(인자가 3개, 3개, 2개 이므로 총 8회 순회) | ||
// 공간복잡도 O(1) -> 파라미터 nums에 대해 의미있는 공간복잡도를 가지는 변수할당이 없음 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hard 난이도의 문제임에도 불구하고 두 가지 알고리즘을 적절히 사용하셔서 잘 풀이하신것 같습니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {string} | ||
*/ | ||
var minWindow = function (s, t) { | ||
let start = 0; | ||
let end = 0; | ||
|
||
/** t의 모든 문자열이 슬라이드 윈도우의 범위에 들어왔을 때, 그 범위의 최소 길이 */ | ||
let min = s.length; | ||
|
||
/** while 문이 한 번 순회할 때, start 또는 end값 중 end값이 증가했는지 여부 */ | ||
let isEndIndexUp = true; | ||
|
||
/** min 값이 갱신되면 s문자열의 start, end 인덱스값을 저장할 변수 */ | ||
let index = null; | ||
|
||
/** 문자열 t가 보유한 문자와 해당 문자가 들어있는 수를 key, value로 설정한 map객체 */ | ||
const charMap = t.split('').reduce((acc, cur) => { | ||
if (acc.has(cur)) { | ||
acc.set(cur, acc.get(cur) + 1); | ||
} else { | ||
acc.set(cur, 1); | ||
} | ||
|
||
return acc; | ||
}, new Map()); | ||
|
||
while (end < s.length) { | ||
const curChar = s[end]; | ||
|
||
if (isEndIndexUp && t.includes(curChar)) { | ||
// end가 증감되었고 s의 end 인덱스의 문자가 t에 존재한다면 해당 키의 값에 1을 감소 | ||
charMap.set(curChar, charMap.get(curChar) - 1); | ||
} | ||
|
||
/** 모든 t의 문자들이 슬라이드 윈도우의 범위에 들어왔는지 여부를 체크하는 불린값 */ | ||
const everyCharCollected = [...charMap].every(([_, value]) => value <= 0); | ||
|
||
if (everyCharCollected) { | ||
// 모든 문자열이 슬라이드 윈도우의 문자열에 포착된 경우 | ||
if (t.includes(s[start])) { | ||
// s의 start 인덱스 문자가 t에 존재한다면 charMap에 해당 키의 값에 1을 증감 | ||
charMap.set(s[start], (charMap.get(s[start]) || 0) + 1); | ||
} | ||
|
||
const gap = end - start; | ||
if (gap < min) { | ||
// t의 모든 문자가 슬라이드 윈도우의 범위에 있고, 현재 그 윈도우의 길이가 이전의 min값 보다 작다면 업데이트 | ||
min = gap; | ||
index = [start, end]; | ||
} | ||
|
||
start++; | ||
isEndIndexUp = false; | ||
} else { | ||
// 윈도우에 문자열이 하나라도 부족한 경우 | ||
end++; | ||
isEndIndexUp = true; | ||
} | ||
} | ||
|
||
return index ? s.slice(index[0], index[1] + 1) : ''; | ||
}; | ||
|
||
// 공간복잡도 O(t) -> 최대 t의 길이에 따라 맵의 크기가 결정되므로 | ||
// 시간복잡도 O(s * t) -> s를 순회하는 while 문에서 최대 O(t)의 공간복잡도를 따르는 map 객체가 배열로 변환되어 every 메소드가 호출되기 때문에 |
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.
binary search 가 메인 포인트지만, 해당 문제의 경우 앞, 뒤 구획을 나누면서 푸는게 요점이라고 생각합니다.
잘 풀어주신것 같아요!