第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 ( N l o g N ) O(NlogN) O(NlogN)
空间复杂度 O ( N ) 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。一个单一的价值主张,贯穿各个产品层:功能,体系结构,交互,可用性,品牌,沟通。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值