[Amazon Online Assessment]

805 阅读3分钟

OA1已过,七道debug非常简单都是原题,OA2 coding 70min两道题。准备如下

  1. Longest Palindromic Substring

Leetcode解法4,采用中心扩展法。

java:

public static String longestPalindrome(String s){
    if(s == null || s.length() < 1) return "";
    int start = 0, end = 0;
    for(int i = 0; i < s.length(); i++){
        int len1 = expandAroundCenter(s, i, i);
        int len2 = expandAroundCenter(s, i, i+1);
        int len = Math.max(len1, len2);
        if(len > end - start + 1){
            start = i - (len - 1) / 2;
            end = i + len /2;
        }
    }
    return s.substring(start, end + 1);
    
}

private static int expandAroundCenter(String s, int start, int end){
    int L = start, R = end;
    while(L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)){
        L--;
        R++;
    }
    return R-L-1;
}

执行结果: 通过 显示详情 执行用时 :8 ms, 在所有 java 提交中击败了95.86% 的用户 内存消耗 :36.5 MB, 在所有 java 提交中击败了90.70%的用户

  1. K-Closest Points to Origin

采用了比较弱的方法,时间复杂度O(NlogN)

java:

import java.util.Arrays;
public static int[][] kClosest(int[][] points, int K){
    int N = points.length;
    int[] distance = new int[N];
    for(int i = 0; i < N; i++){
        distance[i] = dist(points[i]);
    }
    Arrays.sort(distance);
    int distofK = distance[K-1];
    int[][] ans = new int[K][2];
    int t = 0;
    for(int i = 0; i < N; i++){
        if(dist(points[i]) <= distofK){
            ans[t++] = points[i];
        }
    }
    return ans;
}

public static int dist(int[] p){
    return p[0] * p[0] + p[1] * p[1];
}

执行结果: 通过 显示详情 执行用时 :14 ms, 在所有 java 提交中击败了87.90% 的用户 内存消耗 :62.8 MB, 在所有 java 提交中击败了84.80%的用户

  1. Path With Maximum Minimum Value

Given a matrix of integers A with R rows and C columns, find the maximum score of a path starting at [0,0] and ending at [R-1,C-1].

The score of a path is the minimum value in that path. For example, the value of the path 8 → 4 → 5 → 9 is 4.

A path moves some number of times from one visited cell to any neighbouring unvisited cell in one of the 4 cardinal directions (north, east, west, south).

Example 1:

Input: [[5,4,5],[1,2,6],[7,4,6]] Output: 4 Explanation: The path with the maximum score is highlighted in yellow.

Example 2:

Input: [[2,2,1,2,2,2],[1,2,2,2,1,2]] Output: 2

DFS:

java:

private row, col, max, min;
public static int maxMinPath(int[][] matrix){
    row = matrix.length;
    col = matrix[0].length;
    max = Integer.MIN_VALUE;
    min = Integer.MAX_VALUE;
    dfshelper(matrix, min, 0, 0);
    return max;
}

public static void dfshelper(int[][] matrix, int min, int i, int j){
    if(i >= row || j >= col) return;
    if(i == row - 1 && j == col - 1){
        min = Math.min(min, matrix[i][j]);
        max = Math.max(min, max);
    }
    min = Math.min(min, matrix[i][j]);
    dfshelper(matrix, min, i, j+1);
    dfshelper(matrix, min, i+1, j);
}

a) Two Sum

java:

public static int[] twoSum(int[] nums, int target){
    HashMap<Integer, Integer> m = new HashMap<>();
    for(int i = 0; i < nums.length; i++){
        int diff = target - nums[i];
        if(m.containsKey(diff))
            return new int[]{m.get(diff), i};
        m.put(nums[i], i);
    }
    throw new IllegalArgumentException();
}

执行结果: 通过 显示详情 执行用时 :3 ms, 在所有 java 提交中击败了98.87% 的用户 内存消耗 :37.3 MB, 在所有 java 提交中击败了90.54%的用户

b)

  1. Merge Two Sorted Linked Lists

java:

public ListNode mergeTwoLists(ListNode l1, ListNode l2){
    if(l1 == null || l2 == null) return l1 == null ? l2 : l1;
    ListNode dummy = new ListNode(0);
    ListNode cur = dummy;
    while(l1 != null || l2 != null){
        int v1 = l1 == null ? Integer.MAX_VALUE : l1.val;
        int v2 = l2 == null ? Integer.MAX_VALUE : l2.val;
        if(v1 <= v2){
            cur.next = l1;
            cur = cur.next;
            l1 = l1.next;
        }
        else{
            cur.next = l2;
            cur = cur.next;
            l2 = l2.next;
        }
    }
    return dummy.next;
}

执行结果: 通过 显示详情 执行用时 :1 ms, 在所有 java 提交中击败了92.63% 的用户 内存消耗 :39.4 MB, 在所有 java 提交中击败了68.61%的用户

  1. Most Common Word

java:

import java.util.*;

public class mostCommonWord { public static String mostCommonWord(String paragraph, String[] banned){ for(int i = 0; i< banned.length; i++){ banned[i] = banned[i].toLowerCase(); } for(int i = 0; i< banned.length; i++){ System.out.println(banned[i]); } Set set = new HashSet<>(Arrays.asList(banned)); set.add(""); paragraph += "."; String res = " "; int times = 0; Map<String, Integer> map = new HashMap<>(); int i = 0; for(int k = 0; k<paragraph.length(); k++){ char c = paragraph.charAt(k); if((c>='a' && c<='z') || (c>='A' && c <= 'Z')){ continue; } else{ String word = paragraph.substring(i, k); word = word.toLowerCase(); if(!set.contains(word)){ map.put(word, map.getOrDefault(word, 0)+1); if(map.get(word) > times){ res = word; times = map.get(word); } } i =k +1; }

    }
    return res;
}

public static void main(String[] args){
    String paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";
    String[] banned = {"hIJt"};
    System.out.println(mostCommonWord(paragraph, banned));


}

}

  1. Substring of size k with k distinct characters

Given a string s and an int k, return all unique substrings of s of size k with k distinct characters.

Example 1:

Input: s = "abcabc", k = 3 Output: ["abc", "bca", "cab"]

Example 2:

Input: s = "abacab", k = 3 Output: ["bac", "cab"]

Example 3:

Input: s = "awaglknagawunagwkwagl", k = 4 Output: ["wagl", "aglk", "glkn", "lkna", "knag", "gawu", "awun", "wuna", "unag", "nagw", "agwk", "kwag"] Explanation: Substrings in order are: "wagl", "aglk", "glkn", "lkna", "knag", "gawu", "awun", "wuna", "unag", "nagw", "agwk", "kwag", "wagl" "wagl" is repeated twice, but is included in the output once.

Constraints:

The input string consists of only lowercase English letters [a-z]
0 ≤ k ≤ 26