Skip to content

Commit

Permalink
Merge pull request #918 from HiGeuni/main
Browse files Browse the repository at this point in the history
[khyo] Week 6
  • Loading branch information
SamTheKorean authored Jan 19, 2025
2 parents 0b25e76 + df8a888 commit e7dd108
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
28 changes: 28 additions & 0 deletions container-with-most-water/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {number[]} height
* @return {number}
*
* complexity
* time: O(n)
* space: O(1)
*/

var maxArea = function(height) {
let answer = 0;
let l = 0, r = height.length - 1;

while(l < r) {
const area = (r - l) * Math.min(height[l], height[r]);

answer = area > answer ? area : answer;

if(height[l] < height[r]) {
l += 1;
}else {
r -= 1;
}
}

return answer;
};

41 changes: 41 additions & 0 deletions longest-increasing-subsequence/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {number[]} nums
* @return {number}
*
* complexity
* time: O(nlogn) : lower_bound : O(logn), for문 : O(n)
* space: O(n)
*/
var lengthOfLIS = function(nums) {
const lower_bound = (arr, value) => {
let ret = -1;
let l = 0;
let r = arr.length - 1;

while(l <= r) {
const mid = Math.floor((l + r) / 2);
if(arr[mid] >= value) {
ret = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}

return ret;
}

let sequence = [];

for(let i=0; i<nums.length; i++) {
const ret = lower_bound(sequence, nums[i])
if(ret === -1) {
sequence.push(nums[i]);
} else {
sequence[ret] = nums[i]
}
}

return sequence.length;
};

47 changes: 47 additions & 0 deletions valid-parentheses/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Stack {
constructor() {
this.container = [];
}

push(item) {
this.container.push(item);
}

pop() {
return this.container.pop();
}

top() {
if (this.container.length === 0) return null;
return this.container[this.container.length - 1];
}

isEmpty() {
return this.container.length === 0;
}
}

/**
* @param {string} s
* @return {boolean}
*
* complexity
* time: O(n)
* space: O(n)
*/
var isValid = function(s) {
const stack = new Stack();
for (let x of s) {
if (x === '(' || x === '[' || x === '{') {
stack.push(x);
} else {
if (stack.isEmpty()) return false;
if (x === ')' && stack.top() !== '(') return false;
if (x === ']' && stack.top() !== '[') return false;
if (x === '}' && stack.top() !== '{') return false;
stack.pop();
}
}
return stack.isEmpty();
};

0 comments on commit e7dd108

Please sign in to comment.