Created
November 1, 2017 17:35
-
-
Save jianminchen/ed2549205e7cb13543e5a8d52bda749c to your computer and use it in GitHub Desktop.
Women codesprint - extra sweet - first sweet - first submission - issues found - mix the setup of linkedlist with the calculation of extra sweet.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
class Solution | |
{ | |
internal class Node | |
{ | |
public int Index { get; set; } | |
public Node Previous { get; set; } | |
public Node Next { get; set; } | |
public int Left { get; set; } | |
public int Right { get; set; } | |
public long ExtraSweet { get; set; } | |
public bool Used { get; set; } | |
public Node(int value) | |
{ | |
Index = value; | |
} | |
/// <summary> | |
/// calculate sweat | |
/// </summary> | |
public void CalculateSweat() | |
{ | |
ExtraSweet = (Left + Right) * (Right - Left + 1) / 2; | |
} | |
public void AddExtraLeft(Node[] nodes, int n) | |
{ | |
var leftNode = FindExtraLeft(nodes, n); | |
if(leftNode == null) | |
{ | |
return; | |
} | |
if (leftNode.Used) | |
{ | |
return; | |
} | |
ExtraSweet += leftNode.Index; | |
leftNode.Used = true; | |
setLeftNodeLeftNeighbor(leftNode, nodes, n); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="node"></param> | |
/// <param name="nodes"></param> | |
/// <param name="n"></param> | |
private void setLeftNodeLeftNeighbor(Node node, Node[] nodes, int n) | |
{ | |
// check node left neighbor | |
// two case: with/ no left neighbor | |
var withPrevious = node.Previous != null; | |
var current = withPrevious ? node.Previous : (node.Index >= 1 ? nodes[node.Index - 1] : null); | |
if(current == null) | |
{ | |
return; | |
} | |
var rightNode = nodes[Right].FindExtraRight(nodes, n); | |
if(rightNode == null) | |
{ | |
return; | |
} | |
// two case: with/ no right neighbor | |
var withNext = rightNode.Next != null; | |
var next = withNext ? rightNode.Next : (rightNode.Index < n - 1 ? nodes[rightNode.Index + 1] : null); | |
if(next == null) | |
{ | |
return; | |
} | |
// left neighbor's next to right neighbor | |
current.Next = next; | |
// right neighbor's previous to left neighbor | |
next.Previous = current; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="nodes"></param> | |
/// <param name="n"></param> | |
public void AddExtraRight(Node[] nodes, int n) | |
{ | |
var rightNode = nodes[Right].FindExtraRight(nodes, n); | |
if (rightNode == null) | |
{ | |
return; | |
} | |
if(rightNode.Used) | |
{ | |
return; | |
} | |
ExtraSweet += rightNode.Index; | |
rightNode.Used = true; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="nodes"></param> | |
/// <param name="n"></param> | |
/// <returns></returns> | |
public Node FindExtraLeft(Node[] nodes, int n) | |
{ | |
var withPrevious = this.Previous != null; | |
return withPrevious ? this.Previous : (this.Index > 0 ? nodes[this.Index - 1] : null); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="nodes"></param> | |
/// <param name="n"></param> | |
/// <returns></returns> | |
public Node FindExtraRight(Node[] nodes, int n) | |
{ | |
var withNext = this.Next != null; | |
return withNext ? this.Next : (this.Index < n - 1 ? nodes[this.Index + 1] : null); | |
} | |
} | |
static void Main(String[] args) | |
{ | |
ProcessInput(); | |
//RunTestcase(); | |
} | |
public static void RunTestcase() | |
{ | |
int n = 10; | |
int queries = 3; | |
var nodes = new Node[n]; | |
for (int index = 0; index < n; index++) | |
{ | |
nodes[index] = new Node(index); | |
} | |
var leftNodes = new int[] {2, 6, 9}; | |
var rightNodes = new int[] {4, 7, 9}; | |
var sweat = new long[queries]; | |
for (int index = 0; index < queries; index++) | |
{ | |
int l = leftNodes[index]; | |
int r = rightNodes[index]; | |
nodes[l].Left = l; | |
nodes[l].Right = r; | |
nodes[l].CalculateSweat(); | |
nodes[l].AddExtraLeft(nodes, n); | |
nodes[l].AddExtraRight(nodes, n); | |
sweat[index] = nodes[l].ExtraSweet; | |
} | |
for (int i = 0; i < queries; i++) | |
{ | |
Console.WriteLine(sweat[i]); | |
} | |
} | |
public static void ProcessInput() | |
{ | |
string[] tokens_n = Console.ReadLine().Split(' '); | |
int n = Convert.ToInt32(tokens_n[0]); | |
int queries = Convert.ToInt32(tokens_n[1]); | |
var nodes = new Node[n]; | |
for (int index = 0; index < n; index++ ) | |
{ | |
nodes[index] = new Node(index); | |
} | |
var sweat = new long[queries]; | |
for (int index = 0; index < queries; index++) | |
{ | |
string[] tokens_l = Console.ReadLine().Split(' '); | |
int l = Convert.ToInt32(tokens_l[0]); | |
int r = Convert.ToInt32(tokens_l[1]); | |
nodes[l].Left = l; | |
nodes[l].Right = r; | |
nodes[l].CalculateSweat(); | |
nodes[l].AddExtraLeft(nodes, n); | |
nodes[l].AddExtraRight(nodes, n); | |
sweat[index] = nodes[l].ExtraSweet; | |
} | |
for (int i = 0; i < queries; i++) | |
{ | |
Console.WriteLine(sweat[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment