-
-
Notifications
You must be signed in to change notification settings - Fork 195
[gmlwls96] week9 #987
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
[gmlwls96] week9 #987
Changes from all commits
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,24 @@ | ||
class Solution { | ||
// 시간 : O(logN) 공간 : O(1) | ||
// 이분탐색. | ||
fun findMin(nums: IntArray): Int { | ||
var left = 0 | ||
var right = nums.lastIndex | ||
|
||
while (left <= right){ | ||
val mid = (left + right)/2 | ||
when{ | ||
nums[mid-1] > nums[mid] -> { | ||
return nums[mid] | ||
} | ||
nums[0] < nums[mid] -> { | ||
left = mid + 1 | ||
} | ||
else -> { | ||
right = mid -1 | ||
} | ||
} | ||
} | ||
return nums[0] | ||
} | ||
} |
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. set을 사용하여 중복검사를 통해 문제 풀어주셨네요 :) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution { | ||
// 시간 : O(n) | ||
// 세트에 head.val 값을 추가하면서 동일한 값이 있는지 체크. 동일한 값이 존재하면 순회한다고 판단. | ||
fun hasCycle(head: ListNode?): Boolean { | ||
val set = mutableSetOf<Int>() | ||
var next = head | ||
while (next != null) { | ||
if (set.contains(next.`val`)) { | ||
return true | ||
} | ||
set.add(next.`val`) | ||
next = next.next | ||
} | ||
return false | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
fun maxProduct(nums: IntArray): Int { | ||
var max = 1 | ||
var min = 1 | ||
var result = nums[0] | ||
nums.forEach { num -> | ||
val v1 = min * num | ||
val v2 = max * num | ||
min = min(min(v1, v2), num) | ||
max = max(max(v1, v2), num) | ||
result = max(max, result) | ||
} | ||
return result | ||
} | ||
} |
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. 오.........사실 해당 문제는 binary search와 sliding window를 사용하여 푸는 문제인데요, 로직 구현에 상당히 공을 들이셨을것 같습니다. 고생하셨습니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Solution { | ||
fun minWindow(s: String, t: String): String { | ||
if (s.length < t.length) { | ||
return "" | ||
} | ||
val containIndexList = mutableListOf<Int>() | ||
for (i in s.indices) { | ||
if (t.contains(s[i])) { | ||
containIndexList.add(i) | ||
} | ||
} | ||
var answer = "" | ||
val regex = | ||
t.toCharArray().joinToString(separator = "", prefix = "^", postfix = ".+$") { """(?=.*${it})""" }.toRegex() | ||
for (i in 0..containIndexList.size - t.length) { | ||
val startIndex = containIndexList[i] | ||
for (l in t.length..containIndexList.size - i) { | ||
val endIndex = containIndexList[(i + l) - 1] + 1 | ||
val subStr = s.substring(startIndex, endIndex) | ||
if (regex.containsMatchIn(subStr)) { | ||
if (answer.isEmpty()) { | ||
answer = subStr | ||
} else if (subStr.length < answer.length) { | ||
answer = subStr | ||
} | ||
break | ||
} | ||
} | ||
} | ||
return answer | ||
} | ||
} |
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.
조금만 더 생각해 보시면 3개의 조건이 아닌 2가지 조건으로도 가능할 거에요 :)