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 @@ +
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
+ ++
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:
+ +1 <= s.length <= 104s consists of parentheses only '()[]{}'.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:
+ +"bat"."nat" and "tan" are anagrams as they can be rearranged to form each other."ate", "eat", and "tea" are anagrams as they can be rearranged to form each other.Example 2:
+ +Input: strs = [""]
+ +Output: [[""]]
+Example 3:
+ +Input: strs = ["a"]
+ +Output: [["a"]]
++
Constraints:
+ +1 <= strs.length <= 1040 <= strs[i].length <= 100strs[i] consists of lowercase English letters.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:
+ +m == matrix.lengthn == matrix[i].length1 <= m, n <= 10-100 <= matrix[i][j] <= 100Given 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:
+ +1 <= n <= 20There 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:
+ +1 <= m, n <= 100Given 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:
+ +m == grid.lengthn == grid[i].length1 <= m, n <= 2000 <= grid[i][j] <= 200Given 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:
+ +1 <= triangle.length <= 200triangle[0].length == 1triangle[i].length == triangle[i - 1].length + 1-104 <= triangle[i][j] <= 104+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 @@
+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:
+ +version1 < version2, return -1.version1 > version2, return 1.+
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:
+ +1 <= version1.length, version2.length <= 500version1 and version2 only contain digits and '.'.version1 and version2 are valid version numbers.version1 and version2 can be stored in a 32-bit integer.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:
+ +m == grid.lengthn == grid[i].length1 <= m, n <= 300grid[i][j] is '0' or '1'.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:
+ +m == matrix.lengthn == matrix[i].length1 <= m, n <= 300matrix[i][j] is '0' or '1'.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:
+ +n == nums.length1 <= n <= 1040 <= nums[i] <= nnums are unique.+
Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
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):
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:
+ +m == board.lengthn == board[i].length1 <= m, n <= 25board[i][j] is 0 or 1.+
Follow up:
+ +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:
+ +1 <= ransomNote.length, magazine.length <= 105ransomNote and magazine consist of lowercase English letters.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:
+ +1 <= num1.length, num2.length <= 104num1 and num2 consist of only digits.num1 and num2 don't have any leading zeros except for the zero itself.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:
+ +m == mat.lengthn == mat[i].length1 <= m, n <= 1041 <= m * n <= 104-105 <= mat[i][j] <= 105You 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:
x.
+
+ x.'+'.
+ 'D'.
+ 'C'.
+ 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:
+ +1 <= operations.length <= 1000operations[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104]."+", there will always be at least two previous scores on the record."C" and "D", there will always be at least one previous score on the record.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:
+ +color.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:
+ +m == image.lengthn == image[i].length1 <= m, n <= 500 <= image[i][j], color < 2160 <= sr < m0 <= sc < nGiven 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:
+ +1 <= s.length, t.length <= 200s and t only contain lowercase letters and '#' characters.+
Follow up: Can you solve it in O(n) time and O(1) space?