Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6b57c22

Browse files
Diego Flores CastilloDiego Flores Castillo
Diego Flores Castillo
authored and
Diego Flores Castillo
committed
majority_element001 (fix): implement majorityElement without filter
1 parent 062fc88 commit 6b57c22

File tree

72 files changed

+672
-516
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+672
-516
lines changed

.idea/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/Algorithms-Leetcode-Javascript.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/Project.xml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jsLinters/eslint.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/prettier.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LeetcodeProblems/Algorithms/Majority_Element.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,20 @@ Note: You should have a better solution than O(N)
2222
* @param {number[]} nums
2323
* @return {number}
2424
*/
25-
var majorityElement = function (nums) {
26-
if (nums.length === 0) return -1;
27-
28-
var candidate = nums[0];
29-
var proves = 1;
30-
31-
for (var i = 1; i < nums.length; i++) {
32-
if (nums[i] === candidate) proves++;
33-
else {
34-
proves--;
35-
if (proves === 0) {
36-
candidate = nums[i];
37-
proves = 1;
25+
26+
const majorityElement = function (nums) {
27+
const length = nums.length;
28+
29+
for (let num of nums) {
30+
let quantity = 0;
31+
for (let i = 0; i < nums.length; i++) {
32+
if (nums[i] === num) {
33+
quantity++;
3834
}
3935
}
36+
if (quantity > length / 2) return num;
37+
quantity = 0;
4038
}
41-
42-
return candidate;
4339
};
4440

4541
module.exports.majorityElement = majorityElement;

LeetcodeProblems/Algorithms/Minimum_Window_Substring.js

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -68,64 +68,65 @@ var getHash = function (t) {
6868
return hash;
6969
};
7070

71-
72-
7371
// Solution 2
7472
// Similar idea code slightly different;
75-
var buildHash = function(t) {
76-
let hash = {};
77-
let occ = 0;
78-
for(var i = 0; i < t.length; i++) {
79-
occ = hash[t[i]] == undefined ? 0 : hash[t[i]];
80-
hash[t[i]] = occ + 1;
81-
}
82-
return hash;
83-
}
84-
85-
var minWindow2 = function(s, t) {
86-
var hashT = buildHash(t);
87-
var start = 0;
88-
var end = 0;
89-
var countLeft = t.length;
90-
var minWindow = "";
91-
var minWindowLeft = -1;
92-
var maxWindowRight = -1;
93-
94-
while(start < s.length && end < s.length) {
95-
if(countLeft > 0) { // window does not contain all elements
96-
if(hashT[s[end]] !== undefined) {
97-
hashT[s[end]] = hashT[s[end]] - 1;
98-
if(hashT[s[end]] >= 0) {
99-
countLeft--;
100-
}
101-
}
102-
103-
if(countLeft > 0) {
104-
end++;
105-
}
106-
} else { // found window
107-
if(minWindowLeft == -1 || ((maxWindowRight - minWindowLeft + 1) > (end - start + 1)) ) {
108-
minWindowLeft = start;
109-
maxWindowRight = end;
110-
}
111-
if(hashT[s[start]] !== undefined) {
112-
hashT[s[start]] = hashT[s[start]] + 1;
113-
if(hashT[s[start]] > 0) {
114-
countLeft++;
115-
end++;
116-
}
117-
}
118-
start++;
73+
var buildHash = function (t) {
74+
let hash = {};
75+
let occ = 0;
76+
for (var i = 0; i < t.length; i++) {
77+
occ = hash[t[i]] == undefined ? 0 : hash[t[i]];
78+
hash[t[i]] = occ + 1;
79+
}
80+
return hash;
81+
};
82+
83+
var minWindow2 = function (s, t) {
84+
var hashT = buildHash(t);
85+
var start = 0;
86+
var end = 0;
87+
var countLeft = t.length;
88+
var minWindow = "";
89+
var minWindowLeft = -1;
90+
var maxWindowRight = -1;
91+
92+
while (start < s.length && end < s.length) {
93+
if (countLeft > 0) {
94+
// window does not contain all elements
95+
if (hashT[s[end]] !== undefined) {
96+
hashT[s[end]] = hashT[s[end]] - 1;
97+
if (hashT[s[end]] >= 0) {
98+
countLeft--;
11999
}
100+
}
101+
102+
if (countLeft > 0) {
103+
end++;
104+
}
105+
} else {
106+
// found window
107+
if (
108+
minWindowLeft == -1 ||
109+
maxWindowRight - minWindowLeft + 1 > end - start + 1
110+
) {
111+
minWindowLeft = start;
112+
maxWindowRight = end;
113+
}
114+
if (hashT[s[start]] !== undefined) {
115+
hashT[s[start]] = hashT[s[start]] + 1;
116+
if (hashT[s[start]] > 0) {
117+
countLeft++;
118+
end++;
119+
}
120+
}
121+
start++;
120122
}
121-
122-
if(minWindowLeft > -1) {
123-
return s.substring(minWindowLeft, maxWindowRight + 1);
124-
}
125-
126-
return "";
127-
};
123+
}
128124

125+
if (minWindowLeft > -1) {
126+
return s.substring(minWindowLeft, maxWindowRight + 1);
127+
}
129128

129+
return "";
130+
};
130131

131132
module.exports.minWindow = minWindow;
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
const assert = require('assert');
2-
const threeSum = require('../../LeetcodeProblems/Algorithms/3Sum').threeSum;
1+
const assert = require("assert");
2+
const threeSum = require("../../LeetcodeProblems/Algorithms/3Sum").threeSum;
33

44
var test = function () {
55
assert.deepEqual(threeSum([]), []);
66
assert.deepEqual(threeSum([0]), []);
77
assert.deepEqual(threeSum([0, 0]), []);
8+
assert.deepEqual(threeSum([0, 0, 0]), [[0, 0, 0]]);
89
assert.deepEqual(
9-
threeSum([0, 0, 0]),
10+
threeSum([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
1011
[[0, 0, 0]]
1112
);
12-
assert.deepEqual(
13-
threeSum([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),
14-
[[0, 0, 0]]
15-
);
16-
assert.deepEqual(
17-
threeSum([-1, 0, 1, 2, -1, -4]),
18-
[ [ -1, 2, -1 ], [ 0, 1, -1 ] ]
19-
);
20-
}
13+
assert.deepEqual(threeSum([-1, 0, 1, 2, -1, -4]), [
14+
[-1, 2, -1],
15+
[0, 1, -1],
16+
]);
17+
};
2118

2219
module.exports.test = test;
Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
const assert = require('assert');
2-
const addTwoNumbers = require('../../LeetcodeProblems/Algorithms/Add_Two_Numbers').addTwoNumbers;
3-
const ListNodeTestHelper = require('../../UtilsClasses/ListNodeTestHelper');
4-
var ListNode = require('../../UtilsClasses/ListNode').ListNode;
1+
const assert = require("assert");
2+
const addTwoNumbers =
3+
require("../../LeetcodeProblems/Algorithms/Add_Two_Numbers").addTwoNumbers;
4+
const ListNodeTestHelper = require("../../UtilsClasses/ListNodeTestHelper");
5+
var ListNode = require("../../UtilsClasses/ListNode").ListNode;
56

67
function test() {
7-
const list1 = ListNode.linkenList([1,2,3,4]);
8-
const list2 = ListNode.linkenList([1,2,3,4]);
9-
ListNodeTestHelper.assertList(
10-
addTwoNumbers(list1, list2),
11-
[2, 4, 6, 8]
12-
);
8+
const list1 = ListNode.linkenList([1, 2, 3, 4]);
9+
const list2 = ListNode.linkenList([1, 2, 3, 4]);
10+
ListNodeTestHelper.assertList(addTwoNumbers(list1, list2), [2, 4, 6, 8]);
1311

1412
const list3 = ListNode.linkenList([1]);
15-
const list4 = ListNode.linkenList([1,2]);
16-
ListNodeTestHelper.assertList(
17-
addTwoNumbers(list3, list4),
18-
[2, 2]
19-
);
13+
const list4 = ListNode.linkenList([1, 2]);
14+
ListNodeTestHelper.assertList(addTwoNumbers(list3, list4), [2, 2]);
2015

2116
const list5 = ListNode.linkenList([]);
22-
const list6 = ListNode.linkenList([1,2]);
23-
ListNodeTestHelper.assertList(
24-
addTwoNumbers(list5, list6),
25-
[1, 2]
26-
);
17+
const list6 = ListNode.linkenList([1, 2]);
18+
ListNodeTestHelper.assertList(addTwoNumbers(list5, list6), [1, 2]);
2719
}
2820

2921
module.exports.test = test;
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
const assert = require('assert');
2-
const cutAwardBadges = require('../../LeetcodeProblems/Algorithms/Award_Budget_Cuts').cutAwardBadges;
1+
const assert = require("assert");
2+
const cutAwardBadges =
3+
require("../../LeetcodeProblems/Algorithms/Award_Budget_Cuts").cutAwardBadges;
34

45
function test() {
56
assert.deepEqual(
67
cutAwardBadges([2, 100, 50, 120, 1000], 190),
7-
[ 47, 47, 47, 47, 2 ]
8-
);
9-
assert.deepEqual(
10-
cutAwardBadges([2, 100, 50, 120, 1000], 5),
11-
[ 1, 1, 1, 1, 1 ]
8+
[47, 47, 47, 47, 2]
129
);
10+
assert.deepEqual(cutAwardBadges([2, 100, 50, 120, 1000], 5), [1, 1, 1, 1, 1]);
1311
}
1412

1513
module.exports.test = test;

LeetcodeProblemsTests/Algorithms/Backspace_String_Compare_Test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
const assert = require('assert');
2-
const backspaceCompare = require('../../LeetcodeProblems/Algorithms/Backspace_String_Compare').backspaceCompare;
3-
const backspaceCompare2 = require('../../LeetcodeProblems/Algorithms/Backspace_String_Compare').backspaceCompare2;
1+
const assert = require("assert");
2+
const backspaceCompare =
3+
require("../../LeetcodeProblems/Algorithms/Backspace_String_Compare").backspaceCompare;
4+
const backspaceCompare2 =
5+
require("../../LeetcodeProblems/Algorithms/Backspace_String_Compare").backspaceCompare2;
46

57
function test() {
68
assert.equal(backspaceCompare("ab#c", "ad#c"), true); // true
@@ -13,6 +15,7 @@ function test() {
1315
assert.equal(backspaceCompare2("a##c", "#a#c"), true); // true
1416
assert.equal(backspaceCompare2("a#c", "b"), false); // false
1517
}
18+
1619
test();
1720

1821
module.exports.test = test;
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const assert = require('assert');
2-
const maxProfit = require('../../LeetcodeProblems/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II').maxProfit;
1+
const assert = require("assert");
2+
const maxProfit =
3+
require("../../LeetcodeProblems/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II").maxProfit;
34

45
function test() {
5-
assert.equal(maxProfit([7,1,5,3,6,4]), 7);
6-
assert.equal(maxProfit([7,1,5,3320,6,4]), 3319);
6+
assert.equal(maxProfit([7, 1, 5, 3, 6, 4]), 7);
7+
assert.equal(maxProfit([7, 1, 5, 3320, 6, 4]), 3319);
78
}
89

910
module.exports.test = test;

LeetcodeProblemsTests/Algorithms/Binary_Gap_Test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const assert = require('assert');
2-
const binaryGap = require('../../LeetcodeProblems/Algorithms/Binary_Gap').binaryGap;
1+
const assert = require("assert");
2+
const binaryGap =
3+
require("../../LeetcodeProblems/Algorithms/Binary_Gap").binaryGap;
34

45
function test() {
56
assert.equal(binaryGap(22), 2); // 10110
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var test = function() {
1+
var test = function () {
22
// TODO
3-
}
3+
};
44

55
module.exports.test = test;

0 commit comments

Comments
 (0)