邓心一 6 years ago
parent
commit
7d6c0a5c8d

+ 20 - 0
medium/609.find-duplicate-file-in-system.go

@@ -0,0 +1,20 @@
+func findDuplicate(paths []string) [][]string {
+	res := make([][]string, 0)
+	m := make(map[string]int)
+	idx := 0
+	for _, path := range paths {
+		strs := strings.Split(path, " ")
+		for i := 1; i < len(strs); i++ {
+			names := strings.Split(strs[i], "(")
+			file := strs[0] + "/" + names[0]
+			if v, ok := m[names[1]]; ok {
+				res[v] = append(res[v], file)
+			} else {
+				res = append(res, []string{file})
+				m[names[1]] = idx
+				idx++
+			}
+		}
+	}
+	return res
+}

+ 30 - 0
medium/611.valid-triangle-number.go

@@ -0,0 +1,30 @@
+func triangleNumber(nums []int) int {
+	n, cnt := len(nums), 0
+	sort.Ints(nums)
+	m := make(map[int]int)
+	for i := 0; i < n-2; i++ {
+		for j := i + 1; j < n-1; j++ {
+			sum := nums[i] + nums[j]
+			if v, ok := m[sum]; ok {
+				if j < v {
+					cnt += v - j - 1
+				}
+			} else {
+				beg, end := j, n-1 // Not j+1, but j
+				for beg <= end {
+					mid := beg + (end-beg)/2
+					if nums[mid] < sum {
+						beg = mid + 1 // So beg is the first idx that >= sum
+					} else {
+						end = mid - 1
+					}
+				}
+				m[sum] = beg
+				if j < beg {
+					cnt += beg - j - 1
+				}
+			}
+		}
+	}
+	return cnt
+}

+ 14 - 0
medium/621.task-scheduler.go

@@ -0,0 +1,14 @@
+func leastInterval(tasks []byte, n int) int {
+	freq := make([]int, 26)
+	for _, t := range tasks {
+		freq[t-'A']++
+	}
+	sort.Ints(freq)
+	max, cnt := freq[25], 0
+	for _, i := range freq {
+		if i == max {
+			cnt++
+		}
+	}
+	return (max-1)*n + cnt
+}