publicclassSolution { publicintLeastBricks(IList<IList<int>> wall) { Dictionary<int, int> cnt = new Dictionary<int, int>(); foreach (IList<int> widths in wall) { int n = widths.Count; int sum = 0; for (int i = 0; i < n - 1; i++) { sum += widths[i]; if (!cnt.ContainsKey(sum)) { cnt.Add(sum, 1); } else { cnt[sum]++; } } } int maxCnt = 0; foreach (var entry in cnt) { maxCnt = Math.Max(maxCnt, entry.Value); } return wall.Count - maxCnt; } }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var leastBricks = function(wall) { const cnt = newMap(); for (const widths of wall) { const n = widths.length; let sum = 0; for (let i = 0; i < n - 1; i++) { sum += widths[i]; cnt.set(sum, (cnt.get(sum) || 0) + 1); } } let maxCnt = 0; for (const [_, c] of cnt.entries()) { maxCnt = Math.max(maxCnt, c); } return wall.length - maxCnt; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcleastBricks(wall [][]int)int { cnt := map[int]int{} for _, widths := range wall { sum := 0 for _, width := range widths[:len(widths)-1] { sum += width cnt[sum]++ } } maxCnt := 0 for _, c := range cnt { if c > maxCnt { maxCnt = c } } returnlen(wall) - maxCnt }