Mock Interview: Apple #10

JJ·2021년 6월 13일
0

MockTest

목록 보기
31/60

애플 폰인터뷰 #1

383. Ransom Note

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] mag = new int[26];

        for (int i = 0; i < magazine.length(); i++) {
            mag[magazine.charAt(i) - 'a']++;
        }
        
        for (int j = 0; j < ransomNote.length(); j++) {
            if (mag[ransomNote.charAt(j) - 'a'] <= 0) {
                return false;
            } else {
                mag[ransomNote.charAt(j) - 'a']--;
            }
        }
        
        return true; 
    }
}

Runtime: 3 ms, faster than 78.29% of Java online submissions for Ransom Note.
Memory Usage: 39.3 MB, less than 62.16% of Java online submissions for Ransom Note.

DP 사용~

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        mag = collections.Counter(magazine)
        note = collections.Counter(ransomNote)
        
        result = mag.subtract(note)

        
        result.reverse()
        
        if result[0] < 0
            return false
        else
            return true

이선이로 대충 이렇게 풀어볼려고도 했는데..
이선이 죄다 까먹었어요^^

def canConstruct(self, ransomNote: str, magazine: str) -> bool:
    
    # Check for obvious fail case.
    if len(ransomNote) > len(magazine): return False

    # In Python, we can use the Counter class. It does all the work that the
    # makeCountsMap(...) function in our pseudocode did!
    letters = collections.Counter(magazine)
    
    # For each character, c, in the ransom note:
    for c in ransomNote:
        # If there are none of c left, return False.
        if letters[c] <= 0:
            return False
        # Remove one of c from the Counter.
        letters[c] -= 1
    # If we got this far, we can successfully build the note.
    return True

루션이로 파이선 공부하기~

341. Flatten Nested List Iterator

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *
 *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
 *     public boolean isInteger();
 *
 *     // @return the single integer that this NestedInteger holds, if it holds a single integer
 *     // Return null if this NestedInteger holds a nested list
 *     public Integer getInteger();
 *
 *     // @return the nested list that this NestedInteger holds, if it holds a nested list
 *     // Return null if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * }
 */
public class NestedIterator implements Iterator<Integer> {

    private int loc;
    private ArrayList<Integer> result;
    
    public NestedIterator(List<NestedInteger> nestedList) {
        this.loc = 0; 
        this.result = new ArrayList<Integer>();
        
        flatten(nestedList);
    }
    
    public void flatten(List<NestedInteger> nestList) {
        for (NestedInteger n : nestList) {
            if (n.isInteger()) {
                this.result.add(n.getInteger());
            } else {
                flatten(n.getList());
            }
        }
    }

    @Override
    public Integer next() {
        if (hasNext()) {
            loc++; 
            return result.get(loc - 1);
        } else {
            return -1;
        }
    }

    @Override
    public boolean hasNext() {
        if (loc < result.size()) {
            return true;
        } else {
            return false;
        }
    }
}

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i = new NestedIterator(nestedList);
 * while (i.hasNext()) v[f()] = i.next();
 */

Runtime: 2 ms, faster than 97.43% of Java online submissions for Flatten Nested List Iterator.
Memory Usage: 41.3 MB, less than 53.54% of Java online submissions for Flatten Nested List Iterator.

진심 계속 머만하면 에러 떠서 너무 빡쳤던..^^
재귀로 만약에 array면 계속 파고드는 방식으로 풀었읍니다~

n.getInteger() 이거때문에 진심 개고생했네요 ㅡㅡ

0개의 댓글