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 | 29 | 30 |
Tags
- 파이썬알고리즘풀기
- 릿코드 파이썬
- 파이썬알고리즘
- 파이썬 알고리즘
- 릿코드 풀기
- LeetCode
- binary search
- 잇츠디모
- python Leetcode
- 알고리즘풀이
- 코틀린기초
- leetcode풀이
- python 알고리즘
- 릿코드풀기
- python 릿코드
- python xor
- 릿코드풀이
- python priority queue
- leetcode 풀기
- 파이썬 릿코드
- 상가수익률계산기
- leetcode풀기
- 파이썬 프로그래머스
- 파이썬릿코드
- 알고리즘풀기
- python sorted
- python zip_longest
- 파이썬 알고리즘 풀기
- 파이썬릿코드풀기
- 릿코드
Archives
- Today
- Total
소프트웨어에 대한 모든 것
2148. Count Elements With Strictly Smaller and Greater Elements 본문
알고리즘/LeetCode
2148. Count Elements With Strictly Smaller and Greater Elements
앤테바 2022. 12. 27. 20:57반응형
제목
문제)
2148. Count Elements With Strictly Smaller and Greater Elements
Given an integer array nums, return the number of elements that have both a strictly smaller and a strictly greater element appear in nums.
Example 1:
Input: nums = [11,7,2,15]
Output: 2
Explanation: The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it.
Element 11 has element 7 strictly smaller than it and element 15 strictly greater than it.
In total there are 2 elements having both a strictly smaller and a strictly greater element appear in nums.
Example 2:
Input: nums = [-3,3,3,90]
Output: 2
Explanation: The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it.
Since there are two elements with the value 3, in total there are 2 elements having both a strictly smaller and a strictly greater element appear in nums.
Constraints:
- 1 <= nums.length <= 100
- -105 <= nums[i] <= 105
솔루션1)
- 오름차순 정렬 후 배열을 순회하면서 해당 element가 양 끝단 값 사이에 있는지 체크
class Solution:
def countElements(self, nums: List[int]) -> int:
if len(nums) < 3:
return 0
nums.sort()
count = 0
for i in range(1, len(nums)-1):
if nums[0] < nums[i] < nums[-1]:
count += 1
return count
솔루션2)
- 솔루션1을 정렬없이 코드 수정
class Solution:
def countElements(self, nums: List[int]) -> int:
min_val = min(nums)
max_val = max(nums)
return sum([1 for n in nums if min_val < n < max_val])
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
872. Leaf-Similar Trees (0) | 2022.12.27 |
---|---|
2149. Rearrange Array Elements by Sign (0) | 2022.12.27 |
2103. Rings and Rods (0) | 2022.12.27 |
1365. How Many Numbers Are Smaller Than the Current Number (0) | 2022.12.25 |
2389. Longest Subsequence With Limited Sum (0) | 2022.12.25 |
Comments