Problem:
 Score : 700 points    Problem Statement Takahashi is hosting an sports meet.
There are N people who will participate. These people are conveniently numbered 1 through N .
Also, there are M options of sports for this event. These sports are numbered 1 through M .
Among these options, Takahashi will select one or more sports (possibly all) to be played in the event.  Takahashi knows that Person i 's j -th favorite sport is Sport A_{ij} .
Each person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.  Takahashi is worried that one of the sports will attract too many people.
Therefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized.
Find the minimum possible number of the participants in the sport with the largest number of participants.      Constraints  1 \leq N \leq 300  1 \leq M \leq 300  A_{i1} , A_{i2} , ... , A_{iM} is a permutation of the integers from 1 to M .         Input Input is given from Standard Input in the following format:  N  M  A_{11}  A_{12}  ...  A_{1M}  A_{21}  A_{22}  ...  A_{2M}  :  A_{N1}  A_{N2}  ...  A_{NM}       Output Print the minimum possible number of the participants in the sport with the largest number of participants.        Sample Input 1 4 5
5 1 3 4 2
2 5 3 1 4
2 3 1 4 5
2 5 4 3 1      Sample Output 1 2  Assume that Sports 1 , 3 and 4 are selected to be played. In this case, Person 1 will participate in Sport 1 , Person 2 in Sport 3 , Person 3 in Sport 3 and Person 4 in Sport 4 .
Here, the sport with the largest number of participants is Sport 3 , with two participants.
There is no way to reduce the number of participants in the sport with the largest number of participants to 1 . Therefore, the answer is 2 .       Sample Input 2 3 3
2 1 3
2 1 3
2 1 3      Sample Output 2 3  Since all the people have the same taste in sports, there will be a sport with three participants, no matter what sports are selected.
Therefore, the answer is 3 .

Solution (Python):

from collections import Counter
N, M = map(int, input().split())
data = [list(map(int, input().split())) for i in range(N)]
ans = N
for i in range(M):
    K = []
    for j in range(N):
        K.append(data[j][0])
    index, weight = Counter(K).most_common()[0]
    ans = min(ans, weight)
    for k in range(N):
        data[k].remove(index)
print(ans)

Solution (Python):
from collections import defaultdict
N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]

hold = defaultdict(list)
like = [0] * N
sport = 0
NG = [False] * (M + 1)
ans = []

while all(like[i] < M for i in range(N)):
    hold = defaultdict(list)
    for i in range(N):
        hold[A[i][like[i]]].append(i)

    res = -1
    for k, v in hold.items():
        if len(v) > res:
            res = len(v)
            sport = k
    NG[sport] = True
    ans.append(res)

    for i in range(N):
        while like[i] < M:
            if NG[A[i][like[i]]]:
                like[i] += 1
            else:
                break

print(min(ans))

Solution (Python):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]

s = set()
indices = [0] * N
nums = dict(zip(range(1, M+1), [[] for i in range(M)]))
nn, mm = 0, 0
for i, idx in enumerate(indices):
  m = A[i][idx]
  nums[m].append(i)
  if len(nums[m]) > nn:
    nn = len(nums[m])
    mm = m

ans = nn
for _ in range(M-1):
  s.add(mm)
  for i in nums[mm]:
    idx = indices[i] + 1
    m = A[i][idx]
    while m in s:
      idx += 1
      m = A[i][idx]
    indices[i] = idx
    nums[m].append(i)
  nums.pop(mm)
  nn = 0
  for k in nums:
    if len(nums[k]) > nn:
      nn = len(nums[k])
      mm = k
  ans = min(ans, nn)

print(ans)

Solution (Python):
n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
ans = 3000
t = [True] * m
for _ in range(m):
    p = [0] * m
    for i in range(n):
        for x in a[i]:
            if t[x - 1]:
                p[x - 1] += 1
                break
    ma = max(p)
    ans = min(ans, ma)
    t[p.index(ma)] = False
print(ans)

Solution (Python):
from collections import Counter
from operator import itemgetter
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
As = [list(map(int, input().split())) for _ in range(N)]
indices = [0 for i in range(N)] 


min_turnout = N
canceled_sports = set()
for _ in range(M-1):
    popular_sport, turnout = max(Counter(As[i][indices[i]] for i in range(N)).items(), key = itemgetter(1))
    min_turnout = min(min_turnout, turnout)
    canceled_sports.add(popular_sport)
    for i in range(N):
        while As[i][indices[i]] in canceled_sports:
            indices[i] += 1

print(min_turnout)

Solution (Python):
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

mod = 998244353 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def f(A, S):
    T = [0] * len(A[0])
    for a in A:
        for k in a:
            if k in S:
                continue
            T[k] += 1
            break
    
    v = max(T)
    return (T.index(v), v)


def main():
    N, M = read_values()
    A = [list(read_index()) for _ in range(N)]

    K = [0] * N

    res = V(min, N)
    S = set()
    for _ in range(M - 1):
        k, v = f(A, S)
        S.add(k)
        res.ud(v)

    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

mod = 998244353 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def f(A, S):
    T = [0] * len(A[0])
    for a in A:
        for k in a:
            if k in S:
                continue
            T[k] += 1
            break
    
    v = max(T)
    return (T.index(v), v)


def main():
    N, M = read_values()
    A = [list(read_index()) for _ in range(N)]

    res = V(min, N)
    S = set()
    for _ in range(M - 1):
        k, v = f(A, S)
        S.add(k)
        res.ud(v)

    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque, Counter
n, m = map(int, input().split())

cnt = Counter()
ques = []
for _ in range(n):
    a = list(map(int, input().split()))
    cnt[a[0]] += 1
    ques.append(deque(a))

ans = n
used = [False for _ in range(m+1)]
while True:
    cnt_most = cnt.most_common()
    if cnt_most[0][1] == 0:
        break
    ans = min(ans, cnt_most[0][1])
    for x, t in cnt_most:
        if t >= ans:
            used[x] = True
        else:
            break
    for i in range(n):
        while len(ques[i]) > 0 and used[ques[i][0]]:
            x = ques[i].popleft()
            cnt[x] -= 1
            if len(ques[i]) > 0:
                cnt[ques[i][0]] += 1

print(ans)

Solution (Python):
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))


def main():
	N, M = getlist()
	D = defaultdict(lambda:1)
	A = []
	for i in range(N):
		a = list(reversed(getlist()))
		A.append(a)

	if M == 1:
		print(N)
		return

	ans = INF
	for i in range(M - 1):
		cnt = [0] * M
		for j in range(N):
			cnt[A[j][-1] - 1] += 1

		

		var = max(cnt)
		ans = min(ans, var)
		ind = cnt.index(var)
		D[ind] = 0
		for j in range(N):
			while True:
				if D[A[j][-1] - 1] == 0:
					A[j].pop()
				else:
					break

	print(ans)

if __name__ == '__main__':
	main()

Solution (Python):
import sys
input = sys.stdin.readline

def pop_ind(ind, n):
    for i in range(n):
        A[i].remove(ind)

def count(m):
    counter = [0] * (m+1)
    ind = -1
    max_cnt = 0
    for i in range(N):
        counter[A[i][0]] += 1
        if counter[A[i][0]] > max_cnt:
            max_cnt = counter[A[i][0]]
            ind = A[i][0]
    return ind, max_cnt

N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
ans = float('inf')
for _ in range(M):
    ind, tmp = count(M)
    pop_ind(ind, N)
    ans = min(ans, tmp)
print(ans)

Solution (Python):
N,M=map(int,input().split())

A=[]
for _ in range(N):
    A.append(list(map(int,input().split())))

now=[0]*N
is_hold=[1]*M

ans=10**9
for _ in range(M):
    sanka=[0]*M
    for i in range(N):
        
        sanka[A[i][now[i]]-1]+=1
    
    temp=max(sanka)
    ans=min(ans,temp)
    
    for i in range(len(sanka)):
        if sanka[i]==temp:
            tgt=i
            break
    is_hold[tgt]=0
    
    
    if _ == M-1:
        break
    for i in range(N):
        
        while is_hold[A[i][now[i]]-1]==0:
            
            now[i]+=1
        
print(ans)

Solution (Python):

"""
Created on Wed Apr 29 22:05:28 2020
"""

import sys


sys.setrecursionlimit(10 ** 9)


mod = 10**9+7


N, M = map(int,input().split())
A = [list(map(int,input().split())) for i in range(N)]



p = [0 for _ in range(N)]
un_held = set([])
ans = M + 10**3

while len(un_held) < M:
    s = [0 for _ in range(M+1)]
    for i in range(N):
        s[A[i][p[i]]] += 1
    tmp = 0
    for j in range(1,M+1):
        if s[j] > tmp:
            tmp = s[j]
            un = j
    ans = min(tmp,ans)
    un_held.add(un)
    for i in range(N):
        while p[i] < M and A[i][p[i]] in un_held:
            p[i] += 1
    




print(ans)

Solution (Python):
N,M=map(int,input().split())
A=[[] for i in range(N)]
for i in range(0,N):
    A[i]=list(map(int,input().split()))

pointer={i:0 for i in range(0,N)}
cancel=set([])
ans=N
while True:
    dic={i:0 for i in range(1,M+1)}
    for i in range(0,N):
        dic[A[i][pointer[i]]]+=1
    most=max(dic[i] for i in range(1,M+1))
    ans=min(ans,most)
    for i in range(1,M+1):
        if dic[i]==most:
            cancel.add(i)

    for i in range(0,N):
        while A[i][pointer[i]] in cancel:
            pointer[i]+=1
            if pointer[i]==M:
                print(ans)
                exit()

Solution (Python):
from collections import defaultdict
N, M  = map(int, input().split())
li1 = []
li2 = []
for i in range(N):
    A = list(map(int, input().split()))
    li1.append(A)
U = 300
L = 0
while U-L > 1:
    m = (U+L)//2
    li = [0] * M
    flag = False
    while not flag:
        d = defaultdict(int)
        if not 0 in li:
            break
        for i in range(N):
            for j in range(M):
                if li[li1[i][j]-1] == 0:
                    d[li1[i][j]] += 1 
                    break
        f = True
        for k in d:
            if d[k] > m:
                li[k-1] = 1
                f = False
        if f:
            flag = True
    if flag:
        U = m
    else:
        L = m
print(U)

Solution (Python):
from collections import deque, Counter
n,m = map(int,input().split())
ls = [deque(map(int,input().split())) for i in range(n)]
seen = [0 for i in range(m+1)]
content = [0 for i in range(n)]
flg = [0 for i in range(n)]
cnt = 0
ans = 500
for i in range(n):
  content[i] = ls[i][0]
c = Counter(content)
while cnt<m:
  for i in range(n):
    if flg[i]:
      while ls[i]:
        ls[i].popleft()
        if seen[ls[i][0]] >= 1:
          continue
        else:
          content[i] = ls[i][0]
          break
      flg[i] = 0
  c = Counter(content)
  x = c.most_common()[0]
  ans = min(ans,x[1])
  seen[x[0]] += 1
  for i in range(n):
    if content[i] == x[0]:
      flg[i] = 1
  cnt += 1
print(ans)

Solution (Python):
N, M = map(int, input().split())
A = [list(reversed(list(map(int, input().split())))) for _ in range(N)]

res = N
removed = [0 for _ in range(M)]

while not all(removed):
    S = [0 for _ in range(M)]
    for a in A:
        S[a[-1] - 1] += 1
    res = min(res, max(S))
    x = S.index(max(S))
    removed[x] = 1
    for a in A:
        while a and removed[a[-1] - 1]:
            a.pop()

print(res)

Solution (Python):
N, M = map(int, input().split())
A = [list(reversed(list(map(int, input().split())))) for _ in range(N)]

res = N
removed = [0 for _ in range(M)]

while not all(removed):
    S = [0 for _ in range(M)]
    for a in A:
        S[a[-1] - 1] += 1
    res = min(res, max(S))
    x = S.index(max(S))
    removed[x] = 1
    for a in A:
        while a and removed[a[-1] - 1]:
            a.pop()

print(res)

Solution (Python):
n,m = map(int,input().split())
a = []
for i in range(n):
    a0 = list(map(int,input().split()))
    a.append(a0)
l = [0 for i in range(m)]
d = []
kl = [0 for i in range(n)]
for i in range(n):
    l[a[i][0] - 1] += 1
for i in range(m):
    d.append([l[i],i])
d.sort(reverse=True)
ans = d[0][0]
for i in range(m-1):
    l[d[0][1]] = -1
    for i in range(n):
        
        if a[i][kl[i]]-1 == d[0][1]:
            while l[a[i][kl[i]]-1] < 0:
                kl[i] += 1
            l[a[i][kl[i]]-1] += 1
        
    d = []
    for i in range(m):
        if l[i] >= 0:
            d.append([l[i],i])
    d.sort(reverse=True)
    ans = min(ans,d[0][0])
print(ans)

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
lb, ub = 0, N
while ub - lb > 1:
    m = (lb + ub) // 2
    idx = [0] * N
    not_use = set()
    ok = True
    while True:
        count = [0] * (M+1)
        for i in range(N):
            if idx[i] == -1:
                continue
            while idx[i] < M and A[i][idx[i]] in not_use:
                idx[i] += 1
            if idx[i] == M:
                ok = False
                break
            count[A[i][idx[i]]] += 1

        if not ok:
            break
        if max(count) <= m:
            break
        for i in range(1, M+1):
            if count[i] > m:
                not_use.add(i)
    if ok:
        ub = m
    else:
        lb = m
print(ub)

Solution (Python):
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(n)]
check=[1]*m
ans=n
for i in range(m):
  cnt=[0]*m
  for j in range(n):
    for k in range(m):
      if check[a[j][k]-1]:
        cnt[a[j][k]-1]+=1
        break
  if max(cnt)<ans:
    ans=max(cnt)
  check[cnt.index(max(cnt))]=0
print(ans)

Solution (Python):

from collections import deque, defaultdict

N,M = map(int, input().split())
A = [[int(i) - 1 for i in input().split()] for _ in range(N)]

def check(X):
    """
    最多参加のスポーツの参加人数がX以下になるか
    """
    
    que = [deque() for _ in range(N)]
    
    ng = [False] * M
    for i in range(N):
        for j in range(M):
            que[i].append(A[i][j])
    
    while True:
        
        cnt = defaultdict(int)
        for i in range(N):
            cnt[que[i][0]] += 1

        
        flag = False
        for s, c in cnt.items():
            if X < c:
                flag = True
                ng[s] = True

        
        if not flag:
            return True
        
        for i in range(N):
            
            while que[i] and ng[que[i][0]]:
                que[i].popleft()
            
            if not que[i]:
                return False

def main():
    ng, ok = 0, N
    while ok - ng > 1:
        mid = (ng + ok) // 2
        if check(mid):
            ok = mid
        else:
            ng = mid
    print(ok)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, M, *A = map(int, read().split())
A = list(map(list, map(reversed, zip(*[iter(A)] * M))))


p = [0] * (M + 1)
canceled = set()
answer = []


def solve(s, n):
    
    
    if n == 1:
        answer.append(N)
    elif n == M:
        for i in A:
            p[i[-1]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1)
    else:
        canceled.add(s)
        p[s] = 0
        for i in range(N):
            if A[i][-1] in canceled:
                while A[i][-1] in canceled:
                    A[i].pop()
                p[A[i][-1]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1)


solve(0, M)
print(min(answer))

Solution (Python):
import sys
input = sys.stdin.buffer.readline

def main():
    N,M = map(int,input().split())
    a = [list(map(int,input().split())) for _ in range(N)]
    
    def p(s):
        if all(s) == True:
            return 300
        al = [0 for _ in range(M)]
        for i in range(N):
            for j in a[i]:
                if not s[j-1]:
                    al[j-1] += 1
                    break
        count,index = max(al),al.index(max(al))
        s[index] = True
        return min(count,p(s))
    
    s = [False for _ in range(M)]
    print(p(s))

if __name__ == "__main__":
    main()

Solution (Python):
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
check=[1 for i in range(m)]
ans=n

for i in range(m):
  count=[0]*m
  for j in range(n):
    for k in range(m):
      if check[l[j][k]-1]==1:
        count[l[j][k]-1]+=1
        break
  if max(count)<ans:
    ans=max(count)
  check[count.index(max(count))]=0
print(ans)

Solution (Python):
N, M, *X = map(int, open(0).read().split())
A = []
for a in zip(*[iter(X)]*M):
  A.append(a)
log = set(range(1,M+1))
ans = []
while log:
  dic = {}
  for i in range(N):
    for j in range(M):
      if A[i][j] in log:
        dic[A[i][j]] = dic.get(A[i][j],0)+1
        break
  ans.append(max(dic.values()))
  c = max(dic.keys(),key=lambda x:dic[x])
  log.remove(c)
print(min(ans))

Solution (Python):
n, m = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]
for i in range(n):
    for j in range(m):
        A[i][j] -= 1
S = [0] * m
for i in range(n):
    S[A[i][0]] += 1

l = 0
r = n+1
mi = (l+r) // 2
while r-l>1:
    T = S[:]
    ng = 0
    I = [0] * n
    Y = [0] * m
    while ng == 0:
        M = [0] * m
        for i in range(m):
            if T[i] > mi:
                M[i] = 1
                Y[i] = 1
        
        if sum(M) == 0:
            break
        for i in range(n):
            if M[A[i][I[i]]] == 1:
                T[A[i][I[i]]] -= 1
                while I[i] < m and Y[A[i][I[i]]] == 1:
                    I[i] += 1
                if I[i] == m:
                    ng = 1
                    break
                T[A[i][I[i]]] += 1
    if ng == 1:
        l = mi
    else:
        r = mi
    mi = (l+r) // 2
print(r)

Solution (Python):
N,M = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(N)]

L = [0]*(M+1) 
I = [0]*N 
ans = N
flag = True
while flag:
    cnt = [0]*(M+1)
    for i in range(N):
        while I[i] < N and L[A[i][I[i]]] == 1:
            I[i] += 1
        if I[i] == N:
            flag = False
            break
        cnt[A[i][I[i]]] += 1
    if flag:
        num = max(cnt)
        ans = min(ans, num)
        for i in range(1, M+1):
            if cnt[i] == num:
                L[i] = 1
    if sum(L) == M:
        flag = False

print(ans)

Solution (Python):

import sys
input = sys.stdin.readline
n,m = map(int, input().split( ))
a = [list(map(int, input().split( ))) for _ in range(n)]

M = {i for i in range(1,m+1)}
cnt = [0]*(m+1)
cursor = [0]*n
for i in range(n):
    cnt[a[i][0]]+=1

q = max(cnt)
ans = q
p = cnt.index(q)
for _ in range(m):
    cnt[p]=0
    M.remove(p)
    for i in range(n):
        if a[i][cursor[i]]==p:
            while a[i][cursor[i]] not in M:
                cursor[i]+=1
                if cursor[i]==m:
                    print(ans)
                    exit()
            cnt[a[i][cursor[i]]]+=1
    q2 = max(cnt)
    ans = min(ans,q2)
    p = cnt.index(q2)
print(ans)

Solution (Python):
import collections

def judge(k):
  pos=[0]*n
  dic=collections.defaultdict(int)
  s=set()
  for i in range(n):
    dic[arr[i][0]]+=1
  while 1:
    key=list(dic.keys())
    for i in key:
      if dic[i]>k:
        s.add(i)
        del dic[i]
    flag=True
    for i in range(n):
      if arr[i][pos[i]] in s:
        flag=False
        pos[i]+=1
        if pos[i]==m:
          return False
        dic[arr[i][pos[i]]]+=1
    if flag==True:
      return True

n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(n)]
l=0
r=n+1
while r-l!=1:
  mid=(l+r)//2
  if judge(mid)==True:
    r=mid
  else:
    l=mid
print(r)

Solution (Python):
import collections

def judge(k):
  pos=[0]*n
  dic=collections.defaultdict(int)
  s=set()
  for i in range(n):
    dic[arr[i][0]]+=1
  while 1:
    key=list(dic.keys())
    for i in key:
      if dic[i]>k:
        s.add(i)
        del dic[i]
    flag=True
    for i in range(n):
      if arr[i][pos[i]] in s:
        flag=False
        pos[i]+=1
        if pos[i]==m:
          return False
        dic[arr[i][pos[i]]]+=1
    if flag==True:
      return True

n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(n)]
l=0
r=n+1
while r-l!=1:
  mid=(l+r)//2
  if judge(mid)==True:
    r=mid
  else:
    l=mid
print(r)

Solution (Python):
n,m=map(int,input().split())
A=[[0]*m for i in range(n)]
for i in range(n):
  A[i]=list(map(int,input().split()))
B=[0]*m
for i in range(n):
  B[A[i][0]-1]+=1
a=0
b=0
for i in range(m):
  if B[i]>a:
    a=B[i]
    b=i
ans=a
C=[b+1]
i=0
while i<=m-2:
  B=[0]*m
  for j in range(n):
    for k in range(m):
      if A[j][k] not in C:
        B[A[j][k]-1]+=1
        break
  a=0
  b=0
  for l in range(m):
    if B[l]>a:
      a=B[l]
      b=l
  if ans>a:
    ans=a
  C.append(b+1)
  i=i+1
print(ans)

Solution (Python):
n,m = map(int,input().split())
a = [list(map(int,input().split())) for _ in range(n)]
flag = [True for i in range(m+1)]
ans = n
for _ in range(m-1):
    cnt = [0 for i in range(m+1)]
    for i in range(n):
        for j in range(m):
            if flag[a[i][j]]:
                cnt[a[i][j]]+=1
                break
    t = 0
    cat = 0
    for i in range(m+1):
        if cnt[i]>t:
            cat = i
            t = cnt[i]
    ans = min(ans,t)
    flag[cat] = False
print(ans)

Solution (Python):
N, M = map(int, input().split())
A = [list(map(lambda x:int(x)-1, input().split())) for _ in range(N)]
def C(x):
    count = [0]*M
    index = [0]*N
    flag = [False]*M
    for i in range(N):
        count[A[i][0]]+=1
    m = max(count)
    while m>x:
        tmp = [0]*M
        for i in range(N):
            j = index[i]
            if count[A[i][j]]>x:
                flag[A[i][j]] = True
        for i in range(N):
            j = index[i]
            while flag[A[i][j]]:
                index[i]+=1
                j+=1
                if j == M:
                    return False
            tmp[A[i][j]]+=1
        count = [t for t in tmp]
        m = max(count)
    return True

ng = 0
ok = N
while ok-ng>1:
    mid = (ok+ng)//2
    if C(mid):ok = mid
    else:ng = mid
print(ok)

Solution (Python):
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    N, M = LI()
    A = [LI() for _ in range(N)]
    not_flag = [False]*M
    ans = N
    for i in range(M):
        d = defaultdict(int)
        for k in range(N):
            for j in range(M):
                cur = A[k][j]-1
                if not_flag[cur]:
                    continue
                d[cur] +=1
                break
        d = sorted(d.items(),key=lambda x:x[1],reverse=True)

        not_flag[d[0][0]] = True
        ans = min(ans,d[0][1])
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Solution (Python):
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
t = [0] * (M + 1)
p = [0] * N
s = set()
res = float("inf")
for _ in range(M):
  t = [0] * (M + 1)
  for i in range(N): t[a[i][p[i]]] += 1
  x = 0
  y = 0
  for j in range(1, M + 1):
    if y < t[j]:
      x = j
      y = t[j]
  s.add(x)
  for i in range(N):
    while a[i][p[i]] in s:
      p[i] += 1
      if p[i] >= M:
        break
  if max(p) >= M: break
  res = min(res, max(t))
  
print(min(res, N))

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def foo(x):
    b = []
    for i in range(n):
        b.append([y for y in a[i]])
    
    cnt = 0
    rem = [False]*m
    while cnt<m:
        
        
        
        top = [0]*m
        ok = True
        for i in range(n):
            top[b[i][-1]] += 1
        for j in range(m):
            if top[j]>x:
                ok = False
                rem[j] = True
                cnt += 1
                if cnt==m:
                    return False
        if ok:
            return True
        for i in range(n):
            while len(b[i])>0 and rem[b[i][-1]]:
                b[i].pop()
    return True

n,m = inm()
a = []
for i in range(n):
    aa = inl()
    aa = [x-1 for x in aa]
    aa.reverse()
    a.append(aa)

ddprint(a)
mx = n
mn = 0
while mx>mn+1:
    mid = (mx+mn)//2
    if foo(mid):
        mx = mid
    else:
        mn = mid
print(mx)

Solution (Python):
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
inva = [[0] * M for _ in range(N)]
for i in range(N):
  for j in range(M):
    inva[i][a[i][j] - 1] = j
dp = [N * M] * M
t = [0] * N
vis = set()

for i in range(M):
  x = 0
  y = 0
  for j in range(M):
    z = 0
    for k in range(N):
      z += inva[k][j] == t[k]
    if z > y:
      y = z
      x = j
  dp[i] = y
  vis.add(x)
  for k in range(N):
    w = M
    for j in range(M):
      if j in vis: continue
      w = min(w, inva[k][j])
    t[k] = w
print(min(dp))

Solution (Python):
from collections import Counter

def update_rank_lis(rank_lis, drop_sport):
    return [
        [sport for sport in ranks if sport != drop_sport]
        for ranks in rank_lis
    ]

def count_first(rank_lis):
    first_cnt = Counter([ranks[0] for ranks in rank_lis])
    return sorted([(cnt, sport) for sport, cnt in first_cnt.items()])[-1]

def solve(N, M, rank_lis):
    min_value = N
    for _ in range(M):
        most_pop_cnt, most_pop_sport = count_first(rank_lis)
        min_value = min(most_pop_cnt, min_value)
        rank_lis = update_rank_lis(rank_lis, most_pop_sport)
    print(min_value)
    
N, M = map(int, input().split())
rank_lis = [list(map(int, input().split())) for _ in range(N)]
solve(N, M, rank_lis)

Solution (Python):
from collections import Counter
n,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(n)]
res = n
for _ in range(m-1):
    tl = [l[i][0] for i in range(n)]
    cmns = Counter(tl).most_common()[0]
    res = min(res,cmns[1])
    for i in range(n):
        l[i].remove(cmns[0])
print(res)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
A = [None] * N
for i in range(N):
    A[i] = [a-1 for a in LIST()]

k = 0
valid = [1] * M
ans = INF
while k < M:
    cnt = [0] * M
    for i in range(N):
        mn = INF
        idx = -1
        for j in range(M):
            a = A[i][j]
            if valid[a]:
                
                cnt[a] += 1
                break
    mx = max(cnt)
    ans = min(ans, mx)
    for i in range(M):
        
        if cnt[i] == mx:
            valid[i] = 0
            k += 1
print(ans)

Solution (Python):
from collections import Counter

N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]

check = [0] * (M + 1)

answer = []
for _ in range(M):
  tmp = []
  for ai in A:
    for aij in ai:
      if check[aij] == 0:
        tmp.append(aij)
        break
  counter = list(Counter(tmp).items())
  counter.sort(key = lambda x: -x[1])  
  max_kind, max_cnt = counter[0][0], counter[0][1]
  check[max_kind] = 1
  answer.append(max_cnt)
  
print(min(answer))

Solution (Python):
from collections import Counter

N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]

check = [0] * (M + 1)

answer = []
for _ in range(M):
  tmp = []
  for ai in A:
    for aij in ai:
      if check[aij] == 0:
        tmp.append(aij)
        break
  
  counter = list(Counter(tmp).items())
  counter.sort(key = lambda x: -x[1])  
  answer.append(counter[0][1])
  check[counter[0][0]] = 1
  
print(min(answer))