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
| final List<int> heap = []; | |
| bubble(int index) { | |
| while(index > 0) { | |
| int parent = index ~/ 2; | |
| if(heap[index] < heap[parent]) { | |
| // Swap values. |
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 Vertex { | |
| int distance; | |
| int weight; | |
| List<Vertex> edges; | |
| int value; | |
| bool visited; | |
| String toString() => '$value'; | |
| } | |
| class PriorityQueue { |
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 'dart:math'; | |
| longest(List values) { | |
| final LIS = new List(values.length); | |
| LIS[0] = 1; | |
| for(int i = 1; i < values.length; i++) { | |
| var max = 1; | |
| for(int j = i; j > 0; j--) { | |
| if(values[j] < values[i] && LIS[j] > max) { | |
| max = LIS[j]; |
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 'dart:math'; | |
| // The cost function c(i, j) = ... | |
| final costs = [ | |
| [1,2,3,4,6], // 0 | |
| [5,6,2,3,1], // 1 | |
| [6,2,0,2,6], // 2 | |
| [5,0,2,8,6], // 3 | |
| [1,2,3,4,5], // 4 | |
| // 0 1 2 3 4 |
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
| // This method takes O(n) time but only O(1) space. | |
| fib(int n) { | |
| if(n == 0) { | |
| return 0; | |
| } | |
| else { | |
| var previousFib = 0; | |
| var currentFib = 1; | |
| while(--n != 0) { // loop is skipped if n = 1 |
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
| var results = { | |
| 0: 0, | |
| 1: 1 | |
| }; | |
| fib(n) { | |
| if(!results.containsKey(n)) { | |
| results[n] = fib(n - 1) + fib(n - 2); | |
| } | |
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 Activity { | |
| String name; | |
| int start; | |
| int end; | |
| @override | |
| String toString() => '$name: $start, $end'; | |
| } | |
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
| String encode(String message) { | |
| final Map frequencies = count(message); | |
| final sorted = frequencies.values.toList() | |
| ..sort(); | |
| print(sorted); | |
| return ''; | |
| } | |
| Map<String, int> count(String message) { | |
| Map freq = {}; |
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 { | |
| dynamic value; | |
| bool discovered = false; | |
| List<Node> links = []; | |
| int distance = 0; | |
| int weight; | |
| @override | |
| String toString() => | |
| ''' |
NewerOlder