public class Solution { public int NumSquares(int n) { if(n == 1){ return 1; } var len = n/2; var depth = 1; var table = new List(); for(var i = 1;i <= len; i++){ var x = i * i; if(x == n){ return 1; } if(x < n){ table.Add(x); } } var list = new List (); for(var i = 0 ;i < table.Count; i++){ list.Add(n-table[i]); } Bfs(table, list, depth + 1); return Depth; } private int Depth; private bool Found = false; public void Bfs(IList table , IList target , int depth) { if(Found){ return; } for(var i =0 ;i < table.Count; i++){ for(var j = 0;j < target.Count; j++){ if(table[i] == target[j]){ Depth = depth; Found = true; return; } } } var list = new List (); for(var i = 0;i < target.Count; i++){ var t = table.Where(x=>x