../ Competitive Programming
-rw-r--r--aguang2.5 KiB2 min read

Leet Code Weekly Contest 291

1. Remove Digit From Number to Maximize Result

  • Brute-force.
  • Be careful about the constaints! Use string comparision instead of converting to integer.
c++
class Solution {
public:
    string removeDigit(string number, char digit) {
        string ans = "";
        
        for (int i = 0; i < number.length(); i++) {
            if (number[i] == digit) {
                string t = "";
                for (int j = 0; j < number.length(); j++) {
                    if (j != i) {
                        t.push_back(number[j]);
                    }
                }
                ans = max(ans, t);
            }
        }
        
        return ans;
    }
};

2. Minimum Consecutive Cards to Pick Up

  • Linear-scan.
c++
class Solution {
public:
    int minimumCardPickup(vector<int>& cards) {
        int ans = INT_MAX;
        unordered_map<int, int> pos;
        for (int i = 0; i < cards.size(); i++) {
            if (pos.find(cards[i]) != pos.end()) {
                ans = min(ans, i - pos[cards[i]] + 1);
            }
            pos[cards[i]] = i;
        }
        return ans == INT_MAX ? -1 : ans;
    }
};

3. K Divisible Elements Subarrays

  • Brute-force & Hash.
c++
class Solution {
public:
    int countDistinct(vector<int>& nums, int k, int p) {
        unordered_set<string> ans;
        
        for (int i = 0; i < nums.size(); i++) {
            string t = "";
            int cnt = 0;
            
            for (int j = i; j < nums.size(); j++) {
                if (t.length() > 0) {
                    t += ",";
                }
                t += to_string(nums[j]);
                cnt += (nums[j] % p == 0) ? 1 : 0;
                if (cnt <= k) {
                    ans.insert(t);
                }
            }
        }
        
        return ans.size();
    }
};

4. Total Appeal of A String

  • A character will contribute the appeal only if it is the first occurence of some substring.
  • That means, for some chracter s[i], if the previous occurnce of s[i] is j, then (i - j + 1) * (n - i) substrings will gain one appeal.
c++
typedef long long LL;

class Solution {
private:
    unordered_map<char, int> pos;
    
public:
    LL appealSum(string s) {
        LL n = s.length(), ans = 0;
        
        for (LL i = 0; i < n; i++) {
            ans += (pos.count(s[i]) ? (i - pos[s[i]]) : (i + 1)) * (n - i);
            pos[s[i]] = i;
        }
        
        return ans;
    }
};

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