-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #918 from HiGeuni/main
[khyo] Week 6
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
|