Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page
  • Spiral 2-D array
  • 395. Longest Substring with At Least K Repeating Characters

Was this helpful?

  1. 16. Divide & Conquer

General

Spiral 2-D array

for loop half of array size, add elements in 4 directions

public static List<Integer> matrixInSpiralOrder(List<List<Integer>> squareMatrix){
    List<Integer> result = new ArrayList<>();
    for (int i =0; i < Math.ceil(squareMatrix.size() * 0.5); i++){
        helper(squareMatrix, result, i);
    }
    return result;
}

private static void helper(List<List<Integer>> squareMatrix, List<Integer> result, int offset){
    int size = squareMatrix.size();
    if(offset * 2 + 1 == size){
        result.add(squareMatrix.get(offset).get(offset));
        return;
    }
    for (int i = offset; i < size - offset - 1; i++){
        result.add(squareMatrix.get(offset).get(i));
    }

    for (int i = offset; i < size - offset - 1; i++){
        result.add(squareMatrix.get(i).get(size - 1 - offset));
    }

    for (int i = size - 1 - offset; i > offset ; i--){
        result.add(squareMatrix.get(size - 1 - offset).get(i));
    }

    for (int i = size - 1 - offset; i > offset ; i--){
        result.add(squareMatrix.get(i).get(offset));
    }
}

395. Longest Substring with At Least K Repeating Characters

Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.

  • Find pivots whose's count less than k,split the string by pivots,untill no pivot in substring

public int longestSubstring(String s, int k) {
    char[] c = s.toCharArray();
    return helper(c, 0, s.length(), k);    
}

private int helper(char[] c, int start, int end, int k){
    if(end-start < k) return 0;

    // count the frequency of char
    Map<Character, Integer> map = new HashMap<>();
    for(int i=start; i<end; i++){
        map.put(c[i], map.getOrDefault(c[i], 0)+1);
    }

    // D & C
    for(Map.Entry<Character, Integer> entry: map.entrySet()){
        if(entry.getValue() < k){
            for(int i=start; i<end; i++){
                if(c[i] == entry.getKey()){
                    int left = helper(c, start, i, k);
                    int right = helper(c, i+1, end, k);
                    return Math.max(left, right);
                }
            }
        }
    }
    return end-start;
}
Previous16. Divide & ConquerNext17. UnionFind

Last updated 5 years ago

Was this helpful?