지난 공부기록/알고리즘 풀이

Python - Algorithm #13

hkl22 2024. 7. 1. 10:05

Python Algorithm

LeetCode

1636. Sort Array by Increasing Frequency

  • Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.
  • Return the sorted array.
  • Constraints
    • 1 nums.length 100
    • -100 nums[i] 100
from collections import Counter

class Solution:
    def frequencySort(self, nums: List[int]) -> List[int]:
        # sorted, counter
        c = Counter(nums)
        # 큰 숫자가 앞으로, 작은 숫자가 뒤로
        return sorted(nums, key=lambda x: (c[x], -x))
class Solution:
    def frequencySort(self, nums: List[int]) -> List[int]:
        answer = []
        count_num = Counter(nums)
        answer = sorted(nums, key=lambda x: (count_num[x], -x))
        return answer

2965. Find Missing and Repeated Values

  • You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. Each integer appears exactly once except a which appears twice and b which is missing. The task is to find the repeating and missing numbers a and b.
  • Return a 0-indexed integer array ans of size 2 where ans[0] equals to a and ans[1] equals to b.
  • Constraints
    • 2 n == grid.length == grid[i].length 50
    • 1 grid[i][j] n * n
    • For all x that 1 x n * n there is exactly one x that is not equal to any of the grid members.
    • For all x that 1 x n * n there is exactly one x that is equal to exactly two of the grid members.
    • For all x that 1 x n * n except two of them there is exatly one pair of i, j that 0 i, j n - 1 and grid[i][j] == x.
from collections import Counter

class Solution:
    def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
        # 2중 for문을 사용할 것
        c = Counter()
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                c[grid[i][j]] += 1
        answer = [-1, -1]
        # 1부터 n^2까지 반복을 진행하면서
        # 2번 등장한 값과 0번 등장한 값을 찾아서 answer에 지정하고 리턴
        n = len(grid)
        for i in range(1, n*n+1):
            if c[i] == 2:
                answer[0] = i
            if c[i] == 0:
                answer[1] = i
        return answer
class Solution:
    def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
        answer = [0, 0]
        l = []
        for i in range(len(grid)):
            for j in range(len(grid)):
                l.append(grid[i][j])
        for n in range(1, len(grid)**2+1):
            if l.count(n) == 2:
                answer[0] = n
            if l.count(n) == 0:
                answer[1] = n
        return answer

https://leetcode.com/problems/sort-array-by-increasing-frequency/

https://leetcode.com/problems/find-missing-and-repeated-values/

 

'지난 공부기록 > 알고리즘 풀이' 카테고리의 다른 글

Python - Algorithm #15  (0) 2024.07.03
Python - Algorithm #14  (0) 2024.07.02
Python - Algorithm #12  (0) 2024.06.28
Python - Algorithm #11  (0) 2024.06.27
SQL - Algorithm #10  (0) 2024.06.24