Skip to content

[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

Merged
merged 4 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions find-minimum-in-rotated-sorted-array/gmlwls96.kt
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
}
}
Comment on lines +10 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조금만 더 생각해 보시면 3개의 조건이 아닌 2가지 조건으로도 가능할 거에요 :)

}
return nums[0]
}
}
16 changes: 16 additions & 0 deletions linked-list-cycle/gmlwls96.kt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set을 사용하여 중복검사를 통해 문제 풀어주셨네요 :)
해당 문제는 LinkedList 의 특성을 살려서 푸는게 키포인트라 생각됩니다.
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
}
}
16 changes: 0 additions & 16 deletions longest-substring-without-repeating-characters/gmlwls96.kt

This file was deleted.

15 changes: 15 additions & 0 deletions maximum-product-subarray/gmlwls96.kt
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
}
}
32 changes: 32 additions & 0 deletions minimum-window-substring/gmlwls96.kt
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
}