../ Competitive Programming
-rw-r--r--aguang6.7 KiB4 min read

Leet Code Weekly Contest 263

1. Check if Numbers Are Ascending in a Sentence

Naive. Simple string processing. O(n)O(n).

c++
class Solution
{
public:
    bool isNum(string &s)
    {
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] < '0' || s[i] > '9')
            {
                return false;
            }
        }
        
        return true;
    }
    
    bool areNumbersAscending(string s)
    {
        int prev_num = 0;
        string current = "";
        
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] == ' ')
            {
                if (isNum(current))
                {
                    int num = stoi(current);
                    
                    if (prev_num != 0 && prev_num >= num)
                    {
                        return false;
                    }
                    
                    prev_num = num;
                }
                
                current = "";
            }
            else
            {
                current += s[i];
            }
        }
        
        if (isNum(current))
        {
            int num = stoi(current);

            if (prev_num != 0 && prev_num >= num)
            {
                return false;
            }
        }
        
        return true;
    }
};

2. Simple Bank System

Use an array to store all balances, the time complexity of every operation is O(1)O(1).

c++
class Bank
{
public:
    vector<long long> balances;
    
    Bank(vector<long long>& balance)
    {
        balances = vector<long long>(balance);
    }
    
    bool validAccount(int account)
    {
        return account >=1 && account <= balances.size();
    }
    
    bool transfer(int account1, int account2, long long money)
    {
        if (!validAccount(account1) || !validAccount(account2))
        {
            return false;
        }
        
        if (balances[account1 - 1] >= money)
        {
            balances[account1 - 1] -= money;
            balances[account2 - 1] += money;
            return true;
        }
        
        return false;
    }
    
    bool deposit(int account, long long money)
    {
        if (!validAccount(account))
        {
            return false;
        }
        
        balances[account - 1] += money;
        return true;
    }
    
    bool withdraw(int account, long long money)
    {
        if (!validAccount(account))
        {
            return false;
        }
        
        if (balances[account - 1] >= money)
        {
            balances[account - 1] -= money;
            return true;
        }
        
        return false;
    }
};

3. Count Number of Maximum Bitwise-OR Subsets

Then given constraint 1 <= nums.length <= 16 is a very obvious HINT for brute-force. A subset can be represented as a mask with nn bits. Just simply enumerate all possible masks. Then the overall time complexity is O(N×2N)O(N \times 2^N).

c++
class Solution
{
public:
    int countMaxOrSubsets(vector<int>& nums)
    {
        map<int, int> cnt;
        
        int n = nums.size(), max_result = 0;
        
        for (int mask = 0; mask < (1 << n); mask++)
        {
            if (mask != 0)
            {
                int or_result = 0;
                
                for (int i = 0; i < n; i++)
                {
                    if (mask & (1 << i))
                    {
                        or_result |= nums[i];
                    }
                }
                
                if (cnt.find(or_result) == cnt.end())
                {
                    cnt[or_result] = 1;
                }
                else
                {
                    cnt[or_result]++;
                }
                
                max_result = max(or_result, max_result);
            }
        }
        
        return cnt[max_result];
    }
};

4. Second Minimum Time to Reach Destination

First, if you arrive at a vertex at time tt, you can calculate the time of leaving immediately. Based on that, this is an extened shortest path problem. We can slightly change the implementation of SPFA to store top 2 shortest distances. Time complexity is O(M)O(M) where MM is the number of edges.

c++
class Solution
{
public:
    vector<int> targets[10000];
    
    vector<int> dists[10000];
    bool visit[10000];
    
    queue<int> q;
    
    int leave_time(int t, int change)
    {
        int period = 2 * change;
        
        if (t % period < change)
        {
            return t;
        }
        
        return t - (t % period) + period;
    }
    
    bool is_diff(vector<int> &x, vector<int> &y)
    {
        if (x.size() != y.size())
        {
            return true;
        }
        
        for (int i = 0; i < x.size(); i++)
        {
            if (x[i] != y[i])
            {
                return true;
            }
        }
        
        return false;
    }
    
    void spfa(int n, int s, int t, int time, int change)
    {
        for (int i = 0; i < n; i++)
        {
            visit[i] = false;
        }
        
        dists[s].push_back(0);
        visit[s] = true;
        
        q.push(s);
        
        while (!q.empty())
        {
            int x = q.front(); q.pop();
            visit[x] = false;
            
            for (auto &y : targets[x])
            {
                for (auto &dist_x : dists[x])
                {
                    int arrive_time = leave_time(dist_x, change) + time;
                    
                    // Has this in y already.
                    if (find(dists[y].begin(), dists[y].end(), arrive_time) != dists[y].end())
                    {
                        continue;
                    }
                    
                    vector<int> prev_y_dists = vector<int>(dists[y]);
                    
                    dists[y].push_back(arrive_time);
                    
                    if (dists[y].size() > 2)
                    {
                        sort(dists[y].begin(), dists[y].end());
                        dists[y].pop_back();
                    }
                    
                    if (is_diff(dists[y], prev_y_dists))
                    {      
                        if (!visit[y])
                        {
                            q.push(y);
                            visit[y] = true;
                        }
                    }
                }
            }
        }
    }
    
    int secondMinimum(int n, vector<vector<int>>& edges, int time, int change)
    {
        for (auto &edge : edges)
        {
            int x = edge[0] - 1, y = edge[1] - 1;
            targets[x].push_back(y);
            targets[y].push_back(x);
        }
        
        spfa(n, 0, n - 1, time, change);
        
        return dists[n - 1][1];
    }
};

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window