This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def knapsack(wt, input_): | |
| arr = [[0 for _ in range(wt + 1)] for _ in range(len(input_))] | |
| for j in range(1, wt + 1): | |
| arr[0][j] = input_[0][1] if input_[0][0] <= j else 0 | |
| for i in range(1, len(input_)): | |
| for j in range(1, wt + 1): | |
| if input_[i][0] > j: | |
| # if wt is greater than current wt | |
| arr[i][j] = arr[i-1][j] | |
| else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MAX = 999999999 | |
| def edit_distance(s1, s2): | |
| d = [] | |
| for i in range(len(s1) + 1): | |
| d.append([i]) | |
| # here d --> [[0], [1], [2], [3]] | |
| for j in range(1, len(s2) + 1): | |
| d[0].append(j) | |
| # here d --> [[0, 1, 2, 3], [1], [2], [3]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node(object): | |
| def __init__(self, data): | |
| self.data = data | |
| self.next = None | |
| def clone(cur): | |
| start = cur | |
| prev = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node(object): | |
| def __init__(self, k, v): | |
| self.k = k | |
| self.v = v | |
| self.next = None | |
| self.prev = None | |
| def __str__(self): | |
| return '%s->%s' % (self.k, self.v) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def do(intervals, interval): | |
| i = 0 | |
| arr = [] | |
| while i < len(intervals) and intervals[i][1] < interval[0]: | |
| arr.append(intervals[i]) | |
| i += 1 | |
| while i < len(intervals) and intervals[i][0] <= interval[1]: | |
| interval = [min(intervals[i][0], interval[0]), max(intervals[i][1], interval[1])] | |
| i += 1 | |
| arr.append(interval) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_local_max(arr, val): | |
| local_max = None | |
| local_max_indx = None | |
| for i, v in enumerate(arr): | |
| if not local_max and v < val: | |
| local_max = v | |
| local_max_indx = i | |
| elif v > local_max and v < val: | |
| local_max = v | |
| local_max_indx = i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import threading | |
| c = threading.Condition() | |
| arr = [i-1 for i in range(1, 101)] | |
| def odd(): | |
| for i in range(1, 100, 2): | |
| with c: | |
| print 'odd: ', arr[i] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package code; | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| public class Main { | |
| public static Map<Character, Integer> getCharacterFrequency(String s) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package code; | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.Comparator; | |
| import java.util.List; | |
| class Meeting { | |
| public int start; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Leetcode https://leetcode.com/problems/top-k-frequent-elements/ | |
| def topKFrequent(self, nums, k): | |
| """ | |
| :type nums: List[int] | |
| :type k: int | |
| :rtype: List[int] | |
| """ | |
| from collections import Counter | |
| d = Counter() |
OlderNewer