본문 바로가기

leetcode8

Leetcode713.SubarrayProductLessThanK 소스코드 문제 713. SubarrayProductLessThanK 풀이 (brute-force) 시간복잡도 O(n 제곱) 아래 문제 풀 때, 실수는 end 를 원위치 시키는 것 연속적이고 부분배열을 구하는거면 Window 를 고려 /** * 10 count 1 * 10 * 5 -> count 2 * 10 * 5 * 2 멈춤 * 5 -> count 3 * 5 * 2 -> count 4 * 5 * 2 * 6 -> count 5 * 2 -> count 6 * 2 * 6 -> 7 * 6 -> 8 * * brute-force * 시간복잡도 O(n 제곱) */ fun numSubarrayProductLessThanKBruteForce(nums: IntArray, k: Int): Int { var .. 2024. 4. 20.
Leetcode61.RotateList (kotlin) 소스코드 문제 61. RotateList 풀이 (brute-force) 방향성은 single list -> 순환 list 를 만든 뒤, 특정 지점에서 순환을 끊는 것이다. 시간 복잡도 O(n) 상세 설명은 주석 fun rotateRight(head: ListNode?, k: Int): ListNode? { // null 과 0 을 체크 if (head == null || k == 0) { return head } var copy = head var size = 1 var tail: ListNode? // 크기 while (copy?.next != null) { copy = copy.next size++ } // tail 용도는 순환구조를 만들기 위함 tail = copy val rotate = k % s.. 2024. 4. 13.
Leetcode 41. First Missing Positive 소스코드 문제 [41. First Missing Positive] 풀이 (brute-force) 순회하면서 HashMap 에 데이터를 마킹한다. HashMap 을 처음부터 순회하면서 missing num 을 찾는다. 시간복잡도, 공간복잡도 O(n) fun firstMissingPositive(nums: IntArray): Int { val hashMap = HashMap() for (num in nums) { if (num > 0) { hashMap[num] = true } } var missingNum = 1 while (hashMap.containsKey(missingNum)) { missingNum++ } return missingNum } 풀이 ( 공간복잡도 O(1) ) 상세 풀이 주석 전체적인 .. 2024. 4. 9.
Leetcode1669.MergeInBetweenLinkedLists 소스코드 문제 1669.MergeInBetweenLinkedLists 풀이 (brute-force) 상세 풀이 주석 시간 복잡도 O(n) /** * 1. 문제 * - listNode1, 2 가 주어짐. * - listNode1 의 a,b 번째를 삭제한다. * - 그 사이에 listNode2 를 채운다. * * 제약 조건 * 3 2024. 3. 26.