Skip to content

Instantly share code, notes, and snippets.

View si-yao's full-sized avatar
🎯
Focusing

si-yao

🎯
Focusing
View GitHub Profile
class Solution {
public int firstMissingPositive(int[] nums) {
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == i + 1) {
continue;
}
int n = nums[i];
nums[i] = -1;
while (n > 0 && n <= nums.length && nums[n - 1] != n) {
int t = nums[n - 1];
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
@si-yao
si-yao / 861.score-after-flipping-matrix.java
Created July 23, 2018 02:41
861.score-after-flipping-matrix.java
// https://leetcode.com/problems/score-after-flipping-matrix/description/
// Solution: Greedy.
// It is always good to make first column all 1s, because it is the most significant bit.
// And for other columns, flip if it gives larger result.
// Correctness: We only need to prove it is optimal for 2-columns matrix.
// Though I don't havr formal proof, we can think about any counter cases:
// is there a case that for a row starting with 0, and we don't flip the row?
// O(R*C) time.
import java.util.*;
class Solution {
@si-yao
si-yao / repeatedStringMatch.java
Created November 29, 2017 01:55
repeatedStringMatch.java
package leetcode.repeatedStringMatch;
/**
* Created by siyao on 9/30/17.
* https://leetcode.com/contest/leetcode-weekly-contest-52/problems/repeated-string-match/
* Solution:
* There are just few cases. Then at some point of repeating, if B is still not a substring,
* then we can just stop. So the key is to find the "point", t and t + 1.
*
* Thinking:
@si-yao
si-yao / longestUnivaluePath.java
Created November 29, 2017 01:54
longestUnivaluePath.java
package leetcode.longestUnivaluePath;
import ds.*;
/**
* Created by siyao on 9/30/17.
* https://leetcode.com/contest/leetcode-weekly-contest-52/problems/longest-univalue-path/
* Solution:
* This does not sound like an easy level question.
* It is not easy to come up with the correct induction or sub-problem.
{
"name": "Amour",
"url": "http://www.metacritic.com/movie/amour",
"rlsdate": "2012-12-19",
"score": "94",
"summary": "Georges and Anne are in their eighties. They are cultivated, retired music teachers. Their daughter, who is also a musician, lives abroad with her family. One day, Anne has an attack. The couple's...",
"rating": "PG-13",
"cast": "Emmanuelle Riva, Isabelle Huppert, Jean-Louis Trintignant",
"genre": "Drama, Romance",
"avguserscore": "8.0",
{
"name":"A Girl Walks Home Alone at Night",
"url":"http://www.metacritic.com/movie/a-girl-walks-home-alone-at-night",
"rlsdate":"2014-11-21",
"score":"81",
"summary":"In the Iranian ghost-town Bad City, a place that reeks of death and loneliness, the townspeople are unaware they are being stalked by a lonesome vampire.",
"rating":"Not Rated",
"cast":"",
"genre":"Thriller, Horror, Romance",
"avguserscore":"7.2",
{
"rc": [
{
"name": "The Babadook",
"url": "http://www.metacritic.com/movie/the-babadook",
"rlsdate": "2014-11-28",
"score": "86",
"summary": "Six years after the violent death of her husband, Amelia (Essie Davis) is at a loss. She struggles to discipline her ‘out of control’ 6 year-old, Samuel (Noah Wiseman), a son she finds impossible...",
"rating": "Not Rated",
"cast": "Barbara West, Daniel Henshall, Essie Davis, Hayley McElhinney, Noah Wiseman",