Skip to content

Commit fc70711

Browse files
authored
Merge pull request #13 from nhistory/main
[Sehwan]Added solutions for week1
2 parents 9f48464 + 5972c4a commit fc70711

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function (prices) {
6+
// Initiate left and right pointer (left: buy price / right: sell price)
7+
// Make profit with initiative value 0
8+
let l = 0;
9+
let r = 1;
10+
let profit = 0;
11+
// Iterate to check profit = prices[r] - prices[l]
12+
while (r < prices.length) {
13+
// If profit is positive, compare profit with previous one
14+
if (prices[r] > prices[l]) {
15+
profit = Math.max(profit, prices[r] - prices[l]);
16+
r++;
17+
} else {
18+
// If profit is negative, move forware left and right pointer
19+
l = r;
20+
r++;
21+
}
22+
}
23+
return profit;
24+
};
25+
26+
// TC: O(n)
27+
// SC: O(3)
28+
29+
console.log(maxProfit([7, 1, 5, 3, 6, 4])); //5
30+
console.log(maxProfit([7, 6, 4, 3, 1])); //0

contains-duplicate/nhistoy.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function (nums) {
6+
// 1. Make hashmap that has previous value
7+
let map = {};
8+
// 2. Iterate nums to check nums[i] is duplicated or not
9+
for (let i = 0; i < nums.length; i++) {
10+
// 2.1 Check there is same value on the map
11+
// 3. Return true when their is duplicated value
12+
if (nums[i] in map) {
13+
return true;
14+
} else map[nums[i]] = i;
15+
}
16+
return false;
17+
};
18+
19+
// TC: O(n)
20+
// SC: O(n)
21+
22+
console.log(containsDuplicate([1, 2, 3, 1])); // true
23+
console.log(containsDuplicate([1, 2, 3, 4])); // false
24+
console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true

two-sum/nhistory.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function (nums, target) {
7+
// 1. Make a Hashmap to store { key(index) : value(target-num)}
8+
let map = {};
9+
// 2. Iterate to find value is equal to (target - nums[i])
10+
// There is only one solution
11+
for (let i = 0; i < nums.length; i++) {
12+
const diff = target - nums[i];
13+
// 3. If there is an index that has different value, return array
14+
if (diff in map) return [map[diff], i];
15+
map[nums[i]] = i;
16+
}
17+
};
18+
19+
// TC: O(n)
20+
// SC: O(n)
21+
22+
console.log(twoSum([2, 7, 11, 15], 9));
23+
console.log(twoSum([3, 2, 4], 6));

valid-anagram/nhistory.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
var isAnagram = function (s, t) {
7+
/*
8+
// Split string and sort charaters
9+
// Compare s and t after sorting
10+
return s.split("").sort().join() === t.split("").sort().join()
11+
// 92ms, 53.29MB
12+
*/
13+
14+
// Made Hashmap and count number of each charater
15+
let map = {};
16+
17+
// Check character length between s and t
18+
if (s.length !== t.length) return false;
19+
// Put a character and add or substract number
20+
for (let i = 0; i < s.length; i++) {
21+
// map[s[i]] = map[s[i]] ? map[s[i]] + 1 : 1;
22+
map[s[i]] = (map[s[i]] ?? 0) + 1;
23+
// map[t[i]] = map[t[i]] ? map[t[i]] - 1 : -1;
24+
map[t[i]] = (map[t[i]] ?? 0) - 1;
25+
}
26+
27+
for (let i = 0; i < s.length; i++) {
28+
if (map[s[i]] !== 0) return false;
29+
}
30+
31+
return true;
32+
};
33+
34+
console.log(isAnagram("anagram", "nagaram"));
35+
console.log(isAnagram("rat", "car"));
36+
37+
// TC: O(n)
38+
// SC: O(n)

valid-palindrome/nhistory.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function (s) {
6+
// 1. Create left and right pointer
7+
let l = 0;
8+
let r = s.length - 1;
9+
// 2. Iterate while loop with l<r condition
10+
while (l < r) {
11+
// 3. Check character with left pointer is alphanumeric character or not
12+
if (
13+
(s[l] >= "a" && s[l] <= "z") ||
14+
(s[l] >= "A" && s[l] <= "Z") ||
15+
(s[l] >= "0" && s[l] <= "9")
16+
) {
17+
// 4. Check character with right pointer is alphanumeric character or not
18+
if (
19+
(s[r] >= "a" && s[r] <= "z") ||
20+
(s[r] >= "A" && s[r] <= "Z") ||
21+
(s[r] >= "0" && s[r] <= "9")
22+
) {
23+
// 5. Compare left and right pointer character
24+
if (s[l].toLowerCase() !== s[r].toLowerCase()) {
25+
return false;
26+
} else {
27+
l++;
28+
r--;
29+
}
30+
// If not, go to next location for right pointer
31+
} else r--;
32+
33+
// If not, go to next location for left pointer
34+
} else l++;
35+
}
36+
return true;
37+
};
38+
39+
// TC: O(n)
40+
// SC: O(n)
41+
42+
console.log(isPalindrome("A man, a plan, a canal: Panama")); //true
43+
console.log(isPalindrome("race a car")); //false

0 commit comments

Comments
 (0)