data shape : 2000000 x 28
# data analysis and wrangling
import pandas as pd
import numpy as np
import random as rnd
pd.set_option('display.max_columns', 100)
# visualization
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
# machine learning
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier
itertools๋ ๋ฒ๋ฆด๊ฒ ํ๋์๋ ์์ฃผ ์ ์ฉํ ๋ชจ๋์ด๋ค.
๋ฌดํ ์ดํฐ๋ ์ดํฐ
countcyclerepeat์ดํฐ๋ ์ดํฐ
accumulatechainchain.from_iterablecompressdropwhilefilterfalsegroupbyisliceteezip_longest์กฐํฉํ
productpermutationscombinationscombinations_with_replacement
functools ๋ชจ๋์์ ํจ์๊ฐ ๊ฝค๋ง์ง๋ง ๋ด๊ฐ ํ๋จํ์ ๋, ์ฝ๋ฉํ
์คํธ์ ํ์ํ ํจ์ ๋ช๊ฐ์ง๋ค๋ง ์ถ๋ ค๋ณด์๋ค. ์ฝ๋ฉํ
์คํธ์๋ ๋ฑํ ๊ฐ์ฒด์งํฅ์ ์ธ ํ๋ก๊ทธ๋จ์ด ํ์ํ์ง ์๊ธฐ ๋๋ฌธ์, class์ ๊ด๋ จ๋ ํจ์๋ ์ ์ธํ์๋ค.
lru_cachereduce

Java๋ฌธ์ (15) + SQL ๋ฌธ์ (9) ์ ์ธ
from collections import defaultdict
from itertools import groupby
def solution(s):
lumps = defaultdict(lambda: defaultdict(int))
for char, group in groupby(s):
lumps[char][len(list(group))] += 1
unpretty = ((n := len(s)) - 1) * n * (n + 1) // 6
for lump in lumps.values():
total = sum(l * count for l, count in lump.items())
both_side = sum(lump.values())
for i in range(1, max(lump) + 1):
unpretty -= total * (total - 1) // 2
total -= both_side
both_side -= lump[i]
return unpretty
from collections import defaultdict
def fill_black(board, h, w):
board[h][w] = (-1 if h == 0 or board[h - 1][w] == -1 else 0)
def check(block, blocks, board) :
if len(blocks) < 4 : return False
h_list = sorted([h for h, w in blocks])
w_list = sorted([w for h, w in blocks])
for h in range(h_list[0], h_list[-1] + 1):
if any(color not in (-1, block) for color in board[h][w_list[0]:w_list[-1] + 1]):
return False
return True
def solution(board):
blocks = defaultdict(list)
answer = 0
for h in range((n := len(board))) :
for w in range(n) :
if (block := board[h][w]) > 0 :
(block_points := blocks[block]).append((h, w))
if not check(block, block_points, board) :
continue
answer += 1
for bh, bw in block_points :
fill_black(board, bh, bw)
elif block == 0 :
fill_black(board, h, w)
return answer
๊ฐ์ฌ๊ฒ์ ๋ฌธ์ ์ ๋น์ทํจ. Trie Tree ๋ฅผ ์ด์ฉํ ๋ฌธ์
def solution(words):
tree = Trie()
for word in words :
tree.insert(word)
return sum(tree.query(word) for word in words)
class TrieNode:
def __init__(self, char):
self.char = char
self.counter = 0
self.children = {}
class Trie(object):
def __init__(self):
self.root = TrieNode("")
def insert(self, word):
node = self.root
for char in word:
node.counter += 1
if char in node.children:
node = node.children[char]
else:
new_node = TrieNode(char)
node.children[char] = new_node
node = new_node
node.counter += 1
def query(self, word) :
node = self.root
for i, char in enumerate(word) :
node = node.children[char]
if node.counter == 1 :
return i+1
return len(word)
from itertools import accumulate
def solution(food_times, k):
n = len(food_times)
food_acc = list(accumulate((f := [0] + sorted(food_times))))
for i in range(1, n+1) :
if food_acc[i] + (n-i)*f[i] > k :
remain_food = [index+1 for index, food in enumerate(food_times) if food >= f[i]]
idx = (k - (food_acc[i-1] + (n-(i-1))*f[i-1])) % (n-(i-1))
return remain_food[idx]
return -1
from itertools import chain
def solution(land, P, Q):
land = sorted(chain.from_iterable(land))
k = len(land) * Q // (P + Q)
h = land[k]
return sum(h - x for x in land[:k])*P + sum(x - h for x in land[k:])*Q
def solution(strs, t):
DP = [0] * ((n := len(t)) + 1)
for i in range(1, n+1) :
DP[i] = min((DP[k] + 1 for k in range(max(0, i - 5), i) if t[k:i] in strs), default = 1e6)
return DP[-1] if DP[-1] < 1e6 else -1