Mock Interview: Amazon # 3

JJ·2021년 3월 13일
0

MockTest

목록 보기
8/60

Diet Plan Performance

class Solution {
    public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {
        //이거 완전 슈의 다이어트 플랜 아니냐
        
        
        Queue<Integer> q = new LinkedList<Integer>();
        
        int cal = 0; 
        int pt = 0; 
        for (int i = 0; i < k - 1; i++) {
            cal += calories[i];
            q.add(calories[i]);
        }
        
        for (int j = k - 1; j < calories.length; j++) {
            cal += calories[j];
            q.add(calories[j]);
            
            if (cal < lower) {
                pt--;
            } else if (cal > upper) {
                pt++;
            }
            
            cal -= q.remove();
        }
        
        return pt; 
    }
}

Runtime: 5 ms, faster than 17.70% of Java online submissions for Diet Plan Performance.
Memory Usage: 46.7 MB, less than 19.07% of Java online submissions for Diet Plan Performance

Queue를 썼는데 솔직히 걍 array로만 했어도 됐을듯??

Subtree of Another Tree

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int root;
    public boolean isSubtree(TreeNode s, TreeNode t) {
        
        root = t.val; 
        
        return helper(s, t);
    }
    
    private boolean helper(TreeNode s, TreeNode t) {
        if (s == null && t == null) {
            return true; 
        } else if (s == null || t == null) {
            return false;
        } else if (s.val != t.val && t.val == root) {
            return helper(s.left, t) || helper(s.right, t);
        } else if (s.val == t.val) {
            return helper(s.left, t.left) && helper(s.right, t.right);
        } else {
            return false; 
        }
    }

}

166 / 183 test cases passed.

문제 잘못 읽어서 삽질하다가 30초 남기고 깨달았읍니다..^^

0개의 댓글