stevenkplus's blog

By stevenkplus, history, 4 years ago, In English

I should be going live in a few minutes, and intend to stream for about an hour or so. Please hop on if you're interested! I will be trying to solve Leetcode problems as fast as possible.

https://www.twitch.tv/stevenkplus

  • Vote: I like it
  • +41
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Getting started now, just solved our first problem. Defanging an IP Address: https://leetcode.com/problems/defanging-an-ip-address

Code:

class Solution {
public:
    string defangIPaddr(string address) {
        string result;
        for (auto c: address) {
            if (c == '.') {
                result += "[.]";
            } else {
                result += c;
            }
        }
        return result;
    }
};

Time to solve: 1 min 10s.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

nice cat

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Problem 2: Number of Steps to Reduce a Number to Zero

https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero

class Solution {
public:
    int numberOfSteps (int num) {
        int ans = 0;
        while (num) {
            if (num % 2 == 1) --num;
            else num /= 2;
            ++ans;
        }
        return ans;
    }
};

Time to solve: 0:46


Problem 3: Jewels and Stones: https://leetcode.com/problems/jewels-and-stones

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        map<int, int> mp;
        for (auto c: J) mp[c] = 1;
        int sum = 0;
        for (auto c: S) sum += mp[c];
        return sum;
    }
};

Time to solve: 0:32


Problem 4:

Decompress Run-Length Encoded List :https://leetcode.com/problems/decompress-run-length-encoded-list/

class Solution {
public:
    vector<int> decompressRLElist(vector<int>& nums) {
        vector<int> res;
        for (int i = 0; i < nums.size(); i += 2) {
            for (int j = 0; j < nums[i]; ++j) {
                res.push_back(nums[i + 1]);
            }
        }
        return res;
    }
};

Time: 0:36


Problem 5: How Many Numbers Are Smaller Than the Current Number: https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
        vector<int> res;
        for (auto i: nums) {
            res.push_back(0);
            for (auto j: nums) {
                if (j < i) ++res.back();
            }
        }
        return res;
    }
};

Time: 0:30

»
4 years ago, # |
  Vote: I like it -10 Vote: I do not like it

Problem 6: Subtract the Product and Sum of Digits of an Integer https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/

class Solution {
public:
    int subtractProductAndSum(int n) {
        int prod = 1, sum = 0;
        while (n) {
            int d = n % 10; n /= 10;
            sum += d;
            prod *= d;
        }
        // 16
        return prod - sum; // abs(sum - prod);
    }
};

Time: 00:53

Problem 7: Find Numbers with Even Number of Digits: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/

class Solution {
public:
    int findNumbers(vector<int>& nums) {
        int ans = 0;
        for (auto i: nums) {
            int cnt = 0;
            while (i) {
                i /= 10;
                ++cnt;
            }
            if (cnt % 2 == 0) ++ans;
        }
        return ans;
    }
};

Time: 00:28

Problem 8:

Split a String in Balanced Strings: https://leetcode.com/problems/split-a-string-in-balanced-strings/

class Solution {
public:
    int balancedStringSplit(string s) {
        int ans = 0;
        int cnt = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == 'L') ++cnt;
            else --cnt;
            if (cnt == 0) ++ans;
        }
        return ans;
    }
};

Time: 01:51


Problem 9:

Create Target Array in the Given Order :https://leetcode.com/problems/create-target-array-in-the-given-order/


class Solution { public: vector<int> createTargetArray(vector<int>& nums, vector<int>& index) { int n = nums.size(); vector<int> bit(n + 2); auto ask = [&](int x) -> int { int ans = 0; while (x) { ans += bit[x]; x &= x - 1; } return ans; }; auto inc = [&](int x) -> void { while (x < bit.size()) { bit[x] += 1; x += x & -x; } }; vector<int> res(n); for (int i = n - 1; i >= 0; --i) { // there's a way to do this query in log(n): // given index[i], find min j, where ask(j) = j - index[i] res[ask(i) + i] } vector<int> res; for (int i = 0; i < nums.size(); ++i) { res.insert(res.begin() + index[i], nums[i]); // for (int j = int(res.size()) - 2; j >= index[i]; --j) { // res[j + 1] = res[j]; // } // res[index[i]] = nums[i]; } return res; } };

Time: 03:00

--- Problem 10: Range Sum of BST :https://leetcode.com/problems/range-sum-of-bst/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int rangeSumBST(TreeNode* root, int L, int R) {
        if (!root) return 0;
        int sum = 0;
        if (root->val >= L && root->val <= R) sum += root->val;
        sum += rangeSumBST(root->left, L, R);
        sum += rangeSumBST(root->right, L, R);
        return sum;
    }
};
»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Problem 11: Convert Binary Number in a Linked List to Integer : https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    int getDecimalValue(ListNode* head) {
        int ans = 0;
        while (head) {
            ans = ans * 2 + head->val;
            head = head->next;
        }
        return ans;
    }
};

Time: 00:19


Problem 12: Minimum Time Visiting All Points :https://leetcode.com/problems/minimum-time-visiting-all-points/

class Solution {
public:
    int minTimeToVisitAllPoints(vector<vector<int>>& points) {
        vector<int> prv;
        int ans = 0;
        bool first = true;
        for (auto v: points) {
            if (!first) {
                ans += max(abs(v[0] - prv[0]), abs(v[1] - prv[1]));
            }
            first = false;
            prv = v;
        }
        return ans;
    }
};

Time: 1:22


Problem 13: https://leetcode.com/problems/reformat-department-table/

SELECT i.id, 
Jan_Revenue,
  Feb_Revenue,
  Mar_Revenue,
  Apr_Revenue,
  May_Revenue,
  Jun_Revenue,
  Jul_Revenue,
  Aug_Revenue,
  Sep_Revenue,
  Oct_Revenue,
  Nov_Revenue,
  Dec_Revenue
FROM
(select distinct(id) from Department) as i
LEFT OUTER JOIN 
(select id, revenue Jan_Revenue from Department where month='Jan') as jan
ON i.id = jan.id
LEFT OUTER JOIN 
(select id, revenue Feb_Revenue from Department where month='Feb') as Feb
ON i.id = Feb.id
LEFT OUTER JOIN 
(select id, revenue Mar_Revenue from Department where month='Mar') as Mar
ON i.id = Mar.id
LEFT OUTER JOIN 
(select id, revenue Apr_Revenue from Department where month='Apr') as Apr
ON i.id = Apr.id
LEFT OUTER JOIN 
(select id, revenue May_Revenue from Department where month='May') as May
ON i.id = May.id
LEFT OUTER JOIN 
(select id, revenue Jun_Revenue from Department where month='Jun') as Jun
ON i.id = Jun.id
LEFT OUTER JOIN 
(select id, revenue Jul_Revenue from Department where month='Jul') as Jul
ON i.id = Jul.id
LEFT OUTER JOIN 
(select id, revenue Aug_Revenue from Department where month='Aug') as Aug
ON i.id = Aug.id
LEFT OUTER JOIN 
(select id, revenue Sep_Revenue from Department where month='Sep') as Sep
ON i.id = Sep.id
LEFT OUTER JOIN 
(select id, revenue Oct_Revenue from Department where month='Oct') as Oct
ON i.id = Oct.id
LEFT OUTER JOIN 
(select id, revenue Nov_Revenue from Department where month='Nov') as Nov
ON i.id = Nov.id
LEFT OUTER JOIN 
(select id, revenue Dec_Revenue from Department where month='Dec') as D
ON i.id = D.id
order by id asc

Time: 26:55


Problem 14: To Lower Case https://leetcode.com/problems/to-lower-case

class Solution {
public:
    string toLowerCase(string str) {
        string res;
        for (char c: str) {
            if (c >= 'a') res += c;
            else if (c >= 'A' && c <= 'Z') res += c + 'a' - 'A';
            else res += c;
        }
        return res;
    }
};

Time: 01:30

Problem 15: Replace Elements with Greatest Element on Right Side https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/

class Solution {
public:
    vector<int> replaceElements(vector<int>& arr) {
        vector<int> v;
        reverse(arr.begin(), arr.end());
        v.push_back(-1);
        for (int i: arr) {
            v.push_back(max(v.back(), i));
        }
        v.pop_back();
        reverse(v.begin(), v.end());
        return v;
    }
};

Problem 16: Maximum 69 Number https://leetcode.com/problems/maximum-69-number/

class Solution {
public:
    int maximum69Number (int num) {
        int pos = 0;
        int c = num;
        int cur = 1;
        while (c) {
            if (c % 10 == 6) {
                pos = cur;
            }
                        cur *= 10;
c /= 10;
        }
        return num + pos * 3;
    }
};

Time: 00:50


Problem 17: Cells with Odd Values in a Matrix https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/

class Solution {
public:
    int oddCells(int n, int m, vector<vector<int>>& indices) {
        map<int, int> cnts;
        int a = 0, b = 0;
        for (auto i: indices) ++cnts[i[0]];
        for (auto p: cnts) if (p.second % 2 == 1) ++a;
        cnts.clear();
        for (auto i: indices) ++cnts[i[1]];
        for (auto p: cnts) if (p.second % 2 == 1) ++b;
        return a * (m - b) + b * (n - a);
    }
};

Time: 03:40


Problem 18: Count Negative Numbers in a Sorted Matrix https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/

class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        int ans = 0;
        for (auto a: grid) {
            for (auto b: a) 
                if (b < 0) ++ans;
        }
        return ans;
    }
};

Problem 19: Remove Outermost Parentheses https://leetcode.com/problems/remove-outermost-parentheses/

class Solution {
public:
    string removeOuterParentheses(string S) {
        string res;
        int cnt = 0;
        for (char c: S) {
            if (c == '(') {
                if (cnt > 0) res += c;
                ++cnt;
            } else {
                --cnt;
                if (cnt > 0) res += c;
            }
        }
        return res;
    }
};

Time: 01:25


Problem 20: Big Countries : https://leetcode.com/problems/big-countries/

# Write your MySQL query statement below

select name, population, area from World
where area >= 3000000 or population >= 25000000

Time: 01:20