본문 바로가기

코테31

[Algorithm][Array] 문제 풀이 #3 - Best Time to Buy and Sell Stock 문제 링크 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 내 코드 public int maxProfit(int[] prices) { if (prices == null || prices.length == 0) return 0; int min = prices[0]; int benefit = 0; for (int i = 1; i = prices[i]) { min = prices[i]; continue; } benefit = Math.max(prices[i] - min, benefit); } return benefit; } 테스트 케이스 결과 통계 아직은 쉬운 문제들이라 괜찮지만 틈틈히 .. 2022. 7. 17.
[Algorithm][Array] 문제 풀이 #2 - Two Sum 문제 링크 - https://leetcode.com/problems/two-sum/ 문제 풀이 public static int[] twoSum(int[] nums, int target) { Map map = new HashMap(); for (int i = 0; i < nums.length; i++) { int rest = target - nums[i]; if(map.containsKey(rest)) return new int[] {map.get(rest), i}; map.put(nums[i], i); } return new int[] {}; } 테스트 케이스 통과 비율 아직은 이지 정도에서 더 갈고 닦아야 미디엄으로 올라갈 수 있을것 같다. 오늘 1시간 동안 미디엄 문제 풀다 테스트 케이스 50% 맞추.. 2022. 7. 16.
[Algorithm][Array] 문제 풀이 #1 - Minimum Size Subarray Sum 문제 링크 - https://leetcode.com/problems/minimum-size-subarray-sum/ 내 풀이 public static int minSubArrayLen(int target, int[] nums) { int result = Integer.MAX_VALUE; int count = 0; if(nums.length == 0) return 0; int sum; for(int i = 0; i = target) { return 1; } for (int j = i + 1; j < nums.length; j++) { sum += nums[j]; count++; if (sum < target) .. 2022. 7. 15.