第50周 ARTS 2019 09 29

Algorithm:1046. 最後一塊石頭的重量
Review: Linked List Problems
Tip/Tech:二叉堆
Share:Great products do less, but better;偉大的產品做的少,但是更好

Algorithm

1046. 最後一塊石頭的重量

https://leetcode-cn.com/problems/last-stone-weight/
直接優先隊列解決。
因爲這裏有個隱藏條件,就是如果兩個石頭沒有被同時粉碎,剩下了一個,那麼這個石頭還是要被放入到石頭堆裏的,所以用優先隊列就很好解決這個問題。

class Solution {
    public int lastStoneWeight(int[] stones) {
        if (stones == null ||stones.length == 0) {
            return 0;
        }
        if (stones.length == 1) {
            return stones[0];
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((o2, o1) -> o1 - o2);
        for (int i = 0, len = stones.length; i < len; ++i) {
            priorityQueue.offer(stones[i]);
        }

        while (priorityQueue.size() > 1) {
            int first = priorityQueue.poll();
            int second = priorityQueue.poll();
            if (first != second) {
                priorityQueue.offer(first - second);    
            }
                
        }
        if (priorityQueue.size() > 0){
            return priorityQueue.poll();
        } else {
            return 0;
        }
        
    }
}

時間複雜度就是O(NlogN)O(NlogN)
空間複雜度O(N)O(N)
答案很簡答,但是提交就奇了怪了,代碼都差不多,但是提交下來,時間卻差了很多。非常的奇怪。。。
不知道LeetCode這個測評機制是咋樣的。

Review

Linked List Problems

這個是在學習數據結構的時候,看到的,一個關於鏈表的論文,裏面充分的列舉了有關於鏈表的18個問題

  • Count()
  • GetNth()
  • DeleteList()
  • Pop()
  • InsertNth()
  • SortedInsert()
  • InsertSort()
  • Append()
  • FrontBackSplit()
  • RemoveDuplicates()
  • MoveNode()
  • AlternatingSplit()
  • ShuffleMerge()
  • SortedMerge()
  • MergeSort()
  • SortedIntersect()
  • Reverse()
  • RecursiveReverse()

論文裏面都是用c來實現的,有空我要用Java來全部實現了。

Tip/Tech

二叉堆的實現。

public class MaxHeapMine<E extends Comparable> implements Heap<E> {

    private ArrayList<E> data;
    private int size;

    public MaxHeapMine() {
        data = new ArrayList<>();
        size = 0;
    }

    public MaxHeapMine(E[] elements) {
        data = new ArrayList<>();
        size = 0;
        for (E item : elements) {
            add(item);
        }
    }
    @Override
    public void add(E e) {
        data.add(e);
        size++;
        shiftUp(size - 1);
    }

    @Override
    public E delete() {
        if (size <= 0) {
            throw new IllegalArgumentException("size under 0.");
        }
        E ans = data.get(0);
        data.set(0, data.get(size - 1));
        data.remove(size - 1);
        size--;
        shiftDown(0);
        return ans;
    }

    @Override
    public E find() {
        if (size <= 0) {
            throw new IllegalArgumentException("size under 0.");
        }
        return data.get(0);
    }

    @Override
    public void replace(E e) {
        data.set(0, e);
        shiftDown(0);
    }

 	/**
     * 上移操作
     * @param index 下移的座標
     */
    private void shiftUp(int index) {
        int parentIndex = (index - 1) / 2;
        while (parentIndex >= 0 && data.get(parentIndex).compareTo(data.get(index)) < 0) {
            swap(index, parentIndex);
            index = parentIndex;
            parentIndex = (index - 1) / 2;
        }
    }

    /**
     * 下移操作
     * @param index 下移的座標
     */
    private void shiftDown(int index) {
        int maxIndex = index;
        while (true) {
            int leftChildIndex = 2 * index + 1;
            if (leftChildIndex < size && data.get(leftChildIndex).compareTo(data.get(maxIndex)) > 0) {
                maxIndex = leftChildIndex;
            }
            int rightChildIndex = 2 * index + 2;
            if (rightChildIndex < size && data.get(rightChildIndex).compareTo(data.get(maxIndex)) > 0) {
                maxIndex = rightChildIndex;
            }
            if (maxIndex == index) {
                break;
            }
            swap(maxIndex, index);
            index = maxIndex;
        }
    }

    private void swap(int a, int b) {
        E temp = data.get(a);
        data.set(a, data.get(b));
        data.set(b, temp);
    }

    public static void main(String[] args) {
        Integer[] test = {1, 2, 3, 4, 0, 9, 8, 7, 6, 5, 20, 19, 18, 17, 16};
        MaxHeapMine<Integer> maxHeapMine = new MaxHeapMine<>(test);
        for (int i = 0, len = test.length; i < len; ++i) {
            System.out.println(maxHeapMine.delete());
            System.out.println(maxHeapMine.find());
        }
    }
}

Share

Great products do less, but better

(1)Products start small and focused.
產品由小而專注的開始
(2)The product scope grows.
產品範圍不斷擴大。
(3)Do less, but better

Do less, but better
Think about the most successful products you know. The ones you use everyday, as a customer. Twitter, Lyft, Venmo, Slack. One single value proposition, articulated through the various product layers: features, architecture, interactions, usability, branding, communications.

少做,但做得更好
考慮一下您所知道的最成功的產品。您每天使用的客戶。 Twitter,Lyft,Venmo,Slack。一個單一的價值主張,貫穿各個產品層:功能,體系結構,交互,可用性,品牌,溝通。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章