dengxinyi 6 年之前
父節點
當前提交
2780311ca9

+ 27 - 16
medium/332.reconstruct-itinerary.go

@@ -1,31 +1,42 @@
 type StringSlice []string
 
-func (ss StringSlice) Len() int { return len(ss) }
+func (ss StringSlice) Len() int           { return len(ss) }
 func (ss StringSlice) Less(i, j int) bool { return ss[i] < ss[j] }
-func (ss StringSlice) Swap(i, j int) { ss[i], ss[j] = ss[j], ss[i] }
+func (ss StringSlice) Swap(i, j int)      { ss[i], ss[j] = ss[j], ss[i] }
 
 func (ss *StringSlice) Push(x interface{}) {
 	*ss = append(*ss, x.(string))
 }
 
 func (ss *StringSlice) Pop() interface{} {
-	top := (*ss)[ss.Len()-1]
-	*ss = (*ss)[:ss.Len()-1]
-	return top
+	old := *ss
+	n := len(old)
+	x := old[n-1]
+	*ss = old[0 : n-1]
+	return x
 }
 
-func findItinerary(tickets [][]string) []string {
-	idx := make(map[string]int)
-	adj := make([][]StringSlice, 0)
+func findItinerary(tickets [][]string) (res []string) {
+	m := make(map[string]StringSlice)
 	for i := 0; i < len(tickets); i++ {
-		if id, ok := idx[tickets[i][0]]; !ok {
-			id = len(idx)
-			adj[id] = []StringSlice{tickets[i][1]}
-			idx[tickets[i][0]] = id
-		} else {
-			heap.Push(&adj[id], tickets[i][1])
-		}
+		ss := m[tickets[i][0]]
+		heap.Push(&ss, tickets[i][1])
+		m[tickets[i][0]] = ss
 	}
-	return []string{}
+	dfs(&m, "JFK", &res)
+	for l, r := 0, len(tickets); l < r; l, r = l+1, r-1 {
+		res[l], res[r] = res[r], res[l]
+	}
+	return res
+}
+
+func dfs(m *map[string]StringSlice, dst string, res *[]string) {
+	for (*m)[dst].Len() != 0 {
+		ss := (*m)[dst]
+		next := heap.Pop(&ss).(string)
+		(*m)[dst] = ss
+		dfs(m, next, res)
+	}
+	*res = append(*res, dst)
 }
 

+ 20 - 0
medium/334.go

@@ -0,0 +1,20 @@
+func increasingTriplet(nums []int) bool {
+	n := len(nums)
+	if n < 3 {
+		return false
+	}
+	st := []int{nums[0]}
+	top := 0
+	for i := 1; top < 2 && i < n; i++ {
+		if num := nums[i]; st[top] < num {
+			st = append(st, num)
+			top++
+		} else if num <= st[0] {
+			st[0] = num
+		} else {
+			st[1] = num
+		}
+	}
+	return top == 2
+}
+

+ 41 - 0
medium/337.house-robber-iii.go

@@ -0,0 +1,41 @@
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+type node struct {
+	root *TreeNode
+	done bool
+}
+
+func rob(root *TreeNode) int {
+	m := make(map[node]int)
+	return maxInt(helper(root, false, &m), helper(root, true, &m))
+}
+
+func helper(root *TreeNode, done bool, m *map[node]int) int {
+	if root == nil {
+		return 0
+	}
+	if max, ok := (*m)[node{root, done}]; ok { // Memory search
+		return max
+	}
+	var max int
+	if done {
+		max = helper(root.Left, false, m) + helper(root.Right, false, m)
+	} else {
+		max = maxInt(root.Val+helper(root.Left, true, m)+helper(root.Right, true, m), helper(root.Left, false, m)+helper(root.Right, false, m))
+	}
+	(*m)[node{root, done}] = max
+	return max
+}
+
+func maxInt(x, y int) int {
+	if x < y {
+		return y
+	}
+	return x
+}

+ 16 - 0
medium/338.counting-bits.go

@@ -0,0 +1,16 @@
+func countBits(num int) []int {
+	res := make([]int, num+1)
+	if num == 0 {
+		return res
+	}
+	res[1] = 1
+	for i, j := 2, 0; i <= num; i++ {
+		if i&1 == 0 { // If i is even, bits(i) = bits(i/2)
+			j++
+			res[i] = res[j]
+		} else { // If i is odd, bits(i) = bits(i/2) + 1
+			res[i] = res[j] + 1
+		}
+	}
+	return res
+}

+ 56 - 0
medium/341.flatten-nested-list-iterator.js

@@ -0,0 +1,56 @@
+/**
+ * // This is the interface that allows for creating nested lists.
+ * // You should not implement it, or speculate about its implementation
+ * function NestedInteger() {
+ *
+ *     Return true if this NestedInteger holds a single integer, rather than a nested list.
+ *     @return {boolean}
+ *     this.isInteger = function() {
+ *         ...
+ *     };
+ *
+ *     Return the single integer that this NestedInteger holds, if it holds a single integer
+ *     Return null if this NestedInteger holds a nested list
+ *     @return {integer}
+ *     this.getInteger = function() {
+ *         ...
+ *     };
+ *
+ *     Return the nested list that this NestedInteger holds, if it holds a nested list
+ *     Return null if this NestedInteger holds a single integer
+ *     @return {NestedInteger[]}
+ *     this.getList = function() {
+ *         ...
+ *     };
+ * };
+ */
+/**
+ * @constructor
+ * @param {NestedInteger[]} nestedList
+ */
+var NestedIterator = function(nestedList) {
+    
+};
+
+
+/**
+ * @this NestedIterator
+ * @returns {boolean}
+ */
+NestedIterator.prototype.hasNext = function() {
+    
+};
+
+/**
+ * @this NestedIterator
+ * @returns {integer}
+ */
+NestedIterator.prototype.next = function() {
+    
+};
+
+/**
+ * Your NestedIterator will be called like this:
+ * var i = new NestedIterator(nestedList), a = [];
+ * while (i.hasNext()) a.push(i.next());
+*/

+ 26 - 0
medium/343.integer-break.go

@@ -0,0 +1,26 @@
+func integerBreak(n int) int {
+	if n < 4 {
+		return n - 1
+	}
+	sqrt := int(math.Sqrt(float64(n)))
+	max := helper(n, sqrt)
+	for i := sqrt + 1; i < n/2; i++ {
+		if tmp := helper(n, i); max < tmp {
+			max = tmp
+		}
+	}
+	return max
+}
+
+func helper(n, k int) int {
+	div, mod, prod := n/k, n%k, 1
+	for i := 0; i < k; i++ {
+		if i < mod {
+			prod *= div + 1
+		} else {
+			prod *= div
+		}
+	}
+	return prod
+}
+

+ 43 - 0
medium/347.top-k-frequent-elements.go

@@ -0,0 +1,43 @@
+type pair struct {
+	_1 int
+	_2 int
+}
+
+type pairs []pair
+
+func (ps pairs) Len() int           { return len(ps) }
+func (ps pairs) Less(i, j int) bool { return ps[i]._1 < ps[j]._1 }
+func (ps pairs) Swap(i, j int)      { ps[i], ps[j] = ps[j], ps[i] }
+
+func (ps *pairs) Push(x interface{}) {
+	*ps = append(*ps, x.(pair))
+}
+
+func (ps *pairs) Pop() interface{} {
+	i := ps.Len() - 1
+	x := (*ps)[i]
+	*ps = (*ps)[:i]
+	return x
+}
+
+func topKFrequent(nums []int, k int) []int {
+	freq := make(map[int]int)
+	for _, i := range nums {
+		freq[i]++
+	}
+	var topK pairs
+	i := 0
+	for key, val := range freq {
+		heap.Push(&topK, pair{val, key})
+		if k == i {
+			heap.Pop(&topK)
+		} else {
+			i++
+		}
+	}
+	res := make([]int, k)
+	for i := range topK {
+		res[i] = topK[i]._2
+	}
+	return res
+}