updates
This commit is contained in:
@@ -53,7 +53,7 @@ int bsearch(int n, std::vector<int> arr)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
std::ifstream input(argc > 1 ? argv[1] : "bsearch.txt");
|
||||
std::ifstream input(argc > 1 ? argv[1] : "nums.txt");
|
||||
std::vector<int> arr;
|
||||
|
||||
for (std::string line; std::getline(input, line);
|
||||
|
||||
@@ -48,32 +48,14 @@ bool isAnagram(string s, string t)
|
||||
{
|
||||
if (s.size() != t.size())
|
||||
return false;
|
||||
int table[26] = {0};
|
||||
array<int, 26> table{};
|
||||
for (size_t i = 0; i < s.size(); ++i)
|
||||
{
|
||||
table[s[i] - 'a'] += 1;
|
||||
table[t[i] - 'a'] -= 1;
|
||||
}
|
||||
|
||||
return std::all_of(table, table + 26, [](int i) { return i == 0; });
|
||||
}
|
||||
#+end_src
|
||||
** DONE (#1) Two sum
|
||||
Given a target find two elements which sum to it.
|
||||
#+begin_src c++ :tangle 1.cpp :comments link
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
vector<int> twoSum(vector<int> &nums, int target)
|
||||
{
|
||||
unordered_map<int, int> diffs;
|
||||
for (int i = 0; i < nums.size(); ++i)
|
||||
{
|
||||
if (diffs.find(target - nums[i]) != diffs.end())
|
||||
return {diffs[target-nums[i]], i};
|
||||
else
|
||||
diffs[nums[i]] = i;
|
||||
}
|
||||
return {};
|
||||
return std::all_of(begin(table), end(table), [](int i) { return i == 0; });
|
||||
}
|
||||
#+end_src
|
||||
** DONE (#49) Group anagrams
|
||||
@@ -233,7 +215,6 @@ bool isValidSudoku(vector<vector<char>> &board)
|
||||
}
|
||||
#+end_src
|
||||
** DONE (#128) Longest consecutive sequence
|
||||
|
||||
#+begin_src c++ :tangle 128.cpp :comments link
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
@@ -244,12 +225,16 @@ int longestConsecutive(vector<int> &nums)
|
||||
size_t size = 0;
|
||||
for (auto num : nums)
|
||||
{
|
||||
// Skip all considered numbers (i.e. numbers already part of some
|
||||
// consecutive sequence)
|
||||
if (s.find(num - 1) != s.end())
|
||||
continue;
|
||||
|
||||
// Compute the size of the consecutive sequence
|
||||
size_t seq = 1;
|
||||
for (size_t x = num; s.find(x + 1) != s.end(); ++x, ++seq)
|
||||
continue;
|
||||
size = size < seq ? seq : size;
|
||||
size = max(size, seq);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
@@ -319,8 +304,150 @@ int maxSubArray(vector<int> &nums)
|
||||
return max_sum;
|
||||
}
|
||||
#+end_src
|
||||
* Two pointers
|
||||
** DONE (#1) Two sum
|
||||
Given a target find two elements which sum to it.
|
||||
#+begin_src c++ :tangle 1.cpp :comments link
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
vector<int> twoSum(vector<int> &nums, int target)
|
||||
{
|
||||
unordered_map<int, int> diffs;
|
||||
for (int i = 0; i < nums.size(); ++i)
|
||||
{
|
||||
if (diffs.find(target - nums[i]) != diffs.end())
|
||||
return {diffs[target-nums[i]], i};
|
||||
else
|
||||
diffs[nums[i]] = i;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
#+end_src
|
||||
** DONE (#167) Two Sum II
|
||||
Given an input array of numbers, already sorted, find the two numbers
|
||||
that add up to a target number. Cannot use the same element twice and
|
||||
cannot use additional space.
|
||||
|
||||
#+begin_src c++ :tangle 167.cpp :comments link
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<int> twoSum(vector<int> &numbers, int target)
|
||||
{
|
||||
int start = 0, end = numbers.size() - 1;
|
||||
for (int total = numbers[start] + numbers[end]; total != target;
|
||||
total = numbers[start] + numbers[end])
|
||||
{
|
||||
if (total < target)
|
||||
{
|
||||
++start;
|
||||
}
|
||||
else
|
||||
{
|
||||
--end;
|
||||
}
|
||||
}
|
||||
return {start + 1, end + 1};
|
||||
}
|
||||
#+end_src
|
||||
** DONE (#15) 3Sum
|
||||
Given an array of numbers, return all triples which add up to 0. No
|
||||
duplicates allowed. O(n^2) time and O(1) space.
|
||||
|
||||
If we select /every/ item as the first member of the triple, the rest
|
||||
of the array can be treated as a Two Sum problem. Looking at Two Sum
|
||||
II, we can do Two Sum in O(nlog(n)) time.
|
||||
|
||||
#+begin_src c++ :tangle 15.cpp :comments link
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<vector<int>> threeSum(vector<int> &nums)
|
||||
{
|
||||
sort(begin(nums), end(nums));
|
||||
vector<vector<int>> result;
|
||||
|
||||
for (int i = 0; i < nums.size(); ++i)
|
||||
{
|
||||
// Ensure we don't have any duplicate trips
|
||||
if (i > 0 && nums[i] == nums[i - 1])
|
||||
continue;
|
||||
|
||||
// sorted twosum
|
||||
int target = 0 - nums[i];
|
||||
int start = i + 1;
|
||||
int end = nums.size() - 1;
|
||||
for (int total = nums[start] + nums[end]; start < end;
|
||||
total = nums[start] + nums[end];)
|
||||
{
|
||||
if (total < target)
|
||||
{
|
||||
++start;
|
||||
}
|
||||
else if (total > target)
|
||||
{
|
||||
--end;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.push_back({nums[i], nums[start], nums[end]});
|
||||
for (++start; nums[start] == nums[start - 1] && start < end; ++start)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#+end_src
|
||||
** TODO (#11) Container with most water
|
||||
Given an array of heights of unit width bars, find the highest volume
|
||||
of water that can be contained by choosing any two bars.
|
||||
|
||||
Given two heights (h1, h2) at indices (i1, i2), the volume of water
|
||||
that may be contained by them is (i2 - i1) * min(h1 ,h2). Maximise
|
||||
this quantity.
|
||||
|
||||
#+begin_src c++ :tangle 11.cpp :comments link
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int volume(int i1, int h1, int i2, int h2)
|
||||
{
|
||||
return (i2 - i1) * min(h1, h2);
|
||||
}
|
||||
|
||||
int maxArea(vector<int> &height)
|
||||
{
|
||||
int start = 0, end = height.size() - 1;
|
||||
int best_vol = 0;
|
||||
while (start < end)
|
||||
{
|
||||
int h_start = height[start];
|
||||
int h_end = height[end];
|
||||
int vol = volume(start, h_start, end, h_end);
|
||||
best_vol = max(vol, best_vol);
|
||||
|
||||
if (h_start > h_end)
|
||||
{
|
||||
--end;
|
||||
}
|
||||
else
|
||||
{
|
||||
++start;
|
||||
}
|
||||
}
|
||||
|
||||
return best_vol;
|
||||
}
|
||||
#+end_src
|
||||
* Stack
|
||||
** DONE (#20) Valid Parentheses
|
||||
Given a string S, validate whether all braces are closed correctly.
|
||||
|
||||
#+begin_src c++ :tangle 20.cpp :comments link
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
@@ -563,7 +690,7 @@ vector<string> generateParenthesis(int n)
|
||||
return results;
|
||||
}
|
||||
#+end_src
|
||||
** TODO (#739) Daily temperatures
|
||||
** DONE (#739) Daily temperatures
|
||||
Given an array of temperatures t[i], generate an array /a/ where a[i]
|
||||
is the number of days it takes to surpass t[i]. If there is no
|
||||
temperature which surpasses t[i], a[i] = 0.
|
||||
@@ -591,7 +718,7 @@ vector<int> dailyTemperatures(vector<int> &temperatures)
|
||||
}
|
||||
#+end_src
|
||||
* Linked Lists
|
||||
** TODO (#2) Add two numbers
|
||||
** DONE (#2) Add two numbers
|
||||
Given two linked lists representing the digits of a number in reverse
|
||||
order, return their sum.
|
||||
#+begin_src c++ :tangle 2.cpp :comments link
|
||||
@@ -622,6 +749,16 @@ struct ListNode
|
||||
}
|
||||
};
|
||||
|
||||
inline int get_value(ListNode *node)
|
||||
{
|
||||
return node ? node->val : 0;
|
||||
}
|
||||
|
||||
inline ListNode *next(ListNode *node)
|
||||
{
|
||||
return node ? node->next : nullptr;
|
||||
}
|
||||
|
||||
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
|
||||
{
|
||||
if (!(l1 && l2) ||
|
||||
@@ -629,43 +766,31 @@ ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
|
||||
return l1;
|
||||
|
||||
int carry = 0;
|
||||
int sum = l1->val + l2->val;
|
||||
int sum = 0;
|
||||
|
||||
ListNode *root = new ListNode(sum % 10);
|
||||
ListNode *root = nullptr;
|
||||
ListNode *end = root;
|
||||
|
||||
carry = sum / 10;
|
||||
l1 = l1->next;
|
||||
l2 = l2->next;
|
||||
|
||||
for (; l1 && l2; l1 = l1->next, l2 = l2->next)
|
||||
while (l1 || l2 || carry)
|
||||
{
|
||||
sum = l1->val + l2->val + carry;
|
||||
sum = get_value(l1) + get_value(l2) + carry;
|
||||
carry = sum / 10;
|
||||
sum %= 10;
|
||||
|
||||
auto node = new ListNode(sum, nullptr);
|
||||
end->next = node;
|
||||
end = node;
|
||||
}
|
||||
|
||||
if (l1 || l2)
|
||||
{
|
||||
auto remaining = l1 ? l1 : l2;
|
||||
for (; remaining; remaining = remaining->next)
|
||||
if (!end)
|
||||
{
|
||||
remaining->val += carry;
|
||||
carry = remaining->val / 10;
|
||||
remaining->val %= 10;
|
||||
end->next = remaining;
|
||||
end = remaining;
|
||||
root = node;
|
||||
end = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
end->next = node;
|
||||
end = node;
|
||||
}
|
||||
}
|
||||
|
||||
if (carry != 0)
|
||||
{
|
||||
auto node = new ListNode(carry, nullptr);
|
||||
end->next = node;
|
||||
l1 = next(l1);
|
||||
l2 = next(l2);
|
||||
}
|
||||
|
||||
return root;
|
||||
|
||||
Reference in New Issue
Block a user