Competitive Programming_
Notes from the workbench. Pick a file and dive in.
166 entries│subscribe --rss ↗
Leet Code Daily 2024-08-10
$ head -n 12 daily-2024-08-10.md
* [Description](https://leetcode.com/problems/regions-cut-by-slashes) ## Solution Disjoint sets. ## Code ```c++const int kTop = 0, kLeft = 1, kRight = 2, kBottom = 3; class Solution { · 2 minLeet Code Daily 2024-08-09
$ head -n 12 daily-2024-08-09.md
* [Description](https://leetcode.com/problems/magic-squares-in-grid/) ## Code ```c++const vector<int> kAllowedVals = { 1, 2, 3, 4, 5, 6, 7, 8, 9,}; class Solution {public: int numMagicSquaresInside(vector<vector<int>>& grid) { · 2 minLeet Code Daily 2024-08-08
$ head -n 12 daily-2024-08-08.md
* [Description](https://leetcode.com/problems/spiral-matrix-iii/) ## Code ```c++const vector<pair<int, int>> kDir = { {0, +1}, {+1, 0}, {0, -1}, {-1, 0},}; · 1 minLeet Code Daily 2024-08-07
$ head -n 12 daily-2024-08-07.md
## Solution Simulation. ## Code ```c++constexpr int kBillion = 1E9;constexpr int kMillion = 1E6;constexpr int kThousand = 1E3;constexpr int kHundred = 1E2;constexpr int kTwenty = 2E1; · 2 minLeet Code Daily 2024-08-06
$ head -n 12 daily-2024-08-06.md
* [Description](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii/) ## Code ```c++class Solution {public: int minimumPushes(string word) { vector<int> freq(26, 0); for (const auto& c : word) { freq[c - 'a']++; } · 1 minLeet Code Daily 2024-08-05
$ head -n 12 daily-2024-08-05.md
* [Description](https://leetcode.com/problems/kth-distinct-string-in-an-array/) ## Code ```c++class Solution {public: string kthDistinct(vector<string>& arr, int k) { unordered_map<string, int> cnt; for (const auto& a : arr) { cnt[a]++; } · 1 minLeet Code Daily 2024-08-04
$ head -n 12 daily-2024-08-04.md
* [Description](https://leetcode.com/problems/range-sum-of-sorted-subarray-sums) ## Code ```c++constexpr int kMod = 1E9 + 7; class Solution {public: int rangeSum(vector<int>& nums, int n, int left, int right) { vector<int> sums; for (int i = 0; i < n; i++) { · 1 minLeet Code Daily 2024-08-03
$ head -n 12 daily-2024-08-03.md
* [Description](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/) ## Code ```c++class Solution {public: bool canBeEqual(vector<int>& target, vector<int>& arr) { sort(target.begin(), target.end()); sort(arr.begin(), arr.end()); for (int i = 0; i < target.size(); i++) { if (target[i] != arr[i]) { · 1 minLeet Code Daily 2024-08-02
$ head -n 12 daily-2024-08-02.md
* [Description](https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/) ## Code ```c++class Solution {public: int minSwaps(vector<int>& nums) { int num_ones = 0; for (const auto& x : nums) { num_ones += (x == 1); · 1 minLeet Code Daily 2024-08-01
$ head -n 12 daily-2024-08-01.md
* [Description](https://leetcode.com/problems/number-of-senior-citizens/) ## Code ```c++class Solution {public: int countSeniors(vector<string>& details) { int ret = 0; for (const auto& detail : details) { if ((detail[11] == '6' && detail[12] >= '1') || (detail[11] >= '7')) { · 1 minLeet Code Daily 2024-07-31
$ head -n 12 daily-2024-07-31.md
* [Description](https://leetcode.com/problems/filling-bookcase-shelves/) ## Solution **DP**. ## Code ```c++class Solution {public: int minHeightShelves(vector<vector<int>>& books, int shelfWidth) { · 1 minLeet Code Daily 2024-07-30
$ head -n 12 daily-2024-07-30.md
* [Description](https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/) ## Code ```c++class Solution {public: int minimumDeletions(string s) { int CBL = 0, CAR = 0; for (const auto& c : s) { if (c == 'a') { · 1 minLeet Code Daily 2024-07-29
$ head -n 12 daily-2024-07-29.md
* [Description](https://leetcode.com/problems/count-number-of-teams/) ## Solution * Binary search. ## Code ```c++class Solution {public: int numTeams(vector<int>& rating) { · 1 minLeet Code Daily 2024-07-28
$ head -n 12 daily-2024-07-28.md
* [Description](https://leetcode.com/problems/second-minimum-time-to-reach-destination/) ## Code ```c++constexpr int kVeryLong = 100000000; class Solution {public: int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) { auto neighborhood = vector<vector<int>>(n); · 1 minLeet Code Daily 2024-07-27
$ head -n 12 daily-2024-07-27.md
* [Description](https://leetcode.com/problems/minimum-cost-to-convert-string-i/) ## Solution Floyd-Warshall Algorithm. ## Code ```c++constexpr int kFarAway = 2600000; class Solution { · 1 minLeet Code Daily 2024-07-26
$ head -n 12 daily-2024-07-26.md
* [Description](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance) ## Solution Floyd-Warshall Algorithm. ## Code ```c++constexpr int kFarAway = 1000000; class Solution { · 1 minLeet Code Daily 2024-07-25
$ head -n 12 daily-2024-07-25.md
* [Description](https://leetcode.com/problems/sort-an-array/) ## Code ```c++constexpr int kNumMin = -50000;constexpr int kNumMax = +50000; class Solution {public: vector<int> sortArray(vector<int>& nums) { vector<int> freq(kNumMax - kNumMin + 1, 0); · 1 minLeet Code Daily 2024-07-24
$ head -n 12 daily-2024-07-24.md
* [Description](https://leetcode.com/problems/sort-the-jumbled-numbers/) ## Code ```c++class Solution {public: vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) { auto mapped_val = [&](int x) { int ret = 0; for (const auto& c : to_string(x)) { ret = 10 * ret + mapping[c - '0']; · 1 minLeet Code Daily 2024-07-23
$ head -n 12 daily-2024-07-23.md
* [Description](https://leetcode.com/problems/sort-array-by-increasing-frequency/) ## Code ```c++class Solution {public: vector<int> frequencySort(vector<int>& nums) { unordered_map<int, int> freq; for (const auto& num : nums) { freq[num]++; } · 1 minLeet Code Daily 2024-07-22
$ head -n 12 daily-2024-07-22.md
* [Description](https://leetcode.com/problems/sort-the-people/) ## Code ```c++class Solution {public: vector<string> sortPeople(vector<string>& names, vector<int>& heights) { const int n = names.size(); vector<shared_ptr<pair<string, int>>> people; for (int i = 0; i < n; i++) { · 1 minLeet Code Daily 2024-07-21
$ head -n 12 daily-2024-07-21.md
* [Description](https://leetcode.com/problems/build-a-matrix-with-conditions) ## Solution Topological Sort. ## Code ```c++class Solution {public: pair<bool, shared_ptr<vector<int>>> tsort(int k, · 1 minLeet Code Daily 2024-07-20
$ head -n 12 daily-2024-07-20.md
* [Description](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) ## Code ```c++class Solution {public: vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) { const int n = rowSum.size(), m = colSum.size(); auto ret = vector<vector<int>>(n, vector<int>(m, 0)); for (int i = 0; i < n; i++) { · 1 minLeet Code Daily 2024-07-19
$ head -n 12 daily-2024-07-19.md
* [Description](https://leetcode.com/problems/lucky-numbers-in-a-matrix/) ## Code ```c++class Solution {public: vector<int> luckyNumbers(vector<vector<int>>& matrix) { const int n = matrix.size(), m = matrix.front().size(); auto min_per_row = vector<int>(n, numeric_limits<int>::max()); auto max_per_col = vector<int>(m, numeric_limits<int>::min()); for (int i = 0; i < n; i++) { · 1 minLeet Code Daily 2024-07-18
$ head -n 12 daily-2024-07-18.md
* [Description](https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/) ## Code ```c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} · 1 minLeet Code Daily 2024-07-17
$ head -n 12 daily-2024-07-17.md
* [Description](https://leetcode.com/problems/delete-nodes-and-return-forest/description/) ## Code ```c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} · 2 minLeet Code Daily 2024-07-16
$ head -n 12 daily-2024-07-16.md
* [Description](https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/) ## Code ```c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} · 2 minLeet Code Daily 2024-07-15
$ head -n 12 daily-2024-07-15.md
* [Description](https://leetcode.com/problems/create-binary-tree-from-descriptions/) ## Code ```c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} · 1 minLeet Code Daily 2024-07-14
$ head -n 12 daily-2024-07-14.md
* [Description](https://leetcode.com/problems/number-of-atoms/) ## Solution **Stack**. ## Code ```c++class Formula { public: · 2 minLeet Code Daily 2024-07-13
$ head -n 12 daily-2024-07-13.md
* [Description](https://leetcode.com/problems/robot-collisions/) ## Solution **Sort** & **Stack**. ## Code ```c++struct robot { int position; int health; · 1 minLeet Code Daily 2024-07-12
$ head -n 12 daily-2024-07-12.md
* [Description](https://leetcode.com/problems/maximum-score-from-removing-substrings) ## Solution * **Greedy** + **Stack**. ## Code ```c++class Solution {public: int maximumGain(string s, int x, int y) { · 1 minLeet Code Daily 2024-07-11
$ head -n 12 daily-2024-07-11.md
* [Description](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) ## Solution **Stack** + **Tree Traversial**. ## Code ```c++enum class NodeType { kOpen = 0, kChar = 1, · 1 minLeet Code Daily 2024-07-10
$ head -n 12 daily-2024-07-10.md
* [Description](https://leetcode.com/problems/crawler-log-folder/) ## Code ```c++class Solution {public: int minOperations(vector<string>& logs) { int ans = 0; for (const auto& log : logs) { if (log[0] != '.') { ans++; · 1 minLeet Code Daily 2024-07-09
$ head -n 12 daily-2024-07-09.md
* [Description](https://leetcode.com/problems/average-waiting-time/) ## Code ```c++class Solution {public: double averageWaitingTime(vector<vector<int>>& customers) { double total_wait_time = 0; int last_idle_time = INT_MIN; for (const auto& customer : customers) { last_idle_time = max(last_idle_time, customer[0]) + customer[1]; · 1 minLeet Code Daily 2024-07-08
$ head -n 12 daily-2024-07-08.md
* [Description](https://leetcode.com/problems/find-the-winner-of-the-circular-game/) ## Code ```c++class Solution {public: int findTheWinner(int n, int k) { vector<int> friends; for (int i = 1; i <= n; i++) { friends.push_back(i); · 1 minLeet Code Daily 2024-07-07
$ head -n 12 daily-2024-07-07.md
* [Description](https://leetcode.com/problems/water-bottles/) ## Code ```c++class Solution {public: int numWaterBottles(int numBottles, int numExchange) { int ans = numBottles; while (numBottles >= numExchange) { int t = numBottles / numExchange; ans += t; · 1 minLeet Code Daily 2024-07-06
$ head -n 12 daily-2024-07-06.md
* [Description](https://leetcode.com/problems/pass-the-pillow/) ## Code ```c++class Solution {public: int passThePillow(int n, int time) { int p = time / (n - 1), q = time % (n - 1); return p % 2 == 0 ? (1 + q) : (n - q); }}; · 1 minLeet Code Daily 2024-07-05
$ head -n 12 daily-2024-07-05.md
* [description](https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/) ## Code ```c++/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} · 1 minLeet Code Daily 2024-07-04
$ head -n 12 daily-2024-07-04.md
* [Description](https://leetcode.com/problems/merge-nodes-in-between-zeros) ## Code ```c++/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} · 1 minLeet Code Daily 2024-07-03
$ head -n 12 daily-2024-07-03.md
* [Description](https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/) ## Code ```c++class Solution {public: int minDifference(vector<int>& nums) { sort(nums.begin(), nums.end()); int n = nums.size(), ans = INT_MAX; for (int i = 0; i < 4 && i < n; i++) { ans = min(ans, nums[max(n + i - 4, i)] - nums[i]); · 1 minLeet Code Daily 2024-07-02
$ head -n 12 daily-2024-07-02.md
* [Description](https://leetcode.com/problems/intersection-of-two-arrays-ii/) ## Code ```c++class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { sort(nums1.begin(), nums1.end()); sort(nums2.begin(), nums2.end()); vector<int> ret; for (int i = 0, j = 0; i < nums1.size() && j < nums2.size();) { · 1 minLeet Code Daily 2024-07-01
$ head -n 12 daily-2024-07-01.md
* [Description](https://leetcode.com/problems/three-consecutive-odds) ## Code ```c++class Solution {public: bool threeConsecutiveOdds(vector<int>& arr) { for (int i = 1; i + 1 < arr.size(); i++) { if (arr[i - 1] % 2 == 1 && arr[i] % 2 == 1 && arr[i + 1] % 2 == 1) { return true; } · 1 minLeet Code Daily 2024-06-30
$ head -n 12 daily-2024-06-30.md
* [Description](https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/) ## Solution **Greedy + Disjoint Sets** ## Code ```c++class Solution {public: int find(vector<int>& parent, int x) { · 2 minLeet Code Daily 2024-06-29
$ head -n 12 daily-2024-06-29.md
* [Description](https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph) ## Code ```c++class Solution {public: void compute(int x, unordered_map<int, set<int>>& ancestors, unordered_map<int, set<int>>& parent) { if (ancestors.find(x) == ancestors.end()) { for (const auto y : parent[x]) { compute(y, ancestors, parent); · 1 minLeet Code Daily 2024-06-28
$ head -n 12 daily-2024-06-28.md
* [Description](https://leetcode.com/problems/maximum-total-importance-of-roads/) ## Solution **Greedy**. The total importance of all roads is: $$\sum_{i \in \text{nodes}} \text{value}_i \times \text{degree}_i$$ · 1 minLeet Code Daily 2024-06-27
$ head -n 12 daily-2024-06-27.md
* [Description](https://leetcode.com/problems/find-center-of-star-graph/) ## Code ```c++class Solution {public: int findCenter(vector<vector<int>>& edges) { unordered_set<int> nodes_with_edge; for (const auto& edge : edges) { for (const auto& node : edge) { if (nodes_with_edge.contains(node)) { · 1 minLeet Code Daily 2024-06-26
$ head -n 12 daily-2024-06-26.md
* [Description](https://leetcode.com/problems/balance-a-binary-search-tree) ## Code ```c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} · 1 minLeet Code Daily 2024-06-25
$ head -n 12 daily-2024-06-25.md
* [Description](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree) ## Solution BFS ## Code ```c++/** * Definition for a binary tree node. * struct TreeNode { · 1 minLeet Code Daily 2024-06-24
$ head -n 12 daily-2024-06-24.md
* [Description](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/) ## Solution Greedy, always flip from the first 0 bit. ## Code ```c++class Solution {public: int minKBitFlips(vector<int>& nums, int k) { · 1 minLeet Code Daily 2024-06-23
$ head -n 12 daily-2024-06-23.md
* [Description](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) ## Code ```c++class Solution {public: int longestSubarray(vector<int>& nums, int limit) { int n = nums.size(), ans = 0; multiset<int> s; for (int i = 0, j = 0; j < n; j++) { s.insert(nums[j]); · 1 minLeet Code Daily 2024-06-22
$ head -n 12 daily-2024-06-22.md
* [Description](https://leetcode.com/problems/count-number-of-nice-subarrays) ## Solution Preffix sum. ## Code ```c++class Solution {public: int numberOfSubarrays(vector<int>& nums, int k) { · 1 minLeet Code Daily 2024-06-21
$ head -n 12 daily-2024-06-21.md
* [Description]() ## Solution * Sliding window. ## Code ```c++class Solution {public: int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) { · 1 minLeet Code Daily 2024-06-20
$ head -n 12 daily-2024-06-20.md
* [Description](https://leetcode.com/problems/magnetic-force-between-two-balls) ## Solution Binary search. ## Code ```c++class Solution {public: // Given `position` sorted in increasing order and `min_dis`, · 1 minLeet Code Daily 2024-06-19
$ head -n 12 daily-2024-06-19.md
* [Description](https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets) ## Solution Binary search. ## Code ```c++class Solution {public: int cntBouquets(vector<int>& bloomDay, int day, int k) { · 1 minLeet Code Daily 2024-06-18
$ head -n 12 daily-2024-06-18.md
* [Description](https://leetcode.com/problems/most-profit-assigning-work) ## Solution * Given each worker is independent, we can see this as RMQ problem and each worker represents one query.* Or we can just sort. ## Code ```c++class Solution {public: · 1 minLeet Code Daily 2024-06-17
$ head -n 12 daily-2024-06-17.md
* [Description](https://leetcode.com/problems/sum-of-square-numbers) ## Code ```c++class Solution {public: bool judgeSquareSum(int c) { for (int a = 0; a <= sqrt(c); a++) { int b = sqrt(c - a * a); if (a * a + b * b == c) { return true; · 1 minLeet Code Daily 2024-06-16
$ head -n 12 daily-2024-06-16.md
* [Description](https://leetcode.com/problems/patching-array) ## Solution Assuming we have completed range `[1, x]` and the next given but not used number is `y`, * If `y <= x + 1`, then we just use $y$ and expand the range to `[1, x + y]`.* Otherwise, we must patch a new number `x + 1`, and the new range would be `[1, x + x + 1]`. ## Code ```c++ · 1 minLeet Code Daily 2024-06-15
$ head -n 12 daily-2024-06-15.md
* [Description](https://leetcode.com/problems/ipo) ## Solution **Greedy**, always take current available project with maximum profit. ## Code ```c++class Solution {public: int findMaximizedCapital(int k, int w, vector<int>& profits, · 1 minLeet Code Daily 2024-06-14
$ head -n 12 daily-2024-06-14.md
* [Description](https://leetcode.com/problems/minimum-increment-to-make-array-unique) ## Code ```c++class Solution {public: int minIncrementForUnique(vector<int>& nums) { sort(nums.begin(), nums.end()); int ans = 0; for (int i = 1; i < nums.size(); i++) { int diff = max(nums[i - 1] + 1, nums[i]) - nums[i]; · 1 minLeet Code Daily 2024-06-13
$ head -n 12 daily-2024-06-13.md
* [Description](https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone) ## Code ```c++class Solution {public: int minMovesToSeat(vector<int>& seats, vector<int>& students) { sort(seats.begin(), seats.end()); sort(students.begin(), students.end()); int ans = 0, n = seats.size(); for (int i = 0; i < n; i++) { · 1 minLeet Code Daily 2024-06-12
$ head -n 12 daily-2024-06-12.md
* [Description](https://leetcode.com/problems/sort-colors) ## Code ```c++class Solution {public: void sortColors(vector<int>& nums) { int cnt[3] = {0}; for (const auto& x : nums) { cnt[x]++; } · 1 minLeet Code Daily 2024-06-11
$ head -n 12 daily-2024-06-11.md
* [Description](https://leetcode.com/problems/relative-sort-array) ## Code ```c++class Solution {public: vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) { map<int, int> freq; for (const auto& x : arr1) { freq[x]++; } · 1 minLeet Code Daily 2024-06-10
$ head -n 12 daily-2024-06-10.md
* [Description](https://leetcode.com/problems/height-checker) ## Solution ```c++class Solution {public: int heightChecker(vector<int>& heights) { vector<int> expected(heights.begin(), heights.end()); sort(expected.begin(), expected.end()); int ans = 0; for (int i = 0; i < heights.size(); i++) { · 1 minLeet Code Daily 2024-06-09
$ head -n 12 daily-2024-06-09.md
* [Description](https://leetcode.com/problems/subarray-sums-divisible-by-k) ## Code ```c++class Solution {public: int subarraysDivByK(vector<int>& nums, int k) { unordered_map<int, int> cnt; cnt[0] = 1; int sum = 0, ans = 0; for (const auto& x : nums) { · 1 minLeet Code Daily 2024-06-08
$ head -n 12 daily-2024-06-08.md
## Solution **Preffix sum** & **hash**. ## Code ```c++class Solution {public: bool checkSubarraySum(vector<int>& nums, int k) { vector<int> sums = {0}; for (const auto& x : nums) { · 1 minLeet Code Daily 2024-06-07
$ head -n 12 daily-2024-06-07.md
* [Description](https://leetcode.com/problems/replace-words) ## Solution **Trie** ## Code ```c++constexpr int kMaxChars = 26; typedef struct trie trie; · 1 minLeet Code Daily 2024-06-06
$ head -n 12 daily-2024-06-06.md
* [Description](https://leetcode.com/problems/hand-of-straights) ## Code ```c++class Solution {public: bool isNStraightHand(vector<int>& hand, int groupSize) { unordered_map<int, int> cnt_unused; for (const auto& val : hand) { cnt_unused[val]++; } · 1 minLeet Code Daily 2024-06-05
$ head -n 12 daily-2024-06-05.md
* [Description](https://leetcode.com/problems/find-common-characters/description) ## Code ```c++class Solution {public: vector<string> commonChars(vector<string>& words) { unordered_map<char, int> min_cnt; for (char c = 'a'; c <= 'z'; c++) { min_cnt[c] = INT_MAX; } · 1 minLeet Code Daily 2024-06-04
$ head -n 12 daily-2024-06-04.md
* [Description](https://leetcode.com/problems/longest-palindrome/) ## Code ```c++class Solution {public: int longestPalindrome(string s) { unordered_map<char, int> cnt_chars; for (const auto& c : s) { cnt_chars[c]++; } · 1 minLeet Code Daily 2024-06-03
$ head -n 12 daily-2024-06-03.md
* [Description](https://leetcode.com/problems/append-characters-to-string-to-make-subsequence) ## Code ```c++constexpr int kMaxSize = 100001;constexpr int kNumChars = 26; class Solution {public: int appendCharacters(string s, string t) { int n = s.size(), m = t.size(); · 1 minLeet Code Daily 2024-06-02
$ head -n 12 daily-2024-06-02.md
* [Description](https://leetcode.com/problems/reverse-string) ## Code ```c++class Solution {public: void reverseString(vector<char>& s) { for (int i = 0, j = s.size() - 1; i < j;) { swap(s[i++], s[j--]); } } · 1 minLeet Code Daily 2024-06-01
$ head -n 12 daily-2024-06-01.md
* [Description](https://leetcode.com/problems/score-of-a-string/description) ## Code ```c++class Solution {public: int scoreOfString(string s) { int n = s.size(), ret = 0; for (int i = 0; i + 1 < n; i++) { ret += abs(s[i] - s[i + 1]); } · 1 minLeet Code Daily 2024-05-31
$ head -n 12 daily-2024-05-31.md
* [Description](https://leetcode.com/problems/single-number-iii/description) ## Solution Let's say $x$ and $y$ are the numbers we are looking for. We know: $$x \oplus y = \oplus_{i = 0}^{n - 1} A_i$$ · 1 minLeet Code Daily 2024-05-30
$ head -n 12 daily-2024-05-30.md
* [Description](https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/) ## Solution **Preffix Sum + Hash** First, let's define preffix sum under `XOR` operator: $$S_{i} = \begin{cases} 0 & i = 0 \\ · 1 minLeet Code Daily 2024-05-29
$ head -n 12 daily-2024-05-29.md
* [Description](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one) ## Solution **Simulation** ## Code ```c++class Solution {public: int numSteps(string s) { · 1 minLeet Code Daily 2024-05-28
$ head -n 12 daily-2024-05-28.md
* [Description](https://leetcode.com/problems/get-equal-substrings-within-budget) ## Solution Linear scan. ## Code ```c++class Solution {public: int equalSubstring(string s, string t, int maxCost) { · 1 minLeet Code Daily 2024-05-27
$ head -n 12 daily-2024-05-27.md
* [Description](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x) ## Code ```c++class Solution {public: int specialArray(vector<int>& nums) { sort(nums.begin(), nums.end()); for (int i = 0; i < nums.size(); i++) { int c = nums.size() - i; if (c <= nums[i] && (i == 0 || nums[i - 1] < c)) { · 1 minLeet Code Daily 2024-05-26
$ head -n 12 daily-2024-05-26.md
## Leet Code Daily 2024-05-26 - Student Attendance Record II * [Description](https://leetcode.com/problems/student-attendance-record-ii) ## Solution It's just math.. Let's say $F(n)$ is the number of possible student attendance records of which: - The last record is `P`.- It contains no record `A`. · 2 minLeet Code Daily 2024-05-25
$ head -n 12 daily-2024-05-25.md
* [Description](https://leetcode.com/problems/word-break-ii) ## Solution **DFS** ## Code ```c++class Solution {public: unordered_set<string> wordSet; · 1 minLeet Code Daily 2024-05-24
$ head -n 12 daily-2024-05-24.md
* [Description](https://leetcode.com/problems/maximum-score-words-formed-by-letters) ## Solution **DFS** ## Code ```c++constexpr int kNumLetters = 26; class Solution { · 2 minLeet Code Daily 2024-05-23
$ head -n 12 daily-2024-05-23.md
* [Description](https://leetcode.com/problems/the-number-of-beautiful-subsets) ## Code ```c++class Solution {public: int dfs(vector<int>& nums, unordered_map<int, int>& cnt, int i, int k) { if (i == nums.size()) { return cnt.empty() ? 0 : 1; } int ret = dfs(nums, cnt, i + 1, k); · 1 minLeet Code Daily 2024-05-22
$ head -n 12 daily-2024-05-22.md
* [Description](https://leetcode.com/problems/palindrome-partitioning) ## Code ```c++class Solution {public: void dfs(int x, int n, vector<vector<string>>& p, vector<string>& current, vector<vector<string>>& ans) { if (x == n) { ans.push_back(vector<string>(current.begin(), current.end())); return; · 1 minLeet Code Daily 2024-05-21
$ head -n 12 daily-2024-05-21.md
* [Description](https://leetcode.com/problems/subsets) ## Code ```c++class Solution {public: vector<vector<int>> subsets(vector<int>& nums) { vector<vector<int>> ret; const int n = nums.size(); for (int mask = 0; mask < (1 << n); mask++) { vector<int> s; · 1 minLeet Code Daily 2024-05-20
$ head -n 12 daily-2024-05-20.md
* [Description](https://leetcode.com/problems/sum-of-all-subset-xor-totals/) ## Code ```c++class Solution {public: int dfs(vector<int>& nums, int i, int current_xor) { return (i >= nums.size()) ? current_xor : (dfs(nums, i + 1, current_xor) + dfs(nums, i + 1, current_xor ^ nums[i])); } · 1 minLeet Code Daily 2024-05-19
$ head -n 12 daily-2024-05-19.md
* [Description](https://leetcode.com/problems/find-the-maximum-sum-of-node-values) ## Solution **Tree DP**. $$F(x, c) = \text{maximum sum of sub-tree x when the parent edge is operated } c \text{ times}$$ $$F(x, c) = \max{ · 2 minLeet Code Daily 2024-05-18
$ head -n 12 daily-2024-05-18.md
* [Description](https://leetcode.com/problems/distribute-coins-in-binary-tree) ## Code ```c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} · 1 minLeet Code Daily 2024-05-17
$ head -n 12 daily-2024-05-17.md
* [Description](https://leetcode.com/problems/delete-leaves-with-a-given-value) ## Code ```c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} · 1 minLeet Code Daily 2024-05-16
$ head -n 12 daily-2024-05-16.md
* [Description](https://leetcode.com/problems/evaluate-boolean-binary-tree) ## Code ```c++/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} · 1 minLeet Code Daily 2024-05-15
$ head -n 12 daily-2024-05-15.md
* [Description](https://leetcode.com/problems/find-the-safest-path-in-a-grid) ## Solution First, we can compute $F$ by **BFS**, where $$F_{i,j} = \text{ min manhattan distance from any leave to cell (i, j)}$$ Then we just need to find the best path $P$ where$\max_{(i, j) \in P}{F_{i, j}}$ is minimized. That can be done by **SPFA**. · 2 minLeet Code Daily 2024-05-14
$ head -n 12 daily-2024-05-14.md
* [Description](https://leetcode.com/problems/path-with-maximum-gold) ## Solution **DFS** ## Code ```c++vector<pair<int, int>> kDir = { { -1, 0 }, { +1, 0 }, · 1 minLeet Code Daily 2024-05-13
$ head -n 12 daily-2024-05-13.md
* [Description](https://leetcode.com/problems/score-after-flipping-matrix) ## Solution Enumerate $2^m$ possible states of toggling columns and for each case, decide the bestresult per row. ## Code ```c++class Solution {public: · 1 minLeet Code Daily 2024-05-12
$ head -n 12 daily-2024-05-12.md
* [Description](https://leetcode.com/problems/largest-local-values-in-a-matrix) ## Code ```c++class Solution {public: vector<vector<int>> largestLocal(vector<vector<int>>& grid) { int n = grid.size(), m = grid[0].size(); vector<vector<int>> ans; for (int i = 1; i + 1 < n; i++) { vector<int> row; · 1 minLeet Code Daily 2024-05-11
$ head -n 12 daily-2024-05-11.md
* [Description](https://leetcode.com/problems/minimum-cost-to-hire-k-workers) ## Solution Let's say $H$ is the set of hired workers and $C_i$ is the cost of hired worker $i$. $$\lvert H \rvert = k$$ We know all hired workers must be paid equally. · 2 minLeet Code Daily 2024-05-10
$ head -n 12 daily-2024-05-10.md
* [Description](https://leetcode.com/problems/k-th-smallest-prime-fraction) ## Solution ### Monotonicity Optimization First, let's define: $$F(x, y) =\lvert \{ · 2 minLeet Code Daily 2024-05-09
$ head -n 12 daily-2024-05-09.md
* [Description](https://leetcode.com/problems/maximize-happiness-of-selected-children) ## Code ```c++class Solution {public: long long maximumHappinessSum(vector<int>& happiness, int k) { sort(happiness.begin(), happiness.end()); long long ans = 0; for (int i = happiness.size() - 1, d = 0; i >= 0 && d < k; i--, d++) { ans += max(0, happiness[i] - d); · 1 minLeet Code Daily 2024-05-08
$ head -n 12 daily-2024-05-08.md
* [Description](https://leetcode.com/problems/relative-ranks) ## Code ```c++unordered_map<int, string> kNames = { { 1, "Gold Medal" }, { 2, "Silver Medal" }, { 3, "Bronze Medal" },}; class Solution { · 1 minLeet Code Daily 2024-05-07
$ head -n 12 daily-2024-05-07.md
* [Description](https://leetcode.com/problems/double-a-number-represented-as-a-linked-list) ## Code ```c++/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} · 1 minLeet Code Daily 2024-05-06
$ head -n 12 daily-2024-05-06.md
* [Description](https://leetcode.com/problems/remove-nodes-from-linked-list) ## Code ```c++/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} · 1 minLeet Code Daily 2024-05-05
$ head -n 12 daily-2024-05-05.md
* [Description](https://leetcode.com/problems/delete-node-in-a-linked-list) ## Code ```c++/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; · 1 minLeet Code Daily 2024-05-04
$ head -n 12 daily-2024-05-04.md
* [Description](https://leetcode.com/problems/boats-to-save-people) ## Solution * First, sort all `people` in incresing order.* Let's say `people[i..j]` are the people which are not shipped yet.* There are several cases: * `i == j`, then we only need last boat. * `people[i] + people[j] <= limit`, then we carry both of them. * otherwise, `people[j]` must be shipped alone. ### Code · 1 minLeet Code Daily 2024-05-03
$ head -n 12 daily-2024-05-03.md
* [Description](https://leetcode.com/problems/compare-version-numbers) ## Code ```c++constexpr int kMaxLen = 500;typedef long long ll; class Solution {public: void parseVersion(const string version, ll* v) { int i = 0; · 1 minLeet Code Daily 2024-05-02
$ head -n 12 daily-2024-05-02.md
* [Description](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative) ## Code ```c++constexpr int kMaxNum = 1000; class Solution {public: int findMaxK(vector<int>& nums) { bool has_pos[kMaxNum + 1] = { false }; bool has_neg[kMaxNum + 1] = { false }; · 1 minLeet Code Daily 2024-05-01
$ head -n 12 daily-2024-05-01.md
* [Description](https://leetcode.com/problems/reverse-prefix-of-word) ## Solution * Naive. ## Code ```c++class Solution {public: string reversePrefix(string word, char ch) { · 1 minLeet Code Daily 2024-04-30
$ head -n 12 daily-2024-04-30.md
* [Description](https://leetcode.com/problems/number-of-wonderful-substrings) ## Solution `Preffix Sum` + `Bit Mask`. ## Code ```c++class Solution {public: int flip(int mask, int pos) { · 1 minLeet Code Daily 2024-04-29
$ head -n 12 daily-2024-04-29.md
* [Description](https://leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k) ## Solution * We need to flip each bit at most once.* So the answer is the number of `1` bits in $\oplus_{i=0}^{n - 1} \text{nums}_i \oplus k$. ## Code ```c++class Solution {public: · 1 minLeet Code Daily 2024-04-28
$ head -n 12 daily-2024-04-28.md
* [Description](https://leetcode.com/problems/sum-of-distances-in-tree/description) ## Solution Let's take node `0` as the root and form the graph as a tree. By DFS, we can get: * $C(i) = \text{set of children of node } i$* $P(i) = \text{parent of node } i$ If we define: · 3 minLeet Code Daily 2024-04-27
$ head -n 12 daily-2024-04-27.md
* [Description](https://leetcode.com/problems/freedom-trail) ## Solution First, let's say: * $M = len(\text{ring})$* $N = len(\text{key})$ ### DP If we define: · 2 minLeet Code Weekly Contest 297
$ head -n 12 weekly-contest-297.md
## 1. Calculate Amount Paid in Taxes * Simulation. ```c++class Solution {public: double calculateTax(vector<vector<int>>& brackets, int income) { double ans = 0; for (int i = brackets.size() - 1; i > 0; i--) { brackets[i][0] -= brackets[i - 1][0]; } · 3 minLeet Code Weekly Contest 296
$ head -n 12 weekly-contest-296.md
## 1. Min Max Game * Simulation. ```c++class Solution {public: int minMaxGame(vector<int>& nums) { while (nums.size() > 1) { vector<int> tmp; for (int i = 0, j = 0; i + 1 < nums.size(); i += 2, j = 1 - j) { tmp.push_back(j == 0 ? min(nums[i], nums[i + 1]) : max(nums[i], nums[i + 1])); · 2 minLeet Code Weekly Contest 295
$ head -n 12 weekly-contest-295.md
## 1. Rearrange Characters to Make Target String * Hash-map. ```c++class Solution {public: int rearrangeCharacters(string s, string target) { unordered_map<char, int> cnt, tgt; for (auto &c : s) { cnt[c]++; } · 4 minLeet Code Weekly Contest 294
$ head -n 12 weekly-contest-294.md
## 1. Percentage of Letter in String * Naive. ```c++class Solution {public: int percentageLetter(string s, char letter) { int cnt = 0, len = s.length(); for (auto &c : s) { if (c == letter) { cnt++; · 4 minLeet Code Weekly Contest 293
$ head -n 12 weekly-contest-293.md
## 1. Find Resultant Array After Removing Anagrams * Bruce-force. ```c++class Solution {public: vector<string> removeAnagrams(vector<string>& words) { vector<string> ans; ans.push_back(words.front()); for (int i = 1; i < words.size(); i++) { string x = ans.back(), y = words[i]; · 3 minLeet Code Weekly Contest 292
$ head -n 12 weekly-contest-292.md
## 1. Largest 3-Same-Digit Number in String * Naive. ```c++class Solution {public: string largestGoodInteger(string num) { string ans = ""; for (int i = 0; i + 2 < num.length(); i++) { string s = num.substr(i, 3); if (s[0] == s[1] && s[1] == s[2] && s > ans) { · 4 minLeet Code Weekly Contest 291
$ head -n 12 weekly-contest-291.md
## 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++) { · 2 minLeet Code Weekly Contest 290
$ head -n 12 weekly-contest-290.md
## 1. Intersection of Multiple Arrays * Take a **hash-map** to count the frequency of numbers.* The numbers with frequency $n$ should be part of the answer. ```c++class Solution {public: vector<int> intersection(vector<vector<int>>& nums) { int n = nums.size(); unordered_map<int, int> cnt; for (auto &row : nums) { · 3 minLeet Code Weekly Contest 289
$ head -n 12 weekly-contest-289.md
## 1. Calculate Digit Sum of a String * Simulation, naive :) ```c++class Solution {public: string digitSum(string s, int k) { while (s.length() > k) { string t; for (int i = 0; i < s.length(); i += k) { int sum = 0; · 5 minLeet Code Weekly Contest 288
$ head -n 12 weekly-contest-288.md
## 1. Largest Number After Digit Swaps by Parity * Well, just sort :) ```c++class Solution {public: int largestInteger(int num) { string digits = to_string(num); for (int i = 0; i < digits.length(); i++) { for (int j = i + 1; j < digits.length(); j++) { int x = digits[i] - '0', y = digits[j] - '0'; · 4 minLeet Code Weekly Contest 287
$ head -n 12 weekly-contest-287.md
## 1. Minimum Number of Operations to Convert Time * Greedy. ```c++class Solution {public: int toMins(string time) { int h = 10 * (time[0] - '0') + (time[1] - '0'); int m = 10 * (time[3] - '0') + (time[4] - '0'); return h * 60 + m; } · 3 minLeet Code Weekly Contest 286
$ head -n 12 weekly-contest-286.md
## 1. Find the Difference of Two Arrays * Hash-set. ```c++class Solution {public: vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> set1(nums1.begin(), nums1.end()), set2(nums2.begin(), nums2.end()); vector<vector<int>> ans(2); for (auto &x : set1) { · 3 minLeet Code Weekly Contest 284
$ head -n 12 weekly-contest-284.md
## 1. Find All K-Distant Indices in an Array * $O(n^2)$ brute-force is enough. ```c++class Solution{public: vector<int> findKDistantIndices(vector<int>& nums, int key, int k) { int n = nums.size(); · 4 minLeet Code Weekly Contest 283
$ head -n 12 weekly-contest-283.md
## 1. Cells in a Range on an Excel Sheet * Brute-force. ```c++class Solution{public: vector<string> cellsInRange(string s) { vector<string> ans; · 3 minLeet Code Weekly Contest 282
$ head -n 12 weekly-contest-282.md
## 1. Counting Words With a Given Prefix * Brute-force. ```c++class Solution{public: bool valid(string &word, string &ref) { if (ref.length() > word.length()) · 3 minLeet Code Weekly Contest 281
$ head -n 12 weekly-contest-281.md
## 1. Count Integers With Even Digit Sum * Naive. ```c++class Solution{public: int digitSum(int x) { int ret = 0; · 4 minLeet Code Weekly Contest 280
$ head -n 12 weekly-contest-280.md
## 1. Count Operations to Obtain Zero * Naive. ```c++class Solution{public: int countOperations(int num1, int num2) { int ans = 0; · 5 minLeet Code Weekly Contest 279
$ head -n 12 weekly-contest-279.md
## 1. Sort Even and Odd Indices Independently Naive. ```c++class Solution{public: vector<int> sortEvenOdd(vector<int>& nums) { vector<int> a, b; · 4 minLeet Code Weekly Contest 278
$ head -n 12 weekly-contest-278.md
## 1. Keep Multiplying Found Values by Two * Hash. ```c++class Solution{public: int findFinalValue(vector<int>& nums, int original) { unordered_set<int> s(nums.begin(), nums.end()); · 3 minLeet Code Weekly Contest 277
$ head -n 12 weekly-contest-277.md
## 1. Count Elements With Strictly Smaller and Greater Elements Count the numbers which are strictly greater than the minimum and strictly lower than the maximum. ```c++class Solution{public: int countElements(vector<int>& nums) { int a = INT_MAX, b = INT_MIN; · 3 minLeet Code Weekly Contest 276
$ head -n 12 weekly-contest-276.md
## 1. Divide a String Into Groups of Size k * Naive. ```c++class Solution{public: vector<string> divideString(string s, int k, char fill) { vector<string> ans = { "" }; · 2 minLeet Code Weekly Contest 275
$ head -n 12 weekly-contest-275.md
## 1. Check if Every Row and Column Contains All Numbers * Naive ```c++class Solution{public: bool valid(set<int> &s, int n) { for (int i = 1; i <= n; i++) { · 3 minLeet Code Weekly Contest 274
$ head -n 12 weekly-contest-274.md
## 1. Check if All A's Appears Before All B's It can be done in $O(n)$ time. But to save time, $O(n^2)$ brute-force is enough :). ```c++class Solution{public: bool checkString(string s) { for (int i = 0; i < s.length(); i++) { · 4 minLeet Code Weekly Contest 273
$ head -n 12 weekly-contest-273.md
## 1. A Number After a Double Reversal Naive. ```c++class Solution{public: int rev(int x) { int ret = 0; · 4 minLeet Code Weekly Contest 272
$ head -n 12 weekly-contest-272.md
## 1. Find First Palindromic String in the Array Palindromic string detection, naive :) ```c++class Solution{public: bool valid(string &s) { for (int i = 0, j = s.length() - 1; i < j; i++, j--) · 3 minLeet Code Weekly Contest 271
$ head -n 12 weekly-contest-271.md
## 1. Rings and Rods Native. ```c++class Solution {public: int countPoints(string rings) { bool has[10][3]; for (int i = 0; i < 10; i++) · 4 minLeet Code Weekly Contest 270
$ head -n 12 weekly-contest-270.md
## 1. Finding 3-Digit Even Numbers Brute-force. ```c++class Solution {public: vector<int> findEvenNumbers(vector<int>& digits) { unordered_set<int> ret; for (int i = 0; i < digits.size(); i++) · 3 minLeet Code Weekly Contest 269
$ head -n 12 weekly-contest-269.md
## 1. Find Target Indices After Sorting Array Naive, sort and linear-scan. ```c++class Solution{public: vector<int> targetIndices(vector<int>& nums, int target) { sort(nums.begin(), nums.end()); · 3 minLeet Code Weekly Contest 268
$ head -n 12 weekly-contest-268.md
## 1. Two Furthest Houses With Different Colors Simply enumerate all pairs of indexes. $O(n^2)$. ```c++class Solution {public: int maxDistance(vector<int>& colors) { int ans = INT_MIN; for (int i = 0; i < colors.size(); i++) · 5 minLeet Code Weekly Contest 267
$ head -n 12 weekly-contest-267.md
## 1. Time Needed to Buy Tickets Simulate the process as descriped. ```c++class Solution{public: int timeRequiredToBuy(vector<int>& tickets, int k) { queue<pair<int, int>> q; · 3 minLeet Code Weekly Contest 266
$ head -n 12 weekly-contest-266.md
## 1. Count Vowel Substrings of a String Brute-force, time complexity: $O(n^2)$ ```c++class Solution {public: bool valid(string s) { bool has[5]; · 4 minLeet Code Weekly Contest 265
$ head -n 12 weekly-contest-265.md
## 1. Smallest Index With Equal Value $O(n)$ linear scan. ```c++class Solution{public: int smallestEqual(vector<int>& nums) { for (int i = 0; i < nums.size(); i++) { · 6 minLeet Code Weekly Contest 264
$ head -n 12 weekly-contest-264.md
## 1. Number of Valid Words in a Sentence Naive, simple string processing. ```c++inline vector<string> split(string s, char delimiter = ' '){ auto words = vector<string>({ "" }); for (auto &c : s) { if (c == delimiter) · 4 minLeet Code Weekly Contest 263
$ head -n 12 weekly-contest-263.md
## 1. Check if Numbers Are Ascending in a Sentence Naive. Simple string processing. $O(n)$. ```c++class Solution{public: bool isNum(string &s) { for (int i = 0; i < s.length(); i++) { · 4 minLeet Code Weekly Contest 262
$ head -n 12 weekly-contest-262.md
## 1. [Two Out of Three](https://leetcode.com/contest/weekly-contest-262/problems/two-out-of-three/) Naive. Just do it as described. ```c++class Solution {public: vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) { set<int> any, s[3]; for (int i = 0; i < nums1.size(); i++) · 4 minLeet Code Weekly Contest 261
$ head -n 12 weekly-contest-261.md
## 1. [Minimum Moves to Convert String](https://leetcode.com/contest/weekly-contest-261/problems/minimum-moves-to-convert-string/) Simple DP. $$F_i = \begin{cases} F_{i + 1} & S_i = 'O' \\ F_{i + 3} + 1 & S_i = '1' \land i + 3 <= n \\ 1 & S_i = '1' \land i + 3 > n \\\end{cases}$$ · 5 minLeet Code Weekly Contest 260
$ head -n 12 weekly-contest-260.md
## 1. Maximum Difference Between Increasing Elements Linear scan. Time complexity: $O(n)$. ```c++class Solution {public: int maximumDifference(vector<int>& nums) { int ans = -1, n = nums.size(); · 4 minLeet Code Weekly Contest 191
$ head -n 12 weekly-contest-191.md
## 1. Maximum Product of Two Elements in an Array Naive. ```c++class Solution {public: int maxProduct(vector<int>& nums) { int ans = 0; for (int i = 0; i < nums.size(); i++) · 2 minLeet Code Weekly Contest 190
$ head -n 12 weekly-contest-190.md
## 1. Check If a Word Occurs As a Prefix of Any Word in a Sentence Naive. ```javascriptvar isPrefixOfWord = function(sentence, searchWord) { return sentence .split(' ') .reduce((ans, word, idx) => (ans == -1 && word.startsWith(searchWord)) ? (idx + 1) : ans , -1); · 2 minLeet Code Weekly Contest 189
$ head -n 12 weekly-contest-189.md
## 1. Number of Students Doing Homework at a Given Time No need to talk. ```javascriptvar busyStudent = function(startTime, endTime, queryTime) { return startTime.filter((idx, s) => s <= queryTime && queryTime <= endTime[idx]).length;};``` · 2 minLeet Code Weekly Contest 188
$ head -n 12 weekly-contest-188.md
## 1. Build an Array With Stack Operations Stack is a FIFO container. Pop all useless elements before pushing the nextelement. ```c++class Solution {public: vector<string> buildArray(vector<int>& target, int n) { bool useful[101]; · 4 minLeet Code Weekly Contest 187
$ head -n 12 weekly-contest-187.md
## 1. Destination City Let's say, * $S = \{ x \mid (x, y) \text{ is a path} \}$* $T = \{ y \mid (x, y) \text{ is a path} \}$ The answer is $T - S$. ```c++class Solution {public: · 3 minLeet Code Weekly Contest 186
$ head -n 12 weekly-contest-186.md
## 1. Maximum Score After Splitting a String Preffix-sum. ```c++class Solution {public: int maxScore(string s) { int s0[501],s1[501]; s0[0] = s1[0] = 0; · 3 minLeet Code Weekly Contest 185
$ head -n 12 weekly-contest-185.md
## 1. Reformat The String Divide the characters into two groups: `digits` and `letters`, just try`digits` first or `letters` first. ```c++class Solution {public: string reformat(string s) { string s1 = ""; string s2 = ""; · 5 minLeet Code Weekly Contest 184
$ head -n 12 weekly-contest-184.md
## 1. [String Matching in an Array](https://leetcode.com/contest/weekly-contest-184/problems/string-matching-in-an-array/) KMP. ```c++class kmp { public: static vector<int> pmt_of(string p) { vector<int> pmt = { -1 }; · 4 minLeet Code Weekly Contest 183
$ head -n 12 weekly-contest-183.md
## 1. [Minimum Subsequence in Non-Increasing Order](https://leetcode.com/contest/weekly-contest-183/problems/minimum-subsequence-in-non-increasing-order/) Just sort. ```c++#include<vector> using namespace std; template<typename T>class quick_sort { · 8 minLeet Code Weekly Contest 182
$ head -n 12 weekly-contest-182.md
## 1. [Find Lucky Integer in an Array](https://leetcode.com/contest/weekly-contest-182/problems/find-lucky-integer-in-an-array/) $O(n)$, just show the code. ```c++class Solution {public: int findLucky(vector<int>& arr) { int f[501]; for (int i = 1; i <= 500; i++) { · 7 minLeet Code Weekly Contest 181
$ head -n 12 weekly-contest-181.md
## 1. [Create Target Array in the Given Order](https://leetcode.com/contest/weekly-contest-181/problems/create-target-array-in-the-given-order/) Naive, no need to talk. ```c++int* createTargetArray(int* nums, int numsSize, int* index, int indexSize, int* returnSize){ int i, j, *ret = (int*) malloc(sizeof(int) * numsSize); for (i = 0; i < numsSize && i < indexSize; i++) { for (j = i; j > index[i]; j--) { · 3 minLeet Code Weekly Contest 180
$ head -n 12 weekly-contest-180.md
## 1. [Lucky Numbers in a Matrix](https://leetcode.com/contest/weekly-contest-180/problems/lucky-numbers-in-a-matrix/) Saddle point of a matrix. ```c++int* luckyNumbers (int** matrix, int matrixSize, int* matrixColSize, int* returnSize){ int *min = (int*) malloc(sizeof(int) * matrixSize); int *max = (int*) malloc(sizeof(int) * matrixColSize[0]); int i, j, k, *ret; · 5 minLeet Code Weekly Contest 179
$ head -n 12 weekly-contest-179.md
## 1. [Generate a String With Characters That Have Odd Counts](https://leetcode.com/contest/weekly-contest-179/problems/generate-a-string-with-characters-that-have-odd-counts/) One simple solution could be: $$\text{answer} = \begin{cases}\underbrace{\text{a}, \dots, \text{a}}_n, \text{b} & n \mod 2 = 0 \\\underbrace{\text{a}, \dots, \text{a}}_n & n \mod 2 = 1 \\ \end{cases}$$ · 4 minLeet Code Weekly Contest 178
$ head -n 12 weekly-contest-178.md
## 1. [How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/contest/weekly-contest-178/problems/how-many-numbers-are-smaller-than-the-current-number/) Brute force. ```c++class Solution {public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { vector<int> result; for (int i = 0; i < nums.size(); i++) { · 5 minLeet Code Weekly Contest 177
$ head -n 12 weekly-contest-177.md
## 1. Number of Days Between Two Dates To save time, I use javascript for this problem. ```javascriptvar daysBetweenDates = function(date1, date2) { return Math.abs(Math.floor((Date.parse(date2) - Date.parse(date1)) / 86400000));};``` ## 2. Validate Binary Tree Nodes · 4 minSPOJ 4061 - MPOLY - Polygon
$ head -n 12 4061.md
There are N points in a plane whose coordinates are natural numbers. A convex polygon with maximal number of vertices is a convex polygon whose vertices are some of given points and the origin having maximal possible number of vertices. Origin, i.e. point with coordinates (0,0), must be one of vertices of a convex polygon with maximal number of vertices. Write a program that will determine number of vertices in such polygon. A polygon is convex if every line segment whose endpoints are inside that polygon is also completely inside it. Consecutive edges of a polygon must not be parallel. ## Input The first line of input file contains a natural number N, 2 ≤ N ≤ 100, a number of given points. Each of the following N lines contains two natural numbers X and Y, 1 ≤ X ≤ 100, 1 ≤ Y ≤ 100, separated by a space character, coordinates of one point. · 4 minSPOJ 34409 - DISGRAPH - Disconnected country
$ head -n 12 34409.md
Cities of the ancient country GRAPH connected by two-way (bidirectional) roads so that from any city you can get to any other city. The Sultan wants to destroy some roads and divide the country into two separate (disconnected) areas, so that from the city of one area it was impossible to get to the city of another area. You are the Minister of transportation and your task is to minimize the number of roads that need to be destroyed. You have a lot of time and the Sultan hopes :-) that you will solve this task. ## Input The first line of input will contain one integer number 8 ≤ N ≤ 1400, number of cities in GRAPH. Follow N lines. Each line represents cities (direct neighbors) connected to the city number i (cities numbering is zero based) by one road. There will be 7 input files. ## Output One integer number. The minimum number of roads that need to be destroyed. ## Example · 4 minSPOJ 5732 - PARADOX - Paradox
$ head -n 12 5732.md
A paradox is a statement or group of statements that leads to a contradiction. Consider the following two statements. * "The statement below is false."* "The statement above is true." If we assume that 1st statement is true then according to 1st statement the 2nd statement is false. Since the 2nd statement is false and it says that 1st statement is true, 1st statement must be false which is a contradiction. If we assume that 1st statement is false then the 2nd statement must be true (since 1st statement says 2nd statement is false). Then according to 2nd statement 1st statement must be true which is a contradiction. Thus the statement cannot be classified as true or false. This is a paradox. Given N statements of the form "Statement X is true/false", 1<=X<=N, your task is to find if these set of statements are paradoxical or not. A set of statements is paradoxical if one or more statements cannot be classified as true or false. ## Input Multiple test cases. Each test cases begins with an integer N representing the number of statements. **1<=N<=100**. Then follow N lines, representing N statements in the order 1,2,3, ...,N. Each line contains an integer X and a string S separated by a space. **1<=X<=N. S is either "true" or "false"** (quotes for clarity), which means the statement is "Statement X is S". N=0 indicates the end of testcases and should not be processed. There are atmost 10000 testcases. · 4 minSPOJ 10394 - ABA12C - Buying Apples!
$ head -n 12 10394.md
Harish went to a supermarket to buy exactly ‘k’ kilograms apples for his ‘n’ friends. The supermarket was really weird. The pricing of items was very different. He went to the Apples section and enquired about the prices. The salesman gave him a card in which he found that the prices of apples were not per kg. The apples were packed into covers, each containing ‘x’ kg of apples, x > 0 and ‘x’ is an integer. An ‘x’ kg packet would be valued at ‘y’ rupees. So, the placard contained a table with an entry ‘y’ denoting the price of an ‘x’ kg packet. If ‘y’ is -1 it means that the corresponding packet is not available. Now as apples are available only in packets, he decides to buy atmost ‘n’ packets for his ‘n’ friends i.e he will not buy more than n packets of apples. Harish likes his friends a lot and so he does not want to disappoint his friends. So now, he will tell you how many friends he has and you have to tell him the minimum amount of money he has to spend for his friends. ## Input The first line of input will contain the number of test cases, C. Each test case will contain two lines. The first line containing N and K, the number of friends he has and the amount of Apples in kilograms which he should buy. · 4 minSPOJ 5 - PALIN - The Next Palindrome
$ head -n 12 5.md
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros. ## Input The first line contains integer t, the number of test cases. Integers K are given in the next t lines. ## Output For each K, output the smallest palindrome larger than K. ## Example · 2 minSPOJ 4 - ONP - Transform the Expression
$ head -n 12 4.md
Transform the algebraic expression with brackets into RPN form (Reverse Polish Notation). Two-argument operators: +, -, \*, /, ^ (priority from the lowest to the highest), brackets ( ). Operands: only letters: a,b,...,z. Assume that there is only one RPN form (no expressions like a\*b\*c). ## Input ```t [the number of expressions <= 100]expression [length <= 400][other expressions]``` Text grouped in [ ] does not appear in the input file. · 4 minSPOJ 2 - PRIME1 - Prime Generator
$ head -n 12 2.md
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! ## Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space. ## Output For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line. ## Example · 4 minSPOJ 1 - TEST - Life, the Universe, and Everything
$ head -n 12 1.md
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits. ## Example ```textInput:12884299 · 1 minNo matches. Try a shorter filename.
end of directory listing. ▉