본문 바로가기
  • Jetpack 알아보기

LeetCode18

[Java] LeetCode 455 : Assign Cookies LeetCode # 455 Example 1: Input: g = [1,2,3], s = [1,1] Output: 1 Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content. You need to output 1. Example 2: Input: g = [1,2], s = [1,2,3] Output: 2 Explanation: You have 2 children and 3 .. 2021. 5. 14.
[Java] LeetCode 561 : Array Partition I LeetCode # 561 Example 1: Input: nums = [1,4,3,2] Output: 4 Explanation: All possible pairings (ignoring the ordering of elements) are: 1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3 2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3 3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4 So the maximum possible sum is 4. Example 2: Input: nums = [6,2,6,5,1,2] Output: 9 Explanation: T.. 2021. 5. 14.
[Java] LeetCode 441 : Arranging Coins LeetCode # 441 Example 1: Input: n = 5 Output: 2 Explanation: Because the 3rd row is incomplete, we return 2. Example 2: Input: n = 8 Output: 3 Explanation: Because the 4th row is incomplete, we return 3. Note: 1 2021. 5. 14.
[Java] LeetCode 989 : Add to Array-Form of Integer LeetCode # 989 Example 1: Input: num = [1,2,0,0], k = 34 Output: [1,2,3,4] Explanation: 1200 + 34 = 1234 Example 2: Input: num = [9,9,9,9,9,9,9,9,9,9], k = 1 Output: [1,0,0,0,0,0,0,0,0,0,0] Explanation: 9999999999 + 1 = 10000000000 Constraints: 1 = 0 || sum > 0) { if (i >= 0) sum += (num[i]); list.addFirst(sum % 10); sum /= 10; i--; } return list; } } Result #2 💡 ArrayList에서 LinkedList로 자료형 변경 L.. 2021. 5. 13.
[Java] LeetCode 415 : Add Strings LeetCode # 415 Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string. Example 1: Input: num1 = "11", num2 = "123" Output: "134" Constraints: 1 2021. 5. 12.
[Java] LeetCode 258 : Add Digits LeetCode # 258 Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. Example 1: Input: num = 38 Output: 2 Explanation: The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. Constraints: 0 9 && num == 0) { num = sum; sum = 0; } } return sum; } } Result #1 More Algorithm! 👇👇 github.com/ggujangi/ggu.leet-code g.. 2021. 5. 12.
[Java] LeetCode 67 : Add Binary LeetCode # 67 Given two binary strings a and b, return their sum as a binary string. Example 1: Input: a = "11", b = "1" Output: "100" Constraints: 1 = 0) { result += Math.pow(2, s.length() - 1 - i) * (s.charAt(i) - 48); i--; } return result; } } Result #1 💡 2진수를 10진수로 변환하는 방법은 틀렸다. 가장 직관적으로 생각할 수 있는 방법은 2진수를 10진수로, 10진수를 2진수로 변환하여 풀이하는 것이다. 하지만 이 방법은 틀렸다. 자료형의 범위때문에 잘못된 값이 출력된다. Solution #2 cla.. 2021. 5. 12.
[Java] LeetCode 717 : 1-bit and 2-bit Characters LeetCode # 717 Example 1: Input: bits = [1, 0, 0] Output: True Explanation: The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character. Example 2: Input: bits = [1, 1, 1, 0] Output: False Explanation: The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character. Note: 1 2021. 4. 21.
[Java] LeetCode 953 : Verifying an Alien Dictionary LeetCode # 953 Example 1: Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz" Output: true Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted. Example 2: Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz" Output: false Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence i.. 2021. 4. 21.
[Java] LeetCode 874 : Walking Robot Simulation LeetCode # 874 Example 1: Input: commands = [4,-1,3], obstacles = [] Output: 25 Explanation: The robot starts at (0, 0): 1. Move north 4 units to (0, 4). 2. Turn right. 3. Move east 3 units to (3, 4). The furthest point away from the origin is (3, 4), which is 32 + 42 = 25 units away. Solution #1 (11ms) import java.awt.*; import java.util.HashSet; import java.util.Set; class Solution { int[] xSt.. 2021. 4. 17.