Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- JavaScript
- 코딩테스트 준비
- 파이썬
- 프로그래머스
- 전략패턴 #StrategyPattern #디자인패턴
- Spring multimodule
- 항해99
- 구글 OAuth login
- 99클럽
- KPT회고
- Python
- 커스텀 헤더
- TiL
- 디자인패턴
- @FeignClient
- 디자인 패턴
- jwttoken
- 개발자 취업
- infcon 2024
- 단기개발자코스
- 개발자부트캠프추천
- 1주일회고
- DesignPattern
- 인프콘 2024
- spring batch 5.0
- 취업리부트코스
- 빈 충돌
- jwt
- 프로그래머스 이중우선순위큐
- 빈 조회 2개 이상
Archives
- Today
- Total
m1ndy5's coding blog
LeetCode 1337. The K Weakest Rows in a Matrix with Python 본문
알고리즘 with python/알고리즘 스터디
LeetCode 1337. The K Weakest Rows in a Matrix with Python
정민됴 2024. 1. 5. 10:44https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/
import heapq
class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
order = [row.count(1) for row in mat]
heap = order[:]
heapq.heapify(heap)
visited = [0 for _ in range(len(order))]
answer = []
for _ in range(k):
m = heapq.heappop(heap)
for i, v in enumerate(order):
if v == m and visited[i] == 0:
answer.append(i)
visited[i] = 1
break
return answer
1의 개수로 어떤 행이 제일 약한지 알아내야하는 문제였다.
만약 1의 개수가 같다면 행 번호가 더 작은 애가 약한 것이었기 때문에 반복문을 돌려서 해결하였다.
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
LeetCode 206.Reverse Linked List with Python (0) | 2024.01.06 |
---|---|
LeetCode 215. Kth Largest Element in an Array with Python (0) | 2024.01.05 |
LeetCode 1464. Maximum Product of Two Elements in an Array with Python (0) | 2024.01.05 |
LeetCode 347. Top K Frequent Elements with Python (0) | 2024.01.04 |
LeetCode 3. Longest Substring Without Repeating Characters with Python (1) | 2024.01.04 |