Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

surrounded-region.py to surrounded-regions.py #1

Open
wants to merge 3,232 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3232 commits
Select commit Hold shift + click to select a range
b3de78c
Update coin-change.cpp
megazone87 Oct 25, 2016
b3b1b74
Merge pull request #34 from songmeixu/patch-1
kamyu104 Oct 25, 2016
e7951ff
Create find-all-duplicates-in-an-array.cpp
kamyu104 Oct 25, 2016
81a7ef6
Create find-all-duplicates-in-an-array.py
kamyu104 Oct 25, 2016
1acac9e
Update README.md
kamyu104 Oct 25, 2016
83a060d
Update find-all-duplicates-in-an-array.cpp
kamyu104 Oct 25, 2016
cc40d17
Create non-overlapping-intervals.cpp
kamyu104 Oct 31, 2016
98da66f
Create non-overlapping-intervals.py
kamyu104 Oct 31, 2016
7c521fe
Create find-right-interval.py
kamyu104 Oct 31, 2016
b335c53
Create find-right-interval.cpp
kamyu104 Oct 31, 2016
a2d4032
Create arranging-coins.cpp
kamyu104 Oct 31, 2016
88b1c97
Create arranging-coins.py
kamyu104 Oct 31, 2016
d3e0fad
Update arranging-coins.py
kamyu104 Oct 31, 2016
597d760
Update arranging-coins.cpp
kamyu104 Oct 31, 2016
deb2fd7
Create sequence-reconstruction.py
kamyu104 Oct 31, 2016
0c086a7
Update sequence-reconstruction.py
kamyu104 Nov 5, 2016
728153d
Create sequence-reconstruction.cpp
kamyu104 Nov 5, 2016
616e690
Update sequence-reconstruction.py
kamyu104 Nov 5, 2016
e698759
Create add-two-numbers-ii.cpp
kamyu104 Nov 5, 2016
ebb710c
Create add-two-numbers-ii.py
kamyu104 Nov 5, 2016
2a8f063
Create find-all-numbers-disappeared-in-an-array.cpp
kamyu104 Nov 5, 2016
43cc703
Create find-all-numbers-disappeared-in-an-array.
kamyu104 Nov 5, 2016
ca25fd2
Create serialize-and-deserialize-bst.py
kamyu104 Nov 5, 2016
391eaaf
Create sort-characters-by-frequency.py
kamyu104 Nov 5, 2016
0381a9b
Create delete-node-in-a-bst.cpp
kamyu104 Nov 5, 2016
f1e5cc6
Update sort-characters-by-frequency.py
kamyu104 Nov 5, 2016
51999c2
Create sort-characters-by-frequency.cpp
kamyu104 Nov 5, 2016
d6bcecf
Create delete-node-in-a-bst.py
kamyu104 Nov 5, 2016
8b4354c
Create serialize-and-deserialize-bst.cpp
kamyu104 Nov 5, 2016
429231d
Update serialize-and-deserialize-bst.py
kamyu104 Nov 5, 2016
a2f278f
Update serialize-and-deserialize-bst.cpp
kamyu104 Nov 5, 2016
93e4b48
Update README.md
kamyu104 Nov 6, 2016
858c55d
Create minimum-moves-to-equal-array-elements.cpp
kamyu104 Nov 6, 2016
35f9817
Create minimum-moves-to-equal-array-elements.py
kamyu104 Nov 6, 2016
6732efa
Create number-of-boomerangs.cpp
kamyu104 Nov 6, 2016
d6e3597
Update number-of-boomerangs.cpp
kamyu104 Nov 6, 2016
5333371
Create number-of-boomerangs.py
kamyu104 Nov 6, 2016
681b069
Create minimum-number-of-arrows-to-burst-balloons.cpp
kamyu104 Nov 6, 2016
95f0517
Create minimum-number-of-arrows-to-burst-balloons.py
kamyu104 Nov 6, 2016
1e74b6f
Create arithmetic-slices-ii-subsequence.py
kamyu104 Nov 6, 2016
137f58f
Create arithmetic-slices-ii-subsequence.cpp
kamyu104 Nov 6, 2016
43d20a2
Update arithmetic-slices-ii-subsequence.py
kamyu104 Nov 6, 2016
586aabb
Update arithmetic-slices-ii-subsequence.cpp
kamyu104 Nov 6, 2016
a3e2b85
Update arithmetic-slices-ii-subsequence.cpp
kamyu104 Nov 12, 2016
b7480da
Fixes incorrect link.
bhainesva Nov 16, 2016
2187a3f
Merge pull request #36 from bhainesva/patch-1
kamyu104 Nov 16, 2016
289d116
Updates difficulty levels to match LeetCode
bhainesva Nov 17, 2016
978273a
Merge pull request #38 from bhainesva/patch-1
kamyu104 Nov 17, 2016
2851855
Merge pull request #25 from xiaopeng163/master
kamyu104 Nov 17, 2016
d608aa1
Update alien-dictionary.cpp
kamyu104 Nov 26, 2016
b9ebbc9
Update alien-dictionary.py
kamyu104 Nov 26, 2016
e767055
Create lfu-cache.cpp
kamyu104 Dec 1, 2016
7cf265b
Update lfu-cache.cpp
kamyu104 Dec 1, 2016
bb85209
Update lfu-cache.cpp
kamyu104 Dec 1, 2016
853d641
Update lfu-cache.cpp
kamyu104 Dec 1, 2016
c4a7831
Fix variable typo
wilbeibi Dec 2, 2016
9a9d407
Merge pull request #40 from Wilbeibi/patch-1
kamyu104 Dec 5, 2016
73213f2
fix typo in anagrams.py
daniil-sharou Dec 5, 2016
dddda0b
Merge pull request #41 from daniil-sharou/patch-1
kamyu104 Dec 17, 2016
31b98cb
Create number-of-segments-in-a-string.cpp
kamyu104 Dec 17, 2016
4b95c81
Create number-of-segments-in-a-string.py
kamyu104 Dec 17, 2016
97f67a2
Create 4sum-ii.py
kamyu104 Dec 17, 2016
f2b397c
Create 4sum-ii.cpp
kamyu104 Dec 17, 2016
dec433d
Create assign-cookies.py
kamyu104 Dec 17, 2016
78c0329
Create assign-cookies.cpp
kamyu104 Dec 17, 2016
0ce66ae
Create 132-pattern.cpp
kamyu104 Dec 17, 2016
8b90bd0
Create 132-pattern.py
kamyu104 Dec 17, 2016
0c0d869
Create repeated-substring-pattern.py
kamyu104 Dec 17, 2016
112818b
Create repeated-substring-pattern.cpp
kamyu104 Dec 17, 2016
b59cfd8
Create minimum-moves-to-equal-array-elements-ii.py
kamyu104 Dec 17, 2016
55cab67
Create minimum-moves-to-equal-array-elements-ii.cpp
kamyu104 Dec 17, 2016
9f1b4fa
Create island-perimeter.cpp
kamyu104 Dec 17, 2016
e72327a
Create island-perimeter.py
kamyu104 Dec 17, 2016
ed803ef
Create can-i-win.py
kamyu104 Dec 17, 2016
1dc0dcd
Create can-i-win.cpp
kamyu104 Dec 17, 2016
b988f02
Create optimal-account-balancing.cpp
kamyu104 Dec 18, 2016
9d54a63
Update optimal-account-balancing.cpp
kamyu104 Dec 18, 2016
e7c330b
Update optimal-account-balancing.cpp
kamyu104 Dec 18, 2016
c05ddab
Create optimal-account-balancing.py
kamyu104 Dec 18, 2016
44d8512
Update optimal-account-balancing.py
kamyu104 Dec 18, 2016
fc78d2a
Update optimal-account-balancing.cpp
kamyu104 Dec 18, 2016
4f62afa
Update optimal-account-balancing.cpp
kamyu104 Dec 18, 2016
77dafd8
Update optimal-account-balancing.py
kamyu104 Dec 18, 2016
2987bfb
Update shortest-word-distance-iii.py
kamyu104 Dec 19, 2016
fe55698
Update shortest-word-distance-iii.cpp
kamyu104 Dec 19, 2016
e289705
Create hamming-distance.cpp
kamyu104 Dec 20, 2016
f9d8dae
Update hamming-distance.cpp
kamyu104 Dec 20, 2016
9e44778
Create hamming-distance.py
kamyu104 Dec 20, 2016
45482ae
Create count-the-repetitions.cpp
kamyu104 Dec 20, 2016
559735b
Update count-the-repetitions.cpp
kamyu104 Dec 20, 2016
1250747
Create count-the-repetitions.py
kamyu104 Dec 20, 2016
1ea4366
Update count-the-repetitions.cpp
kamyu104 Dec 20, 2016
818e8b0
Update count-the-repetitions.py
kamyu104 Dec 20, 2016
982aa8e
Create unique-substrings-in-wraparound-string.py
kamyu104 Dec 20, 2016
543aa7d
Create unique-substrings-in-wraparound-string.cpp
kamyu104 Dec 20, 2016
a2b7248
Create validate-ip-address.py
kamyu104 Dec 20, 2016
343af7b
Create validate-ip-address.cpp
kamyu104 Dec 20, 2016
93dcfc6
Update validate-ip-address.py
kamyu104 Dec 20, 2016
cce60c4
Create convex-polygon.cpp
kamyu104 Dec 20, 2016
623caf0
Create convex-polygon.py
kamyu104 Dec 20, 2016
70e3b34
Create encode-string-with-shortest-length.cpp
kamyu104 Dec 20, 2016
8782811
Create encode-string-with-shortest-length.py
kamyu104 Dec 20, 2016
93470c2
Create concatenated-words.cpp
kamyu104 Dec 20, 2016
7690f0e
Create concatenated-words.py
kamyu104 Dec 20, 2016
a30210a
Create matchsticks-to-square.cpp
kamyu104 Dec 20, 2016
3c350f9
Update matchsticks-to-square.cpp
kamyu104 Dec 20, 2016
d127187
Update matchsticks-to-square.cpp
kamyu104 Dec 20, 2016
fb9116d
Create matchsticks-to-square.py
kamyu104 Dec 20, 2016
c98b1ce
Create ones-and-zeroes.py
kamyu104 Dec 20, 2016
30a89e6
Create ones-and-zeroes.cpp
kamyu104 Dec 20, 2016
4deef97
Create heaters.py
kamyu104 Dec 20, 2016
d6ba0dd
Create heaters.cpp
kamyu104 Dec 20, 2016
1959a71
Create total-hamming-distance.cpp
kamyu104 Dec 20, 2016
f20adef
Create total-hamming-distance.py
kamyu104 Dec 20, 2016
3adebff
Update total-hamming-distance.cpp
kamyu104 Dec 20, 2016
a4cd7ac
Create poor-pigs.cpp
kamyu104 Dec 21, 2016
bcaeab5
Create poor-pigs.py
kamyu104 Dec 21, 2016
e6a4632
Create circular-array-loop.cpp
kamyu104 Dec 21, 2016
55a8322
Create circular-array-loop.py
kamyu104 Dec 21, 2016
63e3afc
Update circular-array-loop.py
kamyu104 Dec 21, 2016
cb40921
Update README.md
kamyu104 Dec 21, 2016
5a51c3d
Update README.md
kamyu104 Dec 26, 2016
8f79929
Update README.md
kamyu104 Dec 26, 2016
941b572
Update README.md
kamyu104 Dec 26, 2016
5e0d4d0
Update README.md
kamyu104 Dec 26, 2016
a65d8b7
edit 4 solutions
Jan 1, 2017
5ed5a1d
edit some solutions
Jan 2, 2017
9b6e305
add some solutions
Jan 2, 2017
7dcf298
add some solutions
Jan 3, 2017
f9681ee
add some solutions
Jan 4, 2017
f930439
add some solutions
Jan 4, 2017
00d783e
add some solutions
Jan 7, 2017
dc6a2ea
add some solutions
Jan 8, 2017
493994d
edit jump-game-ii.py
Jan 8, 2017
89fb7f3
add some solutions
Jan 10, 2017
6d6d930
add some solutions
Jan 11, 2017
b7f1e6e
add some solutions
Jan 11, 2017
cba7e1b
Merge pull request #43 from JiaKunUp/master
kamyu104 Jan 11, 2017
b5c46b8
Create sliding-window-median.cpp
kamyu104 Jan 15, 2017
7eb30ff
Update sliding-window-median.cpp
kamyu104 Jan 15, 2017
07c84ad
add an if constrain when the input is []
chihyaoma Jan 15, 2017
8726fe3
Use floor division instead of regular division
chihyaoma Jan 16, 2017
9f9a4fc
Create number-complement.cpp
kamyu104 Feb 2, 2017
9804f4e
Update number-complement.py
kamyu104 Feb 2, 2017
9a1bb7d
Create magical-string.cpp
kamyu104 Feb 2, 2017
2ebc93f
Create magical-string.py
kamyu104 Feb 2, 2017
ca906ff
Create license-key-formatting.cpp
kamyu104 Feb 4, 2017
5bb7ee6
Create icense-key-formatting.py
kamyu104 Feb 4, 2017
3ac5c55
Rename icense-key-formatting.py to license-key-formatting.py
kamyu104 Feb 4, 2017
95276ae
Create smallest-good-base.py
kamyu104 Feb 4, 2017
59c0934
Create smallest-good-base.cpp
kamyu104 Feb 4, 2017
5d50569
Create find-permutation.py
kamyu104 Feb 4, 2017
96d060a
Create find-permutation.cpp
kamyu104 Feb 4, 2017
05b301b
Create max-consecutive-ones.py
kamyu104 Feb 4, 2017
b25bfc2
Create max-consecutive-ones.cpp
kamyu104 Feb 4, 2017
3787d58
Create max-consecutive-ones-ii.py
kamyu104 Feb 4, 2017
f0cf11f
Create max-consecutive-ones-ii.cpp
kamyu104 Feb 4, 2017
4fbede8
fix minor error
wilbeibi Feb 17, 2017
18aaddc
Create predict-the-winner.cpp
kamyu104 Feb 27, 2017
555f4c1
Create predict-the-winner.py
kamyu104 Feb 27, 2017
7a98253
Create zuma-game.cpp
kamyu104 Feb 27, 2017
893f28d
Update zuma-game.cpp
kamyu104 Feb 27, 2017
81e2e0e
Update zuma-game.cpp
kamyu104 Feb 27, 2017
ed27fd5
Update zuma-game.cpp
kamyu104 Feb 27, 2017
e8efc78
Create zuma-game.py
kamyu104 Feb 27, 2017
de8501c
Update zuma-game.py
kamyu104 Feb 27, 2017
74fb978
Create reverse-pairs.cpp
kamyu104 Feb 27, 2017
951da1e
Update reverse-pairs.cpp
kamyu104 Feb 27, 2017
ecbdce7
Create reverse-pairs.py
kamyu104 Feb 27, 2017
8f854c5
Update reverse-pairs.py
kamyu104 Feb 27, 2017
85d2d9f
Create ipo.cpp
kamyu104 Feb 27, 2017
42fc994
Create ipo.py
kamyu104 Feb 27, 2017
eb7be3b
Create super-washing-machines.cpp
kamyu104 Feb 27, 2017
48f0377
Create super-washing-machines.py
kamyu104 Feb 27, 2017
a76a099
Update super-washing-machines.cpp
kamyu104 Feb 27, 2017
1829b3d
Create target-sum.cpp
kamyu104 Feb 27, 2017
88e84ef
Create target-sum.py
kamyu104 Feb 27, 2017
6227bed
Create contiguous-array.cpp
kamyu104 Feb 27, 2017
fdefe6b
Create contiguous-array.py
kamyu104 Feb 27, 2017
bae9594
Update contiguous-array.cpp
kamyu104 Feb 28, 2017
a098e33
Update contiguous-array.py
kamyu104 Feb 28, 2017
f28a3c6
Create continuous-subarray-sum.cpp
kamyu104 Feb 28, 2017
0ac3b0e
Create continuous-subarray-sum.py
kamyu104 Feb 28, 2017
6d4b371
Create minimum-absolute-difference-in-bst.cpp
kamyu104 Feb 28, 2017
1241de1
Create minimum-absolute-difference-in-bst.py
kamyu104 Feb 28, 2017
9353a18
Create the-maze-iii.cpp
kamyu104 Feb 28, 2017
0f6ff7a
Create the-maze-iii.py
kamyu104 Feb 28, 2017
432ace8
Update the-maze-iii.py
kamyu104 Feb 28, 2017
85f8429
Update the-maze-iii.py
kamyu104 Feb 28, 2017
779a509
Create the-maze-ii.py
kamyu104 Mar 4, 2017
a47fd2a
Update the-maze-iii.cpp
kamyu104 Mar 4, 2017
f2319e3
Create the-maze-ii.cpp
kamyu104 Mar 4, 2017
aac0373
Update the-maze-ii.cpp
kamyu104 Mar 4, 2017
d317fd7
Update the-maze-ii.py
kamyu104 Mar 4, 2017
e60291d
Update the-maze-ii.cpp
kamyu104 Mar 4, 2017
7c5f9d6
Update the-maze-ii.py
kamyu104 Mar 4, 2017
a3a5ca2
Create the-maze.py
kamyu104 Mar 4, 2017
0bce6e7
Create the-maze.cpp
kamyu104 Mar 4, 2017
e8af447
Create increasing-subsequences.cpp
kamyu104 Mar 4, 2017
00c9213
Create increasing-subsequences.py
kamyu104 Mar 4, 2017
8b09352
Create longest-word-in-dictionary-through-deleting.cpp
kamyu104 Mar 4, 2017
468dc85
Create longest-word-in-dictionary-through-deleting.py
kamyu104 Mar 4, 2017
a58403f
Fix to create a complete BST
piyush1911 Mar 15, 2017
be708ad
Create k-diff-pairs-in-an-array.cpp
kamyu104 Mar 18, 2017
9e09067
Create k-diff-pairs-in-an-array.py
kamyu104 Mar 18, 2017
992f104
Create keyboard-row.cpp
kamyu104 Mar 18, 2017
3e985b0
Create keyboard-row.py
kamyu104 Mar 18, 2017
fac3155
Create freedom-trail.cpp
kamyu104 Mar 18, 2017
9bb58d8
Update freedom-trail.cpp
kamyu104 Mar 18, 2017
e2112a8
Update freedom-trail.cpp
kamyu104 Mar 18, 2017
42dbd25
Create freedom-trail.py
kamyu104 Mar 18, 2017
3dc9091
Update freedom-trail.cpp
kamyu104 Mar 18, 2017
bd0dd63
Create word-abbreviation.py
kamyu104 Mar 19, 2017
0259de2
Create word-abbreviation.cpp
kamyu104 Mar 19, 2017
f89e967
Create encode-and-decode-tinyurl.cpp
kamyu104 Mar 19, 2017
b84c7c7
Create encode-and-decode-tinyurl.py
kamyu104 Mar 19, 2017
5bf89ae
Create detect-capital.cpp
kamyu104 Mar 19, 2017
3a22723
Create detect-capital.py
kamyu104 Mar 19, 2017
de82dad
Update word-abbreviation.cpp
kamyu104 Mar 19, 2017
d24c5c8
Create construct-binary-tree-from-string.cpp
kamyu104 Mar 19, 2017
f23da49
Update construct-binary-tree-from-string.cpp
kamyu104 Mar 19, 2017
64bfcf8
Create construct-binary-tree-from-string.py
kamyu104 Mar 19, 2017
39f6fe4
Create next-greater-element-i.cpp
kamyu104 Mar 19, 2017
555dc74
Create next-greater-element-i.py
kamyu104 Mar 19, 2017
3a371b4
Create next-greater-element-ii.cpp
kamyu104 Mar 19, 2017
f16667c
Create next-greater-element-ii.py
kamyu104 Mar 19, 2017
e40af5c
Create base-7.cpp
kamyu104 Mar 19, 2017
9b355f5
Update base-7.cpp
kamyu104 Mar 19, 2017
198b54c
Create base-7.py
kamyu104 Mar 19, 2017
bfa24f3
Create teemo-attacking.cpp
kamyu104 Mar 19, 2017
b3537e3
Create teemo-attacking.py
kamyu104 Mar 19, 2017
240fda8
Create construct-the-rectangle.cpp
kamyu104 Mar 19, 2017
2324548
Create construct-the-rectangle.py
kamyu104 Mar 19, 2017
072f772
Create reverse-string-ii.cpp
kamyu104 Mar 19, 2017
964c38b
Create reverse-string-ii.py
kamyu104 Mar 19, 2017
bb8cdd1
Create diagonal-traverse.cpp
kamyu104 Mar 19, 2017
86cbe7d
Create diagonal-traverse.py
kamyu104 Mar 19, 2017
009af87
Update README.md
kamyu104 Mar 20, 2017
cb38e90
Create 01-matrix.cpp
kamyu104 Mar 20, 2017
51a974c
Update README.md
kamyu104 Mar 20, 2017
b9b76a9
Create 01-matrix.py
kamyu104 Mar 20, 2017
c3dba3c
Update 01-matrix.py
kamyu104 Mar 20, 2017
3919ba0
Update 01-matrix.cpp
kamyu104 Mar 20, 2017
e4893e0
Merge pull request #49 from Wilbeibi/patch-2
kamyu104 Mar 20, 2017
0d8ffea
Update unique-word-abbreviation.cpp
kamyu104 Mar 20, 2017
93353bb
Merge pull request #48 from chihyaoma/patch-2
kamyu104 Mar 20, 2017
0ac098b
Merge pull request #47 from chihyaoma/patch-1
kamyu104 Mar 20, 2017
0475852
Update search-a-2d-matrix.py
kamyu104 Mar 20, 2017
59236ed
Merge pull request #51 from piyush1911/patch-1
kamyu104 Mar 20, 2017
746df5c
Update climbing-stairs.py
kamyu104 Mar 22, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
91 changes: 91 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
.idea/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IPython Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject
37 changes: 37 additions & 0 deletions C++/01-matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time: O(m * n)
// Space: O(m * n)

class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
queue<pair<int, int>> queue;
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < matrix[0].size(); ++j) {
if (matrix[i][j] == 0) {
queue.emplace(i, j);
}
else {
matrix[i][j] = numeric_limits<int>::max();
}
}
}

const vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while (!queue.empty()) {
auto cell = queue.front();
queue.pop();
for (const auto& dir : dirs) {
auto i = cell.first + dir.first;
auto j = cell.second + dir.second;
if (i < 0 || i >= matrix.size() || j < 0 || j >= matrix[0].size() ||
matrix[i][j] <= matrix[cell.first][cell.second] + 1) {
continue;
}
queue.emplace(i, j);
matrix[i][j] = matrix[cell.first][cell.second] + 1;
}
}

return matrix;
}
};
21 changes: 21 additions & 0 deletions C++/132-pattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Time: O(n)
// Space: O(n)

class Solution {
public:
bool find132pattern(vector<int>& nums) {
int ak = numeric_limits<int>::min();
stack<int> st;
for (int i = nums.size() - 1; i >= 0; --i) {
if (nums[i] < ak) {
return true;
} else {
while (!st.empty() && nums[i] > st.top()) {
ak = st.top(), st.pop();
}
}
st.emplace(nums[i]);
}
return false;
}
};
44 changes: 44 additions & 0 deletions C++/3sum-closest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Time: O(n^2)
// Space: O(1)

class Solution {
public:
/**
* @param numbers: Give an array numbers of n integer
* @param target: An integer
* @return: return the sum of the three integers, the sum closest target.
*/
int threeSumClosest(vector<int> nums, int target) {
int ans = numeric_limits<int>::max();
int min_diff = numeric_limits<int>::max();

// Make nums in increasing order. Time: O(nlogn)
sort(nums.begin(), nums.end());

for (int i = 0; i < static_cast<int>(nums.size()) - 2; ++i) {
if (i == 0 || nums[i] != nums[i - 1]) { // Skip duplicated.
int j = i + 1;
int k = nums.size() - 1;

while (j < k) { // Time: O(n) for each i.
const auto sum = nums[i] + nums[j] + nums[k];

if (sum > target) { // Should decrease sum.
--k;
} else if (sum < target) { // Should increase sum.
++j;
} else {
return target;
}

if (abs(sum - target) < min_diff) {
min_diff = abs(sum - target);
ans = sum;
}
}
}
}

return ans;
}
};
25 changes: 25 additions & 0 deletions C++/3sum-smaller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time: O(n^2)
// Space: O(1)

class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
const int n = nums.size();

int count = 0;
for (int k = 2; k < n; ++k) {
int i = 0, j = k - 1;
while (i < j) { // Two Pointers, linear time.
if (nums[i] + nums[j] + nums[k] >= target) {
--j;
} else {
count += j - i;
++i;
}
}
}

return count;
}
};
41 changes: 41 additions & 0 deletions C++/3sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Time: O(n^2)
// Space: O(1)

class Solution {
public:
/**
* @param numbers : Give an array numbers of n integer
* @return : Find all unique triplets in the array which gives the sum of zero.
*/
vector<vector<int>> threeSum(vector<int> &nums) {
vector<vector<int>> ans;
const int target = 0;

// Make nums in increasing order. Time: O(nlogn)
sort(nums.begin(), nums.end());

for (int i = 0; i < static_cast<int>(nums.size()) - 2; ++i) {
if (i == 0 || nums[i] != nums[i - 1]) { // Skip duplicated.
for (int j = i + 1, k = nums.size() - 1; j < k; ) { // Time: O(n) for each i.
if (j - 1 > i && nums[j] == nums[j - 1]) { // Skip duplicated.
++j;
} else if (k + 1 < nums.size() && nums[k] == nums[k + 1]) { // Skip duplicated.
--k;
} else {
const auto sum = nums[i] + nums[j] + nums[k];
if (sum > target) { // Should decrease sum.
--k;
} else if (sum < target) { // Should increase sum.
++j;
} else {
ans.push_back({nums[i], nums[j], nums[k]});
++j, --k;
}
}
}
}
}

return ans;
}
};
23 changes: 23 additions & 0 deletions C++/4sum-ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Time: O(n^2)
// Space: O(n^2)

class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int> A_B_sum;
for (const auto& a : A) {
for (const auto& b : B) {
++A_B_sum[a + b];
}
}
int result = 0;
for (const auto& c : C) {
for (const auto& d : D) {
if (A_B_sum.find(-c - d) != A_B_sum.end()) {
result += A_B_sum[-c - d];
}
}
}
return result;
}
};
79 changes: 79 additions & 0 deletions C++/4sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Time: O(n^3)
// Space: O(1)

class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
int len = num.size();
int left, right, sum;
sort(num.begin(), num.end());
vector<vector<int>> res;
for (int i = 0; i < len - 3; ++i) {
if (i && num[i] == num[i - 1]) {
continue;
}
for (int j = i + 1; j < len - 2; ++j) {
if (j != i + 1 && num[j] == num[j - 1]) {
continue;
}
sum = target - num[i] - num[j];
left = j + 1, right = len - 1;
while (left < right) {
if (num[left] + num[right] == sum) {
res.push_back({num[i], num[j], num[left], num[right]});
++left, --right;
while (left < right && num[left] == num[left - 1]) {
++left;
}
while (left < right && num[right] == num[right + 1]) {
--right;
}
} else {
if (num[left] + num[right] > sum) {
--right;
} else {
++left;
}
}
}
}
}
return res;
}
};

// Time: O(n^4)
// Space: O(n^2)
class Solution2 {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector<vector<int>> ans;
if (num.size() < 4) {
return ans;
}
sort(num.begin(), num.end());
unordered_multimap<int, pair<int, int>> cache;

for (int i = 0; i < num.size(); ++i) {
for (int j = i + 1; j < num.size(); ++j) {
cache.emplace(num[i] + num[j], make_pair(i, j));
}
}

for (auto i = cache.begin(); i != cache.end(); ++i) {
auto a = i->second.first;
auto b = i->second.second;
auto range = cache.equal_range(target - i->first);
for (auto j = range.first; j != range.second; ++j) {
auto c = j->second.first;
auto d = j->second.second;
if (b < c) {
ans.push_back({num[a], num[b], num[c], num[d]});
}
}
}
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
return ans;
}
};
Loading