day25-back tracking-part04-7.27

tasks for today:

1.491.递增子序列

2.46.全排列

3.47.全排列II

4.332.重新安排行程/57.N皇后/37.解数独(optional)

-------------------------------------------------------------------

1.491.递增子序列

Although this practice looks simnilar to practice 99 (组合II), but in this practice, the difficult part lies in: it needs get rid of repetation, but currently repetation require sorting the nums, however, sorting nums will make the problem chaos, because this is a permutaion challenge.

So sorting should not be considered, we need to figure out other way to do the repetation devoiding.

The solution is using a set to control each level under the same father node should not be used twice.

class Solution:
    def findSubsequences(self, nums: List[int]) -> List[List[int]]:
        # in this practice, the difficult part lies in: it needs get rid of repetation, but currently repetation require sort the nums, however, sorting nums will make the problem chaos, because this is a permutaion challenge.
        # so sorting should not be considered, we need to figure out other way to do the repetation devoiding.
        # use a set to control each level under the same father node should not be used twice
        result = []
        path = []
        # nums.sort()
        # used = [False] * len(nums)
        self.backtracking(nums, 0, path, result)
        return result

    def backtracking(self, nums, startIndex, path, result):
        if len(path) >= 2:
            result.append(path[:])

        used = set()
        for i in range(startIndex, len(nums)):
            if (path and nums[i] < path[-1]) or (nums[i] in used):
                continue
            used.add(nums[i])
            path.append(nums[i])
            self.backtracking(nums, i + 1, path, result)
            path.pop()

2.46.全排列

In this practice, this is a permutation problem, the condition is the nums has no repetative elements, so there is no need to to do repetation-free operation;

one key point which is different from combination problem is the startIndex, here, in permutation problem, the startIndex is not needed, because the startIndex is used to control the start point along each branch, but in permutation problem, the sequence difference would result in a different solution.

So the startIndex is not required here, the only control is of the element has been shown in path.

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        result = []
        path = []
        self.backtracking(nums, path, result)
        return result

    def backtracking(self, nums, path, result):
        if len(path) == len(nums):
            result.append(path[:])
            return

        for i in range(len(nums)):
            if nums[i] in path:
                continue
            path.append(nums[i])
            self.backtracking(nums, path, result)
            path.pop()

But this is conditional because there is no repetative element in the nums list, there might be more complicated scenario in next practice because there might be repetitive elements in the nums list, then a used list to record each element's usage.

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        result = []
        path = []
        used = [False] * len(nums)
        self.backtracking(nums, path, result, used)
        return result

    def backtracking(self, nums, path, result, used):
        if len(path) == len(nums):
            result.append(path[:])
            return

        for i in range(len(nums)):
            if used[i]:
                continue
            used[i] = True
            path.append(nums[i])
            self.backtracking(nums, path, result, used)
            path.pop()
            used[i] = False

3.47.全排列II

The key difference between this practice and last practice is that, in this practice, there are repetative elements in the nums list, so aprat from the used list for controlling if the element has been used in once vertical branch, in each level, there also should be a level_control set for recording if one elements has been used, for preventing the repetative result path.

One point is, our previous repetation-free logic by passing in a list for seeing "if i > 0 and nums[i] == nums[i-1] and list[i-1] == False" can also apply in this practice, because this way requires sorting, and sorting will not affect the solution in permutation practice. In this way, both the vertical and the level control can be realized by one list. used = [False] * len(nums). But for this practice, I prefre seperate them for clarity.

class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        result = []
        path = []
        used = [False] * len(nums)
        self.backtracking(nums, path, result, used)
        return result

    def backtracking(self, nums, path, result, used):
        if len(path) == len(nums):
            result.append(path[:])
            return
        
        # newly added
        level_control = set()

        for i in range(len(nums)):

            # newly amended
            if used[i] or (nums[i] in level_control):
                continue

            level_control.add(nums[i])
            used[i] = True
            path.append(nums[i])
            self.backtracking(nums, path, result, used)
            path.pop()
            used[i] = False
  • 20
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Color-based model-free tracking is a popular technique used in computer vision to track objects in video sequences. Despite its simplicity, it has demonstrated high accuracy and robustness in various applications, such as surveillance, sports analysis, and human-computer interaction. One of the key advantages of color-based model-free tracking is its real-time performance. Unlike model-based tracking, which requires complex training and computation, color-based tracking can be implemented using simple algorithms that can run in real-time on low-power devices. This makes it suitable for applications that require fast response time, such as robotics and autonomous systems. Another advantage of color-based tracking is its ability to handle occlusions and partial occlusions. Since color features are less sensitive to changes in lighting and viewing conditions, the tracker can still maintain its accuracy even when the object is partially hidden or obstructed by other objects in the scene. Critics of color-based tracking argue that it is not effective in complex scenes where the object of interest may have similar colors to the background or other objects in the scene. However, recent advancements in machine learning and deep learning have enabled the development of more sophisticated color-based tracking algorithms that can accurately detect and track objects even in challenging scenarios. In summary, color-based model-free tracking is a simple yet effective technique for tracking objects in video sequences. Its real-time performance, robustness, and ability to handle occlusions make it a popular choice for various applications. While it may not be suitable for all scenarios, advancements in machine learning are making it more effective in complex scenes.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值