1번과 2번을 푸는데,
고수는 5분/5분
중수는 10분/10분
정도 걸린듯함.
leetcode.com/problems/check-array-formation-through-concatenation/
Check Array Formation Through Concatenation - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
코드가 예쁜편
class Solution {
public boolean canFormArray(int[] arr, int[][] pieces) {
int n = arr.length;
int i = 0;
while(i < n) {
int found = -1;
for(int j = 0; j < pieces.length; j++) {
if(pieces[j][0] == arr[i]) {
found = j;
break;
}
}
if(found == -1) {
return false;
}
int[] targetPiece = pieces[found];
for(int x : targetPiece) {
if(i >= n || x != arr[i]) {
return false;
}
i++;
}
}
return true;
}
}
Complexity Analysis
Let NN be the length of arr. In the worst case, the size of pieces is \mathcal{O}(N)O(N).
Time Complexity: \mathcal{O}(N^2)O(N2). The time to find the next piece is \mathcal{O}(N)O(N), and we need to find \mathcal{O}(N)O(N) pieces at most.
Space Complexity: \mathcal{O}(1)O(1), since no additional data structure is allocated
Complexity Analysis
Let NN be the length of arr. In the worst case, the size of pieces is \mathcal{O}(N)O(N).
Time Complexity: \mathcal{O}(N\log(N))O(Nlog(N)). The time to find next piece using Binary Search is \mathcal{O}(\log(N))O(log(N)), and we need to find \mathcal{O}(N)O(N) pieces at most.
Space Complexity: \mathcal{O}(1)O(1), since no additional data structure is allocated.
Complexity Analysis
Let NN be the length of arr. In the worst case, the size of pieces is \mathcal{O}(N)O(N).
Time Complexity: \mathcal{O}(N)O(N). The time to find next piece is \mathcal{O}(1)O(1), and we need to find \mathcal{O}(N)O(N) pieces at most.
Space Complexity: \mathcal{O}(N)O(N), since we store a hashmap with \mathcal{O}(N)O(N) elements at most.
class Solution {
public boolean canFormArray(int[] arr, int[][] pieces) {
int n = arr.length; // Let N be the length of arr. In the worst case, the size of pieces is O(N)
// Space complexity is O(N), since we store a hashmap with O(N) elements at most.
Map<Integer, int[]> mapping = new HashMap<>();
for(int[] p : pieces) {
mapping.put(p[0], p);
}
// O(N). The time to find next piece is O(1), and we need to find O(N) pieces at most.
int i = 0;
while(i < n) {
if(!mapping.containsKey(arr[i])) {
return false;
}
int[] targetPiece = mapping.get(arr[i]);
for(int x : targetPiece) {
if(i >= n || x != arr[i]) {
return false;
}
i++;
}
}
return true;
}
}
leetcode.com/problems/count-sorted-vowel-strings/
Count Sorted Vowel Strings - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Complexity Analysis
Since the maximum depth of recursion would be nn, the time complexity to explore all the path would be roughly equal to \mathcal{O}(n^{4})O(n4).
class Solution {
public int countVowelStrings(int n) {
return countVowelStringUtil(n, 5);
}
int countVowelStringUtil(int n, int vowels) {
if(n==1) return vowels;
if(vowels == 1)
return 1;
return countVowelStringUtil(n-1, vowels) + countVowelStringUtil(n, vowels-1);
}
}
class Solution {
public int countVowelStrings(int n) {
return countVowelStringUtil(n, 1);
}
int countVowelStringUtil(int n, int vowels) {
if(n==0) return 1;
int result = 0;
for(int i = vowels; i <= 5; i++) {
result += countVowelStringUtil(n-1, i);
}
return result;
}
}
Complexity Analysis
Time Complexity : \mathcal{O}(n^{4})O(n4), as for a given nn and 55 vowels (a, e, i, o, u), there are roughly n^{4}n4 possible candidates (Refer time complexity of Approach 1).This gives us time complexity as \mathcal{O}(n^{4})O(n4).
Space Complexity: \mathcal{O}(n)O(n). This space will be used to store the recursion stack. As the maximum depth of recursion would be \max(n,5)max(n,5), the space required for internal call stack would roughly \mathcal{O}(n)O(n).
class Solution {
public int countVowelStrings(int n) {
int memo[][] = new int[n+1][6];
return countVowelStringUtil(n, 5, memo);
}
int countVowelStringUtil(int n, int vowels, int memo[][]) {
if(n==1) return vowels;
if(vowels == 1) return 1;
if (memo[n][vowels] != 0)
return memo[n][vowels];
int res = countVowelStringUtil(n - 1, vowels, memo) +
countVowelStringUtil(n, vowels - 1, memo);
memo[n][vowels] = res;
return res;
}
}
Time Complexity : O(n), as for every n and vowels we calculate result only once, the total number of nodes in the recursion tree would be n * 5, which is roughly equal to n.
Space Complexity : O(n), as we use a 2D array memo of size n * 5 and the size of internal call stack would also be n (As explained in Approach 2), the space complexity would be O(n+n) = O(n+n).
class Solution {
public int countVowelStrings(int n) {
int[][] dp = new int[n+1][6];
for(int vowels = 1; vowels <= 5; vowels++)
dp[1][vowels] = vowels;
for(int nValue = 2; nValue <= n; nValue++) {
dp[nValue][1] = 1;
for(int vowels = 2; vowels <= 5; vowels++) {
dp[nValue][vowels] = dp[nValue][vowels-1] + dp[nValue-1][vowels];
}
}
return dp[n][5];
}
}
영어로 설명하기가 좀 어려움
Intuition and Algorithm
The problem is a variant of finding Combinations. Mathematically, the problem can be described as, given 5 vowels (let k = 5k=5), we want to find the number of combinations using only nn vowels. Also, we can repeat each of those vowels multiple times.
In other words, from kk vowels (k = 5k=5), we can choose nn vowels with repetition. Denoted as k \choose n(nk), the formulae for Combination with Repetition is given by,
k \choose n(nk) = \frac{(k + n - 1)!}{ (k - 1)! n!}=(k−1)!n!(k+n−1)!
We know that the kk value is 55 as there are always 55 vowels to choose from. Substituting kk as 55 in above formulae,
5 \choose n(n5) = \frac{(n+4) \cdot (n+3) \cdot (n+2) \cdot (n+1)}{24}=24(n+4)⋅(n+3)⋅(n+2)⋅(n+1)
The derivation can be illustrated as follows.
class Solution {
public int countVowelStrings(int n) {
return (n + 4) * (n + 3) * (n + 2) * (n + 1) / 24;
}
}
Complexity Analysis
Time Complexity: \mathcal{O}(1)O(1), as the approach runs in constant time.
Space Complexity: \mathcal{O}(1)O(1), as the approach uses constant extra space.
leetcode.com/problems/furthest-building-you-can-reach/
Furthest Building You Can Reach - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
class Solution {
public int furthestBuilding(int[] heights, int bricks, int ladders) {
PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
for(int i = 0; i < heights.length - 1; i++){
int diff = heights[i + 1] - heights[i];
if(diff > 0) { // Current building's height is less than the next building's height.
queue.add(diff); // Use ladder and add the difference.
if(queue.size() > ladders) { // If you ran out of ladders!
bricks -= queue.poll(); // Remove the smallest difference and use bricks instead
if(bricks < 0) {
return i; // Ops you ran out of the bricks and you stop here!
}
}
}
}
return heights.length - 1;
}
}
leetcode.com/contest/weekly-contest-213/problems/kth-smallest-instructions/
Account Login - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
곧 4개를 풀수있는날이 올것 같다.. 화이팅!