분류 전체보기
-
leetcode top interview 150 | Ransom Note | HashMapPS 2023. 9. 1. 09:29
Question ransomNote 문자열이 magazine에 있는 문자로만 구성되어있으면 true, 아니면 false 리턴 magazine에 있는 각각의 문자는 ransomNote에서 한번만 사용할 수 있다. Accepted Code import java.util.*; class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] alpha = new int[26]; for(char c : magazine.toCharArray()){ alpha[c - 'a']++; } for(char c : ransomNote.toCharArray()){ int idx = c - 'a'; if(alpha[idx] > 0) ..
-
leetcode top interview 150 | Evaluate Reverse Polish Notation | StackPS 2023. 8. 31. 17:19
Question 후위표기법으로 나타낸 산술표현식이 문자열 배열로 주어졌을때 산출되는 답을 반환 Accepted Code - Stack import java.util.*; import java.util.regex.Pattern; class Solution { public int evalRPN(String[] tokens) { Stack stack = new Stack(); for(String token : tokens){ if(Pattern.matches("[+\\-*/]", token)){ int a = stack.pop(); int b = stack.pop(); switch(token){ case "+" -> stack.push(a+b); case "-" -> stack.push(b-a); case "..
-
leetcode top interview 150 | Minimum Size Subarray Sum | Sliding windowPS 2023. 8. 29. 10:21
Question 양수로 이루어진 정수배열이 주어졌을때, 하위배열의 합이 target number보다 같거나 클 경우 해당 하위배열의 최소 길이를 반환 target number에 해당하는 경우가 없으면 0 리턴 Accepted Code import java.util.*; class Solution { public int minSubArrayLen(int target, int[] nums) { int sum = 0; int minLen = Integer.MAX_VALUE; for(int left = 0, right = 0; right = target){ minLen = Math.min(minLen, right..
-
leetcode top interview 150 | Longest Substring Without Repeating Characters | Sliding windowPS 2023. 8. 29. 09:14
Question 주어진 문자열 s에서 반복되는 문자가 없는 하위문자열의 최대 길이를 리턴 Accepted Code - O(N^2) import java.util.*; class Solution { public int lengthOfLongestSubstring(String s) { int maxLen = 0; Set set = new HashSet();// set으로 문자열 중복을 체크 for(int left = 0; left
-
leetcode top interview 150 | Two Sum II - Input Array Is Sorted | Two PointerPS 2023. 8. 28. 15:37
Question 오름차순으로 정렬되어있는 정수형 배열이 주어졌을때 선택한 두 정수의 합이 target number에 해당 할 경우, index 위치를 담은 길이 2의 배열을 반환 동일한 요소를 두번 이상 사용할 수는 없는 One solution으로 구현 Not Accepted Code - Time Limit Exceeded class Solution { public int[] twoSum(int[] numbers, int target) { for(int i=0; i
-
leetcode top interview 150 | Valid Palindrome | Two PointerPS 2023. 8. 28. 14:29
Question 문장이 펠린드롬이려면 1. 모든 대문자는 소문자로 변환 2. 알파벳과 숫자가 아닌 것은 제거 3. 앞쪽에서 읽었을때와 뒷쪽에서 읽었을때가 동일해야함 주어진 문자열이 펠린드롬이면 true, 아니면 false로 리턴 Accepted Code - Two Pointer class Solution { public boolean isPalindrome(String s) { s = s.toLowerCase(); // 1번조건 int left = 0; int right = s.length() - 1; while(left= 'a' && c = '0' && c
-
leetcode top interview 150 | Best Time to Buy and Sell StockPS 2023. 8. 23. 23:27
Question i일마다 변동되는 주식가격을 담은 prices 배열이 주어지고, 주식을 구매한 일자의 다음날부터 판매할 수 있다. 이때 얻을 수 있는 최대 수익을 반환한다. Not accepted code class Solution { public int maxProfit(int[] prices) { int max=0; for(int i=0; i maxProfit) maxProfit = profit; } return maxProfit; } } solve 수익 = 판매한 가격 - 구매한 가격 수익이 최대화 되려면 판매한 가격과 구매한 가격의 차이가 커야한다. 따라서 판매가격은 크고 구매가격은 작아야한다. 그리고 구매한 다음에 판매할 수 있기 때문에 판매한 가격의 index는 구매한 가격의 index보다 항..