Spring in Action Notes

spring-framework

1. Dependency Injection (DI): decouple, wiring, tie software components together loosely

 

2. Aspect Oriented Programming (AOP): promote separation of concerns in software system

 

3. By default, all beans in Spring are singletons.

 

4. Three primary ways to wire beans together in Spring:

Automatic configuration

Explicit Java-based configuration

Explicit XML-based configuration

 

Donate $5 to me for a coffee with PayPal and read more professional and interesting technical blog articles about web and mobile development.
Follow me @Yaoli0615 at Twitter to get latest tech updates.
Resources:

Core Java Volume I–Fundamentals (10th Edition) (Core Series)

Core Java, Volume II–Advanced Features (10th Edition) (Core Series)

Test-Driven Java Development

Java Concurrency in Practice

Java: An Introduction to Problem Solving and Programming (7th Edition)

Java 9 for Programmers (Deitel Developer Series)

Java SE8 for the Really Impatient: A Short Course on the Basics (Java Series)

Core Java for the Impatient

Java: The Beginners Guide for every non-programmer which will attend you trough your learning process

Java Deep Learning Essentials

Machine Learning in Java

Learning Reactive Programming With Java 8

Java 9 Programming By Example

Thinking in Java (4th Edition)

The Java EE Architect’s Handbook, Second Edition: How to be a successful application architect for Java EE applications

Java Artificial Intelligence: Made Easy, w/ Java Programming

Advertisements
Posted in Book/Movie/Music, CS Research&Application, Uncategorized | Tagged , , | Leave a comment

Softwares in MacOS

software-development-mac-os-user-interface-os-x-10-10-yosemite-apps

iTerm

1Password

Postman

Docker

Tunnelblick

Sequel Pro

SmartGit

Netease Music

Posted in CS Research&Application, Uncategorized | Tagged , | Leave a comment

Blockchain Dev – EOS

Bootstrap Demo – set up a local testnet

https://vimeo.com/252935097

 

Standardize Currencies on EOS.IO Software

https://vimeo.com/253072484

 

Performance Demo of EOS.IO Software

https://vimeo.com/253062268

 

EOS HowTo videos

https://vimeo.com/channels/eosiohowto

 

EOS Developers Telegram Channel

 

EOS中文社区

https://eosfans.io/

 

EOS contracts: Development with the CLion IDE

Posted in CS Research&Application, Uncategorized | Tagged | Leave a comment

JavaEye老炮杂谈之一——比特币和区块链(摘录)

Trustno1v2

 

一直想请教一下@trustno1v2,对比特币和区块链是什么观点?想听听你的理解,雪球上好像没看到你有相关讨论。因为有利于跨国的资金投送么?汇兑方便

 

对投行业务有帮助吧

对金融交易有帮助,区块链还是不错的

 

流通显然不是货币的前提条件。比如弗里德曼的那个很著名的太平洋岛国的故事

 

南太平洋岛国用石头作为货币,历史上某次岛民在外海踩到一块巨石,将它运回来的途中船只沉默,石块跌入大海,但是岛民依然以此石块作为基本货币单位直到白人殖民者的到来。这个石头显然是无法流通的东西,但是依然是某种货币。

实际上,包括黄金,很大程度上都是不流通的

 

货币即信用,信用即债务。我今天欠你100元,给你打一个欠条。如果有足够多的人相信我的品行,那么这个欠条既是货币。

你可以用这个欠条,去认识我的人哪里换任何东西,当然这些人会根据和我的关系远近亲疏,给出不同的折价:比如我老婆那里,大概可以换满100块,如果在robbin那里应该只能换50

 

不是存款创造了货币,而是贷款创造了货币。实际上这个原理,跟多线程是非常类似的

 

《人类简史》的第十章 金钱的味道,讲得很简单易懂

 

银行和金融系统类似一个极端高效的线程切换系统,举个栗子:

比如,你有一笔闲钱1000块,你觉得最近不会太用的着,于是你存入了银行,这个时候银行户头上有多少钱呢?实际上,有1000元资产,同时有1000元负债。这个时候A想要用钱了,去银行借500块,那么这个时候,银行现金流出去了500块,但是资产是多少呢是1500块,负债也是1500块,但是整个市场上,现金一共多少呢?不过还是1000块,但是整个社会的货币膨胀到了1500块。这1000块的现金,其实好比就像是电脑的内存,而每一个交易实体,则类似操作系统的进程,有些实体在某些时候放弃内存资源,有些实体在某些时候争用资源,银行在期间进行高效的资源调度,使得所有实体的货币流动看上去都是无摩擦的,但同时他又是高效的。如果没有信用和债务,那么社会的货币体系就会像批操作系统一样,一批用完,下一批继续用,前现代社会就是这样的。比如大清为何打不过日本,因为很简单,大清的财政靠的是税收,而日本的财政靠的国债,税收就是一个典型的批操作系统,农民赚完分一部分给地主,地主赚完了分一部分给衙门,而国债显然是比这个更家高级,所有国民暂时不用的资金都融给了政府,一个是批操作系统,一个是多线程操作系统,怎么可能打的过?

 

实际上就是一个多线程模型,所谓的挤兑,就是所有的进程同时争用资源。

银行干的活,拿着A的钱借给B,B如果暂时不用再存回银行,银行继续拿着B的钱借给C。换来换去,如果没有外部影响,总共只有A当初存进来那么点钱,但是轮换几次货币规模可能扩张3-4倍

货币规模的扩张的唯一限制,不过是整体社会的预期收益率,就是说银行觉得贷款出去能不能赚到钱,能赚到多少钱的问题。如果银行普遍预期整体社会的收益率下降,那么银行就不会继续发放贷款,换而收缩资产负债表。现在日本,欧洲,所谓的负利率就是这样,整体社会的收益率为0,银行不愿意贷款,结果央行说不行,你不贷款我就扣你钱

说到底,货币总体增量对应的是社会财富的增加

对于bitcoin 这种上限固定的商品,除非整个世界退回工业革命之前,否则几乎没有任何货币意义吧

当然作为通缩型的投资品,那是另外一回事情

参与通缩型的投资,本质上是参与旁氏,要么不完,要么早完

从投资的角度说,知道有泡沫,第一时间就是要冲进去

 

晚了就不要进去了[偷笑],坐等下一波

 

赚旁氏的钱,等于赚傻逼的钱吧,傻逼够多就行

 

我一直不喜欢比特币的原因比较奇怪,因为它是通过毫无意义的浪费电力,制造出来的……

 

呵呵黄金更不环保吧。有一句名言叫做:人类把黄金从地里开采出来,然后再埋入地下,除此之外毫无用处

 

所以我听说区块链可以做有意义的计算时,感觉多半有钱途。

 

对于金融交易来说区块连还是有意义,这玩意会干掉交易所

狗门之类的的dark pool trader 早就看交易所不顺眼了吧

 

干掉交易所的条件是有足够多trader,我对私有链完全没有兴趣

至于比特币是不是货币,我的态度是:它看着像个货币,用起来也像个货币,那么它就是货币….

 

不是。票据清算,这类集中式的交易服务,很消耗时间和资源

注意,主语,谁用起来

 

我们不能证明,一个东西是货币时它就不能是商品,所以比特币看起来像货币或商品,用起来象货币或商品,我们就可以大胆的猜测——它既是货币,也是商品

 

其实,广义上说所有可以交易的东西都是商品,如果一个东西不是商品,只是说明我们还没法将其货币化或者证券化而已

总结我所理解的三类观点:

T1:通缩是大问题,结果指向庞氏骗局

老庄:太费电了,不喜欢 🙂

国宁:不建议交易,因为涉及对赌未来,但是认可比特币的价值

不知道我理解的有没有问题 @trustno1v2 @庄表伟@上海 @lgn21st-吕国宁

 

功利性判断当然不是不可以,但问题是你的语境是是什么?转换语境其实是没有办法对话的。比如说大家在讨论,世界上最好吃的蛋糕是什么,你说我觉得最好吃的就是最好吃的。

显然你的回答不是大家要谈论的问题

 

我主要是关心比特币本身的未来,比如如果是旁氏,那早晚要完蛋

 

也许不一定,比如文物这类玩意,很难完蛋,虽然也是个旁氏

 

哦?还有长期存在的庞氏骗局?

文物怎么是旁氏呢?我们的定义可能不一样

 

通缩型物品取决于进入市场的交易人数的多寡吧。比如说,中国传统的艺术品,在很长时间里在世界上都没啥价值,八国联军抢了不少清宫画册去海外拍卖都没卖出什么价钱。只有东亚经济发达以后,东亚高净值人群增加,这类艺术品价格才水涨船高

BTC 最直接的威胁,大概就是量子计算和量子通信吧

这些也不是很遥远的事情,可能比AI更靠谱

中科大的量子通信已经做的非常好了 (今年建成京沪量子网络)

都已经实用阶段了

 

量子通信对比特币估计没啥影响,量子计算就不好说了

都量子纠缠了,还要算啥总帐呢?量子纠缠本来就不可串改

 

量子计算预计至少3-5年后,量子存储要10年后。目前量子计算还处于科研小范围环境,国内目前实现的是依靠量子网络进行秘钥传输,绝无泄密可能

 

DWAVE,google在用

 

这个和量子纠缠没关系吧,区块链解决的是分布式节点达成共识的问题

不需要达成共识啊,要啥共识,我传给你的,不可改变。所谓的共识就是大家认为这个玩意没被修改过

 

你是说用量子位记录所有比特币状态?这个难以想象,我有点跟不上你的思路了

@fsword@杭州 这不是我想的,是物理学里上个世纪就提出来的假想,叫薛定谔货币

 

PARK

本质上货币交换会被数字交换取代

 

要成为一种广泛使用的支付手段,数字货币须具备区别于电子货币、虚拟货币的显著特点,包括安全性、可控匿名性、周期性、不可重复性、系统无关性,并且要在开放互联环境中达到很高的交易性能(高并发交易量和海量数据处理效率)。

 

如何评价《加密货币和区块链(一):历史的重演》?

类比是一种很低效的分析问题方法,还是得回到本源。

南海泡沫,郁金香泡沫,互联网泡沫都是泡沫。那房价是不是泡沫?学区房是不是泡沫?

如果你说房价永远涨不算泡沫,那九十年代的海南房价为啥崩了?

如果分析成本,螺纹钢水泥卖的比白菜还便宜,那凭啥房子还那么贵?

如果分析使用价值,黄金的工业用途值得所有央行来搞黄金储备么?

是泡沫也好,不是泡沫也好,把自己的逻辑理顺了,再来说三道四。

 

记得15年牛市的时候,有一个同事洋洋得意的说中国股市是巨大泡沫,你看我说对了吧,我问他赚了多少?他说早知道是泡沫,所以从14年起就一分钱不投中国股市,没亏就是赚。我15年翻翻的都没乱说话,倒是这货喊了一年的空,还洋洋得意。我能说啥,只能说他牛逼啊。

 

在我看来,这个市场只有两种人,知道自己在赌什么的,不知道自己在赌什么的。

 

用闲钱风投BTC,并且用BTC币本位而非法币本位的,是真正BTC项目的信仰者,他知道自己在干什么。 另外在二级市场买卖并能够从波动中获利,这也是懂行的。

 

一直嚷嚷比特币会上天,想当价值投资者,结果买入被套就茶饭难安天天看着币价的;以及一直嚷嚷比特币会归0,比特币一跌就出来洋洋得意的。这都是不懂行的。只不过一个用价格永远涨来安慰自己被套只是暂时的。另一个用这玩意就是泡沫来抚平自己踏空时的焦虑。同样是不懂行的,洋洋洒洒写个历史故事来讽刺另外一群不懂行的,有意思么?

 

最后还想说一句,区块链是社会学革命,而非技术革命,技术并不新鲜,中本聪之于BTC顶多也是乔布斯之于苹果。他只是技术的组装者。 所以那些仗着对区块链底层算法了如指掌就指点江山的,还是多了解了解人性吧。

 

Reference:

https://zhuanlan.zhihu.com/p/20882356

如何评价《加密货币和区块链(一):历史的重演》?

 

 

Posted in CS Research&Application, Uncategorized | Tagged , | Leave a comment

LeetCode – 518. Coin Change 2

You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.

Note: You can assume that

  • 0 <= amount <= 5000
  • 1 <= coin <= 5000
  • the number of coins is less than 500
  • the answer is guaranteed to fit into signed 32-bit integer

Example 1:

Input: amount = 5, coins = [1, 2, 5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1

Example 2:

Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.

Example 3:

Input: amount = 10, coins = [10] 
Output: 1

 

Idea: this problem is very similar like Coin Change, the difference is to store the total number of combinations to make up the amount, larger amount (dp[i]) is dependent on the smaller one (dp[i – c]) and coins.

 

class Solution(object):
    def change(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int

        beats 93.06%
        """
        coins.sort()
        dp = [1] + [0] * amount
        for c in coins:
            for i in range(c, amount+1):
                if dp[i-c]:
                    dp[i] += dp[i-c]
        return dp[-1]

    def change0(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int

        https://leetcode.com/problems/coin-change-2/discuss/99210/python-O(n)-space-dp-solution

        beats 55.21%
        """
        dp = [0] * (amount + 1)
        dp[0] = 1
        for i in coins:
            for j in range(1, amount + 1):
                if j >= i:
                    dp[j] += dp[j - i]
        return dp[amount]

    def change1(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int

        https://leetcode.com/problems/coin-change-2/discuss/99235/Classic-problem-python-clean-DP-O(MN)

        beats 34.72%
        """
        dp = [1] + [0] * amount
        for c in coins:
            for i in range(1, amount + 1):
                if i >= c:
                    dp[i] += dp[i - c]
        return dp[-1]

    def change2(self, amount, coins):
        """
        :type amount: int
        :type coins: List[int]
        :rtype: int

        TLE for test case: 500, [3,5,7,8,9,10,11]
        """
        if amount < 0:
            return 0
        if amount == 0:
            return 1
        if coins is None:
            return 0
        if len(coins) == 0:
            if amount == 0:
                return 1
            else:
                return 0

        coins.sort(reverse=True)
        if amount < coins[-1]:
            return 0
        res = [0]
        self.help_func(coins, amount, 0, res)
        return res[0]

    def help_func(self, coins, amount, depth, res):
        """
        :param coins:
        :param amount:
        :param depth:
        :param res_list:
        :return:

        use current largest coin as much as possible
        back tracking to use less largest coin if cannot make the target amount
        repeat above procedure for each coin from large to small
        """
        coins_len = len(coins)
        if amount == 0:
            res[0] += 1
            return
        elif amount < 0:
            return
        else:
            if depth == coins_len:
                return
            else:
                i = depth
                coin_nums = amount // coins[i]
                for cur_coin_num in range(coin_nums, -1, -1):
                    cur_amount = amount - coins[i] * cur_coin_num
                    self.help_func(coins, cur_amount, depth + 1, res)

 

Donate $5 to me for a coffee with PayPal and read more professional and interesting technical blog articles about web and mobile development. Feel free to visit my web app, WhizWallet, to apply for credit, store or gift cards, DealsPlus to browse daily deals and store coupons to save money, BlackFriday.fm to check latest news, ads and sales in BlackFriday shopping season.

Follow me @Yaoli0615 at Twitter to get latest tech updates.

Resources:

Core Java Volume I–Fundamentals (10th Edition) (Core Series)

Core Java, Volume II–Advanced Features (10th Edition) (Core Series)

Test-Driven Java Development

Java Concurrency in Practice

Java: An Introduction to Problem Solving and Programming (7th Edition)

Java 9 for Programmers (Deitel Developer Series)

Java SE8 for the Really Impatient: A Short Course on the Basics (Java Series)

Core Java for the Impatient

Java: The Beginners Guide for every non-programmer which will attend you trough your learning process

Java Deep Learning Essentials

Machine Learning in Java

Learning Reactive Programming With Java 8

Java 9 Programming By Example

Thinking in Java (4th Edition)

The Java EE Architect’s Handbook, Second Edition: How to be a successful application architect for Java EE applications

Java Artificial Intelligence: Made Easy, w/ Java Programming

Posted in Algorithm&DataStructure, Uncategorized | Tagged , | Leave a comment

LeetCode – 322. Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)

Example 2:
coins = [2], amount = 3
return -1.

Note:
You may assume that you have an infinite number of each kind of coin.

 

Idea: this problem is very classic one for demonstrating Dynamic Programming (DP), but DP is always from brute force method and memorized cache mechanism. Brute force way is to process from highest value denomination coin and use it as much as possible, repeat for other lower value denomination coins, back tracking (use less number of the coin) if  cannot make the amount with certain number of the coin.

There are two optimized solution (cache minimum number to achieve amounts which are less than the target amount):

1. start from coins, use them as a base (current queue) to add each coin to calculate current level sum, if there is sum equaling to target amount and return, otherwise keep calculating until consume all the elements.

2. calculate minimum number to achieve each number from zero to the target amount, if current number (dp[i]) is larger or equal to coin denomination, it can definitely be represented by the coin and difference value (dp[i – coin]).

class Solution(object):
    """
    This solution is inspired by the BFS solution for problem Perfect Square.
    Since it is to find the least coin solution (like a shortest path from 0 to amount),
    using BFS gives results much faster than DP.
    https://discuss.leetcode.com/topic/26262/short-python-solution-using-bfs

    beats 97.75%
    """
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int
        """
        if amount == 0:
            return 0
        cur_queue = [0]  # current queue
        next_queue = []  # next queue
        num_counter = 0
        visited = [False] * (amount + 1)
        visited[0] = True
        while cur_queue:
            num_counter += 1
            for v in cur_queue:
                for coin in coins:
                    new_val = v + coin
                    if new_val == amount:
                        return num_counter
                    elif new_val > amount:
                        continue
                    elif not visited[new_val]:
                        visited[new_val] = True
                        next_queue.append(new_val)
            cur_queue, next_queue = next_queue, []
        return -1

    def coinChange1(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int

        Assume dp[i] is the fewest number of coins making up amount i, then for every coin in coins,
        dp[i] = min(dp[i - coin] + 1).
        dp[i] works as a cache, max_val is default value, otherwise is cached

        The time complexity is O(amount * coins.length) and the space complexity is O(amount)

        [dp[amount], -1][dp[amount] == max_val] => [dp[amount], -1][0/1]
        if dp[amount] == max_val: [dp[amount], -1][1] => -1
        else: [dp[amount], -1][0] => dp[amount]

        https://leetcode.com/problems/coin-change/discuss/77372/Clean-dp-python-code

        beats 77.61%
        """
        max_val = float('inf')
        dp = [0] + [int(max_val)] * amount

        for i in range(1, amount + 1):
            dp[i] = min([dp[i - c] if i - c >= 0 else max_val for c in coins]) + 1

        # return [dp[amount], -1][dp[amount] == max_val]

        if dp[amount] == max_val:
            return -1
        else:
            return dp[amount]

    def coinChange2(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int

        brute force

        Time Limit Exceeded (TLE)
        """
        if coins is None or amount < 0:
            return -1
        if amount == 0:
            return 0
        coins.sort(reverse=True)
        if amount < coins[-1]:
            return -1
        return self.help_func(coins, amount, 0, [])

    def help_func(self, coins, amount, depth, res_list):
        """
        :param coins:
        :param amount:
        :param depth:
        :param res_list:
        :return:

        use current largest coin as much as possible
        back tracking to use less largest coin if cannot make the target amount
        repeat above procedure for each coin from large to small
        """
        coins_len = len(coins)
        if depth == coins_len:
            return -1
        if amount == 0:
            return len(res_list)
        elif amount < 0:
            return -1
        else:
            i = depth
            coin_nums = amount // coins[i]
            for cur_coin_num in range(coin_nums, -1, -1):
                cur_amount = amount - coins[i] * cur_coin_num
                tmp_res = self.help_func(coins, cur_amount, depth + 1, res_list + [coins[i]] * cur_coin_num)
                if tmp_res == -1:
                    continue
                else:
                    return tmp_res
            return -1

 

Donate $5 to me for a coffee with PayPal and read more professional and interesting technical blog articles about web and mobile development. Feel free to visit my web app, WhizWallet, to apply for credit, store or gift cards, DealsPlus to browse daily deals and store coupons to save money, BlackFriday.fm to check latest news, ads and sales in BlackFriday shopping season.

Follow me @Yaoli0615 at Twitter to get latest tech updates.

Resources:

Core Java Volume I–Fundamentals (10th Edition) (Core Series)

Core Java, Volume II–Advanced Features (10th Edition) (Core Series)

Test-Driven Java Development

Java Concurrency in Practice

Java: An Introduction to Problem Solving and Programming (7th Edition)

Java 9 for Programmers (Deitel Developer Series)

Java SE8 for the Really Impatient: A Short Course on the Basics (Java Series)

Core Java for the Impatient

Java: The Beginners Guide for every non-programmer which will attend you trough your learning process

Java Deep Learning Essentials

Machine Learning in Java

Learning Reactive Programming With Java 8

Java 9 Programming By Example

Thinking in Java (4th Edition)

The Java EE Architect’s Handbook, Second Edition: How to be a successful application architect for Java EE applications

Java Artificial Intelligence: Made Easy, w/ Java Programming

Posted in Algorithm&DataStructure, Uncategorized | Tagged , | Leave a comment

LeetCode – 697. Degree of an Array

Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.

Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.

Example 1:

Input: [1, 2, 2, 3, 1]
Output: 2
Explanation: 
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.

Example 2:

Input: [1,2,2,3,1,4,2]
Output: 6

Note:

 

  • nums.length will be between 1 and 50,000.
  • nums[i] will be an integer between 0 and 49,999.

 

Idea: use a hash map to store number elements as key and indices as value, then the length of the value is frequency of a specific number element, and difference between first and last indices in the value is the potential minimum length of the sub-array which has the degree as the input array.

 

Code:

def findShortestSubArray1(self, nums):
    """
    :type nums: List[int]
    :rtype: int

    https://leetcode.com/problems/degree-of-an-array/discuss/108647/Python-O(n)-concise-with-explanation-(two-approaches)

    Firstly, group each num and collect the index it appears into a list. The list with the longest length will be
    the degree of the array.

    Next loop through each of the lists, only those lists with the same length as the degree will qualify.
    We simply take difference between the first and last value of each qualifying lists to find the length of such
    a possible subarray.

    For example if we have [1, 1, 2, 2, 2, 3, 1], after grouping by value:

    [1, 1, 2, 2, 2, 3, 1] => { 1: [0, 1, 6], 2: [2, 3, 4], 3: [4] }, degree: 3

    Only have to consider values where the length == degree:

    1: [0, 1, 6] => subarray length: (6 - 0) + 1 = 7
    2: [2, 3, 4] => subarray length: (4 - 2) + 1 = 3 (Winner!)

    beats 27.26%
    """
    nums_map, deg, min_len = collections.defaultdict(list), 0, float('inf')
    for index, num in enumerate(nums):
        nums_map[num].append(index)
        deg = max(deg, len(nums_map[num]))
    for num, indices in nums_map.items():
        if len(indices) == deg:
            min_len = min(min_len, indices[-1] - indices[0] + 1)
    return min_len

def findShortestSubArray2(self, nums):
    """
    :type nums: List[int]
    :rtype: int

    nums_map: k as num, v as indices of num in the array
    deg: degree of array (maximum frequency of any one of its elements)
    min_len: minimum length to hold the continuous sub-array which has the degree

    difference between first and last indices of an element is the degree and potential min length for the num

    beats 52.72%
    """
    nums_map, deg, min_len = collections.defaultdict(list), 0, float('inf')
    for index, num in enumerate(nums):
        nums_map[num].append(index)
        if len(nums_map[num]) == deg:
            min_len = min(min_len, nums_map[num][-1] - nums_map[num][0] + 1)
        elif len(nums_map[num]) > deg:
            deg = len(nums_map[num])
            min_len = nums_map[num][-1] - nums_map[num][0] + 1
    return min_len

Donate $5 to me for a coffee with PayPal and read more professional and interesting technical blog articles about web and mobile development. Feel free to visit my web app, WhizWallet, to apply for credit, store or gift cards, DealsPlus to browse daily deals and store coupons to save money, BlackFriday.fm to check latest news, ads and sales in BlackFriday shopping season.

Follow me @Yaoli0615 at Twitter to get latest tech updates.

Resources:

Core Java Volume I–Fundamentals (10th Edition) (Core Series)

Core Java, Volume II–Advanced Features (10th Edition) (Core Series)

Test-Driven Java Development

Java Concurrency in Practice

Java: An Introduction to Problem Solving and Programming (7th Edition)

Java 9 for Programmers (Deitel Developer Series)

Java SE8 for the Really Impatient: A Short Course on the Basics (Java Series)

Core Java for the Impatient

Java: The Beginners Guide for every non-programmer which will attend you trough your learning process

Java Deep Learning Essentials

Machine Learning in Java

Learning Reactive Programming With Java 8

Java 9 Programming By Example

Thinking in Java (4th Edition)

The Java EE Architect’s Handbook, Second Edition: How to be a successful application architect for Java EE applications

Java Artificial Intelligence: Made Easy, w/ Java Programming

 

Posted in Algorithm&DataStructure, Uncategorized | Tagged , , , | Leave a comment