Cindy's Blog

アマゾンで働いているエンジニアの日常

2022-10-01から1ヶ月間の記事一覧

LeetCode 283. Move Zeroes

Follow up: Could you minimize the total number of operations done? https://leetcode.com/problems/move-zeroes/ //firstZeroIndex -> z // z: -1 // i //[1,3,12,0,0] //when nums[i] = 0 //if z = -1, then z = i //else //if z != -1 (had zero in fr…

LeetCode 200. Number of Islands

UnionFind island count direction https://leetcode.com/problems/number-of-islands/ class Solution { public int numIslands(char[][] grid) { int rows = grid.length; int cols = grid[0].length; int countIslands = 0; int[][] directions = {{0, 1}…

LeetCode 392. Is Subsequence

Easy question https://leetcode.com/problems/is-subsequence/ class Solution { public boolean isSubsequence(String s, String t) { int sIndex = 0; int tIndex = 0; while (sIndex < s.length() && tIndex < t.length()){ //"abc" // s //"ahbcgd" // …

LeetCode 20. Valid Parentheses

Considering with following cases "]" "{[}]" class Solution { public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); for (int i = 0; i < s.length(); i++){ char c = s.charAt(i); if (c == '(' || c == '[' || c == '{'){ stack.push(c); </character>…

LeetCode 967. Numbers With Same Consecutive Differences

interestring question. The order of calculation is very important. class Solution { public int[] numsSameConsecDiff(int n, int k) { //10 ^ n => max 10^9.. List<Integer> answer = new ArrayList<>(); for (int lastDigit = 1; lastDigit <= 9; lastDigit++</integer>…

LeetCode 213. House Robber II

Very interesting question 0 ~ n - 2 また 1 ~ n -1 に2つに分けることを思いつかなかったな。 class Solution { public int rob(int[] nums) { int n = nums.length; if (n <= 3){ int max = 0; for (int i = 0; i < n; i++){ max = Math.max(nums[i], ma…

LeetCode 605. Can Place Flowers

https://leetcode.com/problems/can-place-flowers/ class Solution { public boolean canPlaceFlowers(int[] flowerbed, int n) { //max: 0 adjacent 1 //dont create like this: [1, 0, 0, 1, 0, 0, 1] and do like this [1,0,1,0,0,0,1], then we can put…

LeetCode 22. Generate Parentheses

https://leetcode.com/problems/generate-parentheses/ 問題を分析する、可能な範囲でオーダーを減らす class Solution { public List<String> generateParenthesis(int n) { //1 -> "()" //2 -> "(())", "()()" //3 -> "()()()", "(())()", "()(())", "((()))", "((</string>…

LeetCode 39 Combination Sum

https://leetcode.com/problems/combination-sum/ class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { //pick one element unlimited times => make up sum equals to target //broute force find target List<List<Integer>> answer = new Ar</list<integer></list<integer>…

LeetCode 78 Subsets

Select and not game //select or not game! //if nums.length = n //then you have 2n options //[1,2,3] => 23 = 8 sets class Solution { public List<List<Integer>> subsets(int[] nums) { //select or not select game! //if nums.length = n //then you have 2^n op</list<integer>…

LeetCode 46. permutation

https://leetcode.com/problems/permutations/submissions/ find first element game ! //[1,2,3] // 1 -> 2 -> 3 // -> 3 -> 2 // visited or not visited? // 2 -> 3 -> 1 // 1 -> 3 // 3 -> ... class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer></list<integer></list<integer>…