diff --git a/jiho/0001-two-sum/0001-two-sum.ts b/jiho/0001-two-sum/0001-two-sum.ts new file mode 100644 index 0000000..aa759d9 --- /dev/null +++ b/jiho/0001-two-sum/0001-two-sum.ts @@ -0,0 +1,15 @@ +function twoSum(nums: number[], target: number): number[] { + const map = new Map () + + for(const [i, num] of nums.entries()) { + const rest = target - num + + if(map.has(rest)) { + return [i, map.get(rest)] + } + + map.set(num, i) + } + + return [] +}; \ No newline at end of file diff --git a/jiho/0009-palindrome-number/0009-palindrome-number.ts b/jiho/0009-palindrome-number/0009-palindrome-number.ts new file mode 100644 index 0000000..12ca6df --- /dev/null +++ b/jiho/0009-palindrome-number/0009-palindrome-number.ts @@ -0,0 +1,26 @@ +function get(x:number) { + let count = 1 + while(Math.floor(x / count) >= 10) { + count *=10 + } + + return count +} + +function isPalindrome(x: number): boolean { + if(x < 0) return false + + let div= get(x) + + while(x > 0) { + let left = Math.floor(x/div) + let right = x % 10 + + if(left !== right) return false + + x = Math.floor((x % div) / 10) + div /= 100 + } + + return true +}; \ No newline at end of file diff --git a/jiho/0020-valid-parentheses/0020-valid-parentheses.ts b/jiho/0020-valid-parentheses/0020-valid-parentheses.ts new file mode 100644 index 0000000..ef4ceaf --- /dev/null +++ b/jiho/0020-valid-parentheses/0020-valid-parentheses.ts @@ -0,0 +1,25 @@ +function isValid(s: string): boolean { + if(s.length % 2 === 1) return false + + const stock = [] + + for(const char of s) { + + if(stock.at(-1) === '(' && char === ')') { + stock.pop() + continue + } + if(stock.at(-1) === '{' && char === '}') { + stock.pop() + continue + } + if(stock.at(-1) === '[' && char === ']') { + stock.pop() + continue + } + + stock.push(char) + } + + return stock.length === 0 +}; \ No newline at end of file diff --git a/jiho/0020-valid-parentheses/README.md b/jiho/0020-valid-parentheses/README.md new file mode 100644 index 0000000..1aba866 --- /dev/null +++ b/jiho/0020-valid-parentheses/README.md @@ -0,0 +1,58 @@ +

20. Valid Parentheses

Easy


Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

+ +

An input string is valid if:

+ +
    +
  1. Open brackets must be closed by the same type of brackets.
  2. +
  3. Open brackets must be closed in the correct order.
  4. +
  5. Every close bracket has a corresponding open bracket of the same type.
  6. +
+ +

 

+

Example 1:

+ +
+

Input: s = "()"

+ +

Output: true

+
+ +

Example 2:

+ +
+

Input: s = "()[]{}"

+ +

Output: true

+
+ +

Example 3:

+ +
+

Input: s = "(]"

+ +

Output: false

+
+ +

Example 4:

+ +
+

Input: s = "([])"

+ +

Output: true

+
+ +

Example 5:

+ +
+

Input: s = "([)]"

+ +

Output: false

+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0049-group-anagrams/0049-group-anagrams.ts b/jiho/0049-group-anagrams/0049-group-anagrams.ts new file mode 100644 index 0000000..d8a9368 --- /dev/null +++ b/jiho/0049-group-anagrams/0049-group-anagrams.ts @@ -0,0 +1,18 @@ +function groupAnagrams(strs: string[]): string[][] { + // 목표: 애나그램끼리 그룹핑 + // 완전탐색 필요. 현재의 요소와 모든 요소를 비교해봐야 한다. + + const result = new Map() + + for(const str of strs) { + const key = str.split('').sort().join('') + + if(!result.has(key)) { + result.set(key, []) + } + + result.get(key).push(str) + } + + return Array.from(result.values()) +}; \ No newline at end of file diff --git a/jiho/0049-group-anagrams/README.md b/jiho/0049-group-anagrams/README.md new file mode 100644 index 0000000..9d4e57a --- /dev/null +++ b/jiho/0049-group-anagrams/README.md @@ -0,0 +1,43 @@ +

49. Group Anagrams

Medium


Given an array of strings strs, group the anagrams together. You can return the answer in any order.

+ +

 

+

Example 1:

+ +
+

Input: strs = ["eat","tea","tan","ate","nat","bat"]

+ +

Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

+ +

Explanation:

+ + +
+ +

Example 2:

+ +
+

Input: strs = [""]

+ +

Output: [[""]]

+
+ +

Example 3:

+ +
+

Input: strs = ["a"]

+ +

Output: [["a"]]

+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0054-spiral-matrix/0054-spiral-matrix.ts b/jiho/0054-spiral-matrix/0054-spiral-matrix.ts new file mode 100644 index 0000000..0feb29c --- /dev/null +++ b/jiho/0054-spiral-matrix/0054-spiral-matrix.ts @@ -0,0 +1,39 @@ +function spiralOrder(matrix: number[][]): number[] { + // 무엇을 기억해? 숫자 (x) -> 좌표! + // 한번 움직일 때 바뀌는 것 위치 + // 언제 종료? 다 돌았을 때 + // 예외는? -> 그리드 모서리? 각 경계 + const result = [] + let top = 0 + let right = matrix[0].length -1 + let left = 0 + let bottom = matrix.length -1 + + while(top <= bottom && left <= right){ + for(let col = left; col <=right; col++ ) { + result.push(matrix[top][col]) + } + top+=1 + + for(let row = top; row <= bottom; row++) { + result.push(matrix[row][right]) + } + right-- + + if(top <= bottom) { + for(let col = right; col >= left; col--) { + result.push(matrix[bottom][col]) + } + bottom-- + } + + if(left <= right) { + for(let row = bottom; row >= top; row--){ + result.push(matrix[row][left]) + } + left++ + } + } + + return result +}; \ No newline at end of file diff --git a/jiho/0054-spiral-matrix/README.md b/jiho/0054-spiral-matrix/README.md new file mode 100644 index 0000000..dc1c5d0 --- /dev/null +++ b/jiho/0054-spiral-matrix/README.md @@ -0,0 +1,26 @@ +

54. Spiral Matrix

Medium


Given an m x n matrix, return all elements of the matrix in spiral order.

+ +

 

+

Example 1:

+ +
+Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
+Output: [1,2,3,6,9,8,7,4,5]
+
+ +

Example 2:

+ +
+Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
+Output: [1,2,3,4,8,12,11,10,9,5,6,7]
+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0059-spiral-matrix-ii/0059-spiral-matrix-ii.ts b/jiho/0059-spiral-matrix-ii/0059-spiral-matrix-ii.ts new file mode 100644 index 0000000..32ac6ec --- /dev/null +++ b/jiho/0059-spiral-matrix-ii/0059-spiral-matrix-ii.ts @@ -0,0 +1,41 @@ +function generateMatrix(n: number): number[][] { + const matrix = Array.from({ length: n }, (_, i) => Array(n).fill(0)); + let num = 0 + let top =0 + let left = 0 + let right = n-1 + let bottom = n-1 + + + while(top <= bottom && left <= right) { + //왼 -> 오 + for(let col = left; col <= right; col++){ + matrix[top][col] = num+=1 + } + top++ + + //위 -> 아래 + for(let row = top; row <= bottom; row++) { + matrix[row][right] = num+=1 + } + right-- + + //오 -> 왼 + if(left <= right) { + for(let col = right; col >= left; col--) { + matrix[bottom][col] = num+=1 + } + } + bottom-- + + // 아래 -> 위 + if(top <= bottom) { + for(let row = bottom; row >= top; row--) { + matrix[row][left] = num+=1 + } + } + left++ + } + + return matrix +}; \ No newline at end of file diff --git a/jiho/0059-spiral-matrix-ii/README.md b/jiho/0059-spiral-matrix-ii/README.md new file mode 100644 index 0000000..2760de2 --- /dev/null +++ b/jiho/0059-spiral-matrix-ii/README.md @@ -0,0 +1,23 @@ +

59. Spiral Matrix II

Medium


Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

+ +

 

+

Example 1:

+ +
+Input: n = 3
+Output: [[1,2,3],[8,9,4],[7,6,5]]
+
+ +

Example 2:

+ +
+Input: n = 1
+Output: [[1]]
+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0062-unique-paths/0062-unique-paths.ts b/jiho/0062-unique-paths/0062-unique-paths.ts new file mode 100644 index 0000000..97b97fa --- /dev/null +++ b/jiho/0062-unique-paths/0062-unique-paths.ts @@ -0,0 +1,20 @@ +function uniquePaths(m: number, n: number): number { + const dp = Array.from({length: m}, () => Array(n).fill(0)) + + dp[0][0] = 1 + for(let i = 1; i < m; i++){ + dp[i][0] = 1 + } + + for(let j =1; j62. Unique Paths

Medium


There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

+ +

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

+ +

The test cases are generated so that the answer will be less than or equal to 2 * 109.

+ +

 

+

Example 1:

+ +
+Input: m = 3, n = 7
+Output: 28
+
+ +

Example 2:

+ +
+Input: m = 3, n = 2
+Output: 3
+Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
+1. Right -> Down -> Down
+2. Down -> Down -> Right
+3. Down -> Right -> Down
+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0064-minimum-path-sum/0064-minimum-path-sum.ts b/jiho/0064-minimum-path-sum/0064-minimum-path-sum.ts new file mode 100644 index 0000000..b54f3e2 --- /dev/null +++ b/jiho/0064-minimum-path-sum/0064-minimum-path-sum.ts @@ -0,0 +1,25 @@ +function minPathSum(grid: number[][]): number { + // 대각선 고려 안해도 됨 + const m =grid.length + const n = grid[0].length + // 합 저장 + const dp = Array.from({length: m}, () => Array(n).fill(0)) + + dp[0][0] = grid[0][0] + + for(let i =1; i< m; i++) { + dp[i][0] = dp[i-1][0] + grid[i][0] + } + + for(let j = 1; j < n; j++) { + dp[0][j] = dp[0][j-1] + grid[0][j] + } + + for(let i=1 ; i < m ; i++) { + for(let j = 1; j < n; j++) { + dp[i][j] = Math.min(dp[i][j-1], dp[i-1][j]) + grid[i][j] + } + } + + return dp[m -1 ][n-1] +}; \ No newline at end of file diff --git a/jiho/0064-minimum-path-sum/README.md b/jiho/0064-minimum-path-sum/README.md new file mode 100644 index 0000000..83eb5e5 --- /dev/null +++ b/jiho/0064-minimum-path-sum/README.md @@ -0,0 +1,29 @@ +

64. Minimum Path Sum

Medium


Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.

+ +

Note: You can only move either down or right at any point in time.

+ +

 

+

Example 1:

+ +
+Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
+Output: 7
+Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
+
+ +

Example 2:

+ +
+Input: grid = [[1,2,3],[4,5,6]]
+Output: 12
+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0120-triangle/0120-triangle.ts b/jiho/0120-triangle/0120-triangle.ts new file mode 100644 index 0000000..54cc19d --- /dev/null +++ b/jiho/0120-triangle/0120-triangle.ts @@ -0,0 +1,17 @@ +function minimumTotal(triangle: number[][]): number { + const n = triangle.length + const dp = Array.from({length:n}, (_, i) => Array(i+1).fill(0) ) + + dp[0][0] = triangle[0][0] + + for(let i = 1; i< n ;i ++) { + dp[i][0] = dp[i-1][0] + triangle[i][0] + dp[i][i] = dp[i-1][i-1] + triangle[i][i] + + for(let j = 1; j < i; j++) { + dp[i][j] = Math.min(dp[i-1][j-1], dp[i-1][j]) + triangle[i][j] + } + } + + return Math.min(...dp[n-1]) +}; \ No newline at end of file diff --git a/jiho/0120-triangle/README.md b/jiho/0120-triangle/README.md new file mode 100644 index 0000000..041b5f2 --- /dev/null +++ b/jiho/0120-triangle/README.md @@ -0,0 +1,37 @@ +

120. Triangle

Medium


Given a triangle array, return the minimum path sum from top to bottom.

+ +

For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.

+ +

 

+

Example 1:

+ +
+Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
+Output: 11
+Explanation: The triangle looks like:
+   2
+  3 4
+ 6 5 7
+4 1 8 3
+The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
+
+ +

Example 2:

+ +
+Input: triangle = [[-10]]
+Output: -10
+
+ +

 

+

Constraints:

+ + + +

 

+Follow up: Could you do this using only O(n) extra space, where n is the total number of rows in the triangle? \ No newline at end of file diff --git a/jiho/0125-valid-palindrome/0125-valid-palindrome.ts b/jiho/0125-valid-palindrome/0125-valid-palindrome.ts new file mode 100644 index 0000000..cd44029 --- /dev/null +++ b/jiho/0125-valid-palindrome/0125-valid-palindrome.ts @@ -0,0 +1,29 @@ +function isPalindrome(s: string): boolean { + + const isValid = (s) => { + const code = s.toLowerCase().charCodeAt(0); + + const isNumber = code >= 48 && code <= 57 + const isChar = code >= 97 && code <=122 + + return isNumber || isChar + } + + let cleaned = '' + for(const char of s) { + if(isValid(char)) { + cleaned += char.toLowerCase() + } + } + + let left = 0 + let right = cleaned.length -1 + + while(left < right) { + if(cleaned[left] !== cleaned[right]) return false + left +=1 + right -=1 + } + + return true +}; \ No newline at end of file diff --git a/jiho/0165-compare-version-numbers/0165-compare-version-numbers.ts b/jiho/0165-compare-version-numbers/0165-compare-version-numbers.ts new file mode 100644 index 0000000..5a9ca4f --- /dev/null +++ b/jiho/0165-compare-version-numbers/0165-compare-version-numbers.ts @@ -0,0 +1,17 @@ +function compareVersion(version1: string, version2: string): number { + const maxLen = Math.max(version1.length, version2.length) + const v1 = version1.split('.') + const v2 = version2.split('.') + + for(let i = 0; i < maxLen; i++) { + const number1 = Number(v1[i]??0) + const number2 = Number(v2[i]??0) + + if(number1 > number2) return 1 + if(number2 > number1) return -1 + } + + + + return 0 +}; \ No newline at end of file diff --git a/jiho/0165-compare-version-numbers/README.md b/jiho/0165-compare-version-numbers/README.md new file mode 100644 index 0000000..25173ea --- /dev/null +++ b/jiho/0165-compare-version-numbers/README.md @@ -0,0 +1,58 @@ +

165. Compare Version Numbers

Medium


Given two version strings, version1 and version2, compare them. A version string consists of revisions separated by dots '.'. The value of the revision is its integer conversion ignoring leading zeros.

+ +

To compare version strings, compare their revision values in left-to-right order. If one of the version strings has fewer revisions, treat the missing revision values as 0.

+ +

Return the following:

+ + + +

 

+

Example 1:

+ +
+

Input: version1 = "1.2", version2 = "1.10"

+ +

Output: -1

+ +

Explanation:

+ +

version1's second revision is "2" and version2's second revision is "10": 2 < 10, so version1 < version2.

+
+ +

Example 2:

+ +
+

Input: version1 = "1.01", version2 = "1.001"

+ +

Output: 0

+ +

Explanation:

+ +

Ignoring leading zeroes, both "01" and "001" represent the same integer "1".

+
+ +

Example 3:

+ +
+

Input: version1 = "1.0", version2 = "1.0.0.0"

+ +

Output: 0

+ +

Explanation:

+ +

version1 has less revisions, which means every missing revision are treated as "0".

+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0200-number-of-islands/0200-number-of-islands.ts b/jiho/0200-number-of-islands/0200-number-of-islands.ts new file mode 100644 index 0000000..bfb1b09 --- /dev/null +++ b/jiho/0200-number-of-islands/0200-number-of-islands.ts @@ -0,0 +1,44 @@ +const directions = [ + [0,1], + [1,0], + [-1, 0], + [0, -1] +] + +function numIslands(grid: string[][]): number { + const M = grid.length + const N = grid[0].length + + const bfs =(x: number,y:number) => { + const queue = [[x,y]] + grid[x][y] = "0" + + while(queue.length) { + const [cx,cy] = queue.shift()! + + for(const [dx, dy] of directions) { + const nextX = cx + dx + const nextY = cy + dy + + if(nextX >= 0 && nextY >=0 && nextX < M && nextY < N && grid[nextX][nextY]==='1') { + grid[nextX][nextY] = '0' + queue.push([nextX,nextY]) + } + + } + } + } + + let result = 0 + + for(let i =0; i < M; i ++) { + for(let j =0; j < N; j ++) { + if(grid[i][j]==='1') { + bfs(i,j) + result +=1 + } + } + } + + return result +}; \ No newline at end of file diff --git a/jiho/0200-number-of-islands/README.md b/jiho/0200-number-of-islands/README.md new file mode 100644 index 0000000..42a12c0 --- /dev/null +++ b/jiho/0200-number-of-islands/README.md @@ -0,0 +1,38 @@ +

200. Number of Islands

Medium


Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

+ +

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

+ +

 

+

Example 1:

+ +
+Input: grid = [
+  ["1","1","1","1","0"],
+  ["1","1","0","1","0"],
+  ["1","1","0","0","0"],
+  ["0","0","0","0","0"]
+]
+Output: 1
+
+ +

Example 2:

+ +
+Input: grid = [
+  ["1","1","0","0","0"],
+  ["1","1","0","0","0"],
+  ["0","0","1","0","0"],
+  ["0","0","0","1","1"]
+]
+Output: 3
+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0221-maximal-square/0221-maximal-square.ts b/jiho/0221-maximal-square/0221-maximal-square.ts new file mode 100644 index 0000000..35d9e54 --- /dev/null +++ b/jiho/0221-maximal-square/0221-maximal-square.ts @@ -0,0 +1,31 @@ +function maximalSquare(matrix: string[][]): number { + const m = matrix.length + const n =matrix[0].length + const dp = Array.from({length: m }, () => Array(n).fill(0)) + let max = 0 + + for(let j = 0; j < n; j++) { + dp[0][j] = Number(matrix[0][j]) + max = Math.max(max, dp[0][j]) + } + + for(let i = 0; i < m; i++) { + dp[i][0] = Number(matrix[i][0]) + max = Math.max(max, dp[i][0]) + } + + for(let i = 1; i < m; i++) { + for(let j = 1; j < n; j++) { + if(matrix[i][j] === '0') { + dp[i][j] = 0 + }else { + dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) +1 + } + max = Math.max(max, dp[i][j]) + } + + } + + return max * max + +}; \ No newline at end of file diff --git a/jiho/0221-maximal-square/README.md b/jiho/0221-maximal-square/README.md new file mode 100644 index 0000000..9fc0fb8 --- /dev/null +++ b/jiho/0221-maximal-square/README.md @@ -0,0 +1,33 @@ +

221. Maximal Square

Medium


Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

+ +

 

+

Example 1:

+ +
+Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
+Output: 4
+
+ +

Example 2:

+ +
+Input: matrix = [["0","1"],["1","0"]]
+Output: 1
+
+ +

Example 3:

+ +
+Input: matrix = [["0"]]
+Output: 0
+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0268-missing-number/0268-missing-number.ts b/jiho/0268-missing-number/0268-missing-number.ts new file mode 100644 index 0000000..5f5c084 --- /dev/null +++ b/jiho/0268-missing-number/0268-missing-number.ts @@ -0,0 +1,15 @@ +function missingNumber(nums: number[]): number { + const set = new Set() + for(let i = 0; i <= nums.length; i++){ + set.add(i) + } + + + for(const num of nums) { + if(set.has(num)) set.delete(num) + } + + const result = set.values() + + return result.next().value +}; \ No newline at end of file diff --git a/jiho/0268-missing-number/README.md b/jiho/0268-missing-number/README.md new file mode 100644 index 0000000..9910f82 --- /dev/null +++ b/jiho/0268-missing-number/README.md @@ -0,0 +1,69 @@ +

268. Missing Number

Easy


Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

+ +

 

+

Example 1:

+ +
+

Input: nums = [3,0,1]

+ +

Output: 2

+ +

Explanation:

+ +

n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.

+
+ +

Example 2:

+ +
+

Input: nums = [0,1]

+ +

Output: 2

+ +

Explanation:

+ +

n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.

+
+ +

Example 3:

+ +
+

Input: nums = [9,6,4,2,3,5,7,0,1]

+ +

Output: 8

+ +

Explanation:

+ +

n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.

+
+ +
+
+
 
+ +
+
+
 
+ +
+

 

+ +

 

+
+
+
+
+
+ +

 

+

Constraints:

+ + + +

 

+

Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?

diff --git a/jiho/0289-game-of-life/0289-game-of-life.ts b/jiho/0289-game-of-life/0289-game-of-life.ts new file mode 100644 index 0000000..b94450c --- /dev/null +++ b/jiho/0289-game-of-life/0289-game-of-life.ts @@ -0,0 +1,55 @@ +/** + Do not return anything, modify board in-place instead. + */ + + +const directions = [ + [1, 0], + [-1, 0], + [1, 1], + [0, 1], + [0, -1], + [-1, -1], + [-1, 1], + [1, -1] +] + +function gameOfLife(board: number[][]): void { + // 기억해야 할 것 현재 위치에서의 주변 세포 + // 움직였을때 바뀌는 것 -> 보드의 모든 세포의 상태 (살아있음 1, 죽음 0) + // 종료 조건 + // 예외 ->보드의 양 끝 모서리 + // 주변 live <2 -> 0 + // 주변 live === 2 || live === 3 -> 1 + // 주변 Live > 3 -> 0 + + const N = board[0].length + const M = board.length + + const next = board.map(row => [...row]) + + for(let x = 0; x < M; x++) { + for(let y = 0; y < N; y ++) { + let live = 0 + + for(const [dx, dy] of directions) { + const nx = dx + x + const ny = dy + y + + if(nx >= 0 && ny >= 0 && nx < M && ny < N && next[nx][ny] === 1) { + live +=1 + } + } + + + if(next[x][y] === 1) { + if(live < 2) board[x][y] = 0 + if(live ===2 || live === 3) board[x][y] = 1 + else board[x][y] = 0 + } else { + if(live === 3) board[x][y] = 1 + else board[x][y] = 0 + } + } + } +}; \ No newline at end of file diff --git a/jiho/0289-game-of-life/README.md b/jiho/0289-game-of-life/README.md new file mode 100644 index 0000000..59e6d8e --- /dev/null +++ b/jiho/0289-game-of-life/README.md @@ -0,0 +1,49 @@ +

289. Game of Life

Medium


According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

+ +

The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

+ +
    +
  1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
  2. +
  3. Any live cell with two or three live neighbors lives on to the next generation.
  4. +
  5. Any live cell with more than three live neighbors dies, as if by over-population.
  6. +
  7. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
  8. +
+ +

The next state of the board is determined by applying the above rules simultaneously to every cell in the current state of the m x n grid board. In this process, births and deaths occur simultaneously.

+ +

Given the current state of the board, update the board to reflect its next state.

+ +

Note that you do not need to return anything.

+ +

 

+

Example 1:

+ +
+Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
+Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
+
+ +

Example 2:

+ +
+Input: board = [[1,1],[1,0]]
+Output: [[1,1],[1,1]]
+
+ +

 

+

Constraints:

+ + + +

 

+

Follow up:

+ + diff --git a/jiho/0383-ransom-note/0383-ransom-note.ts b/jiho/0383-ransom-note/0383-ransom-note.ts new file mode 100644 index 0000000..f7f533e --- /dev/null +++ b/jiho/0383-ransom-note/0383-ransom-note.ts @@ -0,0 +1,15 @@ +function canConstruct(ransomNote: string, magazine: string): boolean { + const map = new Map() + + for(const char of ransomNote) { + map.set(char, (map.get(char) ?? 0) +1) + } + + for(const char of magazine) { + if(map.has(char)) { + map.set(char, map.get(char) -1) + } + } + + return ![...map.values()].some((value) => value > 0) +}; \ No newline at end of file diff --git a/jiho/0383-ransom-note/README.md b/jiho/0383-ransom-note/README.md new file mode 100644 index 0000000..7dc5f97 --- /dev/null +++ b/jiho/0383-ransom-note/README.md @@ -0,0 +1,22 @@ +

383. Ransom Note

Easy


Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.

+ +

Each letter in magazine can only be used once in ransomNote.

+ +

 

+

Example 1:

+
Input: ransomNote = "a", magazine = "b"
+Output: false
+

Example 2:

+
Input: ransomNote = "aa", magazine = "ab"
+Output: false
+

Example 3:

+
Input: ransomNote = "aa", magazine = "aab"
+Output: true
+
+

 

+

Constraints:

+ + diff --git a/jiho/0415-add-strings/0415-add-strings.ts b/jiho/0415-add-strings/0415-add-strings.ts new file mode 100644 index 0000000..a91f630 --- /dev/null +++ b/jiho/0415-add-strings/0415-add-strings.ts @@ -0,0 +1,3 @@ +function addStrings(num1: string, num2: string): string { + return String(BigInt(num1) + BigInt(num2)) +}; \ No newline at end of file diff --git a/jiho/0415-add-strings/README.md b/jiho/0415-add-strings/README.md new file mode 100644 index 0000000..f63cdb9 --- /dev/null +++ b/jiho/0415-add-strings/README.md @@ -0,0 +1,34 @@ +

415. Add Strings

Easy


Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.

+ +

You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.

+ +

 

+

Example 1:

+ +
+Input: num1 = "11", num2 = "123"
+Output: "134"
+
+ +

Example 2:

+ +
+Input: num1 = "456", num2 = "77"
+Output: "533"
+
+ +

Example 3:

+ +
+Input: num1 = "0", num2 = "0"
+Output: "0"
+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0498-diagonal-traverse/0498-diagonal-traverse.ts b/jiho/0498-diagonal-traverse/0498-diagonal-traverse.ts new file mode 100644 index 0000000..2b78aad --- /dev/null +++ b/jiho/0498-diagonal-traverse/0498-diagonal-traverse.ts @@ -0,0 +1,47 @@ +function findDiagonalOrder(mat: number[][]): number[] { + const result = [] + + // 나올 수 있는 방향 : 왼 -> 오, 위 -> 대각선 왼쪽 아래, 위 -> 아래, 아래 -> 대각선 우측 위 + // 기억해야 하는 것: 방향. row, col, dirction + // 종료 조건: 마지막 그리드. result.length === m*n + + const M = mat.length + const N = mat[0].length + let row = 0 + let col = 0 + let dir = 1; // 1이면 오른쪽 위, -1이면 왼쪽 아래 + + while (result.length < M * N){ + // 현재 칸을 먼저 result에 넣는다. + result.push(mat[row][col]) + // 다음 칸 후보를 계산한다. + + if(dir === 1) { + // 오른쪽 위로 이동 + if(col === N -1) { + row++ + dir = -1 + } else if(row === 0) { + col++ + dir = -1 + } else { + row--; + col++; + } + }else { + //왼쪽 아래로 이동 + if(row === M - 1 ) { + col++ + dir = 1 + } else if (col === 0) { + row++ + dir = 1 + }else { + row++ + col-- + } + } + } + + return result +}; \ No newline at end of file diff --git a/jiho/0498-diagonal-traverse/README.md b/jiho/0498-diagonal-traverse/README.md new file mode 100644 index 0000000..a5c6d7c --- /dev/null +++ b/jiho/0498-diagonal-traverse/README.md @@ -0,0 +1,27 @@ +

498. Diagonal Traverse

Medium


Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.

+ +

 

+

Example 1:

+ +
+Input: mat = [[1,2,3],[4,5,6],[7,8,9]]
+Output: [1,2,4,7,5,3,6,8,9]
+
+ +

Example 2:

+ +
+Input: mat = [[1,2],[3,4]]
+Output: [1,2,3,4]
+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0682-baseball-game/0682-baseball-game.ts b/jiho/0682-baseball-game/0682-baseball-game.ts new file mode 100644 index 0000000..f0de719 --- /dev/null +++ b/jiho/0682-baseball-game/0682-baseball-game.ts @@ -0,0 +1,29 @@ +function calPoints(operations: string[]): number { + const scores = [] + + for (const operation of operations ){ + const num = Number(operation) + if(!isNaN(num)) { + scores.push(num) + } + + if(operation === 'C') { + scores.pop() + } + + if(operation === 'D') { + scores.push(2 * scores.at(-1)) + } + + if(operation === '+') { + scores.push(scores.at(-1)+scores.at(-2)) + } + } + + let sum = 0 + for(const score of scores ) { + sum+=score + } + + return sum +}; \ No newline at end of file diff --git a/jiho/0682-baseball-game/README.md b/jiho/0682-baseball-game/README.md new file mode 100644 index 0000000..ba8d270 --- /dev/null +++ b/jiho/0682-baseball-game/README.md @@ -0,0 +1,84 @@ +

682. Baseball Game

Easy


You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.

+ +

You are given a list of strings operations, where operations[i] is the ith operation you must apply to the record and is one of the following:

+ + + +

Return the sum of all the scores on the record after applying all the operations.

+ +

The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.

+ +

 

+

Example 1:

+ +
+Input: ops = ["5","2","C","D","+"]
+Output: 30
+Explanation:
+"5" - Add 5 to the record, record is now [5].
+"2" - Add 2 to the record, record is now [5, 2].
+"C" - Invalidate and remove the previous score, record is now [5].
+"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
+"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
+The total sum is 5 + 10 + 15 = 30.
+
+ +

Example 2:

+ +
+Input: ops = ["5","-2","4","C","D","9","+","+"]
+Output: 27
+Explanation:
+"5" - Add 5 to the record, record is now [5].
+"-2" - Add -2 to the record, record is now [5, -2].
+"4" - Add 4 to the record, record is now [5, -2, 4].
+"C" - Invalidate and remove the previous score, record is now [5, -2].
+"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
+"9" - Add 9 to the record, record is now [5, -2, -4, 9].
+"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
+"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
+The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
+
+ +

Example 3:

+ +
+Input: ops = ["1","C"]
+Output: 0
+Explanation:
+"1" - Add 1 to the record, record is now [1].
+"C" - Invalidate and remove the previous score, record is now [].
+Since the record is empty, the total sum is 0.
+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0733-flood-fill/0733-flood-fill.ts b/jiho/0733-flood-fill/0733-flood-fill.ts new file mode 100644 index 0000000..2e5b2a8 --- /dev/null +++ b/jiho/0733-flood-fill/0733-flood-fill.ts @@ -0,0 +1,39 @@ +const directions = [ + [0,1], + [-1, 0], + [1, 0], + [0, -1] +] + +function floodFill(image: number[][], sr: number, sc: number, color: number): number[][] { + const M = image.length + const N = image[0].length + const originColor = image[sr][sc] + + if (originColor === color) return image + + const dfs =(x:number, y:number) =>{ + const stack =[[x, y]] + + image[x][y] = color + + + while(stack.length) { + const [cx, cy] = stack.pop()! + + for(const [dx, dy] of directions) { + const nextX = cx + dx + const nextY = cy + dy + + if(nextX >= 0 && nextY>=0 && nextX < M && nextY < N && image[nextX][nextY] === originColor) { + image[nextX][nextY] = color + stack.push([nextX,nextY]) + } + } + } + } + + dfs(sr, sc) + + return image +}; \ No newline at end of file diff --git a/jiho/0733-flood-fill/README.md b/jiho/0733-flood-fill/README.md new file mode 100644 index 0000000..5f56699 --- /dev/null +++ b/jiho/0733-flood-fill/README.md @@ -0,0 +1,53 @@ +

733. Flood Fill

Easy


You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. Your task is to perform a flood fill on the image starting from the pixel image[sr][sc].

+ +

To perform a flood fill:

+ +
    +
  1. Begin with the starting pixel and change its color to color.
  2. +
  3. Perform the same process for each pixel that is directly adjacent (pixels that share a side with the original pixel, either horizontally or vertically) and shares the same color as the starting pixel.
  4. +
  5. Keep repeating this process by checking neighboring pixels of the updated pixels and modifying their color if it matches the original color of the starting pixel.
  6. +
  7. The process stops when there are no more adjacent pixels of the original color to update.
  8. +
+ +

Return the modified image after performing the flood fill.

+ +

 

+

Example 1:

+ +
+

Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2

+ +

Output: [[2,2,2],[2,2,0],[2,0,1]]

+ +

Explanation:

+ +

+ +

From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.

+ +

Note the bottom corner is not colored 2, because it is not horizontally or vertically connected to the starting pixel.

+
+ +

Example 2:

+ +
+

Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0

+ +

Output: [[0,0,0],[0,0,0]]

+ +

Explanation:

+ +

The starting pixel is already colored with 0, which is the same as the target color. Therefore, no changes are made to the image.

+
+ +

 

+

Constraints:

+ + diff --git a/jiho/0844-backspace-string-compare/0844-backspace-string-compare.ts b/jiho/0844-backspace-string-compare/0844-backspace-string-compare.ts new file mode 100644 index 0000000..3ab5a48 --- /dev/null +++ b/jiho/0844-backspace-string-compare/0844-backspace-string-compare.ts @@ -0,0 +1,18 @@ +function backspaceCompare(s: string, t: string): boolean { + const stackOfS = [] + const stackOfT = [] + + for(const char of s) { + if(char === '#') stackOfS.pop() + else stackOfS.push(char) + } + + for(const char of t) { + if(char === '#') stackOfT.pop() + else stackOfT.push(char) + } + + + + return stackOfT.join('') === stackOfS.join('') +}; \ No newline at end of file diff --git a/jiho/0844-backspace-string-compare/README.md b/jiho/0844-backspace-string-compare/README.md new file mode 100644 index 0000000..be78c6d --- /dev/null +++ b/jiho/0844-backspace-string-compare/README.md @@ -0,0 +1,39 @@ +

844. Backspace String Compare

Easy


Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.

+ +

Note that after backspacing an empty text, the text will continue empty.

+ +

 

+

Example 1:

+ +
+Input: s = "ab#c", t = "ad#c"
+Output: true
+Explanation: Both s and t become "ac".
+
+ +

Example 2:

+ +
+Input: s = "ab##", t = "c#d#"
+Output: true
+Explanation: Both s and t become "".
+
+ +

Example 3:

+ +
+Input: s = "a#c", t = "b"
+Output: false
+Explanation: s becomes "c" while t becomes "b".
+
+ +

 

+

Constraints:

+ + + +

 

+

Follow up: Can you solve it in O(n) time and O(1) space?