thảo luận Leetcode contest, đường tới Guardian

Sáng nay xong 3 câu rồi :D
1715482658546.png
 
Nay vào muộn nên ko tham gia, đọc qua bài 4 khoai thật. Đọc solution mấy lần mới hiểu và implement đc :big_smile:
Python:
class Solution:
    def findPermutation(self, nums: List[int]) -> List[int]:
        n = len(nums)
        nexts = [[float('inf')] * n for _ in range(1 << n)]
        
        @cache
        def dp(mask, first):
            nonlocal nexts
            if mask == (1 << first):
                return abs(first - nums[0])
            
            min_dp = float('inf')
            for after_first in range(n):
                if after_first == first or ((1 << after_first) & mask) == 0:
                    continue
                
                next_mask = mask - (1 << first)
                curr_dp = abs(first - nums[after_first]) + dp(next_mask, after_first)
                
                if curr_dp < min_dp:
                    min_dp = curr_dp
                    nexts[mask][first] = after_first
            
            return min_dp
        
        res = []
        curr_mask = (1 << n) -1
        curr_first = 0
        dp(curr_mask, curr_first)

        while curr_mask:
            res.append(curr_first)
            next_mask = curr_mask - (1 << curr_first)
            next_first = nexts[curr_mask][curr_first]
            curr_mask, curr_first = next_mask, next_first

        return res
 
Bài 4 bitmask làm space n^2*2^n, time n^3*2^n được, tối ưu hơn chịu thua
// đọc sol mới nhận ra luôn để số 0 ở đầu tiên được :LOL:
 
Last edited:
Mình nghĩ họ phải có điều kiện để kết thúc nhánh sớm thì mới pass được.
có mỗi chỗ nếu phí hiện tại lớn hơn cái nhỏ nhất thì kết thúc ạ, cái này thì cũng dễ nhìn ra :LOL:
class Solution
{
public:
vector<int> findPermutation(vector<int> &nums)
{
int n = nums.size();
int min_cost = numeric_limits<int>::max();
vector<int> best_permutation;
vector<bool> used(n, false);
vector<int> perm;
dfs(perm, used, 0, nums, min_cost, best_permutation);
return best_permutation;
}
private:
void dfs(vector<int> &perm, vector<bool> &used, int current_cost, const vector<int> &nums, int &min_cost, vector<int> &best_permutation)
{
int n = nums.size();
if (perm.size() == n)
{
int final_cost = current_cost + abs(perm.back() - nums[perm[0]]);
if (final_cost < min_cost)
{
min_cost = final_cost;
best_permutation = perm;
}
return;
}
for (int i = 0; i < n; ++i)
{
if (!used)
{
if (!perm.empty())
{
int next_cost = current_cost + abs(perm.back() - nums);
if (next_cost < min_cost)
{
used = true;
perm.push_back(i);
dfs(perm, used, next_cost, nums, min_cost, best_permutation);
perm.pop_back();
used = false;
}
}
else
{
used = true;
perm.push_back(i);
dfs(perm, used, 0, nums, min_cost, best_permutation);
perm.pop_back();
used = false;
}
}
}
}
};
 
Last edited:
Em lần đầu tới với thằng leetcode, em là gốc chuyên tin, mấy thím cho em hỏi là ranking tính kiểu sao mà em lại chả thấy update ở đâu cả. Không như codeforce là sẽ hiện cho mình rằng đã tham gia vào contest nào. Hình ảnh contest lúc sáng:
1715532293190.png
 
Back
Top