标签:cti ict max remove length color 优先 solution ||
本题使用回溯法,深度优先搜索。使用隐式条件来进行加速。
public class Solution { int bestp = 0; int[] x; Dictionary<int, int> dic = new Dictionary<int, int>(); void Backtrack(int[] nums, int t) { if (t >= nums.Length) { var sum = 0; for (int i = 0; i < nums.Length; i++) { if (x[i] == 1) { //Console.Write(nums[i]+" "); sum++; } } //Console.WriteLine(); bestp = Math.Max(bestp, sum); return; } if (dic.Count == 0 || dic.LastOrDefault().Value < nums[t]) { x[t] = 1; dic.Add(t, nums[t]); Backtrack(nums, t + 1); dic.Remove(t); } if (dic.Count + nums.Length - (t + 1) > bestp) { x[t] = 0; Backtrack(nums, t + 1); } } public int LengthOfLIS(int[] nums) { if (nums.Length < 2) { return nums.Length; } x = new int[nums.Length]; Backtrack(nums, 0); return bestp; } }
标签:cti ict max remove length color 优先 solution ||
原文地址:https://www.cnblogs.com/asenyang/p/9743172.html