디시인사이드 갤러리

마이너 갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

1095. Find in Mountain Array

흑화뉴비갤로그로 이동합니다. 2023.10.13 01:19:31
조회 37 추천 0 댓글 0
														

https://leetcode.com/problems/find-in-mountain-array/

 


# """

# This is MountainArray's API interface.

# You should not implement it, or speculate about its implementation

# """

#class MountainArray:

#    def get(self, index: int) -> int:

#    def length(self) -> int:


class Solution:

    def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int:

        l, r = 0, mountain_arr.length()

        while l + 2 < r:

            p, q = (l + r)//2, (l + r)//2 + 1

            fp, fq = mountain_arr.get(p), mountain_arr.get(q)

            if fp < fq:

                l = p

            else:

                r = q

        crit, val = l, mountain_arr.get(l)

        for i in range(l+1, min(r+1, mountain_arr.length())):

            fi = mountain_arr.get(i)

            if fi > val:

                crit, val = i, fi

        if val == target:

            return crit

        l, r = 0, crit

        if mountain_arr.get(l) == target:

            return l

        while l + 1 < r:

            m = (l + r)//2

            fm = mountain_arr.get(m)

            if fm >= target:

                r = m

            else:

                l = m

        if mountain_arr.get(r) == target:

            return r

        l, r = crit, mountain_arr.length()-1

        while l + 1 < r:

            m = (l + r)//2

            fm = mountain_arr.get(m)

            if fm > target:

                l = m

            else:

                r = m

        if mountain_arr.get(r) == target:

            return r

        return -1


너무 쓰레기 같이 풀었다 반성해야지

추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 2025년 가장 기억에 남는 인터넷 이슈는? 운영자 25/12/22 - -
275 57. Insert Interval 흑화뉴비갤로그로 이동합니다. 24.03.18 30 0
274 2485. Find the Pivot Integer 흑화뉴비갤로그로 이동합니다. 24.03.13 24 0
273 1750. Minimum Length of String After Del 흑화뉴비갤로그로 이동합니다. 24.03.06 26 0
272 451. Sort Characters By Frequency 흑화뉴비갤로그로 이동합니다. 24.02.07 40 0
271 49. Group Anagrams 흑화뉴비갤로그로 이동합니다. 24.02.06 34 0
270 931. Minimum Falling Path Sum 흑화뉴비갤로그로 이동합니다. 24.01.19 21 0
269 2125. Number of Laser Beams in a Bank 흑화뉴비갤로그로 이동합니다. 24.01.04 29 0
268 455. Assign Cookies 흑화뉴비갤로그로 이동합니다. 24.01.02 28 0
267 1155. Number of Dice Rolls With Target S 흑화뉴비갤로그로 이동합니다. 23.12.26 28 0
266 leetcode 풀기 vs 학교교재로 기초다지기 [1] ㅇㅇ(223.39) 23.11.25 86 0
265 1630. Arithmetic Subarrays 흑화뉴비갤로그로 이동합니다. 23.11.23 30 0
264 1877. Minimize Maximum Pair Sum in Array 흑화뉴비갤로그로 이동합니다. 23.11.19 27 0
263 1269. Number of Ways to Stay in the Same 흑화뉴비갤로그로 이동합니다. 23.10.16 37 0
1095. Find in Mountain Array 흑화뉴비갤로그로 이동합니다. 23.10.13 37 0
261 2749. Minimum Operations to Make the Int 개발뉴비갤로그로 이동합니다. 23.06.26 37 0
260 2448. Minimum Cost to Make Array Equal 개발뉴비갤로그로 이동합니다. 23.06.21 42 0
249 갤주님 구현문제 잘 푸는 방법 좀요 [7] ㅇㅇ(222.236) 23.04.08 135 1
248 오랜만에 오늘의 릿코드 한줄컷 개발뉴비갤로그로 이동합니다. 23.04.05 54 0
247 개강하니 귀신같이 정전갤됐네 ㅇㅇ(210.107) 23.03.31 44 0
246 1319. Number of Operations to Make Netwo 개발뉴비갤로그로 이동합니다. 23.03.23 42 0
245 958. Check Completeness of a Binary Tree 개발뉴비갤로그로 이동합니다. 23.03.15 44 0
244 129. Sum Root to Leaf Numbers 개발뉴비갤로그로 이동합니다. 23.03.14 32 0
242 101. Symmetric Tree 개발뉴비갤로그로 이동합니다. 23.03.13 38 0
241 382. Linked List Random Node 개발뉴비갤로그로 이동합니다. 23.03.11 27 0
240 142. Linked List Cycle II 개발뉴비갤로그로 이동합니다. 23.03.10 32 0
239 142. Linked List Cycle II chromate00갤로그로 이동합니다. 23.03.09 34 0
238 875. Koko Eating Bananas chromate00갤로그로 이동합니다. 23.03.08 28 0
237 875. Koko Eating Bananas 개발뉴비갤로그로 이동합니다. 23.03.08 42 0
236 2187. Minimum Time to Complete Trips 개발뉴비갤로그로 이동합니다. 23.03.08 36 0
235 2187. Minimum Time to Complete Trips chromate00갤로그로 이동합니다. 23.03.07 24 0
234 1539. Kth Missing Positive Number 개발뉴비갤로그로 이동합니다. 23.03.06 34 0
233 1539. Kth Missing Positive Number chromate00갤로그로 이동합니다. 23.03.06 23 0
232 1345. Jump Game IV 개발뉴비갤로그로 이동합니다. 23.03.05 35 0
231 2444. Count Subarrays With Fixed Bounds ㅇㅇ(93.177) 23.03.04 30 0
230 2444. Count Subarrays With Fixed Bounds ㅇㅇ(93.177) 23.03.04 26 0
229 28. Find the Index of the First Occurren chromate00갤로그로 이동합니다. 23.03.03 32 0
228 8. Find the Index of the First Occurrenc 개발뉴비갤로그로 이동합니다. 23.03.03 38 0
227 8. Find the Index of the First Occurrenc ㅇㅇ(46.165) 23.03.03 31 0
226 443. String Compression 개발뉴비갤로그로 이동합니다. 23.03.02 33 0
225 912. Sort an Array 개발뉴비갤로그로 이동합니다. 23.03.01 41 0
224 912. Sort an Array chromate00갤로그로 이동합니다. 23.03.01 32 0
223 임마들 개강했다고 문제 안풀고 빠져가지고... [2] ㅇㅇ(218.234) 23.02.28 85 0
222 427. Construct Quad Tree chromate00갤로그로 이동합니다. 23.02.27 24 0
221 427. Construct Quad Tree 개발뉴비갤로그로 이동합니다. 23.02.27 33 0
220 72. Edit Distance chromate00갤로그로 이동합니다. 23.02.27 27 0
219 72. Edit Distance 개발뉴비갤로그로 이동합니다. 23.02.27 43 0
217 121. Best Time to Buy and Sell Stock 개발뉴비갤로그로 이동합니다. 23.02.25 29 0
216 121. Best Time to Buy and Sell Stock 이벤트갤로그로 이동합니다. 23.02.25 28 0
215 121. Best Time to Buy and Sell Stock chromate00갤로그로 이동합니다. 23.02.25 30 0
214 1675. Minimize Deviation in Array 개발뉴비갤로그로 이동합니다. 23.02.25 28 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2