From adaf03bbc526fca915f5201be77c6202b32e35a3 Mon Sep 17 00:00:00 2001 From: YDZ Date: Tue, 1 Dec 2020 20:20:08 +0800 Subject: [PATCH] Add Weekly 216 --- .../5613. Richest Customer Wealth.go | 20 +++++ .../5613. Richest Customer Wealth_test.go | 0 .... Find the Most Competitive Subsequence.go | 87 +++++++++++++++++++ ...d the Most Competitive Subsequence_test.go | 63 ++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 leetcode/5613. Richest Customer Wealth/5613. Richest Customer Wealth.go create mode 100644 leetcode/5613. Richest Customer Wealth/5613. Richest Customer Wealth_test.go create mode 100644 leetcode/5614.Find-the-Most-Competitive-Subsequence/5614. Find the Most Competitive Subsequence.go create mode 100644 leetcode/5614.Find-the-Most-Competitive-Subsequence/5614. Find the Most Competitive Subsequence_test.go diff --git a/leetcode/5613. Richest Customer Wealth/5613. Richest Customer Wealth.go b/leetcode/5613. Richest Customer Wealth/5613. Richest Customer Wealth.go new file mode 100644 index 00000000..6792948d --- /dev/null +++ b/leetcode/5613. Richest Customer Wealth/5613. Richest Customer Wealth.go @@ -0,0 +1,20 @@ +package leetcode + +func maximumWealth(accounts [][]int) int { + res := 0 + for i := 0; i < len(accounts); i++ { + sum := 0 + for j := 0; j < len(accounts[i]); j++ { + sum += accounts[i][j] + } + res = max(res, sum) + } + return res +} + +func max(a int, b int) int { + if a > b { + return a + } + return b +} diff --git a/leetcode/5613. Richest Customer Wealth/5613. Richest Customer Wealth_test.go b/leetcode/5613. Richest Customer Wealth/5613. Richest Customer Wealth_test.go new file mode 100644 index 00000000..e69de29b diff --git a/leetcode/5614.Find-the-Most-Competitive-Subsequence/5614. Find the Most Competitive Subsequence.go b/leetcode/5614.Find-the-Most-Competitive-Subsequence/5614. Find the Most Competitive Subsequence.go new file mode 100644 index 00000000..441c84b0 --- /dev/null +++ b/leetcode/5614.Find-the-Most-Competitive-Subsequence/5614. Find the Most Competitive Subsequence.go @@ -0,0 +1,87 @@ +package leetcode + +import ( + "fmt" +) + +// 解法一 单调栈 +func mostCompetitive(nums []int, k int) []int { + stack := make([]int, 0, len(nums)) + for i := 0; i < len(nums); i++ { + for len(stack)+len(nums)-i > k && len(stack) > 0 && nums[i] < stack[len(stack)-1] { + stack = stack[:len(stack)-1] + } + stack = append(stack, nums[i]) + } + return stack[:k] +} + +// 解法二 DFS 超时 +func mostCompetitive1(nums []int, k int) []int { + c, visited, res := []int{}, map[int]bool{}, []int{} + for i := 0; i < len(nums)-1; i++ { + if _, ok := visited[nums[i]]; ok { + continue + } else { + visited[nums[i]] = true + generateIncSubsets(nums, i, k, c, &res) + } + } + return res +} + +func generateIncSubsets(nums []int, current, k int, c []int, res *[]int) { + c = append(c, nums[current]) + fmt.Printf("c = %v res = %v\n", c, *res) + if len(c) > k { + return + } + if len(c) < k && len(*res) != 0 { + b, flag := make([]int, len(c)), false + copy(b, c) + for i := 0; i < len(b); i++ { + if b[i] < (*res)[i] { + flag = true + break + } + } + if !flag { + return + } + } + // if len(*res) != 0 && len(c) <= len(*res) && c[len(c)-1] > (*res)[len(c)-1] { + // return + // } + if len(c) == k { + //fmt.Printf("c = %v\n", c) + b, flag := make([]int, len(c)), false + copy(b, c) + if len(*res) == 0 { + *res = b + } else { + for i := 0; i < len(b); i++ { + if b[i] < (*res)[i] { + flag = true + break + } + } + if flag { + *res = b + } + } + fmt.Printf("tmp = %v min = %v\n", b, *res) + } + visited := map[int]bool{} + for i := current + 1; i < len(nums); i++ { + // if nums[current] <= nums[i] { + if _, ok := visited[nums[i]]; ok { + continue + } else { + visited[nums[i]] = true + generateIncSubsets(nums, i, k, c, res) + } + //} + } + c = c[:len(c)-1] + return +} diff --git a/leetcode/5614.Find-the-Most-Competitive-Subsequence/5614. Find the Most Competitive Subsequence_test.go b/leetcode/5614.Find-the-Most-Competitive-Subsequence/5614. Find the Most Competitive Subsequence_test.go new file mode 100644 index 00000000..9d70531c --- /dev/null +++ b/leetcode/5614.Find-the-Most-Competitive-Subsequence/5614. Find the Most Competitive Subsequence_test.go @@ -0,0 +1,63 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question491 struct { + para491 + ans491 +} + +// para 是参数 +// one 代表第一个参数 +type para491 struct { + nums []int + k int +} + +// ans 是答案 +// one 代表第一个答案 +type ans491 struct { + one []int +} + +func Test_Problem491(t *testing.T) { + + qs := []question491{ + + { + para491{[]int{3, 5, 2, 6}, 2}, + ans491{[]int{2, 6}}, + }, + + { + para491{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4}, + ans491{[]int{2, 3, 3, 4}}, + }, + + { + para491{[]int{2, 4, 3, 3, 5, 4, 9, 6}, 4}, + ans491{[]int{2, 3, 3, 4}}, + }, + + { + para491{[]int{71, 18, 52, 29, 55, 73, 24, 42, 66, 8, 80, 2}, 3}, + ans491{[]int{8, 80, 2}}, + }, + + { + para491{[]int{84, 10, 71, 23, 66, 61, 62, 64, 34, 41, 80, 25, 91, 43, 4, 75, 65, 13, 37, 41, 46, 90, 55, 8, 85, 61, 95, 71}, 24}, + ans491{[]int{10, 23, 61, 62, 34, 41, 80, 25, 91, 43, 4, 75, 65, 13, 37, 41, 46, 90, 55, 8, 85, 61, 95, 71}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 491------------------------\n") + + for _, q := range qs { + _, p := q.ans491, q.para491 + fmt.Printf("【input】:%v 【output】:%v\n", p, mostCompetitive(p.nums, p.k)) + } + fmt.Printf("\n\n\n") +}