Note:

1. To maximized the gap, A[i-1] will be eithor floor (1) or ceiling (B[i-1]).

2. Focus on 2 possibilities with A[i] (floor 1 or ceiling B[i]) and their possible preceedings (previous sum with floor or ceiling)

def cost(B):

top, bottom = 0, 0

g0d2 發表在 痞客邦 留言(0) 人氣()


Shuffle a given array

Given an array, write a program to generate a random permutation of array elements. This question is also asked as “shuffle a deck of cards” or “randomize a given array”.



g0d2 發表在 痞客邦 留言(0) 人氣()


Validate if a given string is numeric.

Some examples:
"0"=>true
" 0.1 "=>true
"abc"=>false

g0d2 發表在 痞客邦 留言(0) 人氣()

Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x

g0d2 發表在 痞客邦 留言(0) 人氣()

Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].

g0d2 發表在 痞客邦 留言(0) 人氣()

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2]have the following unique permutations:
[1,1,2],[1,2,1], and[2,1,1].

g0d2 發表在 痞客邦 留言(0) 人氣()

Given a collection ofdistinctnumbers, return all possible permutations.

For example,
[1,2,3]have the following permutations:
[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].

g0d2 發表在 痞客邦 留言(0) 人氣()

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected andit will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonightwithout alerting the police.

class Solution(object):
    def rob(self, nums):
        """

g0d2 發表在 痞客邦 留言(0) 人氣()

Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:

  1. Youmust notmodify the array (assume the array is read only).

  2. You must use only constant,O(1) extra space.

  3. Your runtime complexity should be less thanO(n2).

g0d2 發表在 痞客邦 留言(0) 人氣()

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

class Solution(object):

g0d2 發表在 痞客邦 留言(0) 人氣()

Given two words (beginWordandendWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWordtoendWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

g0d2 發表在 痞客邦 留言(0) 人氣()


A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.
Source: Mathword(http://mathworld.wolfram.com/Permutation.html)

Below are the permutations of string ABC.
ABC, ACB, BAC, BCA, CAB, CBA

g0d2 發表在 痞客邦 留言(0) 人氣()

1 2 3
Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。