리스트에서 최솟값 찾기 구현

2021. 8. 11. 11:42노트/Algorithm : 알고리즘

candidate_max_leaf_nodes = [5, 25, 50, 100, 250, 500]
# Write loop to find the ideal tree size from candidate_max_leaf_nodes
compare = []
for max_leaf_nodes in candidate_max_leaf_nodes:
        
    mae = get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y)
    print('max_leaf_nodes: %d , mae: %s' %(max_leaf_nodes, mae))
    
    if not compare: # 빈 리스트 라면
        compare.append((max_leaf_nodes,mae))
        
    if compare: # 빈리스트가 아니면 
        (com_max_leaf_nodes , compare_mae) = compare.pop()
        if mae <= compare_mae: # 비교 대상보다 작으면, 리스트에 넣어라  
            compare.append((max_leaf_nodes,mae))
        else:
            compare.append((com_max_leaf_nodes , compare_mae))
    

# Store the best value of max_leaf_nodes (it will be either 5, 25, 50, 100, 250 or 500)
best_tree_size = compare[0][0]

'노트 > Algorithm : 알고리즘' 카테고리의 다른 글

[leetcode] 15. 3Sum  (0) 2022.11.03
[leetcode] 42. Trapping Rain Water  (0) 2022.11.02
[leetcode] 21. Merge Two Sorted Lists  (0) 2021.08.10
[leetcode] 20. Valid Parentheses  (0) 2021.08.10
[leetcode] 14. Longest Common Prefix  (0) 2021.08.10