10000 111 by ericzhangdl · Pull Request #1 · ericzhangdl/leetcode · GitHub
[go: up one dir, main page]

Skip to content

111 #1

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

Open
wants to merge 498 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
498 commits
Select commit Hold shift + click to select a range
9c39fd9
refactory the code to make it a bit readable
haoel Mar 30, 2019
cecc733
more solutions
haoel Mar 30, 2019
6153874
more readable implementation
haoel Mar 30, 2019
2b29d11
slight midification and removed the debug code
haoel Mar 31, 2019
07f864b
simplified the implmentation
haoel Mar 31, 2019
460daee
fixed some algorithm bugs
haoel Apr 1, 2019
344c749
fixed the algorithm bug
haoel Apr 2, 2019
4b062ef
update the comments
haoel Apr 2, 2019
270faa0
more clean implementation
haoel Apr 2, 2019
1dd444e
update the implmetation
haoel Apr 2, 2019
b98faee
update the problem description
haoel Apr 3, 2019
14efb7e
refactory the code : show the evolution of the solution
haoel Apr 3, 2019
a6149b9
reformat the comments
haoel Apr 3, 2019
6f38855
more clean implementation
haoel Apr 3, 2019
03ce502
update the comments
haoel Apr 8, 2019
fd961e6
add n TLE solution
haoel Apr 8, 2019
4060151
New Problem Solution - "Two City Scheduling"
haoel Apr 21, 2019
d2f0bf9
New Problem Solution - "Matrix Cells in Distance Order"
haoel Apr 21, 2019
61e5e98
New Problem Solution "Cousins in Binary Tree"
haoel Apr 30, 2019
9effb5a
New Problem Solution "Broken Calculator"
haoel Apr 30, 2019
39bec87
New C++ Solution "Largest Perimeter Triangle"
yue2388253 Apr 7, 2019
c62c383
New C++ Solution "Largest Perimeter Triangle"
yue2388253 Apr 7, 2019
8a1623e
Update IntegerBreak.cpp
LynnTh May 14, 2019
8efd632
recoverTreeFromPreorderTraversal.cpp
AhmedMorsy95 May 19, 2019
522136b
refine the file name and the problem description comment #172
haoel May 29, 2019
4f661e7
addTwoNumbers golang solution
May 29, 2019
93da4fc
modify name to twoSum and readme
May 29, 2019
d0d8c67
add ,
May 29, 2019
033749a
modify golang file name
May 29, 2019
afc1829
add an log alias in git config file
haoel May 29, 2019
f78227a
New Problem Solution "Compare Strings by Frequency of the Smallest Ch…
haoel Oct 1, 2019
b1d9566
New Problem Solution "Video Stitching"
haoel Oct 1, 2019
7eea58d
added 3 problems
3ZadeSSG Oct 6, 2019
e626292
updated readme with links
3ZadeSSG Oct 6, 2019
bd9928b
algorithms: (3/4)Sum(Closest): Improve code readability
Oct 7, 2019
4ae275d
using diamond-operator
Oct 29, 2019
216f2f8
several java code improvements
Oct 29, 2019
8bf57df
Python solution for problem 876
Oct 5, 2019
b9054e7
fix #196 - correct the level of difficulty
haoel Jan 16, 2020
c69dcaf
update the `xidel` installation source on Linux platform
haoel Mar 4, 2020
ba6b1c5
update the `xidel` installation on MacOS to fix #200 issue
haoel Mar 4, 2020
109768f
restate the installation of `xidel` command
haoel Apr 4, 2020
0042d34
add cog in dict in wordLadder.cpp documentation
tmibvishal Jun 14, 2020
4c7451b
New Solution "1071 - Greatest Common Divisor of Strings"
haoel Jul 19, 2020
6ab210d
New Problem Solution "982. Triples with Bitwise AND Equal To Zero"
haoel Jul 26, 2020
c20a011
New Problem Solution "933. Number of Recent Calls"
haoel Jul 26, 2020
54965bc
New Problem Solution "929. Unique Email Addresses"
haoel Jul 26, 2020
44c5fb3
add python solution for problem 55
SysSn13 Oct 1, 2020
5fd14ec
add cpp solution for problem 1573
SysSn13 Oct 1, 2020
2204bd2
Created TimeNeededToInformAllEmployees
shreyavanga Oct 2, 2020
da00269
Created TimeNeededToInformAllEmployees
shreyavanga Oct 2, 2020
7185b04
Merge branch 'shreyavanga-new_algo'
haoel Oct 2, 2020
cf7162c
adjusted the comment and add the solution " Time Needed to Inform All…
haoel Oct 2, 2020
7eacb4a
Create Short_Encoding_of_Words.cpp
yashparmar15 Oct 2, 2020
bd19533
add the solution "820 - Short Encoding of Words" into readme, and add…
haoel Oct 2, 2020
912f166
Added python solution for leetcode0980
soham0-0 Oct 1, 2020
55fcf17
Update README.md
soham0-0 Oct 1, 2020
ec39844
Update README.md
soham0-0 Oct 1, 2020
114245b
Merge branch 'master' into sudesh
haoel Oct 2, 2020
21e47fe
Merge branch 'sudesh1122-sudesh'
haoel Oct 2, 2020
64f99fe
Added solution for firstMissingPositive in python and Updated README.…
samir-0711 Sep 30, 2020
f7afd3b
[Add] Add solution for question targetSum(DP)
Bhavesh0327 Sep 30, 2020
e2956c1
add the "494 - Target Sum" into README.md
haoel Oct 2, 2020
1eedbe8
New Soultion -"1470. Shuffle the Array"
haoel Oct 2, 2020
c626a18
New Solution - "1464. Maximum Product of Two Elements in an Array"
haoel Oct 2, 2020
69b1005
New Solution "1460. Make Two Arrays Equal by Reversing Sub-arrays"
haoel Oct 2, 2020
e1fa704
New Solution "1535. Find the Winner of an Array Game"
haoel Oct 2, 2020
bad3dcd
added the missed files
haoel Oct 2, 2020
ec2298a
New Solution "1541. Minimum Insertions to Balance a Parentheses String"
haoel Oct 2, 2020
1889a62
New Problem Solution "1550. Three Consecutive Odds"
haoel Oct 3, 2020
834c683
New Problem Solution "1551. Minimum Operations to Make Array Equal"
haoel Oct 3, 2020
972bc2e
New Problem Solution - "1513. Number of Substrings With Only 1s"
haoel Oct 3, 2020
09eaedf
added missed file
haoel Oct 3, 2020
61b7010
Add files via upload
CyberIkshit Oct 4, 2020
7106821
Update README.md
CyberIkshit Oct 4, 2020
419594d
Update MaxSubarray.java
CyberIkshit Oct 4, 2020
7f40068
Adding solution to problem 1333
scorpionhiccup Oct 3, 2020
9cc60b6
modify the file and dir name - PR #227
haoel Oct 7, 2020
4777b39
add cpp solution for problem 1605
SysSn13 Oct 3, 2020
74e5adf
New Problem Solution "1525. Number of Good Ways to Split a String"
haoel Oct 7, 2020
3ec5e2f
New Problem Solution "1524. Number of Sub-arrays With Odd Sum"
haoel Oct 7, 2020
329dad5
New Problem Solution "1523. Count Odd Numbers in an Interval Range"
haoel Oct 7, 2020
1f64454
Solution for 'Random Point in Non-overlapping Rectangles'
dhadad Oct 10, 2020
aead07b
change the .gitconfig to git.config.sh
haoel Oct 20, 2020
a5abef3
Create 11. Container With Most Water (#236)
hvsalesforce Nov 1, 2020
934af26
using the relative path instead of full url
haoel Nov 1, 2020
f3a1b88
fix markdown problem
haoel Nov 2, 2020
f7ca152
New Problem Solution "Largest Merge Of Two Strings"
haoel Feb 11, 2021
757d6a3
New Problem Solution - "Maximum Score From Removing Stones"
haoel Feb 11, 2021
59a52fc
New Problem Solution - "Check if Array Is Sorted and Rotated"
haoel Feb 11, 2021
d50de46
fix typo (#246)
tian3rd Feb 11, 2021
36da317
New Problem Solution - "Sum of Unique Elements"
haoel Feb 12, 2021
ee9962f
New Problem Solution - "Maximum Absolute Sum of Any Subarray"
haoel Feb 12, 2021
b74969f
New Problem Solution - "Minimum Length of String After Deleting Simil…
haoel Feb 12, 2021
b7adf90
New Problem Solution - "Maximum Number of Events That Can Be Attended"
haoel Feb 13, 2021
e5e3308
New Problem Solution - "Minimum Changes To Make Alternating Binary St…
haoel Feb 14, 2021
ac35ab5
New Problem Solution -- "Count Number of Homogenous Substrings"
haoel Feb 14, 2021
15d5728
New Problem Solution - "Minimum Limit of Balls in a Bag"
haoel Feb 14, 2021
1244858
New Problem Solution - "Minimum Degree of a Connected Trio in a Graph"
haoel Feb 14, 2021
25fbde9
New Problem Solution - "Closest Subsequence Sum"
haoel Feb 15, 2021
bbe0ead
New Problem Solution - "Maximum Number of Events That Can Be Attended…
haoel Feb 16, 2021
1483a6e
New Problem Solution - "Find the Highest Altitude"
haoel Feb 17, 2021
5e6f6a8
New Problem Solution - "Minimum Number of People to Teach"
haoel Feb 17, 2021
88836fb
New Problem Solution - "Decode XORed Permutation"
haoel Feb 17, 2021
d358ec0
fix the comments error
haoel Feb 17, 2021
d2dd204
New Problem Solution - "Check if Binary String Has at Most One Segmen…
haoel Mar 7, 2021
e13c407
New Problem Solution - "Minimum Elements to Add to Form a Given Sum"
haoel Mar 7, 2021
b80be43
New Problem Solution - "Number of Restricted Paths From First to Last…
haoel Mar 7, 2021
424dec7
New Problem Solution -"Longest Nice Substring"
haoel Mar 8, 2021
895e5aa
New Problem Solution - "Find Nearest Point That Has the Same X or Y C…
haoel Mar 13, 2021
6b2e874
New Problem Solution - "Check if Number is a Sum of Powers of Three"
haoel Mar 13, 2021
2e7a56e
New Problem Solution - "Sum of Beauty of All Substrings"
haoel Mar 13, 2021
7bc04aa
New Problem Solution - "Count Items Matching a Rule"
haoel Mar 14, 2021
be98516
update the scripts
haoel Mar 14, 2021
2639995
New Problem Solution -"Closest Dessert Cost"
haoel Mar 14, 2021
22d4bea
New Problem Solution -"Check if One String Swap Can Make Strings Equal"
haoel Mar 19, 2021
be8ba5b
update the scripts
haoel Mar 19, 2021
2f46c54
New Problem Solution -"Find Center of Star Graph"
haoel Mar 19, 2021
efb00cc
optimizing the script
haoel Mar 19, 2021
3897fa3
New Problem Solution -"Maximum Average Pass Ratio"
haoel Mar 19, 2021
8bef38a
New Problem Solution -"Maximum Score of a Good Subarray"
haoel Mar 19, 2021
37e690c
change ♥ to 🔒
haoel Mar 19, 2021
50e23a4
support other platoform
haoel Mar 19, 2021
5e62e9c
New Problem Solution -"Make the XOR of All Segments Equal to Zero"
haoel Mar 20, 2021
c8e75fd
New Problem Solution -"Merge Strings Alternately"
haoel Mar 20, 2021
55f8340
New Problem Solution -"Minimum Number of Operations to Move All Balls…
haoel Mar 20, 2021
1ef5290
New Problem Solution -"Maximum Ascending Subarray Sum"
haoel Mar 21, 2021
5eff255
New Problem Solution -"Number of Orders in the Backlog"
haoel Mar 21, 2021
dbd221d
New Problem Solution -"Maximum Value at a Given Index in a Bounded Ar…
haoel Mar 21, 2021
5db5219
New Problem Solution -"Count Pairs With XOR in a Range"
haoel Mar 21, 2021
0b4a22f
Update the comments
haoel Mar 22, 2021
823bfa4
refine the comments
haoel Mar 22, 2021
af3bc24
New Problem Solution -"Second Largest Digit in a String"
haoel Mar 22, 2021
23bbd08
replace the `xidel` by `jq` fix #248
haoel Mar 22, 2021
500937a
remove the temp file
haoel Mar 22, 2021
d3dd701
New Problem Solution -"Design Authentication Manager"
haoel Mar 22, 2021
546a6cd
fix a issue: using jq -r to query the raw information
haoel Mar 22, 2021
e6634ee
fix the format in README.md
haoel Mar 22, 2021
b8647e9
add `workflow.sh` in README.md
haoel Mar 22, 2021
2aa068b
New Problem Solution -"Maximum Number of Consecutive Values You Can M…
haoel Mar 22, 2021
f2291a2
New Problem Solution -"Maximize Score After N Operations"
haoel Mar 22, 2021
0ade418
add more comments
haoel Mar 22, 2021
c8f4e5a
New Problem Solution -"Count Pairs Of Nodes"
haoel Mar 23, 2021
a97e9ab
New Problem Solution -"Equal Sum Arrays With Minimum Number of Operat…
haoel Mar 23, 2021
f34d0ff
New Problem Solution -"Largest Substring Between Two Equal Characters"
haoel Mar 24, 2021
ed3c202
New Problem Solution -"Lexicographically Smallest String After Applyi…
haoel Mar 24, 2021
ff70235
New Problem Solution -"Map of Highest Peak"
haoel Mar 25, 2021
5183dad
fix the problem - out of array boundary, and unresonalble ticket pric…
haoel Mar 26, 2021
51a9331
New Problem Solution -"Form Array by Concatenating Subarrays of Anoth…
haoel Mar 27, 2021
603493d
rename the SCRIPTPATH to SCRIPT_PATH, and SCRIPTFILE to SCRIPT_FILE
haoel Mar 27, 2021
2354860
fix the typo and markdown formation
haoel Mar 27, 2021
3c577b4
refine the scripts
haoel Mar 27, 2021
10e14ba
New Problem Solution -"Number Of Rectangles That Can Form The Largest…
haoel Mar 27, 2021
667ba8c
fix bug in workflow.sh
haoel Mar 27, 2021
eadb147
New Problem Solution -"Latest Time by Replacing Hidden Digits"
haoel Mar 27, 2021
69fc31e
New Problem Solution -"Maximum Number of Balls in a Box"
haoel Mar 27, 2021
2ed675b
New Problem Solution -"Longest Palindromic Subsequence"
haoel Mar 27, 2021
3e5e2e9
New Problem Solution -"Maximize Palindrome Length From Subsequences"
haoel Mar 27, 2021
3a4af19
New Problem Solution -"Number of Different Integers in a String"
haoel Mar 28, 2021
e2aa8b1
New Problem Solution -"Minimum Number of Operations to Reinitialize a…
haoel Mar 28, 2021
3d9ce82
New Problem Solution -"Evaluate the Bracket Pairs of a String"
haoel Mar 28, 2021
47dc744
New Problem Solution -"Maximize Number of Nice Divisors"
haoel Mar 28, 2021
d1cf484
New Problem Solution -"Restore the Array From Adjacent Pairs"
haoel Mar 28, 2021
3f87c38
New Problem Solution -"Calculate Money in Leetcode Bank"
haoel Mar 28, 2021
18da053
New Problem Solution -"Maximum Score From Removing Substrings"
haoel Mar 28, 2021
c150b17
New Problem Solution -"Shuffle String"
haoel Mar 29, 2021
6f16a68
New Problem Solution - "1529. Bulb Switcher IV"
haoel Mar 29, 2021
085cd5e
add the question id in commit message
haoel Mar 29, 2021
518ed99
New Problem Solution - "672. Bulb Switcher II"
haoel Mar 29, 2021
93e1e44
New Problem Solution - "672. Bulb Switcher II"
haoel Mar 29, 2021
82a5da2
New Problem Solution - "1375. Bulb Switcher III"
haoel Mar 29, 2021
06eab70
New Problem Solution -"1710. Maximum Units on a Truck"
haoel Mar 30, 2021
0157340
New Problem Solution -"1711. Count Good Meals"
haoel Mar 30, 2021
1e87411
correct the problem id
haoel Mar 30, 2021
6ef88aa
New Problem Solution -"1738. Find Kth Largest XOR Coordinate Value"
haoel Mar 31, 2021
b6ae9de
New Problem Solution -"1770. Maximum Score from Performing Multiplica…
haoel Apr 1, 2021
6de5e32
New Problem Solution -"1766. Tree of Coprimes"
haoel Apr 1, 2021
4e8359e
New Problem Solution -"1816. Truncate Sentence"
haoel Apr 5, 2021
d8529c9
New Problem Solution -"1817. Finding the Users Active Minutes"
haoel Apr 5, 2021
984ca35
New Problem Solution -"1818. Minimum Absolute Sum Difference"
haoel Apr 5, 2021
209fcdd
New Problem Solution -"1819. Number of Different Subsequences GCDs"
haoel Apr 5, 2021
616e340
add a space
haoel Apr 6, 2021
8ac73b4
New Problem Solution - "1812. Determine Color of a Chessboard Square"
haoel Apr 6, 2021
1ab937e
New Problem Solution - "1813. Sentence Similarity III"
haoel Apr 6, 2021
9d268cd
New Problem Solution - "1814. Count Nice Pairs in an Array"
haoel Apr 6, 2021
f0a4ff0
New Problem Solution - "1815. Maximum Number of Groups Getting Fresh …
haoel Apr 8, 2021
1629a81
New Problem Solution - "1739. Building Boxes"
haoel Apr 8, 2021
d26ba3a
New Problem Solution - "1822. Sign of the Product of an Array"
haoel Apr 11, 2021
613ef3c
New Problem Solution - "1823. Find the Winner of the Circular Game"
haoel Apr 11, 2021
55fc93d
New Problem Solution - "1824. Minimum Sideway Jumps"
haoel Apr 11, 2021
0f62b5f
New Problem Solution - "1825. Finding MK Average"
haoel Apr 20, 2021
bf72af5
New Problem Solution - "1827. Minimum Operations to Make the Array In…
haoel Apr 20, 2021
c065bd9
New Problem Solution - "1828. Queries on Number of Points Inside a Ci…
haoel Apr 20, 2021
2e55b86
New Problem Solution - "1829. Maximum XOR for Each Query"
haoel Apr 20, 2021
0be935a
New Problem Solution - "1832. Check if the Sentence Is Pangram"
haoel Apr 20, 2021
01db23d
New Problem Solution - "1833. Maximum Ice Cream Bars"
haoel Apr 20, 2021
738145a
New Problem Solution - "1834. Single-Threaded CPU"
haoel Apr 20, 2021
fb3a12e
New Problem Solution - "1835. Find XOR Sum of All Pairs Bitwise AND"
haoel Apr 20, 2021
81a3fa8
another alogrithm only one loop
haoel Apr 21, 2021
724a718
remove the tail spaces
haoel Apr 21, 2021
e836fa7
New Problem Solution - "1718. Construct the Lexicographically Largest…
haoel Apr 24, 2021
2bdf2cb
New Problem Solution - "1840. Maximum Building Height"
haoel Apr 25, 2021
e834f04
New Problem Solution - "1839. Longest Substring Of All Vowels in Order"
haoel Apr 25, 2021
37209a7
New Problem Solution - "1838. Frequency of the Most Frequent Element"
haoel Apr 25, 2021
a744f2b
New Problem Solution - "1837. Sum of Digits in Base K"
haoel Apr 25, 2021
6f4d553
refine the comments
haoel Apr 25, 2021
6ae726e
New Problem Solution - "1726. Tuple with Same Product"
haoel Apr 27, 2021
3b81950
New Problem Solution - "1844. Replace All Digits with Characters"
haoel May 3, 2021
54105b0
New Problem Solution - "1845. Seat Reservation Manager"
haoel May 3, 2021
b85cc6d
New Problem Solution - "1846. Maximum Element After Decreasing and Re…
haoel May 3, 2021
e128ce4
New Problem Solution - "1847. Closest Room"
haoel May 3, 2021
bf4fe68
New Problem Solution - "1848. Minimum Distance to the Target Element"
haoel May 3, 2021
83fa31a
New Problem Solution - "1849. Splitting a String Into Descending Cons…
haoel May 3, 2021
fb308ad
New Problem Solution - "1850. Minimum Adjacent Swaps to Reach the Kth…
haoel May 3, 2021
d8731d0
New Problem Solution - "1851. Minimum Interval to Include Each Query"
haoel May 3, 2021
2bbd8f1
New Problem Solution - "1727. Largest Submatrix With Rearrangements"
haoel May 6, 2021
2a058bb
New Problem Solution - "1694. Reformat Phone Number"
haoel May 7, 2021
7cbd02a
New Problem Solution - "1695. Maximum Erasure Value"
haoel May 7, 2021
a78c679
New Problem Solution - "1854. Maximum Population Year"
haoel May 9, 2021
f2c22c7
New Problem Solution - "1855. Maximum Distance Between a Pair of Values"
haoel May 9, 2021
2525f8a
New Problem Solution - "1856. Maximum Subarray Min-Product"
haoel May 9, 2021
9894669
New Problem Solution - "1857. Largest Color Value in a Directed Graph"
haoel May 10, 2021
24c5f61
New Problem Solution - "1700. Number of Students Unable to Eat Lunch"
haoel May 10, 2021
88f9a14
New Problem Solution - "1712. Ways to Split Array Into Three Subarrays"
haoel May 11, 2021
f0dd110
New Problem Solution - "1859. Sorting the Sentence"
haoel May 22, 2021
406e77e
New Problem Solution - "1860. Incremental Memory Leak"
haoel May 22, 2021
0e01ea3
New Problem Solution - "1861. Rotating the Box"
haoel May 22, 2021
384ffe8
New Problem Solution - "1862. Sum of Floored Pairs"
haoel May 22, 2021
9288ac4
New Problem Solution - "1869. Longer Contiguous Segments of Ones than…
haoel May 30, 2021
28617d1
New Problem Solution - "1870. Minimum Speed to Arrive on Time"
haoel May 30, 2021
c19a4bb
New Problem Solution - "1871. Jump Game VII"
haoel May 30, 2021
d8e8f74
New Problem Solution - "1880. Check if Word Equals Summation of Two W…
haoel May 30, 2021
631b306
New Problem Solution - "1881. Maximum Value after Insertion"
haoel May 30, 2021
6c839c2
adding the 1880 in readme
haoel May 30, 2021
b1e0be8
New Problem Solution - "1882. Process Tasks Using Servers"
haoel May 30, 2021
0c2f6da
Added C++ code for Diameter of Binary Tree (#257)
iamarshsingh Oct 2, 2021
36168cc
grammar (#259)
cad0p Nov 12, 2021
51ea33c
Fix possible out of bound array access (#254)
zhao-gang Nov 12, 2021
cf08222
New Problem Solution - "1935. Maximum Number of Words You Can Type"
haoel Jul 22, 2021
0391e52
New Problem Solution - "1945. Sum of Digits of String After Convert"
haoel Nov 12, 2021
322889d
New Problem Solution - "1946. Largest Number After Mutating Substring"
haoel Nov 12, 2021
8055bef
New Problem Solution - "1876. Substrings of Size Three with Distinct …
haoel Nov 12, 2021
ab313d1
New Problem Solution - "1877. Minimize Maximum Pair Sum in Array"
haoel Nov 12, 2021
202f098
fix the wrong directory & file position
haoel Nov 12, 2021
ecb5b88
New Problem Solution - "1884. Egg Drop With 2 Eggs and N Floors"
haoel Nov 15, 2021
e00c0f6
Adding Swim In Rising Water Problem LeetCode #776 (#269)
ektedar Jan 31, 2022
6370150
New java solution to problem 1946 and updated readme (#268)
willmccarten Jan 31, 2022
d2ce5c6
Added a java solution to problem 48 and updated the readme for it (#…
willmccarten Jan 31, 2022
5051980
Added python solutions and corrected 128 to medium (#263)
jordankirkland Jan 31, 2022
125c4d8
Update README.md (#271)
Rafat97 Apr 9, 2022
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
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
algorithms-java/out
*.class
713 changes: 509 additions & 204 deletions README.md

Large diffs are not rendered by default.

72 changes: 37 additions & 35 deletions algorithms/cpp/3Sum/3Sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
#include <vector>
#include <set>
#include <algorithm>

using namespace std;


/*
* Simlar like "Two Number" problem, we can have the simlar solution.
* Similar like "Two Number" problem, we can have the simlar solution.
*
* Suppose the input array is S[0..n-1], 3SUM can be solved in O(n^2) time on average by
* inserting each number S[i] into a hash table, and then for each index i and j,
Expand All @@ -47,6 +48,7 @@ using namespace std;
vector<vector<int> > threeSum(vector<int> &num) {

vector< vector<int> > result;
if(num.size() == 0 || num.size() == 1 || num.size() == 2) return result;

//sort the array, this is the key
sort(num.begin(), num.end());
Expand All @@ -55,14 +57,14 @@ vector<vector<int> > threeSum(vector<int> &num) {

for (int i=0; i<n-2; i++) {
//skip the duplication
if (i>0 && num[i-1]==num[i]) continue;
if (i > 0 && num[i - 1] == num[i]) continue;
int a = num[i];
int low = i+1;
int high = n-1;
while ( low < high ) {
int low = i + 1;
int high = n - 1;
while (low < high) {
int b = num[low];
int c = num[high];
if (a+b+c == 0) {
if (a + b + c == 0) {
//got the soultion
vector<int> v;
v.push_back(a);
Expand All @@ -71,17 +73,17 @@ vector<vector<int> > threeSum(vector<int> &num) {
result.push_back(v);
// Continue search for all triplet combinations summing to zero.
//skip the duplication
while(low<n && num[low]==num[low+1]) low++;
while(high>0 && num[high]==num[high-1]) high--;
while(low < n - 1 && num[low] == num[low + 1]) low++;
while(high > 0 && num[high] == num[high - 1]) high--;
low++;
high--;
} else if (a+b+c > 0) {
//skip the duplication
while(high>0 && num[high]==num[high-1]) high--;
while(high > 0 && num[high] == num[high - 1]) high--;
high--;
} else{
} else {
//skip the duplication
while(low<n && num[low]==num[low+1]) low++;
while(low < n - 1 && num[low] == num[low + 1]) low++;
low++;
}
}
Expand All @@ -97,21 +99,21 @@ int sum(vector<int>& v);
vector<vector<int> > threeSum2(vector<int> &num) {
vector< vector<int> > result;
vector< vector<int> > r = combination(num, 3);
for (int i=0; i<r.size(); i++){
if (isSumZero(r[i])){
for (int i = 0; i < r.size(); i++) {
if (isSumZero(r[i])) {
result.push_back(r[i]);
}
}
return result;
}

bool isSumZero(vector<int>& v){
return sum(v)==0;
bool isSumZero(vector < int>& v) {
return sum(v) == 0;
}

int sum(vector<int>& v){
int s=0;
for(int i=0; i<v.size(); i++){
int sum(vector<int>& v) {
int s = 0;
for(int i = 0; i < v.size(); i++) {
s += v[i];
}
return s;
Expand All @@ -122,39 +124,39 @@ vector<vector<int> > combination(vector<int> &v, int k) {
vector<vector<int> > result;
vector<int> d;
int n = v.size();
for (int i=0; i<n; i++){
d.push_back( (i<k) ? 1 : 0 );
for (int i = 0; i < n; i++) {
d.push_back( (i < k) ? 1 : 0 );
}

//1) from the left, find the [1,0] pattern, change it to [0,1]
//2) move all of the 1 before the pattern to the most left side
//3) check all of 1 move to the right
while(1){
while(1) {
vector<int> tmp;
for(int x=0; x<n; x++){
for(int x = 0; x < n; x++) {
if (d[x]) tmp.push_back(v[x]);
}
sort(tmp.begin(), tmp.end());
result.push_back(tmp);
//step 1), find [1,0] pattern
int i;
bool found = false;
int ones =0;
for(i=0; i<n-1; i++){
int ones = 0;
for(i = 0; i < n - 1; i++) {

if (d[i]==1 && d[i+1]==0){
d[i]=0; d[i+1]=1;
if (d[i] == 1 && d[i + 1] == 0) {
d[i] = 0; d[i + 1] = 1;
found = true;
//step 2) move all of right 1 to the most left side
for (int j=0; j<i; j++){
d[j]=( ones > 0 ) ? 1 : 0;
for (int j = 0; j < i; j++) {
d[j] = ( ones > 0 ) ? 1 : 0;
ones--;
}
break;
}
if (d[i]==1) ones++;
if (d[i] == 1) ones++;
}
if (!found){
if (!found) {
break;
}

Expand All @@ -165,9 +167,9 @@ vector<vector<int> > combination(vector<int> &v, int k) {

void printMatrix(vector<vector<int> > &matrix)
{
for(int i=0; i<matrix.size(); i++){
for(int i = 0; i < matrix.size(); i++) {
printf("{");
for(int j=0; j< matrix[i].size(); j++) {
for(int j = 0; j < matrix[i].size(); j++) {
printf("%3d ", matrix[i][j]) ;
}
printf("}\n");
Expand All @@ -178,9 +180,9 @@ void printMatrix(vector<vector<int> > &matrix)

int main()
{
//int a[] = {-1, 0, 1, 2, -1, 1, -4};
int a[] = {-1, 1, 1, 1, -1, -1, 0,0,0};
vector<int> n(a, a+sizeof(a)/sizeof(int));
//int a[] = { -1, 0, 1, 2, -1, 1, -4 };
int a[] = { -1, 1, 1, 1, -1, -1, 0,0,0 };
vector<int> n(a, a + sizeof(a)/sizeof(int));
vector< vector<int> > result = threeSum(n);
printMatrix(result);
return 0;
Expand Down
24 changes: 12 additions & 12 deletions algorithms/cpp/3SumClosest/3SumClosest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

#define INT_MAX 2147483647
Expand Down Expand Up @@ -53,34 +54,33 @@ int threeSumClosest(vector<int> &num, int target) {

for (int i=0; i<n-2; i++) {
//skip the duplication
if (i>0 && num[i-1]==num[i]) continue;
if (i > 0 && num[i - 1] == num[i]) continue;
int a = num[i];
int low = i+1;
int high = n-1;
int low = i + 1;
int high = n - 1;
//convert the 3sum to 2sum problem
while ( low < high ) {
while (low < high) {
int b = num[low];
int c = num[high];
int sum = a+b+c;
int sum = a + b + c;
if (sum - target == 0) {
//got the final soultion
return target;
} else {

//tracking the minmal distance
if (abs(sum-target) < distance ) {
if (abs(sum - target) < distance ) {
distance = abs(sum - target);
result = sum;
}

if (sum -target> 0) {
if (sum - target > 0) {
//skip the duplication
while(high>0 && num[high]==num[high-1]) high--;
while(high > 0 && num[high] == num[high - 1]) high--;
//move the `high` pointer
high--;
} else {
//skip the duplication
while(low<n && num[low]==num[low+1]) low++;
while(low < n && num[low] == num[low + 1]) low++;
//move the `low` pointer
low++;
}
Expand All @@ -96,8 +96,8 @@ int threeSumClosest(vector<int> &num, int target) {

int main()
{
int a[] = {-1, 2, 1, -4};
vector<int> n(a, a+sizeof(a)/sizeof(int));
int a[] = { -1, 2, 1, -4 };
vector<int> n(a, a + sizeof(a)/sizeof(int));
int target = 1;
cout << threeSumClosest(n, target) << endl;
return 0;
Expand Down
43 changes: 22 additions & 21 deletions algorithms/cpp/4Sum/4Sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<vector<int> > threeSum(vector<int> num, int target);
Expand All @@ -36,15 +37,15 @@ vector<vector<int> > threeSum(vector<int> num, int target);

vector<vector<int> > fourSum(vector<int> &num, int target) {
vector< vector<int> > result;
if (num.size()<4) return result;
if (num.size() < 4) return result;
sort( num.begin(), num.end() );
for(int i=0; i<num.size()-3; i++) {

for(int i = 0; i < num.size() - 3; i++) {
//skip the duplication
if (i>0 && num[i-1]==num[i]) continue;
if (i > 0 && num[i - 1] == num[i]) continue;
vector<int> n(num.begin()+i+1, num.end());
vector<vector<int> > ret = threeSum(n, target-num[i]);
for(int j=0; j<ret.size(); j++){
for(int j = 0; j < ret.size(); j++) {
ret[j].insert(ret[j].begin(), num[i]);
result.push_back(ret[j]);
}
Expand All @@ -61,16 +62,16 @@ vector<vector<int> > threeSum(vector<int> num, int target) {

int n = num.size();

for (int i=0; i<n-2; i++) {
for (int i = 0; i < n - 2; i++) {
//skip the duplication
if (i>0 && num[i-1]==num[i]) continue;
if (i > 0 && num[i - 1] == num[i]) continue;
int a = num[i];
int low = i+1;
int high = n-1;
while ( low < high ) {
int low = i + 1;
int high = n - 1;
while (low < high) {
int b = num[low];
int c = num[high];
if (a+b+c == target) {
if (a + b + c == target) {
//got the soultion
vector<int> v;
v.push_back(a);
Expand All @@ -79,17 +80,17 @@ vector<vector<int> > threeSum(vector<int> num, int target) {
result.push_back(v);
// Continue search for all triplet combinations summing to zero.
//skip the duplication
while(low<n && num[low]==num[low+1]) low++;
while(high>0 && num[high]==num[high-1]) high--;
while(low < n && num[low] == num[low + 1]) low++;
while(high > 0 && num[high] == num[high - 1]) high--;
low++;
high--;
} else if (a+b+c > target) {
} else if (a + b + c > target) {
//skip the duplication
while(high>0 && num[high]==num[high-1]) high--;
while(high > 0 && num[high] == num[high - 1]) high--;
high--;
} else{
} else {
//skip the duplication
while(low<n && num[low]==num[low+1]) low++;
while(low < n && num[low] == num[low + 1]) low++;
low++;
}
}
Expand All @@ -100,9 +101,9 @@ vector<vector<int> > threeSum(vector<int> num, int target) {

int printMatrix(vector< vector<int> > &vv)
{
for(int i=0; i<vv.size(); i++) {
for(int i = 0; i < vv.size(); i++) {
cout << "[";
for(int j=0; j<vv[i].size(); j++) {
for(int j = 0; j < vv[i].size(); j++) {
cout << " " << vv[i][j];
}
cout << "]" << endl;;
Expand All @@ -112,14 +113,14 @@ int printMatrix(vector< vector<int> > &vv)

int main()
{
int a[] = {1,0,-1,0,-2,2};
int a[] = { 1, 0, -1, 0, -2, 2 };
vector<int> n(a, a+6);
int t = 0;
vector< vector<int> > v = fourSum(n, t);
printMatrix(v);

n.clear();
int b[] = {-1,-5,-5,-3,2,5,0,4};
int b[] = { -1, -5, -5, -3, 2, 5, 0, 4 };
n.insert(n.begin(), b, b+8);
t = -7;
v = fourSum(n, t);
Expand Down
Loading
0