![[Array] Find the Student that Will Replace the Chalk](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FMYROI%2FbtsJqd9aaZd%2Ffn0MZW8wAcxtb8QtWOo2F1%2Fimg.png)
[Array] Find the Student that Will Replace the Chalk분명 전산학부 졸업 했는데 코딩 개못하는 조준호/알고리즘 초고수 조준호2024. 9. 3. 23:08
728x90
반응형
leetcode/240902_Find the Student that Will Replace the Chalk/explanation.md at main · Yellome-in-the-BOK/leetcode
Contribute to Yellome-in-the-BOK/leetcode development by creating an account on GitHub.
github.com
✨ 인사이트
처음에는 Binary Search를 생각했으나,, 그렇게 찾은 인덱스를 상대로 sum[:mid+1]를 구해야 하기 때문에 결과적으로는 O(nlogn)이 걸린다는 것을 깨달았다!
그냥 우직하게 Linear Search하면 된다..
👟 알고리즘 설명
1. k는 sum(chalk)로 나눈다.
2. 이후 그냥 한 땀 한 땀 빼다가 분필이 부족해지는 시점이 오면 return...💡
✅ 정답
Time Complexity: O(n)
Space Complexity: O(1)
class Solution:
def chalkReplacer(self, chalk: List[int], k: int) -> int:
k = k % sum(chalk)
for i, amount in enumerate(chalk):
if k < amount: return i
k -= amount
728x90
반응형
'분명 전산학부 졸업 했는데 코딩 개못하는 조준호 > 알고리즘 초고수 조준호' 카테고리의 다른 글
[Matrix] Spiral Matrix IV (0) | 2024.09.09 |
---|---|
[Hash Table, Linked List] Delete Nodes From Linked List Present in Array (1) | 2024.09.06 |
[Binary Search, Two Pointers] Find K-th Smallest Pair Distance (0) | 2024.08.17 |
[Backtracking] Combination Sum II (0) | 2024.08.17 |
[BFS, DFS] Regions Cut By Slashes (0) | 2024.08.17 |
@팜팜이S :: 팜팜은행: 한국은행 총재 조준호
한국은행 들어갈 때까지만 합니다
조만간 티비에서 봅시다